From b7a02c73b3b6f58260a7c43c88fe8fb0563a8806 Mon Sep 17 00:00:00 2001 From: Steve Domino Date: Mon, 2 Jul 2018 14:07:57 -0600 Subject: [PATCH] Document swap input plugin and move to separate file (#4342) --- plugins/inputs/system/SWAP_README.md | 30 ++++++++++++++++++ plugins/inputs/system/memory.go | 36 --------------------- plugins/inputs/system/memory_test.go | 22 ------------- plugins/inputs/system/swap.go | 47 ++++++++++++++++++++++++++++ plugins/inputs/system/swap_test.go | 38 ++++++++++++++++++++++ 5 files changed, 115 insertions(+), 58 deletions(-) create mode 100644 plugins/inputs/system/SWAP_README.md create mode 100644 plugins/inputs/system/swap.go create mode 100644 plugins/inputs/system/swap_test.go diff --git a/plugins/inputs/system/SWAP_README.md b/plugins/inputs/system/SWAP_README.md new file mode 100644 index 000000000..75eae9386 --- /dev/null +++ b/plugins/inputs/system/SWAP_README.md @@ -0,0 +1,30 @@ +# Swap Input Plugin + +The swap plugin collects system swap metrics. + +For a more information on what swap memory is, read [All about Linux swap space](https://www.linux.com/news/all-about-linux-swap-space). + +### Configuration: + +```toml +# Read metrics about swap memory usage +[[inputs.swap]] + # no configuration +``` + +### Metrics: + +- swap + - fields: + - free (int) + - total (int) + - used (int) + - used_percent (float) + - sin (int) + - sout (int) + +### Example Output: + +``` +swap total=20855394304i,used_percent=45.43883523785713,used=9476448256i,free=1715331072i 1511894782000000000 +``` diff --git a/plugins/inputs/system/memory.go b/plugins/inputs/system/memory.go index 3cb95610e..b44fabc49 100644 --- a/plugins/inputs/system/memory.go +++ b/plugins/inputs/system/memory.go @@ -42,45 +42,9 @@ func (s *MemStats) Gather(acc telegraf.Accumulator) error { return nil } -type SwapStats struct { - ps PS -} - -func (_ *SwapStats) Description() string { - return "Read metrics about swap memory usage" -} - -func (_ *SwapStats) SampleConfig() string { return "" } - -func (s *SwapStats) Gather(acc telegraf.Accumulator) error { - swap, err := s.ps.SwapStat() - if err != nil { - return fmt.Errorf("error getting swap memory info: %s", err) - } - - fieldsG := map[string]interface{}{ - "total": swap.Total, - "used": swap.Used, - "free": swap.Free, - "used_percent": swap.UsedPercent, - } - fieldsC := map[string]interface{}{ - "in": swap.Sin, - "out": swap.Sout, - } - acc.AddGauge("swap", fieldsG, nil) - acc.AddCounter("swap", fieldsC, nil) - - return nil -} - func init() { ps := newSystemPS() inputs.Add("mem", func() telegraf.Input { return &MemStats{ps: ps} }) - - inputs.Add("swap", func() telegraf.Input { - return &SwapStats{ps: ps} - }) } diff --git a/plugins/inputs/system/memory_test.go b/plugins/inputs/system/memory_test.go index 5d5860a8e..34914db9c 100644 --- a/plugins/inputs/system/memory_test.go +++ b/plugins/inputs/system/memory_test.go @@ -30,17 +30,6 @@ func TestMemStats(t *testing.T) { mps.On("VMStat").Return(vms, nil) - sms := &mem.SwapMemoryStat{ - Total: 8123, - Used: 1232, - Free: 6412, - UsedPercent: 12.2, - Sin: 7, - Sout: 830, - } - - mps.On("SwapStat").Return(sms, nil) - err = (&MemStats{&mps}).Gather(&acc) require.NoError(t, err) @@ -61,15 +50,4 @@ func TestMemStats(t *testing.T) { acc.AssertContainsTaggedFields(t, "mem", memfields, make(map[string]string)) acc.Metrics = nil - - err = (&SwapStats{&mps}).Gather(&acc) - require.NoError(t, err) - - swapfields := map[string]interface{}{ - "total": uint64(8123), - "used": uint64(1232), - "used_percent": float64(12.2), - "free": uint64(6412), - } - acc.AssertContainsTaggedFields(t, "swap", swapfields, make(map[string]string)) } diff --git a/plugins/inputs/system/swap.go b/plugins/inputs/system/swap.go new file mode 100644 index 000000000..f1f7c8e23 --- /dev/null +++ b/plugins/inputs/system/swap.go @@ -0,0 +1,47 @@ +package system + +import ( + "fmt" + + "github.com/influxdata/telegraf" + "github.com/influxdata/telegraf/plugins/inputs" +) + +type SwapStats struct { + ps PS +} + +func (_ *SwapStats) Description() string { + return "Read metrics about swap memory usage" +} + +func (_ *SwapStats) SampleConfig() string { return "" } + +func (s *SwapStats) Gather(acc telegraf.Accumulator) error { + swap, err := s.ps.SwapStat() + if err != nil { + return fmt.Errorf("error getting swap memory info: %s", err) + } + + fieldsG := map[string]interface{}{ + "total": swap.Total, + "used": swap.Used, + "free": swap.Free, + "used_percent": swap.UsedPercent, + } + fieldsC := map[string]interface{}{ + "in": swap.Sin, + "out": swap.Sout, + } + acc.AddGauge("swap", fieldsG, nil) + acc.AddCounter("swap", fieldsC, nil) + + return nil +} + +func init() { + ps := newSystemPS() + inputs.Add("swap", func() telegraf.Input { + return &SwapStats{ps: ps} + }) +} diff --git a/plugins/inputs/system/swap_test.go b/plugins/inputs/system/swap_test.go new file mode 100644 index 000000000..ec9a0fe54 --- /dev/null +++ b/plugins/inputs/system/swap_test.go @@ -0,0 +1,38 @@ +package system + +import ( + "testing" + + "github.com/influxdata/telegraf/testutil" + "github.com/shirou/gopsutil/mem" + "github.com/stretchr/testify/require" +) + +func TestSwapStats(t *testing.T) { + var mps MockPS + var err error + defer mps.AssertExpectations(t) + var acc testutil.Accumulator + + sms := &mem.SwapMemoryStat{ + Total: 8123, + Used: 1232, + Free: 6412, + UsedPercent: 12.2, + Sin: 7, + Sout: 830, + } + + mps.On("SwapStat").Return(sms, nil) + + err = (&SwapStats{&mps}).Gather(&acc) + require.NoError(t, err) + + swapfields := map[string]interface{}{ + "total": uint64(8123), + "used": uint64(1232), + "used_percent": float64(12.2), + "free": uint64(6412), + } + acc.AssertContainsTaggedFields(t, "swap", swapfields, make(map[string]string)) +}