diff --git a/CHANGELOG.md b/CHANGELOG.md index e94713337..754d55d82 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,8 @@ - [#103](https://github.com/influxdb/telegraf/pull/103): Filter by metric tags. Thanks @srfraser! - [#106](https://github.com/influxdb/telegraf/pull/106): Options to filter plugins on startup. Thanks @zepouet! - [#107](https://github.com/influxdb/telegraf/pull/107): Multiple outputs beyong influxdb. Thanks @jipperinbham! +- [#108](https://github.com/influxdb/telegraf/issues/108): Support setting per-CPU and total-CPU gathering. +- [#111](https://github.com/influxdb/telegraf/pull/111): Report CPU Usage in cpu plugin. Thanks @jpalay! ### Bugfixes - [#85](https://github.com/influxdb/telegraf/pull/85): Fix GetLocalHost testutil function for mac users diff --git a/plugins/system/cpu.go b/plugins/system/cpu.go index 1f8cd7ce5..1096b8517 100644 --- a/plugins/system/cpu.go +++ b/plugins/system/cpu.go @@ -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{}} }) }