2018-07-11 23:43:49 +00:00
|
|
|
package cpu
|
2015-05-18 23:01:42 +00:00
|
|
|
|
|
|
|
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"
|
2018-07-11 23:43:49 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/inputs/system"
|
2015-09-17 19:07:15 +00:00
|
|
|
"github.com/shirou/gopsutil/cpu"
|
2015-05-18 23:01:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type CPUStats struct {
|
2018-07-11 23:43:49 +00:00
|
|
|
ps system.PS
|
2017-10-05 23:02:21 +00:00
|
|
|
lastStats map[string]cpu.TimesStat
|
2015-08-13 20:36:18 +00:00
|
|
|
|
2016-08-31 16:27:37 +00:00
|
|
|
PerCPU bool `toml:"percpu"`
|
|
|
|
TotalCPU bool `toml:"totalcpu"`
|
|
|
|
CollectCPUTime bool `toml:"collect_cpu_time"`
|
2017-06-26 22:13:38 +00:00
|
|
|
ReportActive bool `toml:"report_active"`
|
2015-08-12 18:09:20 +00:00
|
|
|
}
|
|
|
|
|
2018-07-11 23:43:49 +00:00
|
|
|
func NewCPUStats(ps system.PS) *CPUStats {
|
2015-08-13 20:36:18 +00:00
|
|
|
return &CPUStats{
|
2016-08-31 16:27:37 +00:00
|
|
|
ps: ps,
|
|
|
|
CollectCPUTime: true,
|
2017-06-26 22:13:38 +00:00
|
|
|
ReportActive: true,
|
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-08-31 16:27:37 +00:00
|
|
|
## If true, collect raw CPU time metrics.
|
|
|
|
collect_cpu_time = false
|
2017-06-26 22:13:38 +00:00
|
|
|
## If true, compute and report the sum of all non-idle CPU states.
|
|
|
|
report_active = false
|
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
|
|
|
|
2017-10-05 23:02:21 +00:00
|
|
|
for _, 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)
|
2017-06-26 22:13:38 +00:00
|
|
|
active := activeCpuTime(cts)
|
2015-08-12 18:09:20 +00:00
|
|
|
|
2016-08-31 16:27:37 +00:00
|
|
|
if s.CollectCPUTime {
|
|
|
|
// Add cpu time metrics
|
|
|
|
fieldsC := 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,
|
|
|
|
}
|
2017-06-26 22:13:38 +00:00
|
|
|
if s.ReportActive {
|
|
|
|
fieldsC["time_active"] = activeCpuTime(cts)
|
|
|
|
}
|
2016-08-31 16:27:37 +00:00
|
|
|
acc.AddCounter("cpu", fieldsC, tags, now)
|
2015-12-11 20:07:32 +00:00
|
|
|
}
|
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
|
|
|
// If it's the 1st gather, can't get CPU Usage stats yet
|
2015-08-13 20:36:18 +00:00
|
|
|
continue
|
|
|
|
}
|
2017-10-05 23:02:21 +00:00
|
|
|
|
|
|
|
lastCts, ok := s.lastStats[cts.CPU]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
2015-09-17 19:23:33 +00:00
|
|
|
lastTotal := totalCpuTime(lastCts)
|
2017-06-26 22:13:38 +00:00
|
|
|
lastActive := activeCpuTime(lastCts)
|
2015-08-12 18:09:20 +00:00
|
|
|
totalDelta := total - lastTotal
|
|
|
|
|
|
|
|
if totalDelta < 0 {
|
2017-10-16 21:25:00 +00:00
|
|
|
err = fmt.Errorf("Error: current total CPU time is less than previous total CPU time")
|
|
|
|
break
|
2015-08-12 18:09:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if totalDelta == 0 {
|
2015-08-13 20:36:18 +00:00
|
|
|
continue
|
2015-08-12 18:09:20 +00:00
|
|
|
}
|
2017-06-26 22:13:38 +00:00
|
|
|
|
2016-08-31 16:27:37 +00:00
|
|
|
fieldsG := map[string]interface{}{
|
2016-12-05 08:35:59 +00:00
|
|
|
"usage_user": 100 * (cts.User - lastCts.User - (cts.Guest - lastCts.Guest)) / totalDelta,
|
2016-08-31 16:27:37 +00:00
|
|
|
"usage_system": 100 * (cts.System - lastCts.System) / totalDelta,
|
|
|
|
"usage_idle": 100 * (cts.Idle - lastCts.Idle) / totalDelta,
|
2016-12-05 08:35:59 +00:00
|
|
|
"usage_nice": 100 * (cts.Nice - lastCts.Nice - (cts.GuestNice - lastCts.GuestNice)) / totalDelta,
|
2016-08-31 16:27:37 +00:00
|
|
|
"usage_iowait": 100 * (cts.Iowait - lastCts.Iowait) / totalDelta,
|
|
|
|
"usage_irq": 100 * (cts.Irq - lastCts.Irq) / totalDelta,
|
|
|
|
"usage_softirq": 100 * (cts.Softirq - lastCts.Softirq) / totalDelta,
|
|
|
|
"usage_steal": 100 * (cts.Steal - lastCts.Steal) / totalDelta,
|
|
|
|
"usage_guest": 100 * (cts.Guest - lastCts.Guest) / totalDelta,
|
|
|
|
"usage_guest_nice": 100 * (cts.GuestNice - lastCts.GuestNice) / totalDelta,
|
|
|
|
}
|
2017-06-26 22:13:38 +00:00
|
|
|
if s.ReportActive {
|
|
|
|
fieldsG["usage_active"] = 100 * (active - lastActive) / totalDelta
|
|
|
|
}
|
2016-08-31 16:27:37 +00:00
|
|
|
acc.AddGauge("cpu", fieldsG, tags, now)
|
2015-05-18 23:01:42 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 23:02:21 +00:00
|
|
|
s.lastStats = make(map[string]cpu.TimesStat)
|
|
|
|
for _, cts := range times {
|
|
|
|
s.lastStats[cts.CPU] = cts
|
|
|
|
}
|
2015-08-12 18:09:20 +00:00
|
|
|
|
2017-10-16 21:25:00 +00:00
|
|
|
return err
|
2015-05-18 23:01:42 +00:00
|
|
|
}
|
|
|
|
|
2016-05-19 14:05:08 +00:00
|
|
|
func totalCpuTime(t cpu.TimesStat) float64 {
|
2015-09-17 19:23:33 +00:00
|
|
|
total := t.User + t.System + t.Nice + t.Iowait + t.Irq + t.Softirq + t.Steal +
|
2016-12-05 08:35:59 +00:00
|
|
|
t.Idle
|
2015-09-17 19:23:33 +00:00
|
|
|
return total
|
2015-08-12 18:09:20 +00:00
|
|
|
}
|
|
|
|
|
2017-06-26 22:13:38 +00:00
|
|
|
func activeCpuTime(t cpu.TimesStat) float64 {
|
|
|
|
active := totalCpuTime(t) - t.Idle
|
|
|
|
return active
|
|
|
|
}
|
|
|
|
|
2015-05-18 23:01:42 +00:00
|
|
|
func init() {
|
2016-01-27 21:21:36 +00:00
|
|
|
inputs.Add("cpu", func() telegraf.Input {
|
2016-02-19 18:56:33 +00:00
|
|
|
return &CPUStats{
|
|
|
|
PerCPU: true,
|
|
|
|
TotalCPU: true,
|
2018-07-11 23:43:49 +00:00
|
|
|
ps: system.NewSystemPS(),
|
2016-02-19 18:56:33 +00:00
|
|
|
}
|
2015-05-18 23:01:42 +00:00
|
|
|
})
|
|
|
|
}
|