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

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 ### TODO
- 日志记录功能
- 外网连接状态 - 外网连接状态
- 三网丢包计算
- 三网延迟计算
- 其他算法优化 - 其他算法优化
### 鸣谢 ### 鸣谢

View File

@ -195,7 +195,7 @@ func SetupCloseHandler() {
signal.Notify(c, os.Interrupt, syscall.SIGTERM) signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() { go func() {
<-c <-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 mainConnect != nil {
if NETWORKCHECK == true { if NETWORKCHECK == true {
pingValueCU.Stop() pingValueCU.Stop()
@ -220,7 +220,7 @@ var pingValueCM *PingValue
func getCurrentDirectory() string { func getCurrentDirectory() string {
dir, err := filepath.Abs(filepath.Dir(os.Args[0])) dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil { 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) os.Exit(-1)
} }
return dir return dir
@ -231,15 +231,15 @@ func main() {
SetupCloseHandler() SetupCloseHandler()
config := NewConfig() config := NewConfig()
path := getCurrentDirectory() + "\\config.json" 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) data, err := ioutil.ReadFile(path)
if err != nil { 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 goto Run
} }
err = jsoniter.Unmarshal(data, &config) err = jsoniter.Unmarshal(data, &config)
if err != nil { 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 != "" { if config.User != "" {
USER = config.User USER = config.User
@ -312,41 +312,41 @@ func main() {
var err error var err error
mainConnect , err = net.DialTimeout("tcp", SERVER + ":" + strconv.Itoa(PORT),defaulttimeout) mainConnect , err = net.DialTimeout("tcp", SERVER + ":" + strconv.Itoa(PORT),defaulttimeout)
if err != nil { 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() 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) buff := make([]byte, 1024)
mainConnect.Read(buff) mainConnect.Read(buff)
str := bytes2str(buff) str := bytes2str(buff)
if strings.Index(str,"Authentication required") > -1 { 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") auth := str2bytes(USER + ":" + PASSWORD + "\n")
_ , err = mainConnect.Write(auth) _ , err = mainConnect.Write(auth)
if err != nil { 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 return
} }
buff = make([]byte, 1024) buff = make([]byte, 1024)
_ , err = mainConnect.Read(buff) _ , err = mainConnect.Read(buff)
if err != nil { 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 return
} }
str = bytes2str(buff) str = bytes2str(buff)
if strings.Index(str,"Authentication required") < 0 { if strings.Index(str,"Authentication required") < 0 {
if strings.Index(str,"Wrong username and/or password.") > -1 { 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 return
}else if strings.Index(str,"Authentication successful. Access granted.") > -1 { }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 { } else {
fmt.Println(str) fmt.Println(str)
return return
} }
} }
} else if strings.Index(str,"You have been banned for 1 minute (Wrong username and/or password.)") > -1{ } 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 return
} else { } else {
fmt.Println(str) fmt.Println(str)
@ -355,11 +355,11 @@ func main() {
buff = make([]byte, 1024) buff = make([]byte, 1024)
_ , err = mainConnect.Read(buff) _ , err = mainConnect.Read(buff)
if err != nil { 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 return
} }
str = bytes2str(buff) str = bytes2str(buff)
fmt.Println("[main]"+str) fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [main]"+str)
} }
//var checkIP int = 0 //var checkIP int = 0
//if strings.Index(str,"IPv4") > -1 { //if strings.Index(str,"IPv4") > -1 {
@ -401,13 +401,13 @@ func main() {
data, err := clientInfo.MarshalToString() data, err := clientInfo.MarshalToString()
//fmt.Println(data) //fmt.Println(data)
if err != nil { 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 break
} }
info := "update " + data + "\n" info := "update " + data + "\n"
_ , err = mainConnect.Write(str2bytes(info)) _ , err = mainConnect.Write(str2bytes(info))
if err != nil { 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 break
} }
} }

View File

@ -46,7 +46,7 @@ func (netSpeed *NetSpeed) Run() {
var bytesRecv uint64 = 0 var bytesRecv uint64 = 0
netInfo, err := nnet.IOCounters(true) netInfo, err := nnet.IOCounters(true)
if err != nil { 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 { for _, v := range netInfo {
if strings.Index(v.Name,"lo") > -1 || if strings.Index(v.Name,"lo") > -1 ||

View File

@ -43,7 +43,7 @@ func (pingValue *PingValue) RunCU() {
t := time.Now() t := time.Now()
conn , err := net.DialTimeout("tcp",CU_ADDR,defaulttimeout) conn , err := net.DialTimeout("tcp",CU_ADDR,defaulttimeout)
if err != nil { 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 lostConnect = true
lostPacket += 1 lostPacket += 1
} }
@ -102,7 +102,7 @@ func (pingValue *PingValue) RunCT() {
t := time.Now() t := time.Now()
conn , err := net.DialTimeout("tcp",CT_ADDR,defaulttimeout) conn , err := net.DialTimeout("tcp",CT_ADDR,defaulttimeout)
if err != nil { 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 lostConnect = true
lostPacket += 1 lostPacket += 1
} }
@ -157,7 +157,7 @@ func (pingValue *PingValue) RunCM() {
t := time.Now() t := time.Now()
conn , err := net.DialTimeout("tcp",CM_ADDR,defaulttimeout) conn , err := net.DialTimeout("tcp",CM_ADDR,defaulttimeout)
if err != nil { 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 lostConnect = true
lostPacket += 1 lostPacket += 1
} }

View File

@ -85,7 +85,7 @@ func (run *Run) StartGetRunInfo() {
run.mtx.Lock() run.mtx.Lock()
memInfo, err := mem.VirtualMemory() memInfo, err := mem.VirtualMemory()
if err != nil { 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.memoryTotal = 0
run.memoryUsed = 0 run.memoryUsed = 0
} else { } else {
@ -95,18 +95,18 @@ func (run *Run) StartGetRunInfo() {
totalPercent, err := cpu.Percent(time.Second, false) totalPercent, err := cpu.Percent(time.Second, false)
if err != nil { 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 run.CPU = 0.0
} else { } else {
if totalPercent != nil { if totalPercent != nil {
run.CPU = totalPercent[0] run.CPU = totalPercent[0]
} else { } 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() hInfo, err := host.Info()
if err != nil { 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 run.uptime = 0
} else { } else {
run.uptime = hInfo.Uptime run.uptime = hInfo.Uptime
@ -114,7 +114,7 @@ func (run *Run) StartGetRunInfo() {
//swap 没有造好的轮子,自己加的 //swap 没有造好的轮子,自己加的
swapMemory, err := mem.SwapMemory() swapMemory, err := mem.SwapMemory()
if err != nil { 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.swapTotal = 0
run.swapUsed = 0 run.swapUsed = 0
} else { } else {
@ -134,7 +134,7 @@ func (run *Run) StartGetRunInfo() {
func trafficCount() { func trafficCount() {
netInfo, err := nnet.IOCounters(true) netInfo, err := nnet.IOCounters(true)
if err != nil { 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 bytesSent uint64 = 0
var bytesRecv uint64 = 0 var bytesRecv uint64 = 0
@ -195,7 +195,7 @@ func getLoad() {
// linux or freebsd only // linux or freebsd only
hInfo, err := host.Info() hInfo, err := host.Info()
if err != nil { 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.load1 = 0.0
run.load5 = 0.0 run.load5 = 0.0
run.load15 = 0.0 run.load15 = 0.0
@ -203,7 +203,7 @@ func getLoad() {
if hInfo.OS == "linux" || hInfo.OS == "freebsd" { if hInfo.OS == "linux" || hInfo.OS == "freebsd" {
l, err := load.Avg() l, err := load.Avg()
if err != nil { 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.load1 = 0.0
run.load5 = 0.0 run.load5 = 0.0
run.load15 = 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() 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("[tupd]Get TCP count error:",err) fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [tupd]Get TCP count error:",err)
} else { } else {
result := bytes2str(byte1) result := bytes2str(byte1)
result = strings.Replace(result, "\n", "", -1) result = strings.Replace(result, "\n", "", -1)
intNum, err := strconv.Atoi(result) intNum, err := strconv.Atoi(result)
if err != nil { 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) clientInfo.TCP = uint64(intNum)
} }
byte2 ,err := exec.Command("bash", "-c","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("[tupd]Get UDP count error:",err) fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [tupd]Get UDP count error:",err)
} else { } else {
result := bytes2str(byte2) result := bytes2str(byte2)
result = strings.Replace(result, "\n", "", -1) result = strings.Replace(result, "\n", "", -1)
intNum, err := strconv.Atoi(result) intNum, err := strconv.Atoi(result)
if err != nil { 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) clientInfo.UDP = uint64(intNum)
} }
byte3 ,err := exec.Command("bash", "-c","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("[tupd]Get process count error:",err) fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [tupd]Get process count error:",err)
} else { } else {
result := bytes2str(byte3) result := bytes2str(byte3)
result = strings.Replace(result, "\n", "", -1) result = strings.Replace(result, "\n", "", -1)
intNum, err := strconv.Atoi(result) intNum, err := strconv.Atoi(result)
if err != nil { 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) clientInfo.Process = uint64(intNum)
} }
byte4 ,err := exec.Command("bash", "-c","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("[tupd]Get threads count error:",err) fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [tupd]Get threads count error:",err)
} else { } else {
result := bytes2str(byte4) result := bytes2str(byte4)
result = strings.Replace(result, "\n", "", -1) result = strings.Replace(result, "\n", "", -1)
intNum, err := strconv.Atoi(result) intNum, err := strconv.Atoi(result)
if err != nil { 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) clientInfo.Thread = uint64(intNum)
} }

View File

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