telegraf/plugins/inputs/mem/memory.go

75 lines
2.0 KiB
Go
Raw Normal View History

package mem
2015-05-18 23:01:42 +00:00
import (
"fmt"
"github.com/influxdata/telegraf"
2016-01-20 18:57:35 +00:00
"github.com/influxdata/telegraf/plugins/inputs"
"github.com/influxdata/telegraf/plugins/inputs/system"
2015-05-18 23:01:42 +00:00
)
type MemStats struct {
ps system.PS
2015-05-18 23:01:42 +00:00
}
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-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,
"active": vm.Active,
"inactive": vm.Inactive,
2018-01-03 00:37:11 +00:00
"wired": vm.Wired,
2017-11-29 18:49:45 +00:00
"slab": vm.Slab,
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),
"commit_limit": vm.CommitLimit,
"committed_as": vm.CommittedAS,
"dirty": vm.Dirty,
"high_free": vm.HighFree,
"high_total": vm.HighTotal,
"huge_page_size": vm.HugePageSize,
"huge_pages_free": vm.HugePagesFree,
"huge_pages_total": vm.HugePagesTotal,
"low_free": vm.LowFree,
"low_total": vm.LowTotal,
"mapped": vm.Mapped,
"page_tables": vm.PageTables,
"shared": vm.Shared,
"sreclaimable": vm.SReclaimable,
"sunreclaim": vm.SUnreclaim,
"swap_cached": vm.SwapCached,
"swap_free": vm.SwapFree,
"swap_total": vm.SwapTotal,
"vmalloc_chunk": vm.VMallocChunk,
"vmalloc_total": vm.VMallocTotal,
"vmalloc_used": vm.VMallocUsed,
"write_back": vm.Writeback,
"write_back_tmp": vm.WritebackTmp,
2015-12-11 20:07:32 +00:00
}
2018-04-17 22:43:10 +00:00
acc.AddGauge("mem", fields, nil)
2015-05-18 23:01:42 +00:00
return nil
}
func init() {
ps := system.NewSystemPS()
inputs.Add("mem", func() telegraf.Input {
2017-04-18 18:42:58 +00:00
return &MemStats{ps: ps}
2015-05-18 23:01:42 +00:00
})
}