Rebase and fixups for PR #111, fixes issue #33

This commit is contained in:
Cameron Sparr
2015-08-13 14:36:18 -06:00
parent 1e742aec04
commit 0e65d8e64e
2 changed files with 13 additions and 12 deletions

View File

@@ -10,19 +10,15 @@ import (
type CPUStats struct {
ps PS
lastStats []cpu.CPUTimesStat
PerCPU bool `toml:"percpu"`
TotalCPU bool `toml:"totalcpu"`
PerCPU bool `toml:"percpu"`
TotalCPU bool `toml:"totalcpu"`
}
func NewCPUStats(ps PS) *CPUStats {
times, _ := ps.CPUTimes()
stats := CPUStats{
ps: ps,
lastStats: times,
return &CPUStats{
ps: ps,
}
return &stats
}
func (_ *CPUStats) Description() string {
@@ -67,6 +63,10 @@ func (s *CPUStats) Gather(acc plugins.Accumulator) error {
add(acc, "busy", busy, tags)
// Add in percentage
if len(s.lastStats) == 0 {
// If it's the 1st gather, can't get CPU stats yet
continue
}
lastCts := s.lastStats[i]
lastBusy, lastTotal := busyAndTotalCpuTime(lastCts)
busyDelta := busy - lastBusy
@@ -77,7 +77,7 @@ func (s *CPUStats) Gather(acc plugins.Accumulator) error {
}
if totalDelta == 0 {
return nil
continue
}
add(acc, "percentageUser", 100*(cts.User-lastCts.User)/totalDelta, tags)
@@ -110,7 +110,6 @@ func busyAndTotalCpuTime(t cpu.CPUTimesStat) (float64, float64) {
func init() {
plugins.Add("cpu", func() plugins.Plugin {
realPS := &systemPS{}
return NewCPUStats(realPS)
return &CPUStats{ps: &systemPS{}}
})
}