2015-05-18 23:01:42 +00:00
|
|
|
package system
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2015-05-22 23:45:14 +00:00
|
|
|
"github.com/influxdb/telegraf/plugins"
|
2015-05-18 23:01:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type CPUStats struct {
|
|
|
|
ps PS
|
2015-08-13 17:27:24 +00:00
|
|
|
|
|
|
|
PerCPU bool `toml:"percpu"`
|
|
|
|
TotalCPU bool `toml:"totalcpu"`
|
2015-05-18 23:01:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (_ *CPUStats) Description() string {
|
|
|
|
return "Read metrics about cpu usage"
|
|
|
|
}
|
|
|
|
|
2015-08-13 17:27:24 +00:00
|
|
|
var sampleConfig = `
|
|
|
|
# Whether to report per-cpu stats or not
|
|
|
|
percpu = true
|
|
|
|
# Whether to report total system cpu stats or not
|
|
|
|
totalcpu = true`
|
|
|
|
|
|
|
|
func (_ *CPUStats) SampleConfig() string {
|
|
|
|
return sampleConfig
|
|
|
|
}
|
2015-05-18 23:01:42 +00:00
|
|
|
|
|
|
|
func (s *CPUStats) Gather(acc plugins.Accumulator) error {
|
2015-08-13 17:27:24 +00:00
|
|
|
times, err := s.ps.CPUTimes(s.PerCPU, s.TotalCPU)
|
2015-05-18 23:01:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error getting CPU info: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, cts := range times {
|
|
|
|
tags := map[string]string{
|
|
|
|
"cpu": cts.CPU,
|
|
|
|
}
|
|
|
|
|
|
|
|
add(acc, "user", cts.User, tags)
|
|
|
|
add(acc, "system", cts.System, tags)
|
|
|
|
add(acc, "idle", cts.Idle, tags)
|
|
|
|
add(acc, "nice", cts.Nice, tags)
|
|
|
|
add(acc, "iowait", cts.Iowait, tags)
|
|
|
|
add(acc, "irq", cts.Irq, tags)
|
|
|
|
add(acc, "softirq", cts.Softirq, tags)
|
|
|
|
add(acc, "steal", cts.Steal, tags)
|
|
|
|
add(acc, "guest", cts.Guest, tags)
|
|
|
|
add(acc, "guestNice", cts.GuestNice, tags)
|
|
|
|
add(acc, "stolen", cts.Stolen, tags)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
plugins.Add("cpu", func() plugins.Plugin {
|
|
|
|
return &CPUStats{ps: &systemPS{}}
|
|
|
|
})
|
|
|
|
}
|