2015-05-18 23:01:42 +00:00
|
|
|
package system
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-12-11 20:07:32 +00:00
|
|
|
"time"
|
2015-05-18 23:01:42 +00:00
|
|
|
|
2016-01-27 21:21:36 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
2016-01-20 18:57:35 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
2015-09-17 19:07:15 +00:00
|
|
|
"github.com/shirou/gopsutil/cpu"
|
2015-05-18 23:01:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type CPUStats struct {
|
2015-08-12 18:09:20 +00:00
|
|
|
ps PS
|
|
|
|
lastStats []cpu.CPUTimesStat
|
2015-08-13 20:36:18 +00:00
|
|
|
|
|
|
|
PerCPU bool `toml:"percpu"`
|
|
|
|
TotalCPU bool `toml:"totalcpu"`
|
2015-08-12 18:09:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewCPUStats(ps PS) *CPUStats {
|
2015-08-13 20:36:18 +00:00
|
|
|
return &CPUStats{
|
|
|
|
ps: ps,
|
2015-08-12 18:09:20 +00:00
|
|
|
}
|
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 = `
|
2016-02-18 21:26:51 +00:00
|
|
|
## Whether to report per-cpu stats or not
|
2015-10-15 21:53:29 +00:00
|
|
|
percpu = true
|
2016-02-18 21:26:51 +00:00
|
|
|
## Whether to report total system cpu stats or not
|
2015-10-15 21:53:29 +00:00
|
|
|
totalcpu = true
|
2016-02-18 21:26:51 +00:00
|
|
|
## Comment this line if you want the raw CPU time metrics
|
2015-12-11 20:07:32 +00:00
|
|
|
drop = ["time_*"]
|
2015-08-26 15:21:39 +00:00
|
|
|
`
|
2015-08-13 17:27:24 +00:00
|
|
|
|
|
|
|
func (_ *CPUStats) SampleConfig() string {
|
|
|
|
return sampleConfig
|
|
|
|
}
|
2015-05-18 23:01:42 +00:00
|
|
|
|
2016-01-27 21:21:36 +00:00
|
|
|
func (s *CPUStats) Gather(acc telegraf.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)
|
|
|
|
}
|
2015-12-11 20:07:32 +00:00
|
|
|
now := time.Now()
|
2015-05-18 23:01:42 +00:00
|
|
|
|
2015-08-12 18:09:20 +00:00
|
|
|
for i, cts := range times {
|
2015-05-18 23:01:42 +00:00
|
|
|
tags := map[string]string{
|
|
|
|
"cpu": cts.CPU,
|
|
|
|
}
|
|
|
|
|
2015-09-17 19:23:33 +00:00
|
|
|
total := totalCpuTime(cts)
|
2015-08-12 18:09:20 +00:00
|
|
|
|
2015-12-11 20:07:32 +00:00
|
|
|
// Add cpu time metrics
|
|
|
|
fields := map[string]interface{}{
|
|
|
|
"time_user": cts.User,
|
|
|
|
"time_system": cts.System,
|
|
|
|
"time_idle": cts.Idle,
|
|
|
|
"time_nice": cts.Nice,
|
|
|
|
"time_iowait": cts.Iowait,
|
|
|
|
"time_irq": cts.Irq,
|
|
|
|
"time_softirq": cts.Softirq,
|
|
|
|
"time_steal": cts.Steal,
|
|
|
|
"time_guest": cts.Guest,
|
|
|
|
"time_guest_nice": cts.GuestNice,
|
|
|
|
}
|
2015-08-12 18:09:20 +00:00
|
|
|
|
|
|
|
// Add in percentage
|
2015-08-13 20:36:18 +00:00
|
|
|
if len(s.lastStats) == 0 {
|
2015-12-11 20:07:32 +00:00
|
|
|
acc.AddFields("cpu", fields, tags, now)
|
|
|
|
// If it's the 1st gather, can't get CPU Usage stats yet
|
2015-08-13 20:36:18 +00:00
|
|
|
continue
|
|
|
|
}
|
2015-08-12 18:09:20 +00:00
|
|
|
lastCts := s.lastStats[i]
|
2015-09-17 19:23:33 +00:00
|
|
|
lastTotal := totalCpuTime(lastCts)
|
2015-08-12 18:09:20 +00:00
|
|
|
totalDelta := total - lastTotal
|
|
|
|
|
|
|
|
if totalDelta < 0 {
|
2015-12-03 23:23:49 +00:00
|
|
|
s.lastStats = times
|
2015-08-12 18:09:20 +00:00
|
|
|
return fmt.Errorf("Error: current total CPU time is less than previous total CPU time")
|
|
|
|
}
|
|
|
|
|
|
|
|
if totalDelta == 0 {
|
2015-08-13 20:36:18 +00:00
|
|
|
continue
|
2015-08-12 18:09:20 +00:00
|
|
|
}
|
|
|
|
|
2015-12-11 20:07:32 +00:00
|
|
|
fields["usage_user"] = 100 * (cts.User - lastCts.User) / totalDelta
|
|
|
|
fields["usage_system"] = 100 * (cts.System - lastCts.System) / totalDelta
|
|
|
|
fields["usage_idle"] = 100 * (cts.Idle - lastCts.Idle) / totalDelta
|
|
|
|
fields["usage_nice"] = 100 * (cts.Nice - lastCts.Nice) / totalDelta
|
|
|
|
fields["usage_iowait"] = 100 * (cts.Iowait - lastCts.Iowait) / totalDelta
|
|
|
|
fields["usage_irq"] = 100 * (cts.Irq - lastCts.Irq) / totalDelta
|
|
|
|
fields["usage_softirq"] = 100 * (cts.Softirq - lastCts.Softirq) / totalDelta
|
|
|
|
fields["usage_steal"] = 100 * (cts.Steal - lastCts.Steal) / totalDelta
|
|
|
|
fields["usage_guest"] = 100 * (cts.Guest - lastCts.Guest) / totalDelta
|
|
|
|
fields["usage_guest_nice"] = 100 * (cts.GuestNice - lastCts.GuestNice) / totalDelta
|
|
|
|
acc.AddFields("cpu", fields, tags, now)
|
2015-05-18 23:01:42 +00:00
|
|
|
}
|
|
|
|
|
2015-08-12 18:09:20 +00:00
|
|
|
s.lastStats = times
|
|
|
|
|
2015-05-18 23:01:42 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-17 19:23:33 +00:00
|
|
|
func totalCpuTime(t cpu.CPUTimesStat) float64 {
|
|
|
|
total := t.User + t.System + t.Nice + t.Iowait + t.Irq + t.Softirq + t.Steal +
|
|
|
|
t.Guest + t.GuestNice + t.Idle
|
|
|
|
return total
|
2015-08-12 18:09:20 +00:00
|
|
|
}
|
|
|
|
|
2015-05-18 23:01:42 +00:00
|
|
|
func init() {
|
2016-01-27 21:21:36 +00:00
|
|
|
inputs.Add("cpu", func() telegraf.Input {
|
2015-08-13 20:36:18 +00:00
|
|
|
return &CPUStats{ps: &systemPS{}}
|
2015-05-18 23:01:42 +00:00
|
|
|
})
|
|
|
|
}
|