diff --git a/CHANGELOG.md b/CHANGELOG.md index fa54d657..21f473f1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ * [ENHANCEMENT] Include additional XFS runtime statistics. #1423 * [BUGFIX] Renamed label `state` to `name` on `node_systemd_service_restart_total`. #1393 * [BUGFIX] Fix netdev nil reference on Darwin #1414 +* [BUGFIX] Strip path.rootfs from mountpoint labels #1421 ## 0.18.1 / 2019-06-04 diff --git a/collector/filesystem_bsd.go b/collector/filesystem_bsd.go index 9f20ec05..b9aa1cc6 100644 --- a/collector/filesystem_bsd.go +++ b/collector/filesystem_bsd.go @@ -69,7 +69,7 @@ func (c *filesystemCollector) GetStats() (stats []filesystemStats, err error) { stats = append(stats, filesystemStats{ labels: filesystemLabels{ device: device, - mountPoint: mountpoint, + mountPoint: rootfsStripPrefix(mountpoint), fsType: fstype, }, size: float64(mnt[i].f_blocks) * float64(mnt[i].f_bsize), diff --git a/collector/filesystem_freebsd.go b/collector/filesystem_freebsd.go index 4df17d70..47c48335 100644 --- a/collector/filesystem_freebsd.go +++ b/collector/filesystem_freebsd.go @@ -73,7 +73,7 @@ func (c *filesystemCollector) GetStats() ([]filesystemStats, error) { stats = append(stats, filesystemStats{ labels: filesystemLabels{ device: device, - mountPoint: mountpoint, + mountPoint: rootfsStripPrefix(mountpoint), fsType: fstype, }, size: float64(fs.Blocks) * float64(fs.Bsize), diff --git a/collector/filesystem_linux.go b/collector/filesystem_linux.go index f76aeaa2..5ba4fe15 100644 --- a/collector/filesystem_linux.go +++ b/collector/filesystem_linux.go @@ -165,7 +165,7 @@ func parseFilesystemLabels(r io.Reader) ([]filesystemLabels, error) { filesystems = append(filesystems, filesystemLabels{ device: parts[0], - mountPoint: parts[1], + mountPoint: rootfsStripPrefix(parts[1]), fsType: parts[2], options: parts[3], }) diff --git a/collector/filesystem_linux_test.go b/collector/filesystem_linux_test.go index ebc6659c..267ad062 100644 --- a/collector/filesystem_linux_test.go +++ b/collector/filesystem_linux_test.go @@ -112,3 +112,30 @@ func TestMountsFallback(t *testing.T) { } } } + +func TestPathRootfs(t *testing.T) { + if _, err := kingpin.CommandLine.Parse([]string{"--path.procfs", "./fixtures_bindmount/proc", "--path.rootfs", "/host"}); err != nil { + t.Fatal(err) + } + + expected := map[string]string{ + // should modify these mountpoints (removes /host, see fixture proc file) + "/media/volume1": "", + "/media/volume2": "", + // should not modify these mountpoints + "/dev/shm": "", + "/run/lock": "", + "/sys/fs/cgroup": "", + } + + filesystems, err := mountPointDetails() + if err != nil { + t.Log(err) + } + + for _, fs := range filesystems { + if _, ok := expected[fs.mountPoint]; !ok { + t.Errorf("Got unexpected %s", fs.mountPoint) + } + } +} diff --git a/collector/fixtures_bindmount/proc/mounts b/collector/fixtures_bindmount/proc/mounts new file mode 100644 index 00000000..2ef64015 --- /dev/null +++ b/collector/fixtures_bindmount/proc/mounts @@ -0,0 +1,5 @@ +/dev/nvme1n1 /host/media/volume1 ext4 rw,seclabel,relatime,data=ordered 0 0 +/dev/nvme1n2 /host/media/volume2 ext4 rw,seclabel,relatime,data=ordered 0 0 +tmpfs /dev/shm tmpfs rw,nosuid,nodev 0 0 +tmpfs /run/lock tmpfs rw,nosuid,nodev,noexec,relatime,size=5120k 0 0 +tmpfs /sys/fs/cgroup tmpfs ro,nosuid,nodev,noexec,mode=755 0 0 diff --git a/collector/paths.go b/collector/paths.go index 5057f756..61506008 100644 --- a/collector/paths.go +++ b/collector/paths.go @@ -15,6 +15,7 @@ package collector import ( "path/filepath" + "strings" "github.com/prometheus/procfs" kingpin "gopkg.in/alecthomas/kingpin.v2" @@ -38,3 +39,10 @@ func sysFilePath(name string) string { func rootfsFilePath(name string) string { return filepath.Join(*rootfsPath, name) } + +func rootfsStripPrefix(path string) string { + if *rootfsPath == "/" { + return path + } + return strings.TrimPrefix(path, *rootfsPath) +}