add system collector
This commit is contained in:
parent
d797e8e8de
commit
fb4f23c6a6
|
@ -15,6 +15,7 @@ import (
|
|||
|
||||
// auto registry
|
||||
_ "flashcat.cloud/categraf/inputs/redis"
|
||||
_ "flashcat.cloud/categraf/inputs/system"
|
||||
)
|
||||
|
||||
type Agent struct{}
|
||||
|
|
|
@ -118,13 +118,13 @@ func convert(item *types.Sample) *prompb.TimeSeries {
|
|||
|
||||
pt := &prompb.TimeSeries{}
|
||||
|
||||
if item.Timestamp < 0xffffffff {
|
||||
// s -> ms
|
||||
item.Timestamp = item.Timestamp * 1000
|
||||
timestamp := item.Timestamp.UnixMilli()
|
||||
if config.Config.Global.Precision == "s" {
|
||||
timestamp = item.Timestamp.Unix()
|
||||
}
|
||||
|
||||
pt.Samples = append(pt.Samples, prompb.Sample{
|
||||
Timestamp: item.Timestamp,
|
||||
Timestamp: timestamp,
|
||||
Value: item.Value,
|
||||
})
|
||||
|
||||
|
|
|
@ -5,6 +5,8 @@ PrintConfig = false
|
|||
Hostname = ""
|
||||
# will not add label(agent_hostname) if true
|
||||
OmitHostname = false
|
||||
# s | ms
|
||||
Precision = "ms"
|
||||
|
||||
[Global.Labels]
|
||||
region = "shanghai"
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
# whether collect metric: system_n_users
|
||||
CollectUserNumber = false
|
||||
|
||||
# # collect interval, default 15s
|
||||
# IntervalSeconds = 15
|
|
@ -16,6 +16,7 @@ type Global struct {
|
|||
Hostname string
|
||||
OmitHostname bool
|
||||
Labels map[string]string
|
||||
Precision string
|
||||
}
|
||||
|
||||
type WriterOpt struct {
|
||||
|
|
10
go.mod
10
go.mod
|
@ -9,6 +9,7 @@ require (
|
|||
github.com/prometheus/client_golang v1.12.1
|
||||
github.com/prometheus/common v0.32.1
|
||||
github.com/prometheus/prometheus v2.5.0+incompatible
|
||||
github.com/shirou/gopsutil/v3 v3.22.3
|
||||
github.com/toolkits/pkg v1.2.9
|
||||
)
|
||||
|
||||
|
@ -16,12 +17,17 @@ require (
|
|||
github.com/BurntSushi/toml v1.1.0 // indirect
|
||||
github.com/fatih/camelcase v1.0.0 // indirect
|
||||
github.com/fatih/structs v1.1.0 // indirect
|
||||
github.com/go-ole/go-ole v1.2.6 // indirect
|
||||
github.com/gogo/protobuf v1.1.1 // indirect
|
||||
github.com/grpc-ecosystem/grpc-gateway v1.16.0 // indirect
|
||||
github.com/stretchr/testify v1.7.1 // indirect
|
||||
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
|
||||
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
|
||||
github.com/tklauser/go-sysconf v0.3.10 // indirect
|
||||
github.com/tklauser/numcpus v0.4.0 // indirect
|
||||
github.com/yusufpapurcu/wmi v1.2.2 // indirect
|
||||
go.uber.org/automaxprocs v1.4.0 // indirect
|
||||
golang.org/x/net v0.0.0-20210525063256-abc453219eb5 // indirect
|
||||
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 // indirect
|
||||
golang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27 // indirect
|
||||
golang.org/x/text v0.3.7 // indirect
|
||||
google.golang.org/genproto v0.0.0-20200825200019-8632dd797987 // indirect
|
||||
google.golang.org/grpc v1.33.1 // indirect
|
||||
|
|
23
go.sum
23
go.sum
|
@ -76,6 +76,8 @@ github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vb
|
|||
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
|
||||
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
|
||||
github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A=
|
||||
github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=
|
||||
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
|
||||
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
|
||||
github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
|
||||
github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
|
||||
|
@ -123,8 +125,10 @@ github.com/google/go-cmp v0.4.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
|
|||
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
|
||||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
|
||||
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
|
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
|
||||
github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
|
||||
|
@ -166,6 +170,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
|||
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
|
||||
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 h1:6E+4a0GO5zZEnZ81pIr0yLvtUWk2if982qA3F3QD6H4=
|
||||
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I=
|
||||
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
|
||||
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
|
@ -180,6 +186,8 @@ github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
|
|||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
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/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c h1:ncq/mPwQF4JjgDlrVEn3C11VoGHZN7m8qihwgMEtzYw=
|
||||
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE=
|
||||
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
|
||||
github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo=
|
||||
github.com/prometheus/client_golang v1.7.1/go.mod h1:PY5Wy2awLA44sXw4AOSfFBetzPP4j5+D6mVACh+pe2M=
|
||||
|
@ -205,6 +213,8 @@ github.com/prometheus/prometheus v2.5.0+incompatible/go.mod h1:oAIUtOny2rjMX0OWN
|
|||
github.com/robfig/go-cache v0.0.0-20130306151617-9fc39e0dbf62/go.mod h1:65XQgovT59RWatovFwnwocoUxiI/eENTnOY5GK3STuY=
|
||||
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
|
||||
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
|
||||
github.com/shirou/gopsutil/v3 v3.22.3 h1:UebRzEomgMpv61e3hgD1tGooqX5trFbdU/ehphbHd00=
|
||||
github.com/shirou/gopsutil/v3 v3.22.3/go.mod h1:D01hZJ4pVHPpCTZ3m3T2+wDF2YAGfd+H4ifUguaQzHM=
|
||||
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
|
||||
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
|
||||
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
|
||||
|
@ -216,6 +226,10 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
|
|||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/tklauser/go-sysconf v0.3.10 h1:IJ1AZGZRWbY8T5Vfk04D9WOA5WSejdflXxP03OUqALw=
|
||||
github.com/tklauser/go-sysconf v0.3.10/go.mod h1:C8XykCvCb+Gn0oNCWPIlcb0RuglQTYaQ2hGm7jmxEFk=
|
||||
github.com/tklauser/numcpus v0.4.0 h1:E53Dm1HjH1/R2/aoCtXtPgzmElmn51aOkhCFSuZq//o=
|
||||
github.com/tklauser/numcpus v0.4.0/go.mod h1:1+UI3pD8NW14VMwdgJNJ1ESk2UnwhAnz5hMwiKKqXCQ=
|
||||
github.com/toolkits/pkg v1.2.9 h1:zGlrJDl+2sMBoxBRIoMtAwvKmW5wctuji2+qHCecMKk=
|
||||
github.com/toolkits/pkg v1.2.9/go.mod h1:ZUsQAOoaR99PSbes+RXSirvwmtd6+XIUvizCmrjfUYc=
|
||||
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
|
||||
|
@ -223,6 +237,8 @@ github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLY
|
|||
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
github.com/yusufpapurcu/wmi v1.2.2 h1:KBNDSne4vP5mbSWnJbO+51IMOXJB67QiYCSBrubbPRg=
|
||||
github.com/yusufpapurcu/wmi v1.2.2/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
|
||||
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
|
||||
go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=
|
||||
go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
|
||||
|
@ -323,6 +339,7 @@ golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7w
|
|||
golang.org/x/sys v0.0.0-20190606165138-5da285871e9c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20191001151750-bb3f8db39f24/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20191228213918-04cbcbbfeed8/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
|
@ -344,11 +361,13 @@ golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1/go.mod h1:h1NjWce9XRLGQEsW7w
|
|||
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200803210538-64077c9b5642/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 h1:XfKQ4OlFl8okEOr5UvAqFRVj8pY/4yfcXrddB8qAbU0=
|
||||
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27 h1:XDXtA5hveEEV8JB2l7nhMTp3t3cHp9ZpwcdjqyEWLlo=
|
||||
golang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
|
|
|
@ -17,3 +17,19 @@ var InputCreators = map[string]Creator{}
|
|||
func Add(name string, creator Creator) {
|
||||
InputCreators[name] = creator
|
||||
}
|
||||
|
||||
func NewSample(metric string, value float64, labels ...map[string]string) *types.Sample {
|
||||
s := &types.Sample{
|
||||
Metric: metric,
|
||||
Value: value,
|
||||
Labels: make(map[string]string),
|
||||
}
|
||||
|
||||
for i := 0; i < len(labels); i++ {
|
||||
for k, v := range labels[i] {
|
||||
s.Labels[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
return s
|
||||
}
|
||||
|
|
|
@ -11,10 +11,11 @@ import (
|
|||
"flashcat.cloud/categraf/types"
|
||||
)
|
||||
|
||||
const InputName = "redis"
|
||||
|
||||
var (
|
||||
DefaultInterval = time.Second * 30
|
||||
DefaultTimeout = time.Second * 20
|
||||
InputName = "redis"
|
||||
)
|
||||
|
||||
type Target struct {
|
||||
|
@ -101,11 +102,11 @@ func (t *Target) LoopGather(r *Redis, queue chan *types.Sample) {
|
|||
}
|
||||
|
||||
func (t *Target) Gather(r *Redis, queue chan *types.Sample) {
|
||||
queue <- &types.Sample{
|
||||
Metric: InputName + "_categraf_test_metric",
|
||||
Timestamp: time.Now().Unix(),
|
||||
Value: float64(time.Now().Unix()),
|
||||
}
|
||||
// queue <- &types.Sample{
|
||||
// Metric: InputName + "_categraf_test_metric",
|
||||
// Timestamp: time.Now(),
|
||||
// Value: float64(time.Now().Unix()),
|
||||
// }
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
|
|
@ -0,0 +1,141 @@
|
|||
package system
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"flashcat.cloud/categraf/config"
|
||||
"flashcat.cloud/categraf/inputs"
|
||||
"flashcat.cloud/categraf/types"
|
||||
"github.com/shirou/gopsutil/v3/cpu"
|
||||
"github.com/shirou/gopsutil/v3/host"
|
||||
"github.com/shirou/gopsutil/v3/load"
|
||||
)
|
||||
|
||||
const InputName = "system"
|
||||
|
||||
var (
|
||||
DefaultInterval = time.Second * 15
|
||||
)
|
||||
|
||||
type SystemStats struct {
|
||||
PrintConfigs bool
|
||||
IntervalSeconds int64
|
||||
Labels map[string]string
|
||||
|
||||
quit chan struct{}
|
||||
|
||||
CollectUserNumber bool
|
||||
}
|
||||
|
||||
func (s *SystemStats) getInterval() time.Duration {
|
||||
if s.IntervalSeconds != 0 {
|
||||
return time.Duration(s.IntervalSeconds) * time.Second
|
||||
}
|
||||
return DefaultInterval
|
||||
}
|
||||
|
||||
// overwrite func
|
||||
func (s *SystemStats) TidyConfig() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// overwrite func
|
||||
func (s *SystemStats) StopGoroutines() {
|
||||
s.quit <- struct{}{}
|
||||
}
|
||||
|
||||
// overwrite func
|
||||
func (s *SystemStats) StartGoroutines(queue chan *types.Sample) {
|
||||
go s.LoopGather(queue)
|
||||
}
|
||||
|
||||
func (s *SystemStats) LoopGather(queue chan *types.Sample) {
|
||||
for {
|
||||
select {
|
||||
case <-s.quit:
|
||||
close(s.quit)
|
||||
return
|
||||
default:
|
||||
time.Sleep(s.getInterval())
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
if strings.Contains(fmt.Sprint(r), "closed channel") {
|
||||
return
|
||||
} else {
|
||||
log.Println("E! gather system metrics panic:", r)
|
||||
}
|
||||
}
|
||||
}()
|
||||
s.Gather(queue)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// overwrite func
|
||||
func (s *SystemStats) Gather(queue chan *types.Sample) {
|
||||
var samples []*types.Sample
|
||||
|
||||
defer func() {
|
||||
now := time.Now()
|
||||
for i := 0; i < len(samples); i++ {
|
||||
samples[i].Timestamp = now
|
||||
samples[i].Metric = InputName + "_" + samples[i].Metric
|
||||
queue <- samples[i]
|
||||
}
|
||||
}()
|
||||
|
||||
loadavg, err := load.Avg()
|
||||
if err != nil && !strings.Contains(err.Error(), "not implemented") {
|
||||
log.Println("E! failed to gather system load:", err)
|
||||
return
|
||||
}
|
||||
|
||||
numCPUs, err := cpu.Counts(true)
|
||||
if err != nil {
|
||||
log.Println("E! failed to gather cpu number:", err)
|
||||
return
|
||||
}
|
||||
|
||||
samples = append(samples,
|
||||
inputs.NewSample("load1", loadavg.Load1),
|
||||
inputs.NewSample("load5", loadavg.Load5),
|
||||
inputs.NewSample("load15", loadavg.Load15),
|
||||
inputs.NewSample("n_cpus", float64(numCPUs)),
|
||||
inputs.NewSample("load_norm_1", loadavg.Load1/float64(numCPUs)),
|
||||
inputs.NewSample("load_norm_5", loadavg.Load5/float64(numCPUs)),
|
||||
inputs.NewSample("load_norm_15", loadavg.Load15/float64(numCPUs)),
|
||||
)
|
||||
|
||||
uptime, err := host.Uptime()
|
||||
if err != nil {
|
||||
log.Println("E! failed to get host uptime:", err)
|
||||
return
|
||||
}
|
||||
|
||||
samples = append(samples, inputs.NewSample("uptime", float64(uptime)))
|
||||
|
||||
if s.CollectUserNumber {
|
||||
users, err := host.Users()
|
||||
if err == nil {
|
||||
samples = append(samples, inputs.NewSample("n_users", float64(len(users))))
|
||||
} else if os.IsNotExist(err) {
|
||||
if config.Config.DebugMode {
|
||||
log.Println("D! reading os users:", err)
|
||||
}
|
||||
} else if os.IsPermission(err) {
|
||||
if config.Config.DebugMode {
|
||||
log.Println("D! reading os users:", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
inputs.Add(InputName, func() inputs.Input {
|
||||
return &SystemStats{}
|
||||
})
|
||||
}
|
|
@ -1,8 +1,10 @@
|
|||
package types
|
||||
|
||||
import "time"
|
||||
|
||||
type Sample struct {
|
||||
Metric string `json:"metric"`
|
||||
Timestamp int64 `json:"timestamp"`
|
||||
Timestamp time.Time `json:"timestamp"`
|
||||
Value float64 `json:"value"`
|
||||
Labels map[string]string `json:"labels"`
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue