Allow a PerCPU configuration variable, issue #108

This commit is contained in:
Cameron Sparr 2015-08-13 11:27:24 -06:00
parent 5d4b6c41a8
commit 04963f12a3
5 changed files with 44 additions and 31 deletions

View File

@ -8,16 +8,27 @@ import (
type CPUStats struct { type CPUStats struct {
ps PS ps PS
PerCPU bool `toml:"percpu"`
TotalCPU bool `toml:"totalcpu"`
} }
func (_ *CPUStats) Description() string { func (_ *CPUStats) Description() string {
return "Read metrics about cpu usage" 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 { 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 { if err != nil {
return fmt.Errorf("error getting CPU info: %s", err) return fmt.Errorf("error getting CPU info: %s", err)
} }

View File

@ -21,7 +21,7 @@ func (m *MockPS) LoadAvg() (*load.LoadAvgStat, error) {
return r0, r1 return r0, r1
} }
func (m *MockPS) CPUTimes() ([]cpu.CPUTimesStat, error) { func (m *MockPS) CPUTimes(perCPU, totalCPU bool) ([]cpu.CPUTimesStat, error) {
ret := m.Called() ret := m.Called()
r0 := ret.Get(0).([]cpu.CPUTimesStat) r0 := ret.Get(0).([]cpu.CPUTimesStat)

View File

@ -25,7 +25,7 @@ type DockerContainerStat struct {
type PS interface { type PS interface {
LoadAvg() (*load.LoadAvgStat, error) LoadAvg() (*load.LoadAvgStat, error)
CPUTimes() ([]cpu.CPUTimesStat, error) CPUTimes(perCPU, totalCPU bool) ([]cpu.CPUTimesStat, error)
DiskUsage() ([]*disk.DiskUsageStat, error) DiskUsage() ([]*disk.DiskUsageStat, error)
NetIO() ([]net.NetIOCountersStat, error) NetIO() ([]net.NetIOCountersStat, error)
DiskIO() (map[string]disk.DiskIOCountersStat, error) DiskIO() (map[string]disk.DiskIOCountersStat, error)
@ -49,8 +49,23 @@ func (s *systemPS) LoadAvg() (*load.LoadAvgStat, error) {
return load.LoadAvg() return load.LoadAvg()
} }
func (s *systemPS) CPUTimes() ([]cpu.CPUTimesStat, error) { func (s *systemPS) CPUTimes(perCPU, totalCPU bool) ([]cpu.CPUTimesStat, error) {
return cpu.CPUTimes(true) 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) { func (s *systemPS) DiskUsage() ([]*disk.DiskUsageStat, error) {

View File

@ -90,13 +90,6 @@ func perCPUTimes() ([]CPUTimesStat, error) {
System: float64(cpu_ticks[C.CPU_STATE_SYSTEM]) / ClocksPerSec, System: float64(cpu_ticks[C.CPU_STATE_SYSTEM]) / ClocksPerSec,
Nice: float64(cpu_ticks[C.CPU_STATE_NICE]) / ClocksPerSec, Nice: float64(cpu_ticks[C.CPU_STATE_NICE]) / ClocksPerSec,
Idle: float64(cpu_ticks[C.CPU_STATE_IDLE]) / ClocksPerSec, Idle: float64(cpu_ticks[C.CPU_STATE_IDLE]) / ClocksPerSec,
Iowait: -1,
Irq: -1,
Softirq: -1,
Steal: -1,
Guest: -1,
GuestNice: -1,
Stolen: -1,
} }
ret = append(ret, c) ret = append(ret, c)
@ -124,13 +117,6 @@ func allCPUTimes() ([]CPUTimesStat, error) {
System: float64(cpuload.cpu_ticks[C.CPU_STATE_SYSTEM]) / ClocksPerSec, System: float64(cpuload.cpu_ticks[C.CPU_STATE_SYSTEM]) / ClocksPerSec,
Nice: float64(cpuload.cpu_ticks[C.CPU_STATE_NICE]) / ClocksPerSec, Nice: float64(cpuload.cpu_ticks[C.CPU_STATE_NICE]) / ClocksPerSec,
Idle: float64(cpuload.cpu_ticks[C.CPU_STATE_IDLE]) / 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,
} }
return []CPUTimesStat{c}, nil return []CPUTimesStat{c}, nil

View File

@ -57,7 +57,8 @@ database = "telegraf" # required.
# Read metrics about cpu usage # Read metrics about cpu usage
[cpu] [cpu]
# no configuration totalcpu = true
percpu = false
# Read metrics about disk usage by mount point # Read metrics about disk usage by mount point
[disk] [disk]