From 3ceb1f1bbf5dde612590b57848ab3dc5cc0136dd Mon Sep 17 00:00:00 2001 From: CHN-STUDENT Date: Tue, 8 Dec 2020 15:52:38 +0800 Subject: [PATCH] =?UTF-8?q?TODO:=E4=B8=89=E7=BD=91=E5=BB=B6=E8=BF=9F?= =?UTF-8?q?=EF=BC=8C=E4=B8=89=E7=BD=91=E4=B8=A2=E5=8C=85=EF=BC=8Ctcp/udp?= =?UTF-8?q?=E8=BF=9E=E6=8E=A5=E6=95=B0=EF=BC=8C=E5=AE=9E=E6=97=B6=E7=BD=91?= =?UTF-8?q?=E7=BB=9C=E9=80=9F=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- clients/golang/awesomeProject/main.go | 163 +++++++++++++++++++++++++- 1 file changed, 161 insertions(+), 2 deletions(-) diff --git a/clients/golang/awesomeProject/main.go b/clients/golang/awesomeProject/main.go index ea5dd6c..a56c47e 100644 --- a/clients/golang/awesomeProject/main.go +++ b/clients/golang/awesomeProject/main.go @@ -95,6 +95,158 @@ func NewDefaultClientInfo() ClientInfo { } } +func trafficCount() { + netInfo, err := nnet.IOCounters(true) + if err != nil { + fmt.Println("Get traffic count error:",err) + } + var bytesSent uint64 = 0 + var bytesRecv uint64 = 0 + for _, v := range netInfo { + if strings.Index(v.Name,"lo") > -1 || + strings.Index(v.Name,"tun") > -1 || + strings.Index(v.Name,"docker") > -1 || + strings.Index(v.Name,"veth") > -1 || + strings.Index(v.Name,"br-") > -1 || + strings.Index(v.Name,"vmbr") > -1 || + strings.Index(v.Name,"vnet") > -1 || + strings.Index(v.Name,"kube") > -1 { + continue + } + bytesSent += v.BytesSent + bytesRecv += v.BytesRecv + } + clientInfo.NetworkIn = bytesRecv + clientInfo.NetworkOut = bytesSent +} + +func spaceCount() { + // golang 没有类似于在 python 的 dict 或 tuple 的 in 查找关键字,自己写多重判断实现 + diskList, _ := disk.Partitions(false) + var total uint64 = 0 + var used uint64 = 0 + for _,d := range diskList { + fsType := strings.ToLower(d.Fstype) + if fsType != "ext4" && + fsType != "ext3" && + fsType != "ext2" && + fsType != "reiserfs" && + fsType != "jfs" && + fsType != "btrfs" && + fsType != "fuseblk" && + fsType != "zfs" && + fsType != "simfs" && + fsType != "ntfs" && + fsType != "fat32" && + fsType != "exfat" && + fsType != "xfs" { + //if(d.Device == "A") { //特殊盘符自己写处理 + continue + //} + } + diskUsageOf, _ := disk.Usage(d.Mountpoint) + used += diskUsageOf.Used + total += diskUsageOf.Total + } + clientInfo.HddUsed = used / 1024.0 / 1024.0 + clientInfo.HddTotal = total / 1024.0 / 1024.0 +} + +func getLoad() { + // linux or freebsd only + if host.Info().OS == "linux" || host.Info().OS == "freebsd" { + l, err := load.Avg() + if err != nil { + fmt.Println("Get CPU Loads failed:",err) + } else { + clientInfo.Load1 = l.Load1 + clientInfo.Load5 = l.Load5 + clientInfo.Load15 = l.Load15 + } + } else { + clientInfo.Load1 = 0.0 + clientInfo.Load5 = 0.0 + clientInfo.Load15 = 0.0 + } +} + +func tupd() { + //if sys.platform.startswith("linux") is True: + //t = int(os.popen('ss -t|wc -l').read()[:-1])-1 + //u = int(os.popen('ss -u|wc -l').read()[:-1])-1 + //p = int(os.popen('ps -ef|wc -l').read()[:-1])-2 + //d = int(os.popen('ps -eLf|wc -l').read()[:-1])-2 + //elif sys.platform.startswith("win") is True: + //t = int(os.popen('netstat -an|find "TCP" /c').read()[:-1])-1 + //u = int(os.popen('netstat -an|find "UDP" /c').read()[:-1])-1 + //p = len(psutil.pids()) + //d = 0 + if host.Info().OS == "linux" { + byte1 ,err := exec.Command("ss -t|wc -l").Output() + if err != nil { + clientInfo.TCP = 0 + fmt.Println("Get TCP count error:",err) + } else { + result := bytes2str(byte1) + intNum, _ := strconv.Atoi(result) + clientInfo.TCP = uint64(intNum) + } + byte2 ,err := exec.Command("ss -u|wc -l").Output() + if err != nil { + clientInfo.UDP = 0 + fmt.Println("Get UDP count error:",err) + } else { + result := bytes2str(byte2) + intNum, _ := strconv.Atoi(result) + clientInfo.UDP = uint64(intNum) + } + byte3 ,err := exec.Command("ps -ef|wc -l").Output() + if err != nil { + clientInfo.Process = 0 + fmt.Println("Get process count error:",err) + } else { + result := bytes2str(byte3) + intNum, _ := strconv.Atoi(result) + clientInfo.Process = uint64(intNum) + } + byte4 ,err := exec.Command("ps -eLf|wc -l").Output() + if err != nil { + clientInfo.Process = 0 + fmt.Println("Get threads count error:",err) + } else { + result := bytes2str(byte4) + intNum, _ := strconv.Atoi(result) + clientInfo.Process = uint64(intNum) + } + } else if host.Info().OS == "windows" { + // 不知道为何,tcp和udp数量没法获取 + byte1 ,err := exec.Command("cmd", "/C","netstat -an|find \"TCP\" /c").Output() + if err != nil { + clientInfo.TCP = 0 + fmt.Println("Get TCP count error:",err) + } else { + result := bytes2str(byte1) + intNum, _ := strconv.Atoi(result) + clientInfo.TCP = uint64(intNum) + } + byte2 ,err := exec.Command("cmd", "/C","netstat -an|find \"UDP\" /c").Output() + if err != nil { + clientInfo.UDP = 0 + fmt.Println("Get UDP count error:",err) + } else { + result := bytes2str(byte2) + intNum, _ := strconv.Atoi(result) + clientInfo.UDP = uint64(intNum) + } + pids, err := process.Processes() + if err != nil { + fmt.Println("Get process count error:",err) + } else { + clientInfo.Process = uint64(len(pids)) + } + clientInfo.Thread = 0 + } +} func str2bytes(s string) []byte { x := (*[2]uintptr)(unsafe.Pointer(&s)) @@ -106,6 +258,8 @@ func bytes2str(b []byte) string { return *(*string)(unsafe.Pointer(&b)) } +var clientInfo ClientInfo + func main() { for _, args := range os.Args { if strings.Index(args,"SERVER") > -1 { @@ -172,7 +326,7 @@ func main() { fmt.Println(str) } fmt.Println(checkIP) - var clientInfo = NewDefaultClientInfo() + clientInfo = NewDefaultClientInfo() for { clientInfo.MemoryTotal = ram.Info().Total / 1024 // 需要转单位 @@ -184,9 +338,14 @@ func main() { swapMemory, _ := mem.SwapMemory() clientInfo.SwapTotal = swapMemory.Total / 1024 // 需要转单位 clientInfo.SwapUsed = swapMemory.Used / 1024 // 需要转单位 - + getLoad() + tupd() + trafficCount() + spaceCount() + //TODO:三网延迟,三网丢包,tcp/udp连接数,实时网络速度 //结构体转json字符串 data, err := jsoniter.MarshalToString(&clientInfo) + //fmt.Println(data) if err != nil { fmt.Println("transformation error: ", err) }