From 04963f12a399fcc407b27780d6570d20f3f15b82 Mon Sep 17 00:00:00 2001 From: Cameron Sparr Date: Thu, 13 Aug 2015 11:27:24 -0600 Subject: [PATCH] Allow a PerCPU configuration variable, issue #108 --- plugins/system/cpu.go | 15 +++++++++++-- plugins/system/mock_PS.go | 2 +- plugins/system/ps.go | 21 +++++++++++++++--- plugins/system/ps/cpu/cpu_darwin.go | 34 +++++++++-------------------- testdata/telegraf-agent.toml | 3 ++- 5 files changed, 44 insertions(+), 31 deletions(-) diff --git a/plugins/system/cpu.go b/plugins/system/cpu.go index e942ccb5e..7114e2b9a 100644 --- a/plugins/system/cpu.go +++ b/plugins/system/cpu.go @@ -8,16 +8,27 @@ import ( type CPUStats struct { ps PS + + PerCPU bool `toml:"percpu"` + TotalCPU bool `toml:"totalcpu"` } func (_ *CPUStats) Description() string { return "Read metrics about cpu usage" } -func (_ *CPUStats) SampleConfig() string { return "" } +var sampleConfig = ` +# Whether to report per-cpu stats or not +percpu = true +# Whether to report total system cpu stats or not +totalcpu = true` + +func (_ *CPUStats) SampleConfig() string { + return sampleConfig +} func (s *CPUStats) Gather(acc plugins.Accumulator) error { - times, err := s.ps.CPUTimes() + times, err := s.ps.CPUTimes(s.PerCPU, s.TotalCPU) if err != nil { return fmt.Errorf("error getting CPU info: %s", err) } diff --git a/plugins/system/mock_PS.go b/plugins/system/mock_PS.go index 17a4fc950..86a3932b2 100644 --- a/plugins/system/mock_PS.go +++ b/plugins/system/mock_PS.go @@ -21,7 +21,7 @@ func (m *MockPS) LoadAvg() (*load.LoadAvgStat, error) { return r0, r1 } -func (m *MockPS) CPUTimes() ([]cpu.CPUTimesStat, error) { +func (m *MockPS) CPUTimes(perCPU, totalCPU bool) ([]cpu.CPUTimesStat, error) { ret := m.Called() r0 := ret.Get(0).([]cpu.CPUTimesStat) diff --git a/plugins/system/ps.go b/plugins/system/ps.go index 760f799ca..687ccdf7e 100644 --- a/plugins/system/ps.go +++ b/plugins/system/ps.go @@ -25,7 +25,7 @@ type DockerContainerStat struct { type PS interface { LoadAvg() (*load.LoadAvgStat, error) - CPUTimes() ([]cpu.CPUTimesStat, error) + CPUTimes(perCPU, totalCPU bool) ([]cpu.CPUTimesStat, error) DiskUsage() ([]*disk.DiskUsageStat, error) NetIO() ([]net.NetIOCountersStat, error) DiskIO() (map[string]disk.DiskIOCountersStat, error) @@ -49,8 +49,23 @@ func (s *systemPS) LoadAvg() (*load.LoadAvgStat, error) { return load.LoadAvg() } -func (s *systemPS) CPUTimes() ([]cpu.CPUTimesStat, error) { - return cpu.CPUTimes(true) +func (s *systemPS) CPUTimes(perCPU, totalCPU bool) ([]cpu.CPUTimesStat, error) { + var cpuTimes []cpu.CPUTimesStat + if perCPU { + if perCPUTimes, err := cpu.CPUTimes(true); err == nil { + cpuTimes = append(cpuTimes, perCPUTimes...) + } else { + return nil, err + } + } + if totalCPU { + if totalCPUTimes, err := cpu.CPUTimes(false); err == nil { + cpuTimes = append(cpuTimes, totalCPUTimes...) + } else { + return nil, err + } + } + return cpuTimes, nil } func (s *systemPS) DiskUsage() ([]*disk.DiskUsageStat, error) { diff --git a/plugins/system/ps/cpu/cpu_darwin.go b/plugins/system/ps/cpu/cpu_darwin.go index c5c0ddc02..0c054d2ef 100644 --- a/plugins/system/ps/cpu/cpu_darwin.go +++ b/plugins/system/ps/cpu/cpu_darwin.go @@ -85,18 +85,11 @@ func perCPUTimes() ([]CPUTimesStat, error) { } c := CPUTimesStat{ - CPU: fmt.Sprintf("cpu%d", i), - User: float64(cpu_ticks[C.CPU_STATE_USER]) / ClocksPerSec, - System: float64(cpu_ticks[C.CPU_STATE_SYSTEM]) / ClocksPerSec, - Nice: float64(cpu_ticks[C.CPU_STATE_NICE]) / ClocksPerSec, - Idle: float64(cpu_ticks[C.CPU_STATE_IDLE]) / ClocksPerSec, - Iowait: -1, - Irq: -1, - Softirq: -1, - Steal: -1, - Guest: -1, - GuestNice: -1, - Stolen: -1, + CPU: fmt.Sprintf("cpu%d", i), + User: float64(cpu_ticks[C.CPU_STATE_USER]) / ClocksPerSec, + System: float64(cpu_ticks[C.CPU_STATE_SYSTEM]) / ClocksPerSec, + Nice: float64(cpu_ticks[C.CPU_STATE_NICE]) / ClocksPerSec, + Idle: float64(cpu_ticks[C.CPU_STATE_IDLE]) / ClocksPerSec, } ret = append(ret, c) @@ -119,18 +112,11 @@ func allCPUTimes() ([]CPUTimesStat, error) { } c := CPUTimesStat{ - CPU: "cpu-total", - User: float64(cpuload.cpu_ticks[C.CPU_STATE_USER]) / ClocksPerSec, - System: float64(cpuload.cpu_ticks[C.CPU_STATE_SYSTEM]) / ClocksPerSec, - Nice: float64(cpuload.cpu_ticks[C.CPU_STATE_NICE]) / ClocksPerSec, - Idle: float64(cpuload.cpu_ticks[C.CPU_STATE_IDLE]) / ClocksPerSec, - Iowait: -1, - Irq: -1, - Softirq: -1, - Steal: -1, - Guest: -1, - GuestNice: -1, - Stolen: -1, + CPU: "cpu-total", + User: float64(cpuload.cpu_ticks[C.CPU_STATE_USER]) / ClocksPerSec, + System: float64(cpuload.cpu_ticks[C.CPU_STATE_SYSTEM]) / ClocksPerSec, + Nice: float64(cpuload.cpu_ticks[C.CPU_STATE_NICE]) / ClocksPerSec, + Idle: float64(cpuload.cpu_ticks[C.CPU_STATE_IDLE]) / ClocksPerSec, } return []CPUTimesStat{c}, nil diff --git a/testdata/telegraf-agent.toml b/testdata/telegraf-agent.toml index fd0221a47..98037dc6b 100644 --- a/testdata/telegraf-agent.toml +++ b/testdata/telegraf-agent.toml @@ -57,7 +57,8 @@ database = "telegraf" # required. # Read metrics about cpu usage [cpu] - # no configuration +totalcpu = true +percpu = false # Read metrics about disk usage by mount point [disk]