forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolves influxdata#4290 - no clear documentation on swap input plugin
- Loading branch information
Showing
5 changed files
with
115 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |