forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpu.go
153 lines (130 loc) · 3.88 KB
/
cpu.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package cpu
import (
"fmt"
"time"
cpuUtil "github.com/shirou/gopsutil/cpu"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/inputs"
"github.com/influxdata/telegraf/plugins/inputs/system"
)
type CPUStats struct {
ps system.PS
lastStats map[string]cpuUtil.TimesStat
PerCPU bool `toml:"percpu"`
TotalCPU bool `toml:"totalcpu"`
CollectCPUTime bool `toml:"collect_cpu_time"`
ReportActive bool `toml:"report_active"`
}
func NewCPUStats(ps system.PS) *CPUStats {
return &CPUStats{
ps: ps,
CollectCPUTime: true,
ReportActive: true,
}
}
func (c *CPUStats) Description() string {
return "Read metrics about cpu usage"
}
var sampleConfig = `
## Whether to report per-cpu stats or not
percpu = true
## Whether to report total system cpu stats or not
totalcpu = true
## If true, collect raw CPU time metrics
collect_cpu_time = false
## If true, compute and report the sum of all non-idle CPU states
report_active = false
`
func (c *CPUStats) SampleConfig() string {
return sampleConfig
}
func (c *CPUStats) Gather(acc telegraf.Accumulator) error {
times, err := c.ps.CPUTimes(c.PerCPU, c.TotalCPU)
if err != nil {
return fmt.Errorf("error getting CPU info: %s", err)
}
now := time.Now()
for _, cts := range times {
tags := map[string]string{
"cpu": cts.CPU,
}
total := totalCPUTime(cts)
active := activeCPUTime(cts)
if c.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,
}
if c.ReportActive {
fieldsC["time_active"] = activeCPUTime(cts)
}
acc.AddCounter("cpu", fieldsC, tags, now)
}
// Add in percentage
if len(c.lastStats) == 0 {
// If it's the 1st gather, can't get CPU Usage stats yet
continue
}
lastCts, ok := c.lastStats[cts.CPU]
if !ok {
continue
}
lastTotal := totalCPUTime(lastCts)
lastActive := activeCPUTime(lastCts)
totalDelta := total - lastTotal
if totalDelta < 0 {
err = fmt.Errorf("current total CPU time is less than previous total CPU time")
break
}
if totalDelta == 0 {
continue
}
fieldsG := map[string]interface{}{
"usage_user": 100 * (cts.User - lastCts.User - (cts.Guest - lastCts.Guest)) / totalDelta,
"usage_system": 100 * (cts.System - lastCts.System) / totalDelta,
"usage_idle": 100 * (cts.Idle - lastCts.Idle) / totalDelta,
"usage_nice": 100 * (cts.Nice - lastCts.Nice - (cts.GuestNice - lastCts.GuestNice)) / totalDelta,
"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,
}
if c.ReportActive {
fieldsG["usage_active"] = 100 * (active - lastActive) / totalDelta
}
acc.AddGauge("cpu", fieldsG, tags, now)
}
c.lastStats = make(map[string]cpuUtil.TimesStat)
for _, cts := range times {
c.lastStats[cts.CPU] = cts
}
return err
}
func totalCPUTime(t cpuUtil.TimesStat) float64 {
total := t.User + t.System + t.Nice + t.Iowait + t.Irq + t.Softirq + t.Steal + t.Idle
return total
}
func activeCPUTime(t cpuUtil.TimesStat) float64 {
active := totalCPUTime(t) - t.Idle
return active
}
func init() {
inputs.Add("cpu", func() telegraf.Input {
return &CPUStats{
PerCPU: true,
TotalCPU: true,
ps: system.NewSystemPS(),
}
})
}