telegraf/plugins/inputs/system/memory.go

51 lines
1.1 KiB
Go
Raw Normal View History

2015-05-19 07:01:42 +08:00
package system
import (
"fmt"
"github.com/influxdata/telegraf"
2016-01-21 02:57:35 +08:00
"github.com/influxdata/telegraf/plugins/inputs"
2015-05-19 07:01:42 +08:00
)
type MemStats struct {
ps PS
}
func (_ *MemStats) Description() string {
return "Read metrics about memory usage"
}
func (_ *MemStats) SampleConfig() string { return "" }
func (s *MemStats) Gather(acc telegraf.Accumulator) error {
2015-05-19 07:01:42 +08:00
vm, err := s.ps.VMStat()
if err != nil {
return fmt.Errorf("error getting virtual memory info: %s", err)
}
2015-12-12 04:07:32 +08:00
fields := map[string]interface{}{
"total": vm.Total,
"available": vm.Available,
"used": vm.Used,
"free": vm.Free,
"cached": vm.Cached,
"buffered": vm.Buffers,
"active": vm.Active,
"inactive": vm.Inactive,
2018-01-03 08:37:11 +08:00
"wired": vm.Wired,
2017-11-30 02:49:45 +08:00
"slab": vm.Slab,
2015-12-12 04:07:32 +08:00
"used_percent": 100 * float64(vm.Used) / float64(vm.Total),
"available_percent": 100 * float64(vm.Available) / float64(vm.Total),
}
2018-04-18 06:43:10 +08:00
acc.AddGauge("mem", fields, nil)
2015-05-19 07:01:42 +08:00
return nil
}
func init() {
2017-04-19 02:42:58 +08:00
ps := newSystemPS()
inputs.Add("mem", func() telegraf.Input {
2017-04-19 02:42:58 +08:00
return &MemStats{ps: ps}
2015-05-19 07:01:42 +08:00
})
}