增加获取网络流量的功能
This commit is contained in:
parent
27b3bfacbd
commit
e857790db1
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue