Skip to content

Commit

Permalink
Rebase and fixups for PR #111
Browse files Browse the repository at this point in the history
  • Loading branch information
sparrc committed Aug 13, 2015
1 parent 1e742ae commit 5f5f34a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 8 additions & 15 deletions plugins/system/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,9 @@ import (
type CPUStats struct {
ps PS
lastStats []cpu.CPUTimesStat

PerCPU bool `toml:"percpu"`
TotalCPU bool `toml:"totalcpu"`
}

func NewCPUStats(ps PS) *CPUStats {
times, _ := ps.CPUTimes()
stats := CPUStats{
ps: ps,
lastStats: times,
}

return &stats
PerCPU bool `toml:"percpu"`
TotalCPU bool `toml:"totalcpu"`
}

func (_ *CPUStats) Description() string {
Expand Down Expand Up @@ -67,6 +57,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
Expand All @@ -77,7 +71,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)
Expand Down Expand Up @@ -110,7 +104,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{}}
})
}

0 comments on commit 5f5f34a

Please sign in to comment.