2015-05-18 23:01:42 +00:00
|
|
|
package system
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2016-01-27 21:21:36 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
2017-02-04 15:58:02 +00:00
|
|
|
"github.com/influxdata/telegraf/registry/inputs"
|
2015-05-18 23:01:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type MemStats struct {
|
|
|
|
ps PS
|
|
|
|
}
|
|
|
|
|
|
|
|
func (_ *MemStats) Description() string {
|
|
|
|
return "Read metrics about memory usage"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (_ *MemStats) SampleConfig() string { return "" }
|
|
|
|
|
2016-01-27 21:21:36 +00:00
|
|
|
func (s *MemStats) Gather(acc telegraf.Accumulator) error {
|
2015-05-18 23:01:42 +00:00
|
|
|
vm, err := s.ps.VMStat()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error getting virtual memory info: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-12-11 20:07:32 +00:00
|
|
|
fields := map[string]interface{}{
|
|
|
|
"total": vm.Total,
|
|
|
|
"available": vm.Available,
|
|
|
|
"used": vm.Used,
|
|
|
|
"free": vm.Free,
|
|
|
|
"cached": vm.Cached,
|
|
|
|
"buffered": vm.Buffers,
|
2016-06-01 14:32:47 +00:00
|
|
|
"active": vm.Active,
|
|
|
|
"inactive": vm.Inactive,
|
2015-12-11 20:07:32 +00:00
|
|
|
"used_percent": 100 * float64(vm.Used) / float64(vm.Total),
|
|
|
|
"available_percent": 100 * float64(vm.Available) / float64(vm.Total),
|
|
|
|
}
|
2016-08-31 16:27:37 +00:00
|
|
|
acc.AddCounter("mem", fields, nil)
|
2015-05-18 23:01:42 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type SwapStats struct {
|
|
|
|
ps PS
|
|
|
|
}
|
|
|
|
|
|
|
|
func (_ *SwapStats) Description() string {
|
|
|
|
return "Read metrics about swap memory usage"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (_ *SwapStats) SampleConfig() string { return "" }
|
|
|
|
|
2016-01-27 21:21:36 +00:00
|
|
|
func (s *SwapStats) Gather(acc telegraf.Accumulator) error {
|
2015-05-18 23:01:42 +00:00
|
|
|
swap, err := s.ps.SwapStat()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error getting swap memory info: %s", err)
|
|
|
|
}
|
|
|
|
|
2016-08-31 16:27:37 +00:00
|
|
|
fieldsG := map[string]interface{}{
|
2015-12-11 20:07:32 +00:00
|
|
|
"total": swap.Total,
|
|
|
|
"used": swap.Used,
|
|
|
|
"free": swap.Free,
|
|
|
|
"used_percent": swap.UsedPercent,
|
|
|
|
}
|
2016-08-31 16:27:37 +00:00
|
|
|
fieldsC := map[string]interface{}{
|
|
|
|
"in": swap.Sin,
|
|
|
|
"out": swap.Sout,
|
|
|
|
}
|
|
|
|
acc.AddGauge("swap", fieldsG, nil)
|
|
|
|
acc.AddCounter("swap", fieldsC, nil)
|
2015-05-18 23:01:42 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2016-01-27 21:21:36 +00:00
|
|
|
inputs.Add("mem", func() telegraf.Input {
|
2015-05-18 23:01:42 +00:00
|
|
|
return &MemStats{ps: &systemPS{}}
|
|
|
|
})
|
|
|
|
|
2016-01-27 21:21:36 +00:00
|
|
|
inputs.Add("swap", func() telegraf.Input {
|
2015-05-18 23:01:42 +00:00
|
|
|
return &SwapStats{ps: &systemPS{}}
|
|
|
|
})
|
|
|
|
}
|