Skip to content

Commit

Permalink
Resolves influxdata#4290 - no clear documentation on swap input plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
sdomino committed Jun 25, 2018
1 parent 23523ff commit 47b7376
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 58 deletions.
30 changes: 30 additions & 0 deletions plugins/inputs/system/SWAP_README.md
Original file line number Diff line number Diff line change
@@ -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
```
36 changes: 0 additions & 36 deletions plugins/inputs/system/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
})
}
22 changes: 0 additions & 22 deletions plugins/inputs/system/memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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))
}
47 changes: 47 additions & 0 deletions plugins/inputs/system/swap.go
Original file line number Diff line number Diff line change
@@ -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}
})
}
38 changes: 38 additions & 0 deletions plugins/inputs/system/swap_test.go
Original file line number Diff line number Diff line change
@@ -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))
}

0 comments on commit 47b7376

Please sign in to comment.