Skip to content

Commit

Permalink
Add device name as a tag in disk stats (#1807)
Browse files Browse the repository at this point in the history
* 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)
  • Loading branch information
mediocretes authored and sparrc committed Dec 5, 2016
1 parent 1d1afe6 commit d518d7d
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 4 additions & 2 deletions plugins/inputs/system/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package system

import (
"fmt"
"strings"

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/inputs"
Expand Down Expand Up @@ -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
Expand Down
32 changes: 29 additions & 3 deletions plugins/inputs/system/disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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{}{
Expand Down
7 changes: 4 additions & 3 deletions plugins/inputs/system/mock_PS.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
13 changes: 8 additions & 5 deletions plugins/inputs/system/ps.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -71,6 +71,7 @@ func (s *systemPS) DiskUsage(
}

var usage []*disk.UsageStat
var partitions []*disk.PartitionStat

for _, p := range parts {
if len(mountPointFilter) > 0 {
Expand All @@ -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]
Expand All @@ -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) {
Expand Down

0 comments on commit d518d7d

Please sign in to comment.