From d518d7d8063fc811e3aa04e625bf6e11cbca0332 Mon Sep 17 00:00:00 2001 From: Nathan D Acuff Date: Mon, 5 Dec 2016 12:42:36 -0500 Subject: [PATCH] Add device name as a tag in disk stats (#1807) * return partition stat alongside disk stat from disk usage method, and report device name (minus /dev/) as a tag in disk stats * update system/disk tests to include new partition stat return value from disk usage method calls * update changelog for #1807 (use device name instead of path to report disk stats) --- CHANGELOG.md | 1 + plugins/inputs/system/disk.go | 6 ++++-- plugins/inputs/system/disk_test.go | 32 +++++++++++++++++++++++++++--- plugins/inputs/system/mock_PS.go | 7 ++++--- plugins/inputs/system/ps.go | 13 +++++++----- 5 files changed, 46 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a980927eb..5958ad099 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ - [#1997](https://github.com/influxdata/telegraf/issues/1997): Non-default HTTP timeouts for RabbitMQ plugin. - [#2074](https://github.com/influxdata/telegraf/pull/2074): "discard" output plugin added, primarily for testing purposes. - [#1965](https://github.com/influxdata/telegraf/pull/1965): The JSON parser can now parse an array of objects using the same configuration. +- [#1807](https://github.com/influxdata/telegraf/pull/1807): Option to use device name rather than path for reporting disk stats. ### Bugfixes diff --git a/plugins/inputs/system/disk.go b/plugins/inputs/system/disk.go index 4f18a4b14..f308a243b 100644 --- a/plugins/inputs/system/disk.go +++ b/plugins/inputs/system/disk.go @@ -2,6 +2,7 @@ package system import ( "fmt" + "strings" "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/plugins/inputs" @@ -41,18 +42,19 @@ func (s *DiskStats) Gather(acc telegraf.Accumulator) error { s.MountPoints = s.Mountpoints } - disks, err := s.ps.DiskUsage(s.MountPoints, s.IgnoreFS) + disks, partitions, err := s.ps.DiskUsage(s.MountPoints, s.IgnoreFS) if err != nil { return fmt.Errorf("error getting disk usage info: %s", err) } - for _, du := range disks { + for i, du := range disks { if du.Total == 0 { // Skip dummy filesystem (procfs, cgroupfs, ...) continue } tags := map[string]string{ "path": du.Path, + "device": strings.Replace(partitions[i].Device, "/dev/", "", -1), "fstype": du.Fstype, } var used_percent float64 diff --git a/plugins/inputs/system/disk_test.go b/plugins/inputs/system/disk_test.go index 71c9bff1b..fc0ff4d0d 100644 --- a/plugins/inputs/system/disk_test.go +++ b/plugins/inputs/system/disk_test.go @@ -50,9 +50,33 @@ func TestDiskStats(t *testing.T) { }, } - mps.On("DiskUsage", []string(nil), []string(nil)).Return(duAll, nil) - mps.On("DiskUsage", []string{"/", "/dev"}, []string(nil)).Return(duFiltered, nil) - mps.On("DiskUsage", []string{"/", "/home"}, []string(nil)).Return(duAll, nil) + psAll := []*disk.PartitionStat{ + { + Device: "/dev/sda", + Mountpoint: "/", + Fstype: "ext4", + Opts: "", + }, + { + Device: "/dev/sdb", + Mountpoint: "/home", + Fstype: "ext4", + Opts: "", + }, + } + + psFiltered := []*disk.PartitionStat{ + { + Device: "/dev/sda", + Mountpoint: "/", + Fstype: "ext4", + Opts: "", + }, + } + + 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) @@ -64,10 +88,12 @@ func TestDiskStats(t *testing.T) { tags1 := map[string]string{ "path": "/", "fstype": "ext4", + "device": "sda", } tags2 := map[string]string{ "path": "/home", "fstype": "ext4", + "device": "sdb", } fields1 := map[string]interface{}{ diff --git a/plugins/inputs/system/mock_PS.go b/plugins/inputs/system/mock_PS.go index f10cc94c0..e9f96a6c7 100644 --- a/plugins/inputs/system/mock_PS.go +++ b/plugins/inputs/system/mock_PS.go @@ -33,13 +33,14 @@ func (m *MockPS) CPUTimes(perCPU, totalCPU bool) ([]cpu.TimesStat, error) { return r0, r1 } -func (m *MockPS) DiskUsage(mountPointFilter []string, fstypeExclude []string) ([]*disk.UsageStat, error) { +func (m *MockPS) DiskUsage(mountPointFilter []string, fstypeExclude []string) ([]*disk.UsageStat, []*disk.PartitionStat, error) { ret := m.Called(mountPointFilter, fstypeExclude) r0 := ret.Get(0).([]*disk.UsageStat) - r1 := ret.Error(1) + r1 := ret.Get(1).([]*disk.PartitionStat) + r2 := ret.Error(2) - return r0, r1 + return r0, r1, r2 } func (m *MockPS) NetIO() ([]net.IOCountersStat, error) { diff --git a/plugins/inputs/system/ps.go b/plugins/inputs/system/ps.go index d740f6748..a885e2d59 100644 --- a/plugins/inputs/system/ps.go +++ b/plugins/inputs/system/ps.go @@ -14,7 +14,7 @@ import ( type PS interface { CPUTimes(perCPU, totalCPU bool) ([]cpu.TimesStat, error) - DiskUsage(mountPointFilter []string, fstypeExclude []string) ([]*disk.UsageStat, error) + DiskUsage(mountPointFilter []string, fstypeExclude []string) ([]*disk.UsageStat, []*disk.PartitionStat, error) NetIO() ([]net.IOCountersStat, error) NetProto() ([]net.ProtoCountersStat, error) DiskIO() (map[string]disk.IOCountersStat, error) @@ -54,10 +54,10 @@ func (s *systemPS) CPUTimes(perCPU, totalCPU bool) ([]cpu.TimesStat, error) { func (s *systemPS) DiskUsage( mountPointFilter []string, fstypeExclude []string, -) ([]*disk.UsageStat, error) { +) ([]*disk.UsageStat, []*disk.PartitionStat, error) { parts, err := disk.Partitions(true) if err != nil { - return nil, err + return nil, nil, err } // Make a "set" out of the filter slice @@ -71,6 +71,7 @@ func (s *systemPS) DiskUsage( } var usage []*disk.UsageStat + var partitions []*disk.PartitionStat for _, p := range parts { if len(mountPointFilter) > 0 { @@ -85,9 +86,10 @@ func (s *systemPS) DiskUsage( if _, err := os.Stat(mountpoint); err == nil { du, err := disk.Usage(mountpoint) if err != nil { - return nil, err + return nil, nil, err } du.Path = p.Mountpoint + // If the mount point is a member of the exclude set, // don't gather info on it. _, ok := fstypeExcludeSet[p.Fstype] @@ -96,10 +98,11 @@ func (s *systemPS) DiskUsage( } du.Fstype = p.Fstype usage = append(usage, du) + partitions = append(partitions, &p) } } - return usage, nil + return usage, partitions, nil } func (s *systemPS) NetProto() ([]net.ProtoCountersStat, error) {