Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add device name as a tag in disk stats #1807

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions plugins/inputs/system/disk.go
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
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
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
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