Moved system package inputs out to top level (#4406)
This commit is contained in:
committed by
Daniel Nelson
parent
9a14d1f074
commit
7b73b0db3a
35
plugins/inputs/mem/README.md
Normal file
35
plugins/inputs/mem/README.md
Normal file
@@ -0,0 +1,35 @@
|
||||
# Mem Input Plugin
|
||||
|
||||
The mem plugin collects system memory metrics.
|
||||
|
||||
For a more complete explanation of the difference between *used* and
|
||||
*actual_used* RAM, see [Linux ate my ram](http://www.linuxatemyram.com/).
|
||||
|
||||
### Configuration:
|
||||
```toml
|
||||
# Read metrics about memory usage
|
||||
[[inputs.mem]]
|
||||
# no configuration
|
||||
```
|
||||
|
||||
### Metrics:
|
||||
|
||||
- mem
|
||||
- fields:
|
||||
- active (int)
|
||||
- available (int)
|
||||
- buffered (int)
|
||||
- cached (int)
|
||||
- free (int)
|
||||
- inactive (int)
|
||||
- slab (int)
|
||||
- total (int)
|
||||
- used (int)
|
||||
- available_percent (float)
|
||||
- used_percent (float)
|
||||
- wired (int)
|
||||
|
||||
### Example Output:
|
||||
```
|
||||
mem cached=7809495040i,inactive=6348988416i,total=20855394304i,available=11378946048i,buffered=927199232i,active=11292905472i,slab=1351340032i,used_percent=45.43883523785713,available_percent=54.56116476214287,used=9476448256i,free=1715331072i 1511894782000000000
|
||||
```
|
||||
51
plugins/inputs/mem/memory.go
Normal file
51
plugins/inputs/mem/memory.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package mem
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/influxdata/telegraf"
|
||||
"github.com/influxdata/telegraf/plugins/inputs"
|
||||
"github.com/influxdata/telegraf/plugins/inputs/system"
|
||||
)
|
||||
|
||||
type MemStats struct {
|
||||
ps system.PS
|
||||
}
|
||||
|
||||
func (_ *MemStats) Description() string {
|
||||
return "Read metrics about memory usage"
|
||||
}
|
||||
|
||||
func (_ *MemStats) SampleConfig() string { return "" }
|
||||
|
||||
func (s *MemStats) Gather(acc telegraf.Accumulator) error {
|
||||
vm, err := s.ps.VMStat()
|
||||
if err != nil {
|
||||
return fmt.Errorf("error getting virtual memory info: %s", err)
|
||||
}
|
||||
|
||||
fields := map[string]interface{}{
|
||||
"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),
|
||||
}
|
||||
acc.AddGauge("mem", fields, nil)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
ps := system.NewSystemPS()
|
||||
inputs.Add("mem", func() telegraf.Input {
|
||||
return &MemStats{ps: ps}
|
||||
})
|
||||
}
|
||||
54
plugins/inputs/mem/memory_test.go
Normal file
54
plugins/inputs/mem/memory_test.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package mem
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/influxdata/telegraf/plugins/inputs/system"
|
||||
"github.com/influxdata/telegraf/testutil"
|
||||
"github.com/shirou/gopsutil/mem"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestMemStats(t *testing.T) {
|
||||
var mps system.MockPS
|
||||
var err error
|
||||
defer mps.AssertExpectations(t)
|
||||
var acc testutil.Accumulator
|
||||
|
||||
vms := &mem.VirtualMemoryStat{
|
||||
Total: 12400,
|
||||
Available: 7600,
|
||||
Used: 5000,
|
||||
Free: 1235,
|
||||
Active: 8134,
|
||||
Inactive: 1124,
|
||||
Slab: 1234,
|
||||
Wired: 134,
|
||||
// Buffers: 771,
|
||||
// Cached: 4312,
|
||||
// Shared: 2142,
|
||||
}
|
||||
|
||||
mps.On("VMStat").Return(vms, nil)
|
||||
|
||||
err = (&MemStats{&mps}).Gather(&acc)
|
||||
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),
|
||||
}
|
||||
acc.AssertContainsTaggedFields(t, "mem", memfields, make(map[string]string))
|
||||
|
||||
acc.Metrics = nil
|
||||
}
|
||||
Reference in New Issue
Block a user