Skip to content

Commit

Permalink
Add laundry to mem input plugin on FreeBSD (#7736)
Browse files Browse the repository at this point in the history
Additionally only report fields that may be set on the platform.
  • Loading branch information
danielnelson authored Jun 25, 2020
1 parent 6d12016 commit 810bb6d
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 101 deletions.
63 changes: 32 additions & 31 deletions plugins/inputs/mem/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,41 +18,42 @@ Available fields are dependent on platform.

- mem
- fields:
- active (integer)
- active (integer, Darwin, FreeBSD, Linux, OpenBSD)
- available (integer)
- buffered (integer)
- cached (integer)
- free (integer)
- inactive (integer)
- slab (integer)
- available_percent (float)
- buffered (integer, FreeBSD, Linux)
- cached (integer, FreeBSD, Linux, OpenBSD)
- commit_limit (integer, Linux)
- committed_as (integer, Linux)
- dirty (integer, Linux)
- free (integer, Darwin, FreeBSD, Linux, OpenBSD)
- high_free (integer, Linux)
- high_total (integer, Linux)
- huge_pages_free (integer, Linux)
- huge_page_size (integer, Linux)
- huge_pages_total (integer, Linux)
- inactive (integer, Darwin, FreeBSD, Linux, OpenBSD)
- laundry (integer, FreeBSD)
- low_free (integer, Linux)
- low_total (integer, Linux)
- mapped (integer, Linux)
- page_tables (integer, Linux)
- shared (integer, Linux)
- slab (integer, Linux)
- sreclaimable (integer, Linux)
- sunreclaim (integer, Linux)
- swap_cached (integer, Linux)
- swap_free (integer, Linux)
- swap_total (integer, Linux)
- total (integer)
- used (integer)
- available_percent (float)
- used_percent (float)
- wired (integer)
- commit_limit (integer)
- committed_as (integer)
- dirty (integer)
- high_free (integer)
- high_total (integer)
- huge_page_size (integer)
- huge_pages_free (integer)
- huge_pages_total (integer)
- low_free (integer)
- low_total (integer)
- mapped (integer)
- page_tables (integer)
- shared (integer)
- sreclaimable (integer)
- sunreclaim (integer)
- swap_cached (integer)
- swap_free (integer)
- swap_total (integer)
- vmalloc_chunk (integer)
- vmalloc_total (integer)
- vmalloc_used (integer)
- write_back (integer)
- write_back_tmp (integer)
- vmalloc_chunk (integer, Linux)
- vmalloc_total (integer, Linux)
- vmalloc_used (integer, Linux)
- wired (integer, Darwin, FreeBSD, OpenBSD)
- write_back (integer, Linux)
- write_back_tmp (integer, Linux)

### Example Output:
```
Expand Down
92 changes: 61 additions & 31 deletions plugins/inputs/mem/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ package mem

import (
"fmt"
"runtime"

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/inputs"
"github.com/influxdata/telegraf/plugins/inputs/system"
)

type MemStats struct {
ps system.PS
ps system.PS
platform string
}

func (_ *MemStats) Description() string {
Expand All @@ -18,6 +20,11 @@ func (_ *MemStats) Description() string {

func (_ *MemStats) SampleConfig() string { return "" }

func (m *MemStats) Init() error {
m.platform = runtime.GOOS
return nil
}

func (s *MemStats) Gather(acc telegraf.Accumulator) error {
vm, err := s.ps.VMStat()
if err != nil {
Expand All @@ -28,39 +35,62 @@ func (s *MemStats) Gather(acc telegraf.Accumulator) error {
"total": vm.Total,
"available": vm.Available,
"used": vm.Used,
"free": vm.Free,
"cached": vm.Cached,
"buffered": vm.Buffers,
"active": vm.Active,
"inactive": vm.Inactive,
"wired": vm.Wired,
"slab": vm.Slab,
"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,
}

switch s.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
}

acc.AddGauge("mem", fields, nil)

return nil
Expand Down
94 changes: 55 additions & 39 deletions plugins/inputs/mem/memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package mem

import (
"testing"
"time"

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/inputs/system"
"github.com/influxdata/telegraf/testutil"
"github.com/shirou/gopsutil/mem"
Expand Down Expand Up @@ -53,48 +55,62 @@ func TestMemStats(t *testing.T) {
}

mps.On("VMStat").Return(vms, nil)
plugin := &MemStats{ps: &mps}

err = (&MemStats{&mps}).Gather(&acc)
err = plugin.Init()
require.NoError(t, err)

memfields := map[string]interface{}{
"total": uint64(12400),
"available": uint64(7600),
"used": uint64(5000),
"available_percent": float64(7600) / float64(12400) * 100,
"used_percent": float64(5000) / float64(12400) * 100,
"free": uint64(1235),
"cached": uint64(0),
"buffered": uint64(0),
"active": uint64(8134),
"inactive": uint64(1124),
"wired": uint64(134),
"slab": uint64(1234),
"commit_limit": uint64(1),
"committed_as": uint64(118680),
"dirty": uint64(4),
"high_free": uint64(0),
"high_total": uint64(0),
"huge_page_size": uint64(4096),
"huge_pages_free": uint64(0),
"huge_pages_total": uint64(0),
"low_free": uint64(69936),
"low_total": uint64(255908),
"mapped": uint64(42236),
"page_tables": uint64(1236),
"shared": uint64(0),
"sreclaimable": uint64(1923022848),
"sunreclaim": uint64(157728768),
"swap_cached": uint64(0),
"swap_free": uint64(524280),
"swap_total": uint64(524280),
"vmalloc_chunk": uint64(3872908),
"vmalloc_total": uint64(3874808),
"vmalloc_used": uint64(1416),
"write_back": uint64(0),
"write_back_tmp": uint64(0),
plugin.platform = "linux"

require.NoError(t, err)
err = plugin.Gather(&acc)
require.NoError(t, err)

expected := []telegraf.Metric{
testutil.MustMetric(
"mem",
map[string]string{},
map[string]interface{}{
"total": uint64(12400),
"available": uint64(7600),
"used": uint64(5000),
"available_percent": float64(7600) / float64(12400) * 100,
"used_percent": float64(5000) / float64(12400) * 100,
"free": uint64(1235),
"cached": uint64(0),
"buffered": uint64(0),
"active": uint64(8134),
"inactive": uint64(1124),
// "wired": uint64(134),
"slab": uint64(1234),
"commit_limit": uint64(1),
"committed_as": uint64(118680),
"dirty": uint64(4),
"high_free": uint64(0),
"high_total": uint64(0),
"huge_page_size": uint64(4096),
"huge_pages_free": uint64(0),
"huge_pages_total": uint64(0),
"low_free": uint64(69936),
"low_total": uint64(255908),
"mapped": uint64(42236),
"page_tables": uint64(1236),
"shared": uint64(0),
"sreclaimable": uint64(1923022848),
"sunreclaim": uint64(157728768),
"swap_cached": uint64(0),
"swap_free": uint64(524280),
"swap_total": uint64(524280),
"vmalloc_chunk": uint64(3872908),
"vmalloc_total": uint64(3874808),
"vmalloc_used": uint64(1416),
"write_back": uint64(0),
"write_back_tmp": uint64(0),
},
time.Unix(0, 0),
telegraf.Gauge,
),
}
acc.AssertContainsTaggedFields(t, "mem", memfields, make(map[string]string))

acc.Metrics = nil
testutil.RequireMetricsEqual(t, expected, acc.GetTelegrafMetrics(), testutil.IgnoreTime())
}

0 comments on commit 810bb6d

Please sign in to comment.