Skip to content

Commit

Permalink
Add fallback for missing /proc/1/mounts (prometheus#1172)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* Add tests.

Signed-off-by: Ben Kochie <[email protected]>

* Add CHANGELOG entry.

Signed-off-by: Ben Kochie <[email protected]>
  • Loading branch information
SuperQ committed Nov 30, 2018
1 parent d7c41fa commit 2d7cad7
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
7 changes: 4 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 5 additions & 0 deletions collector/filesystem_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
21 changes: 21 additions & 0 deletions collector/filesystem_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
1 change: 1 addition & 0 deletions collector/fixtures_hidepid/proc/mounts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rootfs / rootfs rw 0 0

0 comments on commit 2d7cad7

Please sign in to comment.