mirror of https://github.com/cppla/ServerStatus
日志记录功能完成,感谢go-logger库
parent
1b63e5e657
commit
fde504da56
|
@ -1 +1,2 @@
|
|||
*.exe
|
||||
*.log
|
|
@ -13,13 +13,12 @@ go build .
|
|||
```
|
||||
|
||||
### TODO
|
||||
- 日志记录功能
|
||||
- 外网连接状态
|
||||
- 其他算法优化
|
||||
|
||||
### 鸣谢
|
||||
|
||||
- https://github.com/cppla/ServerStatus
|
||||
- https://github.com/bitcav/nitr-core
|
||||
- https://github.com/phachon/go-logger
|
||||
- https://github.com/shirou/gopsutil
|
||||
- https://github.com/json-iterator/go
|
||||
|
|
|
@ -3,7 +3,10 @@ module awesomeProject
|
|||
go 1.15
|
||||
|
||||
require (
|
||||
github.com/bitcav/nitr-core v0.0.0-20200823224936-5500912f5599
|
||||
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect
|
||||
github.com/go-ole/go-ole v1.2.4 // indirect
|
||||
github.com/json-iterator/go v1.1.10
|
||||
github.com/phachon/go-logger v0.0.0-20191215032019-86e4227f71ea
|
||||
github.com/shirou/gopsutil v2.20.7+incompatible
|
||||
golang.org/x/sys v0.0.0-20200106162015-b016eb3dc98e // indirect
|
||||
)
|
||||
|
|
|
@ -1,49 +1,36 @@
|
|||
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d h1:G0m3OIz70MZUWq3EgK3CesDbo8upS2Vm9/P3FtgI+Jk=
|
||||
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg=
|
||||
github.com/bitcav/nitr-core v0.0.0-20200823224936-5500912f5599 h1:/nmGHaIU/fngJx5CpI4aIUP9TJxuZQkeI3ei8cfbKLo=
|
||||
github.com/bitcav/nitr-core v0.0.0-20200823224936-5500912f5599/go.mod h1:OdZtuocMmwA7iXbeIwcb740q4PbeVJKJu1vJCyzfYHo=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
|
||||
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
|
||||
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
|
||||
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
|
||||
github.com/go-ole/go-ole v1.2.4 h1:nNBDSCOigTSiarFpYE9J/KtEA1IOW4CNeqT9TQDqCxI=
|
||||
github.com/go-ole/go-ole v1.2.4/go.mod h1:XCwSNxSkXRo4vlyPy93sltvi/qJq0jqQhjqQNIwKuxM=
|
||||
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
|
||||
github.com/jaypipes/ghw v0.6.1 h1:Ewt3mdpiyhWotGyzg1ursV/6SnToGcG4215X6rR2af8=
|
||||
github.com/jaypipes/ghw v0.6.1/go.mod h1:QOXppNRCLGYR1H+hu09FxZPqjNt09bqUZUnOL3Rcero=
|
||||
github.com/jaypipes/pcidb v0.5.0 h1:4W5gZ+G7QxydevI8/MmmKdnIPJpURqJ2JNXTzfLxF5c=
|
||||
github.com/jaypipes/pcidb v0.5.0/go.mod h1:L2RGk04sfRhp5wvHO0gfRAMoLY/F3PKv/nwJeVoho0o=
|
||||
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
|
||||
github.com/json-iterator/go v1.1.10 h1:Kz6Cvnvv2wGdaG/V8yMvfkmNiXq9Ya2KUv4rouJJr68=
|
||||
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/mitchellh/go-homedir v1.0.0 h1:vKb8ShqSby24Yrqr/yDYkuFz8d0WUjys40rvnGC8aR0=
|
||||
github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
|
||||
github.com/mitchellh/go-ps v1.0.0/go.mod h1:J4lOc8z8yJs6vUwklHw2XEIiT4z4C40KtWVN3nvg8Pg=
|
||||
github.com/mailru/easyjson v0.7.0 h1:aizVhC/NAAcKWb+5QsU1iNOZb4Yws5UO2I+aIprQITM=
|
||||
github.com/mailru/easyjson v0.7.0/go.mod h1:KAzv3t3aY1NaHWoQz1+4F1ccyAH66Jk7yos7ldAVICs=
|
||||
github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA=
|
||||
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
|
||||
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
|
||||
github.com/mattn/go-isatty v0.0.11 h1:FxPOTFNqGkuDUGi3H/qkUbQO4ZiBa2brKq5r0l8TGeM=
|
||||
github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg=
|
||||
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
|
||||
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/phachon/go-logger v0.0.0-20191215032019-86e4227f71ea h1:IkOONr/u7Wy+j2R4r1eMV8PEuN4kmOhZZNaYxDOF+KQ=
|
||||
github.com/phachon/go-logger v0.0.0-20191215032019-86e4227f71ea/go.mod h1:WBIWFH/iYYvuApCvPU+/R6hfX6v0Ogu4apwf0UgzVF0=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU=
|
||||
github.com/shirou/gopsutil v2.20.7+incompatible h1:Ymv4OD12d6zm+2yONe39VSmp2XooJe8za7ngOLW/o/w=
|
||||
github.com/shirou/gopsutil v2.20.7+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
|
||||
github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ=
|
||||
github.com/spf13/pflag v1.0.2/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200106162015-b016eb3dc98e h1:LwyF2AFISC9nVbS6MgzsaQNSUsRXI49GS+YQ5KX/QH0=
|
||||
golang.org/x/sys v0.0.0-20200106162015-b016eb3dc98e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
howett.net/plist v0.0.0-20181124034731-591f970eefbb/go.mod h1:vMygbs4qMhSZSc4lCUl2OEE+rDiIIJAIdR4m7MiMcm0=
|
||||
|
|
|
@ -11,6 +11,7 @@ package main
|
|||
import (
|
||||
"fmt"
|
||||
"github.com/json-iterator/go"
|
||||
"github.com/phachon/go-logger"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"os"
|
||||
|
@ -195,7 +196,8 @@ func SetupCloseHandler() {
|
|||
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
|
||||
go func() {
|
||||
<-c
|
||||
fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [main] Ctrl+C pressed in Terminal,Stop client program")
|
||||
logger.Info("[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()
|
||||
|
@ -217,6 +219,7 @@ var pingValueCU *PingValue
|
|||
var pingValueCT *PingValue
|
||||
var pingValueCM *PingValue
|
||||
|
||||
|
||||
func getCurrentDirectory() string {
|
||||
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
|
||||
if err != nil {
|
||||
|
@ -226,20 +229,57 @@ func getCurrentDirectory() string {
|
|||
return dir
|
||||
}
|
||||
|
||||
var logger *go_logger.Logger
|
||||
|
||||
func main() {
|
||||
|
||||
logger = go_logger.NewLogger()
|
||||
logger.Detach("console")
|
||||
|
||||
// 命令行输出配置
|
||||
consoleConfig := &go_logger.ConsoleConfig{
|
||||
Color: true, // 命令行输出字符串是否显示颜色
|
||||
JsonFormat: true, // 命令行输出字符串是否格式化
|
||||
Format: "", // 如果输出的不是 json 字符串,JsonFormat: false, 自定义输出的格式
|
||||
}
|
||||
// 添加 console 为 logger 的一个输出
|
||||
logger.Attach("console", go_logger.LOGGER_LEVEL_DEBUG, consoleConfig)
|
||||
|
||||
// 文件输出配置
|
||||
fileConfig := &go_logger.FileConfig {
|
||||
Filename : "./test.log", // 日志输出文件名,不自动存在
|
||||
// 如果要将单独的日志分离为文件,请配置LealFrimeNem参数。
|
||||
LevelFileName : map[int]string {
|
||||
logger.LoggerLevel("error"): "./error.log", // Error 级别日志被写入 error .log 文件
|
||||
logger.LoggerLevel("info"): "./info.log", // Info 级别日志被写入到 info.log 文件中
|
||||
logger.LoggerLevel("debug"): "./debug.log", // Debug 级别日志被写入到 debug.log 文件中
|
||||
},
|
||||
MaxSize : 1024 * 1024, // 文件最大值(KB),默认值0不限
|
||||
MaxLine : 100000, // 文件最大行数,默认 0 不限制
|
||||
DateSlice : "d", // 文件根据日期切分, 支持 "Y" (年), "m" (月), "d" (日), "H" (时), 默认 "no", 不切分
|
||||
JsonFormat: true, // 写入文件的数据是否 json 格式化
|
||||
Format: "", // 如果写入文件的数据不 json 格式化,自定义日志格式
|
||||
}
|
||||
// 添加 file 为 logger 的一个输出
|
||||
logger.Attach("file", go_logger.LOGGER_LEVEL_DEBUG, fileConfig)
|
||||
logger.Info(" [main]Logger init complete")
|
||||
|
||||
// Setup our Ctrl+C handler
|
||||
SetupCloseHandler()
|
||||
config := NewConfig()
|
||||
path := getCurrentDirectory() + "\\config.json"
|
||||
fmt.Printf(time.Now().Format("2006-01-02 15:04:05")+" [main]Try to load config file from %s\n",path)
|
||||
logger.Infof(" [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(time.Now().Format("2006-01-02 15:04:05")+" [main]Read config file error:%s\n",err)
|
||||
logger.Alertf(" [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(time.Now().Format("2006-01-02 15:04:05")+" [main]Parse config file error:%s\n",err)
|
||||
logger.Errorf(" [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
|
||||
|
@ -308,58 +348,71 @@ func main() {
|
|||
}
|
||||
run = NewRunInfo()
|
||||
run.StartGetRunInfo()
|
||||
logger.Info("[main] Try to connect server")
|
||||
for {
|
||||
var err error
|
||||
mainConnect , err = net.DialTimeout("tcp", SERVER + ":" + strconv.Itoa(PORT),defaulttimeout)
|
||||
if err != nil {
|
||||
fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [main]Error listening:", err)
|
||||
logger.Errorf("[main]Error listening:%s\n", err)
|
||||
//fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [main]Error listening:", err)
|
||||
}
|
||||
defer mainConnect.Close()
|
||||
fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [main]Listening done,get server info now")
|
||||
logger.Info("[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(time.Now().Format("2006-01-02 15:04:05")," [main]Authentication required,send it to server")
|
||||
logger.Info("[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(time.Now().Format("2006-01-02 15:04:05")," [main]Error sending auth info:", err)
|
||||
logger.Errorf("[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(time.Now().Format("2006-01-02 15:04:05")," [main]Error getting server data:", err)
|
||||
logger.Errorf("[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(time.Now().Format("2006-01-02 15:04:05")," [main]Wrong username and/or password.")
|
||||
logger.Errorf(" [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(time.Now().Format("2006-01-02 15:04:05")," [main]Authentication successful. Access granted.")
|
||||
logger.Info("[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)
|
||||
logger.Error(str)
|
||||
//fmt.Println(str)
|
||||
return
|
||||
}
|
||||
}
|
||||
} else if strings.Index(str,"You have been banned for 1 minute (Wrong username and/or password.)") > -1{
|
||||
fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [main]You have been banned for 1 minute (Wrong username and/or password.)")
|
||||
logger.Alert("[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 {
|
||||
logger.Error(str)
|
||||
fmt.Println(str)
|
||||
}
|
||||
if strings.Index(str,"You are connecting via") < 0 {
|
||||
buff = make([]byte, 1024)
|
||||
_ , err = mainConnect.Read(buff)
|
||||
if err != nil {
|
||||
fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [main]Error getting server data:", err)
|
||||
logger.Errorf("[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(time.Now().Format("2006-01-02 15:04:05")," [main]"+str)
|
||||
//logger.Info("[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,12 +454,15 @@ func main() {
|
|||
data, err := clientInfo.MarshalToString()
|
||||
//fmt.Println(data)
|
||||
if err != nil {
|
||||
fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [main]Error transforming client info: ", err)
|
||||
logger.Errorf("[main]Error transforming client info: ", err)
|
||||
//fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [main]Error transforming client info: ", err)
|
||||
break
|
||||
}
|
||||
logger.Info("\nInfo:\n"+data+"\n")
|
||||
info := "update " + data + "\n"
|
||||
_ , err = mainConnect.Write(str2bytes(info))
|
||||
if err != nil {
|
||||
logger.Errorf("[main]Error sending client info:", err)
|
||||
fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [main]Error sending client info:", err)
|
||||
break
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
nnet "github.com/shirou/gopsutil/net"
|
||||
"strings"
|
||||
"sync"
|
||||
|
@ -46,7 +45,8 @@ func (netSpeed *NetSpeed) Run() {
|
|||
var bytesRecv uint64 = 0
|
||||
netInfo, err := nnet.IOCounters(true)
|
||||
if err != nil {
|
||||
fmt.Println(time.Now().Format("2006-01-02 15:04:05")," Get network speed error:",err)
|
||||
logger.Errorf("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 ||
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"sync"
|
||||
"time"
|
||||
|
@ -43,7 +42,8 @@ func (pingValue *PingValue) RunCU() {
|
|||
t := time.Now()
|
||||
conn , err := net.DialTimeout("tcp",CU_ADDR,defaulttimeout)
|
||||
if err != nil {
|
||||
fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [ping]Error try to connect China unicom :", err)
|
||||
logger.Alertf("[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,8 @@ func (pingValue *PingValue) RunCT() {
|
|||
t := time.Now()
|
||||
conn , err := net.DialTimeout("tcp",CT_ADDR,defaulttimeout)
|
||||
if err != nil {
|
||||
fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [ping]Error try to connect China Telecom :", err)
|
||||
logger.Alertf("[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 +158,8 @@ func (pingValue *PingValue) RunCM() {
|
|||
t := time.Now()
|
||||
conn , err := net.DialTimeout("tcp",CM_ADDR,defaulttimeout)
|
||||
if err != nil {
|
||||
fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [ping]Error try to connect China mobile :", err)
|
||||
logger.Alertf("[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
|
||||
}
|
||||
|
|
|
@ -1,16 +1,15 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
"github.com/shirou/gopsutil/host"
|
||||
"github.com/shirou/gopsutil/cpu"
|
||||
"github.com/shirou/gopsutil/disk"
|
||||
"github.com/shirou/gopsutil/host"
|
||||
"github.com/shirou/gopsutil/load"
|
||||
"github.com/shirou/gopsutil/mem"
|
||||
nnet "github.com/shirou/gopsutil/net"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Run struct {
|
||||
|
@ -85,7 +84,8 @@ func (run *Run) StartGetRunInfo() {
|
|||
run.mtx.Lock()
|
||||
memInfo, err := mem.VirtualMemory()
|
||||
if err != nil {
|
||||
fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [getInfo]Get memory usage error:",err)
|
||||
logger.Errorf("[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,21 @@ func (run *Run) StartGetRunInfo() {
|
|||
|
||||
totalPercent, err := cpu.Percent(time.Second, false)
|
||||
if err != nil {
|
||||
fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [GetInfo]Get cpu usage error:",err)
|
||||
logger.Errorf("[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(time.Now().Format("2006-01-02 15:04:05")," [getInfo]Get cpu usage error:",err)
|
||||
logger.Errorf("[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(time.Now().Format("2006-01-02 15:04:05")," [getInfo]get uptime error",err)
|
||||
logger.Errorf("[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 +117,8 @@ func (run *Run) StartGetRunInfo() {
|
|||
//swap 没有造好的轮子,自己加的
|
||||
swapMemory, err := mem.SwapMemory()
|
||||
if err != nil {
|
||||
fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [getInfo]Get swap memory error:",err)
|
||||
logger.Errorf("[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 +138,8 @@ func (run *Run) StartGetRunInfo() {
|
|||
func trafficCount() {
|
||||
netInfo, err := nnet.IOCounters(true)
|
||||
if err != nil {
|
||||
fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [trafficCount]Getting traffic count error:",err)
|
||||
logger.Errorf("[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 +200,8 @@ func getLoad() {
|
|||
// linux or freebsd only
|
||||
hInfo, err := host.Info()
|
||||
if err != nil {
|
||||
fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [getLoad]get load info error",err)
|
||||
logger.Errorf("[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 +209,8 @@ func getLoad() {
|
|||
if hInfo.OS == "linux" || hInfo.OS == "freebsd" {
|
||||
l, err := load.Avg()
|
||||
if err != nil {
|
||||
fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [getLoad]Get CPU loads failed:",err)
|
||||
logger.Errorf("[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
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
// +build linux
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
@ -11,52 +11,60 @@ func tupd() {
|
|||
byte1 ,err := exec.Command("bash", "-c","ss -t|wc -l").Output()
|
||||
if err != nil {
|
||||
clientInfo.TCP = 0
|
||||
fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [tupd]Get TCP count error:",err)
|
||||
logger.Errorf("[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(time.Now().Format("2006-01-02 15:04:05")," [tupd]Get TCP count error::",err)
|
||||
logger.Errorf("[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(time.Now().Format("2006-01-02 15:04:05")," [tupd]Get UDP count error:",err)
|
||||
logger.Errorf("[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(time.Now().Format("2006-01-02 15:04:05")," [tupd]Get UDP count error:",err)
|
||||
logger.Errorf("[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(time.Now().Format("2006-01-02 15:04:05")," [tupd]Get process count error:",err)
|
||||
logger.Errorf("[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(time.Now().Format("2006-01-02 15:04:05")," [tupd]Get process count error:",err)
|
||||
logger.Errorf("[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(time.Now().Format("2006-01-02 15:04:05")," [tupd]Get threads count error:",err)
|
||||
logger.Errorf("[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(time.Now().Format("2006-01-02 15:04:05")," [tupd]Get threads count error:",err)
|
||||
logger.Errorf("[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)
|
||||
}
|
||||
|
|
|
@ -1,21 +1,20 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/shirou/gopsutil/process"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
func tupd() {
|
||||
cmd ,err := Command("cmd","/c netstat -an|find \"TCP\" /c")
|
||||
if err != nil {
|
||||
clientInfo.TCP = 0
|
||||
fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [tupd]Get TCP count error:",err)
|
||||
logger.Errorf("[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)
|
||||
|
@ -23,14 +22,16 @@ func tupd() {
|
|||
result = strings.Replace(result, "\n", "", -1)
|
||||
intNum, err := strconv.Atoi(result)
|
||||
if err != nil {
|
||||
fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [tupd]Get TCP count error:",err)
|
||||
logger.Errorf("[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(time.Now().Format("2006-01-02 15:04:05")," [tupd]Get UDP count error:",err)
|
||||
logger.Errorf("[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)
|
||||
|
@ -38,13 +39,15 @@ func tupd() {
|
|||
result = strings.Replace(result, "\n", "", -1)
|
||||
intNum, err := strconv.Atoi(result)
|
||||
if err != nil {
|
||||
fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [tupd]Get UDP count error:",err)
|
||||
logger.Errorf("[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(time.Now().Format("2006-01-02 15:04:05")," [tupd]Get process count error:",err)
|
||||
logger.Errorf("[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))
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue