Moved system package inputs out to top level (#4406)
This commit is contained in:
committed by
Daniel Nelson
parent
9a14d1f074
commit
7b73b0db3a
61
plugins/inputs/disk/README.md
Normal file
61
plugins/inputs/disk/README.md
Normal file
@@ -0,0 +1,61 @@
|
||||
# Disk Input Plugin
|
||||
|
||||
The disk input plugin gathers metrics about disk usage.
|
||||
|
||||
Note that `used_percent` is calculated by doing `used / (used + free)`, _not_
|
||||
`used / total`, which is how the unix `df` command does it. See
|
||||
https://en.wikipedia.org/wiki/Df_(Unix) for more details.
|
||||
|
||||
### Configuration:
|
||||
|
||||
```toml
|
||||
# Read metrics about disk usage by mount point
|
||||
[[inputs.disk]]
|
||||
## By default stats will be gathered for all mount points.
|
||||
## Set mount_points will restrict the stats to only the specified mount points.
|
||||
# mount_points = ["/"]
|
||||
|
||||
## Ignore mount points by filesystem type.
|
||||
ignore_fs = ["tmpfs", "devtmpfs", "devfs", "overlay", "aufs", "squashfs"]
|
||||
```
|
||||
|
||||
#### Docker container
|
||||
|
||||
To monitor the Docker engine host from within a container you will need to
|
||||
mount the host's filesystem into the container and set the `HOST_PROC`
|
||||
environment variable to the location of the `/proc` filesystem. If desired, you can
|
||||
also set the `HOST_MOUNT_PREFIX` environment variable to the prefix containing
|
||||
the `/proc` directory, when present this variable is stripped from the
|
||||
reported `path` tag.
|
||||
|
||||
```
|
||||
docker run -v /:/hostfs:ro -e HOST_MOUNT_PREFIX=/hostfs -e HOST_PROC=/hostfs/proc telegraf
|
||||
```
|
||||
|
||||
### Metrics:
|
||||
|
||||
- disk
|
||||
- tags:
|
||||
- fstype (filesystem type)
|
||||
- device (device file)
|
||||
- path (mount point path)
|
||||
- mode (whether the mount is rw or ro)
|
||||
- fields:
|
||||
- free (integer, bytes)
|
||||
- total (integer, bytes)
|
||||
- used (integer, bytes)
|
||||
- used_percent (float, percent)
|
||||
- inodes_free (integer, files)
|
||||
- inodes_total (integer, files)
|
||||
- inodes_used (integer, files)
|
||||
|
||||
### Example Output:
|
||||
|
||||
```
|
||||
disk,fstype=hfs,mode=ro,path=/ free=398407520256i,inodes_free=97267461i,inodes_total=121847806i,inodes_used=24580345i,total=499088621568i,used=100418957312i,used_percent=20.131039916242397 1453832006274071563
|
||||
disk,fstype=devfs,mode=rw,path=/dev free=0i,inodes_free=0i,inodes_total=628i,inodes_used=628i,total=185856i,used=185856i,used_percent=100 1453832006274137913
|
||||
disk,fstype=autofs,mode=rw,path=/net free=0i,inodes_free=0i,inodes_total=0i,inodes_used=0i,total=0i,used=0i,used_percent=0 1453832006274157077
|
||||
disk,fstype=autofs,mode=rw,path=/home free=0i,inodes_free=0i,inodes_total=0i,inodes_used=0i,total=0i,used=0i,used_percent=0 1453832006274169688
|
||||
```
|
||||
|
||||
|
||||
113
plugins/inputs/disk/disk.go
Normal file
113
plugins/inputs/disk/disk.go
Normal file
@@ -0,0 +1,113 @@
|
||||
package disk
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/influxdata/telegraf"
|
||||
"github.com/influxdata/telegraf/plugins/inputs"
|
||||
"github.com/influxdata/telegraf/plugins/inputs/system"
|
||||
)
|
||||
|
||||
type DiskStats struct {
|
||||
ps system.PS
|
||||
|
||||
// Legacy support
|
||||
Mountpoints []string
|
||||
|
||||
MountPoints []string
|
||||
IgnoreFS []string `toml:"ignore_fs"`
|
||||
}
|
||||
|
||||
func (_ *DiskStats) Description() string {
|
||||
return "Read metrics about disk usage by mount point"
|
||||
}
|
||||
|
||||
var diskSampleConfig = `
|
||||
## By default stats will be gathered for all mount points.
|
||||
## Set mount_points will restrict the stats to only the specified mount points.
|
||||
# mount_points = ["/"]
|
||||
|
||||
## Ignore mount points by filesystem type.
|
||||
ignore_fs = ["tmpfs", "devtmpfs", "devfs", "overlay", "aufs", "squashfs"]
|
||||
`
|
||||
|
||||
func (_ *DiskStats) SampleConfig() string {
|
||||
return diskSampleConfig
|
||||
}
|
||||
|
||||
func (s *DiskStats) Gather(acc telegraf.Accumulator) error {
|
||||
// Legacy support:
|
||||
if len(s.Mountpoints) != 0 {
|
||||
s.MountPoints = s.Mountpoints
|
||||
}
|
||||
|
||||
disks, partitions, err := s.ps.DiskUsage(s.MountPoints, s.IgnoreFS)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error getting disk usage info: %s", err)
|
||||
}
|
||||
|
||||
for i, du := range disks {
|
||||
if du.Total == 0 {
|
||||
// Skip dummy filesystem (procfs, cgroupfs, ...)
|
||||
continue
|
||||
}
|
||||
mountOpts := parseOptions(partitions[i].Opts)
|
||||
tags := map[string]string{
|
||||
"path": du.Path,
|
||||
"device": strings.Replace(partitions[i].Device, "/dev/", "", -1),
|
||||
"fstype": du.Fstype,
|
||||
"mode": mountOpts.Mode(),
|
||||
}
|
||||
var used_percent float64
|
||||
if du.Used+du.Free > 0 {
|
||||
used_percent = float64(du.Used) /
|
||||
(float64(du.Used) + float64(du.Free)) * 100
|
||||
}
|
||||
|
||||
fields := map[string]interface{}{
|
||||
"total": du.Total,
|
||||
"free": du.Free,
|
||||
"used": du.Used,
|
||||
"used_percent": used_percent,
|
||||
"inodes_total": du.InodesTotal,
|
||||
"inodes_free": du.InodesFree,
|
||||
"inodes_used": du.InodesUsed,
|
||||
}
|
||||
acc.AddGauge("disk", fields, tags)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type MountOptions []string
|
||||
|
||||
func (opts MountOptions) Mode() string {
|
||||
if opts.exists("rw") {
|
||||
return "rw"
|
||||
} else if opts.exists("ro") {
|
||||
return "ro"
|
||||
} else {
|
||||
return "unknown"
|
||||
}
|
||||
}
|
||||
|
||||
func (opts MountOptions) exists(opt string) bool {
|
||||
for _, o := range opts {
|
||||
if o == opt {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func parseOptions(opts string) MountOptions {
|
||||
return strings.Split(opts, ",")
|
||||
}
|
||||
|
||||
func init() {
|
||||
ps := system.NewSystemPS()
|
||||
inputs.Add("disk", func() telegraf.Input {
|
||||
return &DiskStats{ps: ps}
|
||||
})
|
||||
}
|
||||
374
plugins/inputs/disk/disk_test.go
Normal file
374
plugins/inputs/disk/disk_test.go
Normal file
@@ -0,0 +1,374 @@
|
||||
package disk
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/influxdata/telegraf/plugins/inputs/system"
|
||||
"github.com/influxdata/telegraf/testutil"
|
||||
"github.com/shirou/gopsutil/disk"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/mock"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
type MockFileInfo struct {
|
||||
os.FileInfo
|
||||
}
|
||||
|
||||
func TestDiskUsage(t *testing.T) {
|
||||
mck := &mock.Mock{}
|
||||
mps := system.MockPSDisk{SystemPS: &system.SystemPS{PSDiskDeps: &system.MockDiskUsage{Mock: mck}}, Mock: mck}
|
||||
defer mps.AssertExpectations(t)
|
||||
|
||||
var acc testutil.Accumulator
|
||||
var err error
|
||||
|
||||
psAll := []disk.PartitionStat{
|
||||
{
|
||||
Device: "/dev/sda",
|
||||
Mountpoint: "/",
|
||||
Fstype: "ext4",
|
||||
Opts: "ro,noatime,nodiratime",
|
||||
},
|
||||
{
|
||||
Device: "/dev/sdb",
|
||||
Mountpoint: "/home",
|
||||
Fstype: "ext4",
|
||||
Opts: "rw,noatime,nodiratime,errors=remount-ro",
|
||||
},
|
||||
}
|
||||
duAll := []disk.UsageStat{
|
||||
{
|
||||
Path: "/",
|
||||
Fstype: "ext4",
|
||||
Total: 128,
|
||||
Free: 23,
|
||||
Used: 100,
|
||||
InodesTotal: 1234,
|
||||
InodesFree: 234,
|
||||
InodesUsed: 1000,
|
||||
},
|
||||
{
|
||||
Path: "/home",
|
||||
Fstype: "ext4",
|
||||
Total: 256,
|
||||
Free: 46,
|
||||
Used: 200,
|
||||
InodesTotal: 2468,
|
||||
InodesFree: 468,
|
||||
InodesUsed: 2000,
|
||||
},
|
||||
}
|
||||
|
||||
mps.On("Partitions", true).Return(psAll, nil)
|
||||
mps.On("OSGetenv", "HOST_MOUNT_PREFIX").Return("")
|
||||
mps.On("PSDiskUsage", "/").Return(&duAll[0], nil)
|
||||
mps.On("PSDiskUsage", "/home").Return(&duAll[1], nil)
|
||||
|
||||
err = (&DiskStats{ps: mps}).Gather(&acc)
|
||||
require.NoError(t, err)
|
||||
|
||||
numDiskMetrics := acc.NFields()
|
||||
expectedAllDiskMetrics := 14
|
||||
assert.Equal(t, expectedAllDiskMetrics, numDiskMetrics)
|
||||
|
||||
tags1 := map[string]string{
|
||||
"path": "/",
|
||||
"fstype": "ext4",
|
||||
"device": "sda",
|
||||
"mode": "ro",
|
||||
}
|
||||
tags2 := map[string]string{
|
||||
"path": "/home",
|
||||
"fstype": "ext4",
|
||||
"device": "sdb",
|
||||
"mode": "rw",
|
||||
}
|
||||
|
||||
fields1 := map[string]interface{}{
|
||||
"total": uint64(128),
|
||||
"used": uint64(100),
|
||||
"free": uint64(23),
|
||||
"inodes_total": uint64(1234),
|
||||
"inodes_free": uint64(234),
|
||||
"inodes_used": uint64(1000),
|
||||
"used_percent": float64(81.30081300813008),
|
||||
}
|
||||
fields2 := map[string]interface{}{
|
||||
"total": uint64(256),
|
||||
"used": uint64(200),
|
||||
"free": uint64(46),
|
||||
"inodes_total": uint64(2468),
|
||||
"inodes_free": uint64(468),
|
||||
"inodes_used": uint64(2000),
|
||||
"used_percent": float64(81.30081300813008),
|
||||
}
|
||||
acc.AssertContainsTaggedFields(t, "disk", fields1, tags1)
|
||||
acc.AssertContainsTaggedFields(t, "disk", fields2, tags2)
|
||||
|
||||
// We expect 6 more DiskMetrics to show up with an explicit match on "/"
|
||||
// and /home not matching the /dev in MountPoints
|
||||
err = (&DiskStats{ps: &mps, MountPoints: []string{"/", "/dev"}}).Gather(&acc)
|
||||
assert.Equal(t, expectedAllDiskMetrics+7, acc.NFields())
|
||||
|
||||
// We should see all the diskpoints as MountPoints includes both
|
||||
// / and /home
|
||||
err = (&DiskStats{ps: &mps, MountPoints: []string{"/", "/home"}}).Gather(&acc)
|
||||
assert.Equal(t, 2*expectedAllDiskMetrics+7, acc.NFields())
|
||||
}
|
||||
|
||||
func TestDiskUsageHostMountPrefix(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
partitionStats []disk.PartitionStat
|
||||
usageStats []*disk.UsageStat
|
||||
hostMountPrefix string
|
||||
expectedTags map[string]string
|
||||
expectedFields map[string]interface{}
|
||||
}{
|
||||
{
|
||||
name: "no host mount prefix",
|
||||
partitionStats: []disk.PartitionStat{
|
||||
{
|
||||
Device: "/dev/sda",
|
||||
Mountpoint: "/",
|
||||
Fstype: "ext4",
|
||||
Opts: "ro",
|
||||
},
|
||||
},
|
||||
usageStats: []*disk.UsageStat{
|
||||
&disk.UsageStat{
|
||||
Path: "/",
|
||||
Total: 42,
|
||||
},
|
||||
},
|
||||
expectedTags: map[string]string{
|
||||
"path": "/",
|
||||
"device": "sda",
|
||||
"fstype": "ext4",
|
||||
"mode": "ro",
|
||||
},
|
||||
expectedFields: map[string]interface{}{
|
||||
"total": uint64(42),
|
||||
"used": uint64(0),
|
||||
"free": uint64(0),
|
||||
"inodes_total": uint64(0),
|
||||
"inodes_free": uint64(0),
|
||||
"inodes_used": uint64(0),
|
||||
"used_percent": float64(0),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "host mount prefix",
|
||||
partitionStats: []disk.PartitionStat{
|
||||
{
|
||||
Device: "/dev/sda",
|
||||
Mountpoint: "/hostfs/var",
|
||||
Fstype: "ext4",
|
||||
Opts: "ro",
|
||||
},
|
||||
},
|
||||
usageStats: []*disk.UsageStat{
|
||||
&disk.UsageStat{
|
||||
Path: "/hostfs/var",
|
||||
Total: 42,
|
||||
},
|
||||
},
|
||||
hostMountPrefix: "/hostfs",
|
||||
expectedTags: map[string]string{
|
||||
"path": "/var",
|
||||
"device": "sda",
|
||||
"fstype": "ext4",
|
||||
"mode": "ro",
|
||||
},
|
||||
expectedFields: map[string]interface{}{
|
||||
"total": uint64(42),
|
||||
"used": uint64(0),
|
||||
"free": uint64(0),
|
||||
"inodes_total": uint64(0),
|
||||
"inodes_free": uint64(0),
|
||||
"inodes_used": uint64(0),
|
||||
"used_percent": float64(0),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "host mount prefix exact match",
|
||||
partitionStats: []disk.PartitionStat{
|
||||
{
|
||||
Device: "/dev/sda",
|
||||
Mountpoint: "/hostfs",
|
||||
Fstype: "ext4",
|
||||
Opts: "ro",
|
||||
},
|
||||
},
|
||||
usageStats: []*disk.UsageStat{
|
||||
&disk.UsageStat{
|
||||
Path: "/hostfs",
|
||||
Total: 42,
|
||||
},
|
||||
},
|
||||
hostMountPrefix: "/hostfs",
|
||||
expectedTags: map[string]string{
|
||||
"path": "/",
|
||||
"device": "sda",
|
||||
"fstype": "ext4",
|
||||
"mode": "ro",
|
||||
},
|
||||
expectedFields: map[string]interface{}{
|
||||
"total": uint64(42),
|
||||
"used": uint64(0),
|
||||
"free": uint64(0),
|
||||
"inodes_total": uint64(0),
|
||||
"inodes_free": uint64(0),
|
||||
"inodes_used": uint64(0),
|
||||
"used_percent": float64(0),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
mck := &mock.Mock{}
|
||||
mps := system.MockPSDisk{SystemPS: &system.SystemPS{PSDiskDeps: &system.MockDiskUsage{Mock: mck}}, Mock: mck}
|
||||
defer mps.AssertExpectations(t)
|
||||
|
||||
var acc testutil.Accumulator
|
||||
var err error
|
||||
|
||||
mps.On("Partitions", true).Return(tt.partitionStats, nil)
|
||||
|
||||
for _, v := range tt.usageStats {
|
||||
mps.On("PSDiskUsage", v.Path).Return(v, nil)
|
||||
}
|
||||
|
||||
mps.On("OSGetenv", "HOST_MOUNT_PREFIX").Return(tt.hostMountPrefix)
|
||||
|
||||
err = (&DiskStats{ps: mps}).Gather(&acc)
|
||||
require.NoError(t, err)
|
||||
|
||||
acc.AssertContainsTaggedFields(t, "disk", tt.expectedFields, tt.expectedTags)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDiskStats(t *testing.T) {
|
||||
var mps system.MockPS
|
||||
defer mps.AssertExpectations(t)
|
||||
var acc testutil.Accumulator
|
||||
var err error
|
||||
|
||||
duAll := []*disk.UsageStat{
|
||||
{
|
||||
Path: "/",
|
||||
Fstype: "ext4",
|
||||
Total: 128,
|
||||
Free: 23,
|
||||
Used: 100,
|
||||
InodesTotal: 1234,
|
||||
InodesFree: 234,
|
||||
InodesUsed: 1000,
|
||||
},
|
||||
{
|
||||
Path: "/home",
|
||||
Fstype: "ext4",
|
||||
Total: 256,
|
||||
Free: 46,
|
||||
Used: 200,
|
||||
InodesTotal: 2468,
|
||||
InodesFree: 468,
|
||||
InodesUsed: 2000,
|
||||
},
|
||||
}
|
||||
duFiltered := []*disk.UsageStat{
|
||||
{
|
||||
Path: "/",
|
||||
Fstype: "ext4",
|
||||
Total: 128,
|
||||
Free: 23,
|
||||
Used: 100,
|
||||
InodesTotal: 1234,
|
||||
InodesFree: 234,
|
||||
InodesUsed: 1000,
|
||||
},
|
||||
}
|
||||
|
||||
psAll := []*disk.PartitionStat{
|
||||
{
|
||||
Device: "/dev/sda",
|
||||
Mountpoint: "/",
|
||||
Fstype: "ext4",
|
||||
Opts: "ro,noatime,nodiratime",
|
||||
},
|
||||
{
|
||||
Device: "/dev/sdb",
|
||||
Mountpoint: "/home",
|
||||
Fstype: "ext4",
|
||||
Opts: "rw,noatime,nodiratime,errors=remount-ro",
|
||||
},
|
||||
}
|
||||
|
||||
psFiltered := []*disk.PartitionStat{
|
||||
{
|
||||
Device: "/dev/sda",
|
||||
Mountpoint: "/",
|
||||
Fstype: "ext4",
|
||||
Opts: "ro,noatime,nodiratime",
|
||||
},
|
||||
}
|
||||
|
||||
mps.On("DiskUsage", []string(nil), []string(nil)).Return(duAll, psAll, nil)
|
||||
mps.On("DiskUsage", []string{"/", "/dev"}, []string(nil)).Return(duFiltered, psFiltered, nil)
|
||||
mps.On("DiskUsage", []string{"/", "/home"}, []string(nil)).Return(duAll, psAll, nil)
|
||||
|
||||
err = (&DiskStats{ps: &mps}).Gather(&acc)
|
||||
require.NoError(t, err)
|
||||
|
||||
numDiskMetrics := acc.NFields()
|
||||
expectedAllDiskMetrics := 14
|
||||
assert.Equal(t, expectedAllDiskMetrics, numDiskMetrics)
|
||||
|
||||
tags1 := map[string]string{
|
||||
"path": "/",
|
||||
"fstype": "ext4",
|
||||
"device": "sda",
|
||||
"mode": "ro",
|
||||
}
|
||||
tags2 := map[string]string{
|
||||
"path": "/home",
|
||||
"fstype": "ext4",
|
||||
"device": "sdb",
|
||||
"mode": "rw",
|
||||
}
|
||||
|
||||
fields1 := map[string]interface{}{
|
||||
"total": uint64(128),
|
||||
"used": uint64(100),
|
||||
"free": uint64(23),
|
||||
"inodes_total": uint64(1234),
|
||||
"inodes_free": uint64(234),
|
||||
"inodes_used": uint64(1000),
|
||||
"used_percent": float64(81.30081300813008),
|
||||
}
|
||||
fields2 := map[string]interface{}{
|
||||
"total": uint64(256),
|
||||
"used": uint64(200),
|
||||
"free": uint64(46),
|
||||
"inodes_total": uint64(2468),
|
||||
"inodes_free": uint64(468),
|
||||
"inodes_used": uint64(2000),
|
||||
"used_percent": float64(81.30081300813008),
|
||||
}
|
||||
acc.AssertContainsTaggedFields(t, "disk", fields1, tags1)
|
||||
acc.AssertContainsTaggedFields(t, "disk", fields2, tags2)
|
||||
|
||||
// We expect 6 more DiskMetrics to show up with an explicit match on "/"
|
||||
// and /home not matching the /dev in MountPoints
|
||||
err = (&DiskStats{ps: &mps, MountPoints: []string{"/", "/dev"}}).Gather(&acc)
|
||||
assert.Equal(t, expectedAllDiskMetrics+7, acc.NFields())
|
||||
|
||||
// We should see all the diskpoints as MountPoints includes both
|
||||
// / and /home
|
||||
err = (&DiskStats{ps: &mps, MountPoints: []string{"/", "/home"}}).Gather(&acc)
|
||||
assert.Equal(t, 2*expectedAllDiskMetrics+7, acc.NFields())
|
||||
}
|
||||
Reference in New Issue
Block a user