输出信息加入时间方便排错

pull/92/head
CHN-STUDENT 2021-01-13 16:08:21 +08:00
parent 26cc870a3d
commit 1b63e5e657
7 changed files with 44 additions and 45 deletions

View File

@ -13,10 +13,8 @@ go build .
```
### TODO
- 日志记录功能
- 外网连接状态
- 三网丢包计算
- 三网延迟计算
- 其他算法优化
### 鸣谢

View File

@ -195,7 +195,7 @@ func SetupCloseHandler() {
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
fmt.Println("\r[main] Ctrl+C pressed in Terminal,Stop client program")
fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [main] Ctrl+C pressed in Terminal,Stop client program")
if mainConnect != nil {
if NETWORKCHECK == true {
pingValueCU.Stop()
@ -220,7 +220,7 @@ var pingValueCM *PingValue
func getCurrentDirectory() string {
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
fmt.Println("[main] Get current directory error")
fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [main] Get current directory error")
os.Exit(-1)
}
return dir
@ -231,15 +231,15 @@ func main() {
SetupCloseHandler()
config := NewConfig()
path := getCurrentDirectory() + "\\config.json"
fmt.Printf("[main]Try to load config file from %s\n",path)
fmt.Printf(time.Now().Format("2006-01-02 15:04:05")+" [main]Try to load config file from %s\n",path)
data, err := ioutil.ReadFile(path)
if err != nil {
fmt.Printf("[main]Read config file error:%s\n",err)
fmt.Printf(time.Now().Format("2006-01-02 15:04:05")+" [main]Read config file error:%s\n",err)
goto Run
}
err = jsoniter.Unmarshal(data, &config)
if err != nil {
fmt.Printf("[main]Parse config file error:%s\n",err)
fmt.Printf(time.Now().Format("2006-01-02 15:04:05")+" [main]Parse config file error:%s\n",err)
}
if config.User != "" {
USER = config.User
@ -312,41 +312,41 @@ func main() {
var err error
mainConnect , err = net.DialTimeout("tcp", SERVER + ":" + strconv.Itoa(PORT),defaulttimeout)
if err != nil {
fmt.Println("[main]Error listening:", err)
fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [main]Error listening:", err)
}
defer mainConnect.Close()
fmt.Println("[main]Listening done,get server info now")
fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [main]Listening done,get server info now")
buff := make([]byte, 1024)
mainConnect.Read(buff)
str := bytes2str(buff)
if strings.Index(str,"Authentication required") > -1 {
fmt.Println("[main]Authentication required,send it to server")
fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [main]Authentication required,send it to server")
auth := str2bytes(USER + ":" + PASSWORD + "\n")
_ , err = mainConnect.Write(auth)
if err != nil {
fmt.Println("[main]Error sending auth info:", err)
fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [main]Error sending auth info:", err)
return
}
buff = make([]byte, 1024)
_ , err = mainConnect.Read(buff)
if err != nil {
fmt.Println("[main]Error getting server data:", err)
fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [main]Error getting server data:", err)
return
}
str = bytes2str(buff)
if strings.Index(str,"Authentication required") < 0 {
if strings.Index(str,"Wrong username and/or password.") > -1 {
fmt.Println("[main]Wrong username and/or password.")
fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [main]Wrong username and/or password.")
return
}else if strings.Index(str,"Authentication successful. Access granted.") > -1 {
fmt.Println("[main]Authentication successful. Access granted.")
fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [main]Authentication successful. Access granted.")
} else {
fmt.Println(str)
return
}
}
} else if strings.Index(str,"You have been banned for 1 minute (Wrong username and/or password.)") > -1{
fmt.Println("[main]You have been banned for 1 minute (Wrong username and/or password.)")
fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [main]You have been banned for 1 minute (Wrong username and/or password.)")
return
} else {
fmt.Println(str)
@ -355,11 +355,11 @@ func main() {
buff = make([]byte, 1024)
_ , err = mainConnect.Read(buff)
if err != nil {
fmt.Println("[main]Error getting server data:", err)
fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [main]Error getting server data:", err)
return
}
str = bytes2str(buff)
fmt.Println("[main]"+str)
fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [main]"+str)
}
//var checkIP int = 0
//if strings.Index(str,"IPv4") > -1 {
@ -401,13 +401,13 @@ func main() {
data, err := clientInfo.MarshalToString()
//fmt.Println(data)
if err != nil {
fmt.Println("[main]Error transforming client info: ", err)
fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [main]Error transforming client info: ", err)
break
}
info := "update " + data + "\n"
_ , err = mainConnect.Write(str2bytes(info))
if err != nil {
fmt.Println("[main]Error sending client info:", err)
fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [main]Error sending client info:", err)
break
}
}

View File

@ -46,7 +46,7 @@ func (netSpeed *NetSpeed) Run() {
var bytesRecv uint64 = 0
netInfo, err := nnet.IOCounters(true)
if err != nil {
fmt.Println("Get network speed error:",err)
fmt.Println(time.Now().Format("2006-01-02 15:04:05")," Get network speed error:",err)
}
for _, v := range netInfo {
if strings.Index(v.Name,"lo") > -1 ||

View File

@ -43,7 +43,7 @@ func (pingValue *PingValue) RunCU() {
t := time.Now()
conn , err := net.DialTimeout("tcp",CU_ADDR,defaulttimeout)
if err != nil {
fmt.Println("[ping]Error try to connect China unicom :", err)
fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [ping]Error try to connect China unicom :", err)
lostConnect = true
lostPacket += 1
}
@ -102,7 +102,7 @@ func (pingValue *PingValue) RunCT() {
t := time.Now()
conn , err := net.DialTimeout("tcp",CT_ADDR,defaulttimeout)
if err != nil {
fmt.Println("[ping]Error try to connect China Telecom :", err)
fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [ping]Error try to connect China Telecom :", err)
lostConnect = true
lostPacket += 1
}
@ -157,7 +157,7 @@ func (pingValue *PingValue) RunCM() {
t := time.Now()
conn , err := net.DialTimeout("tcp",CM_ADDR,defaulttimeout)
if err != nil {
fmt.Println("[ping]Error try to connect China mobile :", err)
fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [ping]Error try to connect China mobile :", err)
lostConnect = true
lostPacket += 1
}

View File

@ -85,7 +85,7 @@ func (run *Run) StartGetRunInfo() {
run.mtx.Lock()
memInfo, err := mem.VirtualMemory()
if err != nil {
fmt.Println("[getInfo]Get memory usage error:",err)
fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [getInfo]Get memory usage error:",err)
run.memoryTotal = 0
run.memoryUsed = 0
} else {
@ -95,18 +95,18 @@ func (run *Run) StartGetRunInfo() {
totalPercent, err := cpu.Percent(time.Second, false)
if err != nil {
fmt.Println("[GetInfo]Get cpu usage error:",err)
fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [GetInfo]Get cpu usage error:",err)
run.CPU = 0.0
} else {
if totalPercent != nil {
run.CPU = totalPercent[0]
} else {
fmt.Println("[getInfo]Get cpu usage error:",err)
fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [getInfo]Get cpu usage error:",err)
}
}
hInfo, err := host.Info()
if err != nil {
fmt.Println("[getInfo]get uptime error",err)
fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [getInfo]get uptime error",err)
run.uptime = 0
} else {
run.uptime = hInfo.Uptime
@ -114,7 +114,7 @@ func (run *Run) StartGetRunInfo() {
//swap 没有造好的轮子,自己加的
swapMemory, err := mem.SwapMemory()
if err != nil {
fmt.Println("[getInfo]Get swap memory error:",err)
fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [getInfo]Get swap memory error:",err)
run.swapTotal = 0
run.swapUsed = 0
} else {
@ -134,7 +134,7 @@ func (run *Run) StartGetRunInfo() {
func trafficCount() {
netInfo, err := nnet.IOCounters(true)
if err != nil {
fmt.Println("[trafficCount]Getting traffic count error:",err)
fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [trafficCount]Getting traffic count error:",err)
}
var bytesSent uint64 = 0
var bytesRecv uint64 = 0
@ -195,7 +195,7 @@ func getLoad() {
// linux or freebsd only
hInfo, err := host.Info()
if err != nil {
fmt.Println("[getLoad]get load info error",err)
fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [getLoad]get load info error",err)
run.load1 = 0.0
run.load5 = 0.0
run.load15 = 0.0
@ -203,7 +203,7 @@ func getLoad() {
if hInfo.OS == "linux" || hInfo.OS == "freebsd" {
l, err := load.Avg()
if err != nil {
fmt.Println("[getLoad]Get CPU loads failed:",err)
fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [getLoad]Get CPU loads failed:",err)
run.load1 = 0.0
run.load5 = 0.0
run.load15 = 0.0

View File

@ -11,52 +11,52 @@ func tupd() {
byte1 ,err := exec.Command("bash", "-c","ss -t|wc -l").Output()
if err != nil {
clientInfo.TCP = 0
fmt.Println("[tupd]Get TCP count error:",err)
fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [tupd]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("[tupd]Get TCP count error::",err)
fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [tupd]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("[tupd]Get UDP count error:",err)
fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [tupd]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("[tupd]Get UDP count error:",err)
fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [tupd]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("[tupd]Get process count error:",err)
fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [tupd]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("[tupd]Get process count error:",err)
fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [tupd]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("[tupd]Get threads count error:",err)
fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [tupd]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("[tupd]Get threads count error:",err)
fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [tupd]Get threads count error:",err)
}
clientInfo.Thread = uint64(intNum)
}

View File

@ -8,13 +8,14 @@ import (
"strconv"
"strings"
"syscall"
"time"
)
func tupd() {
cmd ,err := Command("cmd","/c netstat -an|find \"TCP\" /c")
if err != nil {
clientInfo.TCP = 0
fmt.Println("[tupd]Get TCP count error:",err)
fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [tupd]Get TCP count error:",err)
} else {
byte1, err := cmd.Output()
result := bytes2str(byte1)
@ -22,14 +23,14 @@ func tupd() {
result = strings.Replace(result, "\n", "", -1)
intNum, err := strconv.Atoi(result)
if err != nil {
fmt.Println("[tupd]Get TCP count error:",err)
fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [tupd]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("[tupd]Get UDP count error:",err)
fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [tupd]Get UDP count error:",err)
} else {
byte2, err := cmd2.Output()
result := bytes2str(byte2)
@ -37,13 +38,13 @@ func tupd() {
result = strings.Replace(result, "\n", "", -1)
intNum, err := strconv.Atoi(result)
if err != nil {
fmt.Println("[tupd]Get UDP count error:",err)
fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [tupd]Get UDP count error:",err)
}
clientInfo.UDP = uint64(intNum)
}
pids, err := process.Processes()
if err != nil {
fmt.Println("[tupd]Get process count error:",err)
fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [tupd]Get process count error:",err)
} else {
clientInfo.Process = uint64(len(pids))
}