增加获取网络流量的功能

This commit is contained in:
wangjian 2023-03-01 12:00:42 +08:00
parent 27b3bfacbd
commit e857790db1
2 changed files with 29 additions and 2 deletions

View File

@ -75,6 +75,7 @@ func NewStartCmd() *cobra.Command {
for {
select {
case <-statTicker.C:
monitor.TrackNetworkSpeed()
stat := monitor.GetState(node.NodeName).ToByte()
go func() {
_ = generateAndSendData(ap, stat)

View File

@ -28,6 +28,9 @@ var (
"apfs", "ext4", "ext3", "ext2", "f2fs", "reiserfs", "jfs", "btrfs",
"fuseblk", "zfs", "simfs", "ntfs", "fat32", "exfat", "xfs", "fuse.rclone",
}
excludeNetInterfaces = []string{
"lo", "tun", "docker", "veth", "br-", "vmbr", "vnet", "kube",
}
getMacDiskNo = regexp.MustCompile(`\/dev\/disk(\d)s.*`)
Version string
)
@ -46,8 +49,8 @@ func GetHost() *model.Node {
cpuModelCount[ci[i].ModelName]++
}
var cpus []string
for model, count := range cpuModelCount {
cpus = append(cpus, fmt.Sprintf("%s %d %s Core", model, count, cpuType))
for mod, count := range cpuModelCount {
cpus = append(cpus, fmt.Sprintf("%s %d %s Core", mod, count, cpuType))
}
mv, _ := mem.VirtualMemory()
diskTotal, _ := getDiskTotalAndUsed()
@ -208,3 +211,26 @@ func isListContainsStr(list []string, str string) bool {
}
return false
}
func TrackNetworkSpeed() {
var innerNetInTransfer, innerNetOutTransfer uint64
nc, err := net.IOCounters(true)
if err == nil {
for _, v := range nc {
if isListContainsStr(excludeNetInterfaces, v.Name) {
continue
}
innerNetInTransfer += v.BytesRecv
innerNetOutTransfer += v.BytesSent
}
now := uint64(time.Now().Unix())
diff := now - lastUpdateNetStats
if diff > 0 {
netInSpeed = (innerNetInTransfer - netInTransfer) / diff
netOutSpeed = (innerNetOutTransfer - netOutTransfer) / diff
}
netInTransfer = innerNetInTransfer
netOutTransfer = innerNetOutTransfer
lastUpdateNetStats = now
}
}