2015-04-01 16:34:32 +00:00
|
|
|
package system
|
|
|
|
|
|
|
|
import (
|
2015-04-06 16:32:10 +00:00
|
|
|
"fmt"
|
|
|
|
|
2015-04-01 16:34:32 +00:00
|
|
|
"github.com/influxdb/tivan/plugins"
|
2015-04-06 16:32:10 +00:00
|
|
|
"github.com/influxdb/tivan/plugins/system/ps/cpu"
|
2015-04-04 00:22:31 +00:00
|
|
|
"github.com/influxdb/tivan/plugins/system/ps/load"
|
2015-04-01 16:34:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type PS interface {
|
|
|
|
LoadAvg() (*load.LoadAvgStat, error)
|
2015-04-06 16:32:10 +00:00
|
|
|
CPUTimes() ([]cpu.CPUTimesStat, error)
|
2015-04-01 16:34:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type SystemStats struct {
|
2015-04-06 16:32:10 +00:00
|
|
|
ps PS
|
2015-04-01 16:34:32 +00:00
|
|
|
}
|
|
|
|
|
2015-04-06 16:32:10 +00:00
|
|
|
func (s *SystemStats) add(acc plugins.Accumulator, name string, val float64) {
|
|
|
|
if val >= 0 {
|
|
|
|
acc.Add(name, val, nil)
|
2015-04-01 16:34:32 +00:00
|
|
|
}
|
2015-04-06 16:32:10 +00:00
|
|
|
}
|
2015-04-01 16:34:32 +00:00
|
|
|
|
2015-04-06 16:32:10 +00:00
|
|
|
func (s *SystemStats) Gather(acc plugins.Accumulator) error {
|
|
|
|
lv, err := s.ps.LoadAvg()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-04-01 16:34:32 +00:00
|
|
|
}
|
|
|
|
|
2015-04-06 16:32:10 +00:00
|
|
|
acc.Add("load1", lv.Load1, nil)
|
|
|
|
acc.Add("load5", lv.Load5, nil)
|
|
|
|
acc.Add("load15", lv.Load15, nil)
|
2015-04-01 16:34:32 +00:00
|
|
|
|
2015-04-06 16:32:10 +00:00
|
|
|
times, err := s.ps.CPUTimes()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error getting CPU info: %s", err)
|
2015-04-01 16:34:32 +00:00
|
|
|
}
|
|
|
|
|
2015-04-06 16:32:10 +00:00
|
|
|
for _, cts := range times {
|
|
|
|
s.add(acc, cts.CPU+".user", cts.User)
|
|
|
|
s.add(acc, cts.CPU+".system", cts.System)
|
|
|
|
s.add(acc, cts.CPU+".idle", cts.Idle)
|
|
|
|
s.add(acc, cts.CPU+".nice", cts.Nice)
|
|
|
|
s.add(acc, cts.CPU+".iowait", cts.Iowait)
|
|
|
|
s.add(acc, cts.CPU+".irq", cts.Irq)
|
|
|
|
s.add(acc, cts.CPU+".softirq", cts.Softirq)
|
|
|
|
s.add(acc, cts.CPU+".steal", cts.Steal)
|
|
|
|
s.add(acc, cts.CPU+".guest", cts.Guest)
|
|
|
|
s.add(acc, cts.CPU+".guestNice", cts.GuestNice)
|
|
|
|
s.add(acc, cts.CPU+".stolen", cts.Stolen)
|
2015-04-01 16:34:32 +00:00
|
|
|
}
|
|
|
|
|
2015-04-06 16:32:10 +00:00
|
|
|
return nil
|
2015-04-01 16:34:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type systemPS struct{}
|
|
|
|
|
|
|
|
func (s *systemPS) LoadAvg() (*load.LoadAvgStat, error) {
|
|
|
|
return load.LoadAvg()
|
|
|
|
}
|
|
|
|
|
2015-04-06 16:32:10 +00:00
|
|
|
func (s *systemPS) CPUTimes() ([]cpu.CPUTimesStat, error) {
|
|
|
|
return cpu.CPUTimes(true)
|
|
|
|
}
|
|
|
|
|
2015-04-01 16:34:32 +00:00
|
|
|
func init() {
|
|
|
|
plugins.Add("system", func() plugins.Plugin {
|
|
|
|
return &SystemStats{ps: &systemPS{}}
|
|
|
|
})
|
|
|
|
}
|