From 86093ae9f7488c5d1e220f1ac21bcff19bf6f843 Mon Sep 17 00:00:00 2001 From: Pierre Fersing Date: Mon, 22 Feb 2016 18:29:10 +0100 Subject: [PATCH] Ignore boring filesystems from disk plugin Modern Linux has a lots of boring filesystem (tmpfs on /dev, devpts on /dev/pts, lots of cgroup on /sys/fs/cgroup/*, ...). * Ignore filesystem with 0 bytes (this cover cgroup, devpts and other). * Add IgnoreFS to ignore additional FS by their type. Add tmpfs and devtmpfs as default ignored type. --- etc/telegraf.conf | 4 ++++ plugins/inputs/system/disk.go | 11 ++++++++++- plugins/inputs/system/disk_test.go | 6 +++--- plugins/inputs/system/mock_PS.go | 4 ++-- plugins/inputs/system/ps.go | 19 +++++++++++++++---- 5 files changed, 34 insertions(+), 10 deletions(-) diff --git a/etc/telegraf.conf b/etc/telegraf.conf index eaf66db96..4782b2765 100644 --- a/etc/telegraf.conf +++ b/etc/telegraf.conf @@ -97,6 +97,10 @@ # Setting mountpoints will restrict the stats to the specified mountpoints. # mount_points=["/"] + # Ignore some mountpoints by filesystem type. For example (dev)tmpfs (usually + # present on /run, /var/run, /dev/shm or /dev). + ignore_fs = ["tmpfs", "devtmpfs"] + # Read metrics about disk IO by device [[inputs.diskio]] # By default, telegraf will gather stats for all devices including diff --git a/plugins/inputs/system/disk.go b/plugins/inputs/system/disk.go index 0488c839a..f93558de2 100644 --- a/plugins/inputs/system/disk.go +++ b/plugins/inputs/system/disk.go @@ -14,6 +14,7 @@ type DiskStats struct { Mountpoints []string MountPoints []string + IgnoreFS []string `toml:"ignore_fs"` } func (_ *DiskStats) Description() string { @@ -24,6 +25,10 @@ var diskSampleConfig = ` ## By default, telegraf gather stats for all mountpoints. ## Setting mountpoints will restrict the stats to the specified mountpoints. # mount_points = ["/"] + + # Ignore some mountpoints by filesystem type. For example (dev)tmpfs (usually + # present on /run, /var/run, /dev/shm or /dev). + ignore_fs = ["tmpfs", "devtmpfs"] ` func (_ *DiskStats) SampleConfig() string { @@ -36,12 +41,16 @@ func (s *DiskStats) Gather(acc telegraf.Accumulator) error { s.MountPoints = s.Mountpoints } - disks, err := s.ps.DiskUsage(s.MountPoints) + disks, 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 { + if du.Total == 0 { + // Skip dummy filesystem (procfs, cgroupfs, ...) + continue + } tags := map[string]string{ "path": du.Path, "fstype": du.Fstype, diff --git a/plugins/inputs/system/disk_test.go b/plugins/inputs/system/disk_test.go index 86537be23..0a722148b 100644 --- a/plugins/inputs/system/disk_test.go +++ b/plugins/inputs/system/disk_test.go @@ -50,9 +50,9 @@ func TestDiskStats(t *testing.T) { }, } - mps.On("DiskUsage", []string(nil)).Return(duAll, nil) - mps.On("DiskUsage", []string{"/", "/dev"}).Return(duFiltered, nil) - mps.On("DiskUsage", []string{"/", "/home"}).Return(duAll, nil) + 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) err = (&DiskStats{ps: &mps}).Gather(&acc) require.NoError(t, err) diff --git a/plugins/inputs/system/mock_PS.go b/plugins/inputs/system/mock_PS.go index 6e9a5f93e..fd6afda0f 100644 --- a/plugins/inputs/system/mock_PS.go +++ b/plugins/inputs/system/mock_PS.go @@ -33,8 +33,8 @@ func (m *MockPS) CPUTimes(perCPU, totalCPU bool) ([]cpu.CPUTimesStat, error) { return r0, r1 } -func (m *MockPS) DiskUsage(mountPointFilter []string) ([]*disk.DiskUsageStat, error) { - ret := m.Called(mountPointFilter) +func (m *MockPS) DiskUsage(mountPointFilter []string, fstypeExclude []string) ([]*disk.DiskUsageStat, error) { + ret := m.Called(mountPointFilter, fstypeExclude) r0 := ret.Get(0).([]*disk.DiskUsageStat) r1 := ret.Error(1) diff --git a/plugins/inputs/system/ps.go b/plugins/inputs/system/ps.go index 0a505bfc4..f1a1b27d7 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.CPUTimesStat, error) - DiskUsage(mountPointFilter []string) ([]*disk.DiskUsageStat, error) + DiskUsage(mountPointFilter []string, fstypeExclude []string) ([]*disk.DiskUsageStat, error) NetIO() ([]net.NetIOCountersStat, error) NetProto() ([]net.NetProtoCountersStat, error) DiskIO() (map[string]disk.DiskIOCountersStat, error) @@ -53,6 +53,7 @@ func (s *systemPS) CPUTimes(perCPU, totalCPU bool) ([]cpu.CPUTimesStat, error) { func (s *systemPS) DiskUsage( mountPointFilter []string, + fstypeExclude []string, ) ([]*disk.DiskUsageStat, error) { parts, err := disk.DiskPartitions(true) if err != nil { @@ -60,9 +61,13 @@ func (s *systemPS) DiskUsage( } // Make a "set" out of the filter slice - filterSet := make(map[string]bool) + mountPointFilterSet := make(map[string]bool) for _, filter := range mountPointFilter { - filterSet[filter] = true + mountPointFilterSet[filter] = true + } + fstypeExcludeSet := make(map[string]bool) + for _, filter := range fstypeExclude { + fstypeExcludeSet[filter] = true } var usage []*disk.DiskUsageStat @@ -71,7 +76,7 @@ func (s *systemPS) DiskUsage( 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] + _, ok := mountPointFilterSet[p.Mountpoint] if !ok { continue } @@ -81,6 +86,12 @@ func (s *systemPS) DiskUsage( if err != nil { return nil, err } + // If the mount point is a member of the exclude set, + // don't gather info on it. + _, ok := fstypeExcludeSet[p.Fstype] + if ok { + continue + } du.Fstype = p.Fstype usage = append(usage, du) }