mirror of https://github.com/cppla/ServerStatus
修正 tcp/udp/进程/线程无法正确获取的问题
parent
3ceb1f1bbf
commit
8a5d4135a8
|
@ -10,11 +10,21 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/shirou/gopsutil/process"
|
||||||
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
|
"syscall"
|
||||||
|
|
||||||
|
//下面这是已经封装好的轮子
|
||||||
"github.com/bitcav/nitr-core/cpu"
|
"github.com/bitcav/nitr-core/cpu"
|
||||||
"github.com/bitcav/nitr-core/host"
|
"github.com/bitcav/nitr-core/host"
|
||||||
"github.com/bitcav/nitr-core/ram"
|
"github.com/bitcav/nitr-core/ram"
|
||||||
"github.com/json-iterator/go"
|
"github.com/json-iterator/go"
|
||||||
|
//没轮子的自己封装
|
||||||
|
"github.com/shirou/gopsutil/disk"
|
||||||
|
"github.com/shirou/gopsutil/load"
|
||||||
"github.com/shirou/gopsutil/mem"
|
"github.com/shirou/gopsutil/mem"
|
||||||
|
nnet "github.com/shirou/gopsutil/net"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
@ -127,26 +137,28 @@ func spaceCount() {
|
||||||
var used uint64 = 0
|
var used uint64 = 0
|
||||||
for _,d := range diskList {
|
for _,d := range diskList {
|
||||||
fsType := strings.ToLower(d.Fstype)
|
fsType := strings.ToLower(d.Fstype)
|
||||||
if fsType != "ext4" &&
|
//fmt.Println(d.Fstype)
|
||||||
fsType != "ext3" &&
|
if strings.Index(fsType, "ext4") < 0 &&
|
||||||
fsType != "ext2" &&
|
strings.Index(fsType, "ext3") < 0 &&
|
||||||
fsType != "reiserfs" &&
|
strings.Index(fsType, "ext2") < 0 &&
|
||||||
fsType != "jfs" &&
|
strings.Index(fsType, "reiserfs") < 0 &&
|
||||||
fsType != "btrfs" &&
|
strings.Index(fsType, "jfs") < 0 &&
|
||||||
fsType != "fuseblk" &&
|
strings.Index(fsType, "btrfs") < 0 &&
|
||||||
fsType != "zfs" &&
|
strings.Index(fsType, "fuseblk") < 0 &&
|
||||||
fsType != "simfs" &&
|
strings.Index(fsType, "zfs") < 0 &&
|
||||||
fsType != "ntfs" &&
|
strings.Index(fsType, "simfs") < 0 &&
|
||||||
fsType != "fat32" &&
|
strings.Index(fsType, "ntfs")< 0 &&
|
||||||
fsType != "exfat" &&
|
strings.Index(fsType, "fat32") < 0 &&
|
||||||
fsType != "xfs" {
|
strings.Index(fsType, "exfat") < 0 &&
|
||||||
|
strings.Index(fsType, "xfs") < 0 {
|
||||||
//if(d.Device == "A") { //特殊盘符自己写处理
|
//if(d.Device == "A") { //特殊盘符自己写处理
|
||||||
continue
|
continue
|
||||||
//}
|
//}
|
||||||
|
} else {
|
||||||
|
diskUsageOf, _ := disk.Usage(d.Mountpoint)
|
||||||
|
used += diskUsageOf.Used
|
||||||
|
total += diskUsageOf.Total
|
||||||
}
|
}
|
||||||
diskUsageOf, _ := disk.Usage(d.Mountpoint)
|
|
||||||
used += diskUsageOf.Used
|
|
||||||
total += diskUsageOf.Total
|
|
||||||
}
|
}
|
||||||
clientInfo.HddUsed = used / 1024.0 / 1024.0
|
clientInfo.HddUsed = used / 1024.0 / 1024.0
|
||||||
clientInfo.HddTotal = total / 1024.0 / 1024.0
|
clientInfo.HddTotal = total / 1024.0 / 1024.0
|
||||||
|
@ -170,6 +182,23 @@ func getLoad() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
func tupd() {
|
func tupd() {
|
||||||
//if sys.platform.startswith("linux") is True:
|
//if sys.platform.startswith("linux") is True:
|
||||||
//t = int(os.popen('ss -t|wc -l').read()[:-1])-1
|
//t = int(os.popen('ss -t|wc -l').read()[:-1])-1
|
||||||
|
@ -182,60 +211,87 @@ func tupd() {
|
||||||
//p = len(psutil.pids())
|
//p = len(psutil.pids())
|
||||||
//d = 0
|
//d = 0
|
||||||
if host.Info().OS == "linux" {
|
if host.Info().OS == "linux" {
|
||||||
byte1 ,err := exec.Command("ss -t|wc -l").Output()
|
byte1 ,err := exec.Command("bash", "-c","ss -t|wc -l").Output()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
clientInfo.TCP = 0
|
clientInfo.TCP = 0
|
||||||
fmt.Println("Get TCP count error:",err)
|
fmt.Println("Get TCP count error:",err)
|
||||||
} else {
|
} else {
|
||||||
result := bytes2str(byte1)
|
result := bytes2str(byte1)
|
||||||
intNum, _ := strconv.Atoi(result)
|
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)
|
clientInfo.TCP = uint64(intNum)
|
||||||
}
|
}
|
||||||
byte2 ,err := exec.Command("ss -u|wc -l").Output()
|
byte2 ,err := exec.Command("bash", "-c","ss -u|wc -l").Output()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
clientInfo.UDP = 0
|
clientInfo.UDP = 0
|
||||||
fmt.Println("Get UDP count error:",err)
|
fmt.Println("Get UDP count error:",err)
|
||||||
} else {
|
} else {
|
||||||
result := bytes2str(byte2)
|
result := bytes2str(byte2)
|
||||||
intNum, _ := strconv.Atoi(result)
|
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)
|
clientInfo.UDP = uint64(intNum)
|
||||||
}
|
}
|
||||||
byte3 ,err := exec.Command("ps -ef|wc -l").Output()
|
byte3 ,err := exec.Command("bash", "-c","ps -ef|wc -l").Output()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
clientInfo.Process = 0
|
clientInfo.Process = 0
|
||||||
fmt.Println("Get process count error:",err)
|
fmt.Println("Get process count error:",err)
|
||||||
} else {
|
} else {
|
||||||
result := bytes2str(byte3)
|
result := bytes2str(byte3)
|
||||||
intNum, _ := strconv.Atoi(result)
|
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)
|
clientInfo.Process = uint64(intNum)
|
||||||
}
|
}
|
||||||
byte4 ,err := exec.Command("ps -eLf|wc -l").Output()
|
byte4 ,err := exec.Command("bash", "-c","ps -eLf|wc -l").Output()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
clientInfo.Process = 0
|
clientInfo.Process = 0
|
||||||
fmt.Println("Get threads count error:",err)
|
fmt.Println("Get threads count error:",err)
|
||||||
} else {
|
} else {
|
||||||
result := bytes2str(byte4)
|
result := bytes2str(byte4)
|
||||||
intNum, _ := strconv.Atoi(result)
|
result = strings.Replace(result, "\n", "", -1)
|
||||||
clientInfo.Process = uint64(intNum)
|
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" {
|
} else if host.Info().OS == "windows" {
|
||||||
// 不知道为何,tcp和udp数量没法获取
|
cmd ,err := Command("cmd","/c netstat -an|find \"TCP\" /c")
|
||||||
byte1 ,err := exec.Command("cmd", "/C","netstat -an|find \"TCP\" /c").Output()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
clientInfo.TCP = 0
|
clientInfo.TCP = 0
|
||||||
fmt.Println("Get TCP count error:",err)
|
fmt.Println("Get TCP count error:",err)
|
||||||
} else {
|
} else {
|
||||||
|
byte1, err := cmd.Output()
|
||||||
result := bytes2str(byte1)
|
result := bytes2str(byte1)
|
||||||
intNum, _ := strconv.Atoi(result)
|
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)
|
clientInfo.TCP = uint64(intNum)
|
||||||
}
|
}
|
||||||
byte2 ,err := exec.Command("cmd", "/C","netstat -an|find \"UDP\" /c").Output()
|
cmd2 ,err := Command("cmd", "/c netstat -an|find \"UDP\" /c")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
clientInfo.UDP = 0
|
clientInfo.UDP = 0
|
||||||
fmt.Println("Get UDP count error:",err)
|
fmt.Println("Get UDP count error:",err)
|
||||||
} else {
|
} else {
|
||||||
|
byte2, err := cmd2.Output()
|
||||||
result := bytes2str(byte2)
|
result := bytes2str(byte2)
|
||||||
intNum, _ := strconv.Atoi(result)
|
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)
|
clientInfo.UDP = uint64(intNum)
|
||||||
}
|
}
|
||||||
pids, err := process.Processes()
|
pids, err := process.Processes()
|
||||||
|
@ -342,18 +398,18 @@ func main() {
|
||||||
tupd()
|
tupd()
|
||||||
trafficCount()
|
trafficCount()
|
||||||
spaceCount()
|
spaceCount()
|
||||||
//TODO:三网延迟,三网丢包,tcp/udp连接数,实时网络速度
|
//TODO:三网延迟,三网丢包,实时网络速度,网络联通
|
||||||
//结构体转json字符串
|
//结构体转json字符串
|
||||||
data, err := jsoniter.MarshalToString(&clientInfo)
|
data, err := jsoniter.MarshalToString(&clientInfo)
|
||||||
//fmt.Println(data)
|
//fmt.Println(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("transformation error: ", err)
|
fmt.Println("Transformation Error: ", err)
|
||||||
}
|
}
|
||||||
info := "update " + data + "\n"
|
info := "update " + data + "\n"
|
||||||
//fmt.Println(info)
|
//fmt.Println(info)
|
||||||
_ , err = conn.Write(str2bytes(info))
|
_ , err = conn.Write(str2bytes(info))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error Sending auth info:", err)
|
fmt.Println("Error Sending Data Info:", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue