modularize monitor, reduce init usage (#81)

* kill process tree using syscall on windows & cleanup (#80)

* kill process tree using syscall on windows & cleanup

* use job api

* add error check for cmd.Start

* modularize monitor, reduce init usage

* replace slices with sort

* update gopsutil & other dependencies
This commit is contained in:
UUBulb
2024-11-03 21:53:09 +08:00
committed by GitHub
parent 0cba96bae1
commit af41e4d843
23 changed files with 665 additions and 381 deletions
+45
View File
@@ -0,0 +1,45 @@
package nic
import (
"context"
"github.com/shirou/gopsutil/v4/net"
)
type NICKeyType string
const NICKey NICKeyType = "nic"
var excludeNetInterfaces = map[string]bool{
"lo": true,
"tun": true,
"docker": true,
"veth": true,
"br-": true,
"vmbr": true,
"vnet": true,
"kube": true,
}
func GetState(ctx context.Context) ([]uint64, error) {
var netInTransfer, netOutTransfer uint64
nc, err := net.IOCountersWithContext(ctx, true)
if err != nil {
return nil, err
}
allowList := excludeNetInterfaces
if m, ok := ctx.Value(NICKey).(map[string]bool); ok && len(m) > 0 {
allowList = m
}
for _, v := range nc {
if !allowList[v.Name] {
continue
}
netInTransfer += v.BytesRecv
netOutTransfer += v.BytesSent
}
return []uint64{netInTransfer, netOutTransfer}, nil
}