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

@ -15,6 +15,8 @@
- [#103](https://github.com/influxdb/telegraf/pull/103): Filter by metric tags. Thanks @srfraser! - [#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! - [#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! - [#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 ### Bugfixes
- [#85](https://github.com/influxdb/telegraf/pull/85): Fix GetLocalHost testutil function for mac users - [#85](https://github.com/influxdb/telegraf/pull/85): Fix GetLocalHost testutil function for mac users

View File

@ -16,13 +16,9 @@ type CPUStats struct {
} }
func NewCPUStats(ps PS) *CPUStats { func NewCPUStats(ps PS) *CPUStats {
times, _ := ps.CPUTimes() return &CPUStats{
stats := CPUStats{
ps: ps, ps: ps,
lastStats: times,
} }
return &stats
} }
func (_ *CPUStats) Description() string { func (_ *CPUStats) Description() string {
@ -67,6 +63,10 @@ func (s *CPUStats) Gather(acc plugins.Accumulator) error {
add(acc, "busy", busy, tags) add(acc, "busy", busy, tags)
// Add in percentage // 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] lastCts := s.lastStats[i]
lastBusy, lastTotal := busyAndTotalCpuTime(lastCts) lastBusy, lastTotal := busyAndTotalCpuTime(lastCts)
busyDelta := busy - lastBusy busyDelta := busy - lastBusy
@ -77,7 +77,7 @@ func (s *CPUStats) Gather(acc plugins.Accumulator) error {
} }
if totalDelta == 0 { if totalDelta == 0 {
return nil continue
} }
add(acc, "percentageUser", 100*(cts.User-lastCts.User)/totalDelta, tags) add(acc, "percentageUser", 100*(cts.User-lastCts.User)/totalDelta, tags)
@ -110,7 +110,6 @@ func busyAndTotalCpuTime(t cpu.CPUTimesStat) (float64, float64) {
func init() { func init() {
plugins.Add("cpu", func() plugins.Plugin { plugins.Add("cpu", func() plugins.Plugin {
realPS := &systemPS{} return &CPUStats{ps: &systemPS{}}
return NewCPUStats(realPS)
}) })
} }