From 2d7cad7ebc857891a9772949d91380341aef7037 Mon Sep 17 00:00:00 2001 From: Ben Kochie Date: Fri, 30 Nov 2018 14:01:55 +0100 Subject: [PATCH] Add fallback for missing /proc/1/mounts (#1172) * Add fallback for missing /proc/1/mounts On some systems, `/proc/1/mounts` is hidden from non-root users due to the `hidepid` procfs feature. Attempt to fallback to `/proc/mounts` if `/proc/1/mounts` is not found. Signed-off-by: Ben Kochie * Add tests. Signed-off-by: Ben Kochie * Add CHANGELOG entry. Signed-off-by: Ben Kochie --- CHANGELOG.md | 7 ++++--- collector/filesystem_linux.go | 5 +++++ collector/filesystem_linux_test.go | 21 +++++++++++++++++++++ collector/fixtures_hidepid/proc/mounts | 1 + 4 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 collector/fixtures_hidepid/proc/mounts diff --git a/CHANGELOG.md b/CHANGELOG.md index 129670c2..27e7f408 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,9 +5,10 @@ ### Changes * [BUGFIX] -* [CHANGE] -* [ENHANCEMENT] -* [FEATURE] +* [BUGFIX] Add fallback for missing /proc/1/mounts #1172 +* [CHANGE] Add TCPSynRetrans to netstat default filter #1143 +* [ENHANCEMENT] Add Infiniband counters #1120 +* [FEATURE] Add a flag to disable exporter metrics #1148 ## 0.17.0-rc.0 / 2018-10-19 diff --git a/collector/filesystem_linux.go b/collector/filesystem_linux.go index 65614a97..26cc2d18 100644 --- a/collector/filesystem_linux.go +++ b/collector/filesystem_linux.go @@ -134,6 +134,11 @@ func stuckMountWatcher(mountPoint string, success chan struct{}) { func mountPointDetails() ([]filesystemLabels, error) { file, err := os.Open(procFilePath("1/mounts")) + if os.IsNotExist(err) { + // Fallback to `/proc/mounts` if `/proc/1/mounts` is missing due hidepid. + log.Debugf("Got %q reading root mounts, falling back to system mounts", err) + file, err = os.Open(procFilePath("mounts")) + } if err != nil { return nil, err } diff --git a/collector/filesystem_linux_test.go b/collector/filesystem_linux_test.go index 2b0a848b..33e0c540 100644 --- a/collector/filesystem_linux_test.go +++ b/collector/filesystem_linux_test.go @@ -70,3 +70,24 @@ func TestMountPointDetails(t *testing.T) { } } } + +func TestMountsFallback(t *testing.T) { + if _, err := kingpin.CommandLine.Parse([]string{"--path.procfs", "./fixtures_hidepid/proc"}); err != nil { + t.Fatal(err) + } + + expected := map[string]string{ + "/": "", + } + + 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_hidepid/proc/mounts b/collector/fixtures_hidepid/proc/mounts new file mode 100644 index 00000000..fb6a9635 --- /dev/null +++ b/collector/fixtures_hidepid/proc/mounts @@ -0,0 +1 @@ +rootfs / rootfs rw 0 0