-
Notifications
You must be signed in to change notification settings - Fork 5.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds CPU busy time and percentages #111
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,10 +4,22 @@ import ( | |
"fmt" | ||
|
||
"github.com/influxdb/telegraf/plugins" | ||
"github.com/influxdb/telegraf/plugins/system/ps/cpu" | ||
) | ||
|
||
type CPUStats struct { | ||
ps PS | ||
ps PS | ||
lastStats []cpu.CPUTimesStat | ||
} | ||
|
||
func NewCPUStats(ps PS) *CPUStats { | ||
times, _ := ps.CPUTimes() | ||
stats := CPUStats{ | ||
ps: ps, | ||
lastStats: times, | ||
} | ||
|
||
return &stats | ||
} | ||
|
||
func (_ *CPUStats) Description() string { | ||
|
@@ -18,15 +30,19 @@ func (_ *CPUStats) SampleConfig() string { return "" } | |
|
||
func (s *CPUStats) Gather(acc plugins.Accumulator) error { | ||
times, err := s.ps.CPUTimes() | ||
|
||
if err != nil { | ||
return fmt.Errorf("error getting CPU info: %s", err) | ||
} | ||
|
||
for _, cts := range times { | ||
for i, cts := range times { | ||
tags := map[string]string{ | ||
"cpu": cts.CPU, | ||
} | ||
|
||
busy, total := busyAndTotalCpuTime(cts) | ||
|
||
// Add total cpu numbers | ||
add(acc, "user", cts.User, tags) | ||
add(acc, "system", cts.System, tags) | ||
add(acc, "idle", cts.Idle, tags) | ||
|
@@ -38,13 +54,53 @@ func (s *CPUStats) Gather(acc plugins.Accumulator) error { | |
add(acc, "guest", cts.Guest, tags) | ||
add(acc, "guestNice", cts.GuestNice, tags) | ||
add(acc, "stolen", cts.Stolen, tags) | ||
add(acc, "busy", busy, tags) | ||
|
||
// Add in percentage | ||
lastCts := s.lastStats[i] | ||
lastBusy, lastTotal := busyAndTotalCpuTime(lastCts) | ||
busyDelta := busy - lastBusy | ||
totalDelta := total - lastTotal | ||
|
||
if totalDelta < 0 { | ||
return fmt.Errorf("Error: current total CPU time is less than previous total CPU time") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure that this should be an error, will this happen in the case of a reboot? Or after reboot will lastStats start over at 0? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and if it should be an error, can you write a unit test to verify that it get's raised properly? |
||
} | ||
|
||
if totalDelta == 0 { | ||
return nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be |
||
} | ||
|
||
add(acc, "percentageUser", 100*(cts.User-lastCts.User)/totalDelta, tags) | ||
add(acc, "percentageSystem", 100*(cts.System-lastCts.System)/totalDelta, tags) | ||
add(acc, "percentageIdle", 100*(cts.Idle-lastCts.Idle)/totalDelta, tags) | ||
add(acc, "percentageNice", 100*(cts.Nice-lastCts.Nice)/totalDelta, tags) | ||
add(acc, "percentageIowait", 100*(cts.Iowait-lastCts.Iowait)/totalDelta, tags) | ||
add(acc, "percentageIrq", 100*(cts.Irq-lastCts.Irq)/totalDelta, tags) | ||
add(acc, "percentageSoftirq", 100*(cts.Softirq-lastCts.Softirq)/totalDelta, tags) | ||
add(acc, "percentageSteal", 100*(cts.Steal-lastCts.Steal)/totalDelta, tags) | ||
add(acc, "percentageGuest", 100*(cts.Guest-lastCts.Guest)/totalDelta, tags) | ||
add(acc, "percentageGuestNice", 100*(cts.GuestNice-lastCts.GuestNice)/totalDelta, tags) | ||
add(acc, "percentageStolen", 100*(cts.Stolen-lastCts.Stolen)/totalDelta, tags) | ||
|
||
add(acc, "percentageBusy", 100*busyDelta/totalDelta, tags) | ||
|
||
} | ||
|
||
s.lastStats = times | ||
|
||
return nil | ||
} | ||
|
||
func busyAndTotalCpuTime(t cpu.CPUTimesStat) (float64, float64) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please document this function with a comment stating what the two float64 values are |
||
busy := t.User + t.System + t.Nice + t.Iowait + t.Irq + t.Softirq + t.Steal + | ||
t.Guest + t.GuestNice + t.Stolen | ||
|
||
return busy, busy + t.Idle | ||
} | ||
|
||
func init() { | ||
plugins.Add("cpu", func() plugins.Plugin { | ||
return &CPUStats{ps: &systemPS{}} | ||
realPS := &systemPS{} | ||
return NewCPUStats(realPS) | ||
}) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know a lot of the plugins don't conform to this at the moment (this is something I would like to improve), but all exported functions should be commented