From 100bdfba6c45d2d972bb68deaded33fbf61b3769 Mon Sep 17 00:00:00 2001 From: Daniel Nelson Date: Tue, 30 Jan 2018 18:06:53 -0800 Subject: [PATCH] Set path to / if HOST_MOUNT_PREFIX matches full path (#3736) (cherry picked from commit 10e51e4b49dbcefb939850574592763302beee92) --- plugins/inputs/system/disk_test.go | 134 +++++++++++++++++++++++++++++ plugins/inputs/system/ps.go | 3 +- 2 files changed, 136 insertions(+), 1 deletion(-) diff --git a/plugins/inputs/system/disk_test.go b/plugins/inputs/system/disk_test.go index 67494d712..7d320e762 100644 --- a/plugins/inputs/system/disk_test.go +++ b/plugins/inputs/system/disk_test.go @@ -117,6 +117,140 @@ func TestDiskUsage(t *testing.T) { assert.Equal(t, 2*expectedAllDiskMetrics+7, acc.NFields()) } +func TestDiskUsageHostMountPrefix(t *testing.T) { + tests := []struct { + name string + partitionStats []disk.PartitionStat + usageStats []*disk.UsageStat + hostMountPrefix string + expectedTags map[string]string + expectedFields map[string]interface{} + }{ + { + name: "no host mount prefix", + partitionStats: []disk.PartitionStat{ + { + Device: "/dev/sda", + Mountpoint: "/", + Fstype: "ext4", + Opts: "ro", + }, + }, + usageStats: []*disk.UsageStat{ + &disk.UsageStat{ + Path: "/", + Total: 42, + }, + }, + expectedTags: map[string]string{ + "path": "/", + "device": "sda", + "fstype": "ext4", + "mode": "ro", + }, + expectedFields: map[string]interface{}{ + "total": uint64(42), + "used": uint64(0), + "free": uint64(0), + "inodes_total": uint64(0), + "inodes_free": uint64(0), + "inodes_used": uint64(0), + "used_percent": float64(0), + }, + }, + { + name: "host mount prefix", + partitionStats: []disk.PartitionStat{ + { + Device: "/dev/sda", + Mountpoint: "/hostfs/var", + Fstype: "ext4", + Opts: "ro", + }, + }, + usageStats: []*disk.UsageStat{ + &disk.UsageStat{ + Path: "/hostfs/var", + Total: 42, + }, + }, + hostMountPrefix: "/hostfs", + expectedTags: map[string]string{ + "path": "/var", + "device": "sda", + "fstype": "ext4", + "mode": "ro", + }, + expectedFields: map[string]interface{}{ + "total": uint64(42), + "used": uint64(0), + "free": uint64(0), + "inodes_total": uint64(0), + "inodes_free": uint64(0), + "inodes_used": uint64(0), + "used_percent": float64(0), + }, + }, + { + name: "host mount prefix exact match", + partitionStats: []disk.PartitionStat{ + { + Device: "/dev/sda", + Mountpoint: "/hostfs", + Fstype: "ext4", + Opts: "ro", + }, + }, + usageStats: []*disk.UsageStat{ + &disk.UsageStat{ + Path: "/hostfs", + Total: 42, + }, + }, + hostMountPrefix: "/hostfs", + expectedTags: map[string]string{ + "path": "/", + "device": "sda", + "fstype": "ext4", + "mode": "ro", + }, + expectedFields: map[string]interface{}{ + "total": uint64(42), + "used": uint64(0), + "free": uint64(0), + "inodes_total": uint64(0), + "inodes_free": uint64(0), + "inodes_used": uint64(0), + "used_percent": float64(0), + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + mck := &mock.Mock{} + mps := MockPSDisk{&systemPS{&mockDiskUsage{mck}}, mck} + defer mps.AssertExpectations(t) + + var acc testutil.Accumulator + var err error + + mps.On("Partitions", true).Return(tt.partitionStats, nil) + + for _, v := range tt.usageStats { + mps.On("PSDiskUsage", v.Path).Return(v, nil) + } + + mps.On("OSGetenv", "HOST_MOUNT_PREFIX").Return(tt.hostMountPrefix) + + err = (&DiskStats{ps: mps}).Gather(&acc) + require.NoError(t, err) + + acc.AssertContainsTaggedFields(t, "disk", tt.expectedFields, tt.expectedTags) + }) + } +} + func TestDiskStats(t *testing.T) { var mps MockPS defer mps.AssertExpectations(t) diff --git a/plugins/inputs/system/ps.go b/plugins/inputs/system/ps.go index 02239e6e6..81161ae68 100644 --- a/plugins/inputs/system/ps.go +++ b/plugins/inputs/system/ps.go @@ -2,6 +2,7 @@ package system import ( "os" + "path/filepath" "strings" "github.com/influxdata/telegraf" @@ -129,7 +130,7 @@ func (s *systemPS) DiskUsage( continue } - du.Path = strings.TrimPrefix(p.Mountpoint, hostMountPrefix) + du.Path = filepath.Join("/", strings.TrimPrefix(p.Mountpoint, hostMountPrefix)) du.Fstype = p.Fstype usage = append(usage, du) partitions = append(partitions, &p)