feat: add reporter data collect and send messages cyclically
This commit is contained in:
parent
9b8bb34378
commit
87af8ea07d
@ -3,9 +3,15 @@ module tianji-reporter
|
|||||||
go 1.21.1
|
go 1.21.1
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/gofrs/uuid v4.4.0+incompatible // indirect
|
github.com/json-iterator/go v1.1.12
|
||||||
github.com/gomodule/redigo v1.8.9 // indirect
|
github.com/shirou/gopsutil/v3 v3.20.10
|
||||||
github.com/googollee/go-socket.io v1.8.0-rc.1 // indirect
|
)
|
||||||
github.com/gorilla/websocket v1.5.0 // indirect
|
|
||||||
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1 // indirect
|
require (
|
||||||
|
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect
|
||||||
|
github.com/go-ole/go-ole v1.2.4 // indirect
|
||||||
|
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
|
||||||
|
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||||
|
github.com/stretchr/testify v1.8.2 // indirect
|
||||||
|
golang.org/x/sys v0.1.0 // indirect
|
||||||
)
|
)
|
||||||
|
@ -1,66 +1,34 @@
|
|||||||
|
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/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
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/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/gofrs/uuid v4.0.0+incompatible h1:1SD/1F5pU8p29ybwgQSwpQk+mwdRrXCYuPhW6m+TnJw=
|
github.com/go-ole/go-ole v1.2.4 h1:nNBDSCOigTSiarFpYE9J/KtEA1IOW4CNeqT9TQDqCxI=
|
||||||
github.com/gofrs/uuid v4.0.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
|
github.com/go-ole/go-ole v1.2.4/go.mod h1:XCwSNxSkXRo4vlyPy93sltvi/qJq0jqQhjqQNIwKuxM=
|
||||||
github.com/gofrs/uuid v4.4.0+incompatible h1:3qXRTX8/NbyulANqlc0lchS1gqAVxRgsuW1YrTJupqA=
|
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||||
github.com/gofrs/uuid v4.4.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
|
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||||
github.com/gomodule/redigo v1.8.4 h1:Z5JUg94HMTR1XpwBaSH4vq3+PNSIykBLxMdglbw10gg=
|
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||||
github.com/gomodule/redigo v1.8.4/go.mod h1:P9dn9mFrCBvWhGE1wpxx6fgq7BAeLBk+UUUzlpkBYO0=
|
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
|
||||||
github.com/gomodule/redigo v1.8.9 h1:Sl3u+2BI/kk+VEatbj0scLdrFhjPmbxOc1myhDP41ws=
|
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||||
github.com/gomodule/redigo v1.8.9/go.mod h1:7ArFNvsTjH8GMMzB4uy1snslv2BwmginuMs06a1uzZE=
|
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
|
||||||
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
||||||
github.com/googollee/go-socket.io v1.7.0 h1:ODcQSAvVIPvKozXtUGuJDV3pLwdpBLDs1Uoq/QHIlY8=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
github.com/googollee/go-socket.io v1.7.0/go.mod h1:0vGP8/dXR9SZUMMD4+xxaGo/lohOw3YWMh2WRiWeKxg=
|
|
||||||
github.com/googollee/go-socket.io v1.8.0-rc.1 h1:Y5DV+pKDw2KFBtdEyxBp8mSuuU4XS3eHGNb2E3bLACI=
|
|
||||||
github.com/googollee/go-socket.io v1.8.0-rc.1/go.mod h1:oZhC7XylbziHxXhVdXvz6qQB0/jmNc4V3d9jgaEBKHI=
|
|
||||||
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
|
|
||||||
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
|
||||||
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
|
|
||||||
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/shirou/gopsutil/v3 v3.20.10 h1:7zomV9HJv6UGk225YtvEa5+camNLpbua3MAz/GqiVJY=
|
||||||
|
github.com/shirou/gopsutil/v3 v3.20.10/go.mod h1:igHnfak0qnw1biGeI2qKQvu0ZkwvEkUcCLlYhZzdr/4=
|
||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||||
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
|
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||||
|
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
|
||||||
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
golang.org/x/sys v0.0.0-20201024232916-9f70ab9862d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
|
||||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
|
||||||
golang.org/x/crypto v0.1.0/go.mod h1:RecgLatLF4+eUMCP1PoPZQb+cVrJcOPbHkTkbkB9sbw=
|
|
||||||
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1 h1:MGwJjxBy0HJshjDNfLsYO8xppfqWlA5ZT9OhtUUhTNw=
|
|
||||||
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
|
|
||||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
|
||||||
golang.org/x/mod v0.6.0/go.mod h1:4mET923SAdbXp2ki8ey+zGs1SLqsuM2Y0uvdZR/fUNI=
|
|
||||||
golang.org/x/mod v0.11.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
|
||||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
|
||||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
|
||||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
|
||||||
golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco=
|
|
||||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
|
||||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
|
||||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
|
||||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
|
||||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
|
||||||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
|
||||||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
|
||||||
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
|
||||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
|
||||||
golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
|
||||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
|
||||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
|
||||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
|
||||||
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
|
||||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
|
||||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
|
||||||
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
|
||||||
golang.org/x/tools v0.2.0/go.mod h1:y4OqIKeOV/fWJetJ8bXPU1sEVniLMIyDAZWeHdV+NTA=
|
|
||||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
|
||||||
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
|
||||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
@ -1,30 +1,31 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
jsoniter "github.com/json-iterator/go"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
)
|
"tianji-reporter/utils"
|
||||||
|
"time"
|
||||||
var (
|
|
||||||
Url = flag.String("url", "", "The http url of tianji, for example: https://tianji.msgbyte.com")
|
|
||||||
WorkspaceId = flag.String("workspace", "", "The workspace id for tianji, this should be a uuid")
|
|
||||||
Name = flag.String("name", "", "The identification name for this machine")
|
|
||||||
Interval = flag.Float64("interval", 5.0, "Input the INTERVAL")
|
|
||||||
IsVnstat = flag.Bool("vnstat", false, "Use vnstat for traffic statistics, linux only")
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type ReportData struct {
|
type ReportData struct {
|
||||||
WorkspaceId string `json:"workspaceId"`
|
WorkspaceId string `json:"workspaceId"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Hostname string `json:"hostname"`
|
Hostname string `json:"hostname"`
|
||||||
Payload any `json:"payload"`
|
Payload utils.ReportDataPayload `json:"payload"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
Url = flag.String("url", "", "The http url of tianji, for example: https://tianji.msgbyte.com")
|
||||||
|
WorkspaceId = flag.String("workspace", "", "The workspace id for tianji, this should be a uuid")
|
||||||
|
Name = flag.String("name", "", "The identification name for this machine")
|
||||||
|
Interval = flag.Int("interval", 20.0, "Input the INTERVAL")
|
||||||
|
IsVnstat = flag.Bool("vnstat", false, "Use vnstat for traffic statistics, linux only")
|
||||||
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
@ -50,43 +51,52 @@ func main() {
|
|||||||
name = hostname
|
name = hostname
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interval := *Interval
|
||||||
|
|
||||||
|
ticker := time.Tick(time.Duration(interval) * time.Second)
|
||||||
|
|
||||||
|
for {
|
||||||
|
<-ticker
|
||||||
|
log.Println("Send report data to:", parsedURL.String())
|
||||||
sendUDPTestPack(*parsedURL, ReportData{
|
sendUDPTestPack(*parsedURL, ReportData{
|
||||||
WorkspaceId: *WorkspaceId,
|
WorkspaceId: *WorkspaceId,
|
||||||
Name: name,
|
Name: name,
|
||||||
Hostname: hostname,
|
Hostname: hostname,
|
||||||
|
Payload: utils.GetReportDataPaylod(interval, *IsVnstat),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func sendUDPTestPack(url url.URL, payload ReportData) {
|
func sendUDPTestPack(url url.URL, payload ReportData) {
|
||||||
// parse target url
|
// parse target url
|
||||||
addr, err := net.ResolveUDPAddr("udp", url.Hostname()+":"+url.Port())
|
addr, err := net.ResolveUDPAddr("udp", url.Hostname()+":"+url.Port())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error resolving address:", err)
|
log.Println("Error resolving address:", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// create UDP connection
|
// create UDP connection
|
||||||
conn, err := net.DialUDP("udp", nil, addr)
|
conn, err := net.DialUDP("udp", nil, addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error creating connection:", err)
|
log.Println("Error creating connection:", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
|
||||||
// serialized message
|
// serialized message
|
||||||
jsonData, err := json.Marshal(payload)
|
jsonData, err := jsoniter.Marshal(payload)
|
||||||
fmt.Printf("[Report] %s\n", jsonData)
|
log.Printf("[Report] %s\n", jsonData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error encoding JSON:", err)
|
log.Println("Error encoding JSON:", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send message
|
// Send message
|
||||||
_, err = conn.Write(jsonData)
|
_, err = conn.Write(jsonData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error sending message:", err)
|
log.Println("Error sending message:", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("Message sent successfully!")
|
log.Println("Message sent successfully!")
|
||||||
}
|
}
|
221
reporter/utils/utils.go
Normal file
221
reporter/utils/utils.go
Normal file
@ -0,0 +1,221 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
"github.com/shirou/gopsutil/v3/cpu"
|
||||||
|
"github.com/shirou/gopsutil/v3/disk"
|
||||||
|
"github.com/shirou/gopsutil/v3/host"
|
||||||
|
"github.com/shirou/gopsutil/v3/load"
|
||||||
|
"github.com/shirou/gopsutil/v3/mem"
|
||||||
|
pNet "github.com/shirou/gopsutil/v3/net"
|
||||||
|
"log"
|
||||||
|
"math"
|
||||||
|
"net"
|
||||||
|
"os/exec"
|
||||||
|
"runtime"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
"unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ReportDataPayload struct {
|
||||||
|
Uptime uint64 `json:"uptime"`
|
||||||
|
Load jsoniter.Number `json:"load"`
|
||||||
|
MemoryTotal uint64 `json:"memory_total"`
|
||||||
|
MemoryUsed uint64 `json:"memory_used"`
|
||||||
|
SwapTotal uint64 `json:"swap_total"`
|
||||||
|
SwapUsed uint64 `json:"swap_used"`
|
||||||
|
HddTotal uint64 `json:"hdd_total"`
|
||||||
|
HddUsed uint64 `json:"hdd_used"`
|
||||||
|
CPU jsoniter.Number `json:"cpu"`
|
||||||
|
NetworkTx uint64 `json:"network_tx"`
|
||||||
|
NetworkRx uint64 `json:"network_rx"`
|
||||||
|
NetworkIn uint64 `json:"network_in"`
|
||||||
|
NetworkOut uint64 `json:"network_out"`
|
||||||
|
}
|
||||||
|
|
||||||
|
var checkIP int
|
||||||
|
|
||||||
|
func GetReportDataPaylod(interval int, isVnstat bool) ReportDataPayload {
|
||||||
|
payload := ReportDataPayload{}
|
||||||
|
|
||||||
|
var netIn, netOut, netRx, netTx uint64
|
||||||
|
if !isVnstat {
|
||||||
|
netIn, netOut, netRx, netTx = getTraffic(interval)
|
||||||
|
} else {
|
||||||
|
_, _, netRx, netTx = getTraffic(interval)
|
||||||
|
var err error
|
||||||
|
netIn, netOut, err = getTrafficVnstat()
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Please check if the installation of vnStat is correct")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
memoryTotal, memoryUsed, swapTotal, swapUsed := getMemory()
|
||||||
|
hddTotal, hddUsed := getDisk(interval)
|
||||||
|
payload.CPU = jsoniter.Number(fmt.Sprintf("%.1f", getCpu(interval)))
|
||||||
|
payload.Load = jsoniter.Number(fmt.Sprintf("%.2f", getLoad()))
|
||||||
|
payload.Uptime = getUptime()
|
||||||
|
payload.MemoryTotal = memoryTotal
|
||||||
|
payload.MemoryUsed = memoryUsed
|
||||||
|
payload.SwapTotal = swapTotal
|
||||||
|
payload.SwapUsed = swapUsed
|
||||||
|
payload.HddTotal = hddTotal
|
||||||
|
payload.HddUsed = hddUsed
|
||||||
|
payload.NetworkRx = netRx
|
||||||
|
payload.NetworkTx = netTx
|
||||||
|
payload.NetworkIn = netIn
|
||||||
|
payload.NetworkOut = netOut
|
||||||
|
|
||||||
|
return payload
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fork from https://github.com/cokemine/ServerStatus-goclient/blob/master/pkg/status/status.go
|
||||||
|
*/
|
||||||
|
func getMemory() (uint64, uint64, uint64, uint64) {
|
||||||
|
memory, _ := mem.VirtualMemory()
|
||||||
|
|
||||||
|
if runtime.GOOS == "linux" {
|
||||||
|
return memory.Total / 1024.0, memory.Used / 1024.0, memory.SwapTotal / 1024.0, (memory.SwapTotal - memory.SwapFree) / 1024.0
|
||||||
|
} else {
|
||||||
|
swap, _ := mem.SwapMemory()
|
||||||
|
return memory.Total / 1024.0, memory.Used / 1024.0, swap.Total / 1024.0, swap.Used / 1024.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func getUptime() uint64 {
|
||||||
|
bootTime, _ := host.BootTime()
|
||||||
|
return uint64(time.Now().Unix()) - bootTime
|
||||||
|
}
|
||||||
|
|
||||||
|
func getLoad() float64 {
|
||||||
|
theLoad, _ := load.Avg()
|
||||||
|
return theLoad.Load1
|
||||||
|
}
|
||||||
|
|
||||||
|
var cachedFs = make(map[string]struct{})
|
||||||
|
var timer = 0
|
||||||
|
|
||||||
|
func getDisk(interval int) (uint64, uint64) {
|
||||||
|
var (
|
||||||
|
size, used uint64
|
||||||
|
)
|
||||||
|
if timer <= 0 {
|
||||||
|
diskList, _ := disk.Partitions(false)
|
||||||
|
devices := make(map[string]struct{})
|
||||||
|
for _, d := range diskList {
|
||||||
|
_, ok := devices[d.Device]
|
||||||
|
if !ok && checkValidFs(d.Fstype) {
|
||||||
|
cachedFs[d.Mountpoint] = struct{}{}
|
||||||
|
devices[d.Device] = struct{}{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
timer = 300
|
||||||
|
}
|
||||||
|
timer -= interval
|
||||||
|
for k := range cachedFs {
|
||||||
|
usage, err := disk.Usage(k)
|
||||||
|
if err != nil {
|
||||||
|
delete(cachedFs, k)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
size += usage.Total / 1024.0 / 1024.0
|
||||||
|
used += usage.Used / 1024.0 / 1024.0
|
||||||
|
}
|
||||||
|
return size, used
|
||||||
|
}
|
||||||
|
|
||||||
|
func getCpu(interval int) float64 {
|
||||||
|
cpuInfo, _ := cpu.Percent(time.Duration(interval)*time.Second, false)
|
||||||
|
return math.Round(cpuInfo[0]*10) / 10
|
||||||
|
}
|
||||||
|
|
||||||
|
func getNetwork(checkIP int) bool {
|
||||||
|
var HOST string
|
||||||
|
if checkIP == 4 {
|
||||||
|
HOST = "8.8.8.8:53"
|
||||||
|
} else if checkIP == 6 {
|
||||||
|
HOST = "[2001:4860:4860::8888]:53"
|
||||||
|
} else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
conn, err := net.DialTimeout("tcp", HOST, 2*time.Second)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if conn.Close() != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
var prevNetIn uint64
|
||||||
|
var prevNetOut uint64
|
||||||
|
|
||||||
|
func getTraffic(interval int) (uint64, uint64, uint64, uint64) {
|
||||||
|
var (
|
||||||
|
netIn, netOut uint64
|
||||||
|
)
|
||||||
|
netInfo, _ := pNet.IOCounters(true)
|
||||||
|
for _, v := range netInfo {
|
||||||
|
if checkInterface(v.Name) {
|
||||||
|
netIn += v.BytesRecv
|
||||||
|
netOut += v.BytesSent
|
||||||
|
}
|
||||||
|
}
|
||||||
|
rx := uint64(float64(netIn-prevNetIn) / float64(interval))
|
||||||
|
tx := uint64(float64(netOut-prevNetOut) / float64(interval))
|
||||||
|
prevNetIn = netIn
|
||||||
|
prevNetOut = netOut
|
||||||
|
return netIn, netOut, rx, tx
|
||||||
|
}
|
||||||
|
|
||||||
|
func getTrafficVnstat() (uint64, uint64, error) {
|
||||||
|
buf, err := exec.Command("vnstat", "--oneline", "b").Output()
|
||||||
|
if err != nil {
|
||||||
|
return 0, 0, err
|
||||||
|
}
|
||||||
|
vData := strings.Split(BytesToString(buf), ";")
|
||||||
|
if len(vData) != 15 {
|
||||||
|
// Not enough data available yet.
|
||||||
|
return 0, 0, nil
|
||||||
|
}
|
||||||
|
netIn, err := strconv.ParseUint(vData[8], 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return 0, 0, err
|
||||||
|
}
|
||||||
|
netOut, err := strconv.ParseUint(vData[9], 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return 0, 0, err
|
||||||
|
}
|
||||||
|
return netIn, netOut, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var invalidInterface = []string{"lo", "tun", "kube", "docker", "vmbr", "br-", "vnet", "veth"}
|
||||||
|
|
||||||
|
func checkInterface(name string) bool {
|
||||||
|
for _, v := range invalidInterface {
|
||||||
|
if strings.Contains(name, v) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
var validFs = []string{"ext4", "ext3", "ext2", "reiserfs", "jfs", "btrfs", "fuseblk", "zfs", "simfs", "ntfs", "fat32", "exfat", "xfs", "apfs"}
|
||||||
|
|
||||||
|
func checkValidFs(name string) bool {
|
||||||
|
for _, v := range validFs {
|
||||||
|
if strings.ToLower(name) == v {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func BytesToString(b []byte) string {
|
||||||
|
return *(*string)(unsafe.Pointer(&b))
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user