Skip to content

Commit

Permalink
Filter mount points before stats are collected
Browse files Browse the repository at this point in the history
fixes #440
  • Loading branch information
sparrc committed Jan 20, 2016
1 parent d3a5cca commit f28a9a8
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 21 deletions.
2 changes: 1 addition & 1 deletion etc/telegraf.conf
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
[[inputs.disk]]
# By default, telegraf gather stats for all mountpoints.
# Setting mountpoints will restrict the stats to the specified mountpoints.
# Mountpoints=["/"]
# mount_points=["/"]

# Read metrics about disk IO by device
[[inputs.diskio]]
Expand Down
25 changes: 10 additions & 15 deletions plugins/inputs/system/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import (
type DiskStats struct {
ps PS

// Legacy support
Mountpoints []string

MountPoints []string
}

func (_ *DiskStats) Description() string {
Expand All @@ -19,33 +22,25 @@ func (_ *DiskStats) Description() string {
var diskSampleConfig = `
# By default, telegraf gather stats for all mountpoints.
# Setting mountpoints will restrict the stats to the specified mountpoints.
# Mountpoints=["/"]
# mount_points = ["/"]
`

func (_ *DiskStats) SampleConfig() string {
return diskSampleConfig
}

func (s *DiskStats) Gather(acc inputs.Accumulator) error {
disks, err := s.ps.DiskUsage()
if err != nil {
return fmt.Errorf("error getting disk usage info: %s", err)
// Legacy support:
if len(s.Mountpoints) != 0 {
s.MountPoints = s.Mountpoints
}

var restrictMpoints bool
mPoints := make(map[string]bool)
if len(s.Mountpoints) != 0 {
restrictMpoints = true
for _, mp := range s.Mountpoints {
mPoints[mp] = true
}
disks, err := s.ps.DiskUsage(s.MountPoints)
if err != nil {
return fmt.Errorf("error getting disk usage info: %s", err)
}

for _, du := range disks {
_, member := mPoints[du.Path]
if restrictMpoints && !member {
continue
}
tags := map[string]string{
"path": du.Path,
"fstype": du.Fstype,
Expand Down
8 changes: 4 additions & 4 deletions plugins/inputs/system/disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ func TestDiskStats(t *testing.T) {
acc.AssertContainsTaggedFields(t, "disk", fields2, tags2)

// We expect 6 more DiskPoints 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)
// and /home not matching the /dev in MountPoints
err = (&DiskStats{ps: &mps, MountPoints: []string{"/", "/dev"}}).Gather(&acc)
assert.Equal(t, expectedAllDiskPoints+6, acc.NFields())

// We should see all the diskpoints as Mountpoints includes both
// We should see all the diskpoints as MountPoints includes both
// / and /home
err = (&DiskStats{ps: &mps, Mountpoints: []string{"/", "/home"}}).Gather(&acc)
err = (&DiskStats{ps: &mps, MountPoints: []string{"/", "/home"}}).Gather(&acc)
assert.Equal(t, 2*expectedAllDiskPoints+6, acc.NFields())
}

Expand Down
18 changes: 17 additions & 1 deletion plugins/inputs/system/ps.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,31 @@ func (s *systemPS) CPUTimes(perCPU, totalCPU bool) ([]cpu.CPUTimesStat, error) {
return cpuTimes, nil
}

func (s *systemPS) DiskUsage() ([]*disk.DiskUsageStat, error) {
func (s *systemPS) DiskUsage(
mountPointFilter []string,
) ([]*disk.DiskUsageStat, error) {
parts, err := disk.DiskPartitions(true)
if err != nil {
return nil, err
}

// Make a "set" out of the filter slice
filterSet := make(map[string]bool)
for filter := range mountPointFilter {
filterSet[filter] = true
}

var usage []*disk.DiskUsageStat

for _, p := range parts {
if len(mountPointFilter) > 0 {
// If the mount point is not a member of the filter set,
// don't gather info on it.
_, ok := filterSet[p.Mountpoint]
if !ok {
continue
}
}
if _, err := os.Stat(p.Mountpoint); err == nil {
du, err := disk.DiskUsage(p.Mountpoint)
if err != nil {
Expand Down

0 comments on commit f28a9a8

Please sign in to comment.