From 8a220653b1b0377b039750d83295cb8c83d47df9 Mon Sep 17 00:00:00 2001 From: CHN-STUDENT Date: Thu, 10 Dec 2020 11:34:58 +0800 Subject: [PATCH] =?UTF-8?q?tcp=E3=80=81udp=E3=80=81=E8=BF=9B=E7=A8=8B?= =?UTF-8?q?=E3=80=81=E7=BA=BF=E7=A8=8B=E8=B7=A8=E5=B9=B3=E5=8F=B0=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- clients/golang/awesomeProject/README.MD | 27 ++++ clients/golang/awesomeProject/main.go | 126 +----------------- clients/golang/awesomeProject/tudp_linux.go | 64 +++++++++ clients/golang/awesomeProject/tupd_windows.go | 68 ++++++++++ 4 files changed, 165 insertions(+), 120 deletions(-) create mode 100644 clients/golang/awesomeProject/README.MD create mode 100644 clients/golang/awesomeProject/tudp_linux.go create mode 100644 clients/golang/awesomeProject/tupd_windows.go diff --git a/clients/golang/awesomeProject/README.MD b/clients/golang/awesomeProject/README.MD new file mode 100644 index 0000000..89215e5 --- /dev/null +++ b/clients/golang/awesomeProject/README.MD @@ -0,0 +1,27 @@ +# ServerStatus 客户端 go 语言版 + +懒得改项目名了,需要的话自己改。 + +### 使用方法 +可选:更改源码中的连接信息 +``` +# go env -w GOPROXY=https://goproxy.cn,direct (速度慢可以换源) +go build . +./awesomeProject +# 更改链接参数 +./awesomeProject --SERVER=127.0.0.1 --PORT=35601 --USER=test --PASSWORD=test1234 --INTERVAL=1 +``` + +### TODO +- 实时网络速度 +- 外网连接状态 +- 三网丢包计算 +- 三网延迟计算 +- 其他算法优化 + +### 鸣谢 + + - https://github.com/cppla/ServerStatus + - https://github.com/bitcav/nitr-core + - https://github.com/shirou/gopsutil + - https://github.com/json-iterator/go diff --git a/clients/golang/awesomeProject/main.go b/clients/golang/awesomeProject/main.go index 8d17488..2e2b50a 100644 --- a/clients/golang/awesomeProject/main.go +++ b/clients/golang/awesomeProject/main.go @@ -10,11 +10,6 @@ package main import ( "fmt" - "github.com/shirou/gopsutil/process" - "os/exec" - "path/filepath" - "syscall" - //下面这是已经封装好的轮子 "github.com/bitcav/nitr-core/cpu" "github.com/bitcav/nitr-core/host" @@ -236,123 +231,14 @@ func getLoad() { } } -func Command(name, args string) (*exec.Cmd, error) { - // TODO: 这段 Linux 下编译不通过正在研究 - // golang 使用 exec.Comand 运行含管道的 cmd 命令会产生问题(如 netstat -an | find "TCP" /c),因此使用此办法调用 - // 参考:https://studygolang.com/topics/10284 - if filepath.Base(name) == name { - lp, err := exec.LookPath(name) - if err != nil { - return nil, err - } - name = lp - } - return &exec.Cmd{ - Path: name, - SysProcAttr: &syscall.SysProcAttr{CmdLine: name + " " + args}, - }, nil -} - -func tupd() { - if host.Info().OS == "linux" { - byte1 ,err := exec.Command("bash", "-c","ss -t|wc -l").Output() - if err != nil { - clientInfo.TCP = 0 - fmt.Println("Get TCP count error:",err) - } else { - result := bytes2str(byte1) - result = strings.Replace(result, "\n", "", -1) - intNum, err := strconv.Atoi(result) - if err != nil { - fmt.Println("Get TCP count error::",err) - } - clientInfo.TCP = uint64(intNum) - } - byte2 ,err := exec.Command("bash", "-c","ss -u|wc -l").Output() - if err != nil { - clientInfo.UDP = 0 - fmt.Println("Get UDP count error:",err) - } else { - result := bytes2str(byte2) - result = strings.Replace(result, "\n", "", -1) - intNum, err := strconv.Atoi(result) - if err != nil { - fmt.Println("Get UDP count error:",err) - } - clientInfo.UDP = uint64(intNum) - } - byte3 ,err := exec.Command("bash", "-c","ps -ef|wc -l").Output() - if err != nil { - clientInfo.Process = 0 - fmt.Println("Get process count error:",err) - } else { - result := bytes2str(byte3) - result = strings.Replace(result, "\n", "", -1) - intNum, err := strconv.Atoi(result) - if err != nil { - fmt.Println("Get process count error:",err) - } - clientInfo.Process = uint64(intNum) - } - byte4 ,err := exec.Command("bash", "-c","ps -eLf|wc -l").Output() - if err != nil { - clientInfo.Process = 0 - fmt.Println("Get threads count error:",err) - } else { - result := bytes2str(byte4) - result = strings.Replace(result, "\n", "", -1) - intNum, err := strconv.Atoi(result) - if err != nil { - fmt.Println("Get threads count error:",err) - } - clientInfo.Thread = uint64(intNum) - } - } else if host.Info().OS == "windows" { - cmd ,err := Command("cmd","/c netstat -an|find \"TCP\" /c") - if err != nil { - clientInfo.TCP = 0 - fmt.Println("Get TCP count error:",err) - } else { - byte1, err := cmd.Output() - result := bytes2str(byte1) - result = strings.Replace(result, "\r", "", -1) - result = strings.Replace(result, "\n", "", -1) - intNum, err := strconv.Atoi(result) - if err != nil { - fmt.Println("Get TCP count error:",err) - } - clientInfo.TCP = uint64(intNum) - } - cmd2 ,err := Command("cmd", "/c netstat -an|find \"UDP\" /c") - if err != nil { - clientInfo.UDP = 0 - fmt.Println("Get UDP count error:",err) - } else { - byte2, err := cmd2.Output() - result := bytes2str(byte2) - result = strings.Replace(result, "\r", "", -1) - result = strings.Replace(result, "\n", "", -1) - intNum, err := strconv.Atoi(result) - if err != nil { - fmt.Println("Get UDP count error:",err) - } - 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 - } -} +var CU_ADDR = CU + ":" + strconv.Itoa(PORBEPORT) +var CT_ADDR = CU + ":" + strconv.Itoa(PORBEPORT) +var CM_ADDR = CU + ":" + strconv.Itoa(PORBEPORT) func getNetworkStatus() { defaulttimeout := 1 * time.Second count := 0 - - conn , err := net.DialTimeout("tcp", CU + ":" + strconv.Itoa(PORBEPORT),defaulttimeout) + conn , err := net.DialTimeout("tcp",CU_ADDR,defaulttimeout) defer conn.Close() if err != nil { fmt.Println("Error try to connect China unicom :", err) @@ -360,7 +246,7 @@ func getNetworkStatus() { } else { conn.Close() } - conn , err = net.DialTimeout("tcp", CT + ":" + strconv.Itoa(PORBEPORT),defaulttimeout) + conn , err = net.DialTimeout("tcp", CT_ADDR,defaulttimeout) defer conn.Close() if err != nil { fmt.Println("Error try to connect China telecom :", err) @@ -368,7 +254,7 @@ func getNetworkStatus() { } else { conn.Close() } - conn , err = net.DialTimeout("tcp", CM + ":" + strconv.Itoa(PORBEPORT),defaulttimeout) + conn , err = net.DialTimeout("tcp", CM_ADDR,defaulttimeout) defer conn.Close() if err != nil { fmt.Println("Error try to connect China mobile :", err) diff --git a/clients/golang/awesomeProject/tudp_linux.go b/clients/golang/awesomeProject/tudp_linux.go new file mode 100644 index 0000000..6ced325 --- /dev/null +++ b/clients/golang/awesomeProject/tudp_linux.go @@ -0,0 +1,64 @@ +// +build linux + +package main +import ( + "fmt" + "os/exec" + "strconv" + "strings" +) +func tupd() { + byte1 ,err := exec.Command("bash", "-c","ss -t|wc -l").Output() + if err != nil { + clientInfo.TCP = 0 + fmt.Println("Get TCP count error:",err) + } else { + result := bytes2str(byte1) + result = strings.Replace(result, "\n", "", -1) + intNum, err := strconv.Atoi(result) + if err != nil { + fmt.Println("Get TCP count error::",err) + } + clientInfo.TCP = uint64(intNum) + } + byte2 ,err := exec.Command("bash", "-c","ss -u|wc -l").Output() + if err != nil { + clientInfo.UDP = 0 + fmt.Println("Get UDP count error:",err) + } else { + result := bytes2str(byte2) + result = strings.Replace(result, "\n", "", -1) + intNum, err := strconv.Atoi(result) + if err != nil { + fmt.Println("Get UDP count error:",err) + } + clientInfo.UDP = uint64(intNum) + } + byte3 ,err := exec.Command("bash", "-c","ps -ef|wc -l").Output() + if err != nil { + clientInfo.Process = 0 + fmt.Println("Get process count error:",err) + } else { + result := bytes2str(byte3) + result = strings.Replace(result, "\n", "", -1) + intNum, err := strconv.Atoi(result) + if err != nil { + fmt.Println("Get process count error:",err) + } + clientInfo.Process = uint64(intNum) + } + byte4 ,err := exec.Command("bash", "-c","ps -eLf|wc -l").Output() + if err != nil { + clientInfo.Process = 0 + fmt.Println("Get threads count error:",err) + } else { + result := bytes2str(byte4) + result = strings.Replace(result, "\n", "", -1) + intNum, err := strconv.Atoi(result) + if err != nil { + fmt.Println("Get threads count error:",err) + } + clientInfo.Thread = uint64(intNum) + } +} + diff --git a/clients/golang/awesomeProject/tupd_windows.go b/clients/golang/awesomeProject/tupd_windows.go new file mode 100644 index 0000000..39a549f --- /dev/null +++ b/clients/golang/awesomeProject/tupd_windows.go @@ -0,0 +1,68 @@ +package main + +import ( + "fmt" + "github.com/shirou/gopsutil/process" + "os/exec" + "path/filepath" + "strconv" + "strings" + "syscall" +) + +func tupd() { + cmd ,err := Command("cmd","/c netstat -an|find \"TCP\" /c") + if err != nil { + clientInfo.TCP = 0 + fmt.Println("Get TCP count error:",err) + } else { + byte1, err := cmd.Output() + result := bytes2str(byte1) + result = strings.Replace(result, "\r", "", -1) + result = strings.Replace(result, "\n", "", -1) + intNum, err := strconv.Atoi(result) + if err != nil { + fmt.Println("Get TCP count error:",err) + } + clientInfo.TCP = uint64(intNum) + } + cmd2 ,err := Command("cmd", "/c netstat -an|find \"UDP\" /c") + if err != nil { + clientInfo.UDP = 0 + fmt.Println("Get UDP count error:",err) + } else { + byte2, err := cmd2.Output() + result := bytes2str(byte2) + result = strings.Replace(result, "\r", "", -1) + result = strings.Replace(result, "\n", "", -1) + intNum, err := strconv.Atoi(result) + if err != nil { + fmt.Println("Get UDP count error:",err) + } + 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 Command(name, args string) (*exec.Cmd, error) { + // golang 使用 exec.Comand 运行含管道的 cmd 命令会产生问题(如 netstat -an | find "TCP" /c),因此使用此办法调用 + // 参考:https://studygolang.com/topics/10284 + if filepath.Base(name) == name { + lp, err := exec.LookPath(name) + if err != nil { + return nil, err + } + name = lp + } + return &exec.Cmd{ + Path: name, + SysProcAttr: &syscall.SysProcAttr{CmdLine: name + " " + args}, + }, nil +}