diff --git a/cmd/server.go b/cmd/server.go index 634f4f4..957d3c7 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -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) diff --git a/internal/monitor/monitor.go b/internal/monitor/monitor.go index 2395c72..9bf11f5 100644 --- a/internal/monitor/monitor.go +++ b/internal/monitor/monitor.go @@ -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 + } +}