telegraf/plugins/inputs/mem/mem.go

108 lines
2.7 KiB
Go
Raw Normal View History

//go:generate ../../../tools/readme_config_includer/generator
package mem
2015-05-19 07:01:42 +08:00
import (
_ "embed"
2015-05-19 07:01:42 +08:00
"fmt"
"runtime"
2015-05-19 07:01:42 +08:00
"github.com/influxdata/telegraf"
2016-01-21 02:57:35 +08:00
"github.com/influxdata/telegraf/plugins/inputs"
"github.com/influxdata/telegraf/plugins/inputs/system"
2015-05-19 07:01:42 +08:00
)
//go:embed sample.conf
var sampleConfig string
2015-05-19 07:01:42 +08:00
type MemStats struct {
ps system.PS
platform string
2015-05-19 07:01:42 +08:00
}
func (*MemStats) SampleConfig() string {
return sampleConfig
}
func (ms *MemStats) Init() error {
ms.platform = runtime.GOOS
return nil
}
func (ms *MemStats) Gather(acc telegraf.Accumulator) error {
vm, err := ms.ps.VMStat()
2015-05-19 07:01:42 +08:00
if err != nil {
return fmt.Errorf("error getting virtual memory info: %w", err)
2015-05-19 07:01:42 +08:00
}
2015-12-12 04:07:32 +08:00
fields := map[string]interface{}{
"total": vm.Total,
"available": vm.Available,
"used": vm.Used,
"used_percent": 100 * float64(vm.Used) / float64(vm.Total),
"available_percent": 100 * float64(vm.Available) / float64(vm.Total),
}
switch ms.platform {
case "darwin":
fields["active"] = vm.Active
fields["free"] = vm.Free
fields["inactive"] = vm.Inactive
fields["wired"] = vm.Wired
case "openbsd":
fields["active"] = vm.Active
fields["cached"] = vm.Cached
fields["free"] = vm.Free
fields["inactive"] = vm.Inactive
fields["wired"] = vm.Wired
case "freebsd":
fields["active"] = vm.Active
fields["buffered"] = vm.Buffers
fields["cached"] = vm.Cached
fields["free"] = vm.Free
fields["inactive"] = vm.Inactive
fields["laundry"] = vm.Laundry
fields["wired"] = vm.Wired
case "linux":
fields["active"] = vm.Active
fields["buffered"] = vm.Buffers
fields["cached"] = vm.Cached
fields["commit_limit"] = vm.CommitLimit
fields["committed_as"] = vm.CommittedAS
fields["dirty"] = vm.Dirty
fields["free"] = vm.Free
fields["high_free"] = vm.HighFree
fields["high_total"] = vm.HighTotal
fields["huge_pages_free"] = vm.HugePagesFree
fields["huge_page_size"] = vm.HugePageSize
fields["huge_pages_total"] = vm.HugePagesTotal
fields["inactive"] = vm.Inactive
fields["low_free"] = vm.LowFree
fields["low_total"] = vm.LowTotal
fields["mapped"] = vm.Mapped
fields["page_tables"] = vm.PageTables
fields["shared"] = vm.Shared
fields["slab"] = vm.Slab
fields["sreclaimable"] = vm.Sreclaimable
fields["sunreclaim"] = vm.Sunreclaim
fields["swap_cached"] = vm.SwapCached
fields["swap_free"] = vm.SwapFree
fields["swap_total"] = vm.SwapTotal
fields["vmalloc_chunk"] = vm.VmallocChunk
fields["vmalloc_total"] = vm.VmallocTotal
fields["vmalloc_used"] = vm.VmallocUsed
fields["write_back_tmp"] = vm.WriteBackTmp
fields["write_back"] = vm.WriteBack
}
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() {
ps := system.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
})
}