From f1af559f11bdccc09087d0e80835e3d3f424122d Mon Sep 17 00:00:00 2001 From: Simon Fraser Date: Thu, 30 Jul 2015 17:02:58 +0100 Subject: [PATCH 1/4] Filter disk stats by filesystem type, for Linux. If we don't want to record the stats of NFS, tmpfs, /proc and similar then specify: fstypes = ["ext4", "xfs"] In the config file. --- plugins/system/disk.go | 13 +++++++++++-- plugins/system/ps.go | 6 +++--- plugins/system/ps/disk/disk_linux.go | 22 +++++++++++++++------- 3 files changed, 29 insertions(+), 12 deletions(-) diff --git a/plugins/system/disk.go b/plugins/system/disk.go index 1c5bdaef6..5fa3db07d 100644 --- a/plugins/system/disk.go +++ b/plugins/system/disk.go @@ -8,16 +8,25 @@ import ( type DiskStats struct { ps PS + Fstypes []string } func (_ *DiskStats) Description() string { return "Read metrics about disk usage by mount point" } -func (_ *DiskStats) SampleConfig() string { return "" } +var diskSampleConfig = ` +# By default, telegraf gathers stats from any mounted partition, including +# NFS shares, /proc, tmpfs. +# fstypes = ["xfs", "ext4", ...] +` + +func (_ *DiskStats) SampleConfig() string { + return diskSampleConfig +} func (s *DiskStats) Gather(acc plugins.Accumulator) error { - disks, err := s.ps.DiskUsage() + disks, err := s.ps.DiskUsage(s.Fstypes) if err != nil { return fmt.Errorf("error getting disk usage info: %s", err) } diff --git a/plugins/system/ps.go b/plugins/system/ps.go index f4e445dd2..0f3294217 100644 --- a/plugins/system/ps.go +++ b/plugins/system/ps.go @@ -26,7 +26,7 @@ type DockerContainerStat struct { type PS interface { LoadAvg() (*load.LoadAvgStat, error) CPUTimes() ([]cpu.CPUTimesStat, error) - DiskUsage() ([]*disk.DiskUsageStat, error) + DiskUsage(fstypes []string) ([]*disk.DiskUsageStat, error) NetIO() ([]net.NetIOCountersStat, error) DiskIO() (map[string]disk.DiskIOCountersStat, error) VMStat() (*mem.VirtualMemoryStat, error) @@ -53,8 +53,8 @@ func (s *systemPS) CPUTimes() ([]cpu.CPUTimesStat, error) { return cpu.CPUTimes(true) } -func (s *systemPS) DiskUsage() ([]*disk.DiskUsageStat, error) { - parts, err := disk.DiskPartitions(true) +func (s *systemPS) DiskUsage(fstypes []string) ([]*disk.DiskUsageStat, error) { + parts, err := disk.DiskPartitions(fstypes) if err != nil { return nil, err } diff --git a/plugins/system/ps/disk/disk_linux.go b/plugins/system/ps/disk/disk_linux.go index d70685257..f6d99b699 100644 --- a/plugins/system/ps/disk/disk_linux.go +++ b/plugins/system/ps/disk/disk_linux.go @@ -17,7 +17,7 @@ const ( // Get disk partitions. // should use setmntent(3) but this implement use /etc/mtab file -func DiskPartitions(all bool) ([]DiskPartitionStat, error) { +func DiskPartitions(fstypes []string) ([]DiskPartitionStat, error) { filename := "/etc/mtab" lines, err := common.ReadLines(filename) @@ -25,17 +25,25 @@ func DiskPartitions(all bool) ([]DiskPartitionStat, error) { return nil, err } + set := make(map[string]struct{}, len(fstypes)) + for _, s := range fstypes { + set[s] = struct{}{} + } + ret := make([]DiskPartitionStat, 0, len(lines)) for _, line := range lines { fields := strings.Fields(line) - d := DiskPartitionStat{ - Device: fields[0], - Mountpoint: fields[1], - Fstype: fields[2], - Opts: fields[3], + _, ok := set[fields[2]] + if ok || len(fstypes) == 0 { + d := DiskPartitionStat{ + Device: fields[0], + Mountpoint: fields[1], + Fstype: fields[2], + Opts: fields[3], + } + ret = append(ret, d) } - ret = append(ret, d) } return ret, nil From 1a591f982843e837575b803c0a9f9ee1be5ee8b7 Mon Sep 17 00:00:00 2001 From: Simon Fraser Date: Thu, 30 Jul 2015 17:32:40 +0100 Subject: [PATCH 2/4] Filter disk stats by filesystem type, for Darwin & FreeBSD Tested on MacOS, not FreeBSD. --- plugins/system/ps/disk/disk_darwin.go | 12 ++++++++++-- plugins/system/ps/disk/disk_freebsd.go | 14 ++++++++++++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/plugins/system/ps/disk/disk_darwin.go b/plugins/system/ps/disk/disk_darwin.go index cbd94bdab..9c5995b9b 100644 --- a/plugins/system/ps/disk/disk_darwin.go +++ b/plugins/system/ps/disk/disk_darwin.go @@ -9,9 +9,14 @@ import ( "github.com/influxdb/telegraf/plugins/system/ps/common" ) -func DiskPartitions(all bool) ([]DiskPartitionStat, error) { +func DiskPartitions(fstypes []string) ([]DiskPartitionStat, error) { var ret []DiskPartitionStat + set := make(map[string]struct{}, len(fstypes)) + for _, s := range fstypes { + set[s] = struct{}{} + } + count, err := Getfsstat(nil, MntWait) if err != nil { return ret, err @@ -74,7 +79,10 @@ func DiskPartitions(all bool) ([]DiskPartitionStat, error) { Fstype: common.IntToString(stat.Fstypename[:]), Opts: opts, } - ret = append(ret, d) + _, ok := set[d.Fstype] + if ok || len(fstypes) == 0 { + ret = append(ret, d) + } } return ret, nil diff --git a/plugins/system/ps/disk/disk_freebsd.go b/plugins/system/ps/disk/disk_freebsd.go index 5193f3568..7b54c7911 100644 --- a/plugins/system/ps/disk/disk_freebsd.go +++ b/plugins/system/ps/disk/disk_freebsd.go @@ -18,9 +18,14 @@ const ( KernDevstatAll = 772 ) -func DiskPartitions(all bool) ([]DiskPartitionStat, error) { +func DiskPartitions(fstypes []string) ([]DiskPartitionStat, error) { var ret []DiskPartitionStat + set := make(map[string]struct{}, len(fstypes)) + for _, s := range fstypes { + set[s] = struct{}{} + } + // get length count, err := syscall.Getfsstat(nil, MNT_WAIT) if err != nil { @@ -87,7 +92,12 @@ func DiskPartitions(all bool) ([]DiskPartitionStat, error) { Fstype: common.IntToString(stat.Fstypename[:]), Opts: opts, } - ret = append(ret, d) + + _, ok := set[d.Fstype] + if ok || len(fstypes) == 0 { + ret = append(ret, d) + } + } return ret, nil From b823f08bb675da318640ab9ec76c47ed7d697adb Mon Sep 17 00:00:00 2001 From: Simon Fraser Date: Thu, 30 Jul 2015 17:34:58 +0100 Subject: [PATCH 3/4] update test to new function definition --- plugins/system/ps/disk/disk_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/system/ps/disk/disk_test.go b/plugins/system/ps/disk/disk_test.go index 04776b1d8..8270d14d2 100644 --- a/plugins/system/ps/disk/disk_test.go +++ b/plugins/system/ps/disk/disk_test.go @@ -23,7 +23,7 @@ func TestDisk_usage(t *testing.T) { } func TestDisk_partitions(t *testing.T) { - ret, err := DiskPartitions(false) + ret, err := DiskPartitions([]) if err != nil || len(ret) == 0 { t.Errorf("error %v", err) } From 8a99b9216d08292fba84ec53777fe66437b0b88c Mon Sep 17 00:00:00 2001 From: Simon Fraser Date: Thu, 30 Jul 2015 17:35:16 +0100 Subject: [PATCH 4/4] Functionality not present for Windows, but change function definition to ensure build. --- plugins/system/ps/disk/disk_windows.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/system/ps/disk/disk_windows.go b/plugins/system/ps/disk/disk_windows.go index 34341accd..098ed4636 100644 --- a/plugins/system/ps/disk/disk_windows.go +++ b/plugins/system/ps/disk/disk_windows.go @@ -55,7 +55,7 @@ func DiskUsage(path string) (DiskUsageStat, error) { return ret, nil } -func DiskPartitions(all bool) ([]DiskPartitionStat, error) { +func DiskPartitions(fstypes []string) ([]DiskPartitionStat, error) { var ret []DiskPartitionStat lpBuffer := make([]byte, 254) diskret, _, err := procGetLogicalDriveStringsW.Call(