Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[continue 912] strip rootfs prefix for run in docker #1058

Merged
merged 3 commits into from
Oct 4, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,18 @@ The node\_exporter is designed to monitor the host system. It's not recommended
to deploy it as Docker container because it requires access to the host system.
Be aware that any non-root mount points you want to monitor will need bind-mounted
into the container.
If you start container for host monitoring, specify `path.rootfs` argument.
This argument must match path in bind-mount of host root. The node\_exporter will use
`path.rootfs` as prefix to filter entries in ${path.procfs}/mounts and to
cleanup it from `mountpoint` label.

```bash
docker run -d \
--net="host" \
--pid="host" \
quay.io/prometheus/node-exporter
-v "/:/host:ro,rslave" \
quay.io/prometheus/node-exporter \
--path.rootfs /host
```

On some systems, the `timex` collector requires an additional Docker flag,
Expand Down
10 changes: 7 additions & 3 deletions collector/filesystem_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (c *filesystemCollector) GetStats() ([]filesystemStats, error) {
go stuckMountWatcher(labels.mountPoint, success)

buf := new(syscall.Statfs_t)
err = syscall.Statfs(labels.mountPoint, buf)
err = syscall.Statfs(rootfsFilePath(labels.mountPoint), buf)

stuckMountsMtx.Lock()
close(success)
Expand All @@ -86,7 +86,7 @@ func (c *filesystemCollector) GetStats() ([]filesystemStats, error) {
labels: labels,
deviceError: 1,
})
log.Debugf("Error on statfs() system call for %q: %s", labels.mountPoint, err)
log.Debugf("Error on statfs() system call for %q: %s", rootfsFilePath(labels.mountPoint), err)
continue
}

Expand Down Expand Up @@ -143,6 +143,10 @@ func mountPointDetails() ([]filesystemLabels, error) {
scanner := bufio.NewScanner(file)
for scanner.Scan() {
parts := strings.Fields(scanner.Text())
// skip non rootfs paths if rootfsPath defined
if !rootfsPathDetect(parts[1]) {
continue
}

// Ensure we handle the translation of \040 and \011
// as per fstab(5).
Expand All @@ -151,7 +155,7 @@ func mountPointDetails() ([]filesystemLabels, error) {

filesystems = append(filesystems, filesystemLabels{
device: parts[0],
mountPoint: parts[1],
mountPoint: rootfsStripPrefix(parts[1]),
fsType: parts[2],
options: parts[3],
})
Expand Down
33 changes: 31 additions & 2 deletions collector/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@ package collector

import (
"path"
"strings"

"github.com/prometheus/procfs"
"gopkg.in/alecthomas/kingpin.v2"
)

var (
// The path of the proc filesystem.
procPath = kingpin.Flag("path.procfs", "procfs mountpoint.").Default(procfs.DefaultMountPoint).String()
sysPath = kingpin.Flag("path.sysfs", "sysfs mountpoint.").Default("/sys").String()
procPath = kingpin.Flag("path.procfs", "procfs mountpoint.").Default(procfs.DefaultMountPoint).String()
sysPath = kingpin.Flag("path.sysfs", "sysfs mountpoint.").Default("/sys").String()
rootfsPath = kingpin.Flag("path.rootfs", "rootfs mountpoint.").Default("").String()
)

func procFilePath(name string) string {
Expand All @@ -33,3 +35,30 @@ func procFilePath(name string) string {
func sysFilePath(name string) string {
return path.Join(*sysPath, name)
}

func rootfsFilePath(name string) string {
if name == "/" {
return *rootfsPath
}
return path.Join(*rootfsPath, name)
}

func rootfsStripPrefix(path string) string {
if path == *rootfsPath {
return "/"
}
if strings.HasPrefix(path, *rootfsPath) {
return strings.TrimPrefix(path, *rootfsPath)
}
return path
}

func rootfsPathDetect(path string) bool {
if *rootfsPath == "" {
return true
}
if strings.HasPrefix(path, *rootfsPath) {
return true
}
return false
}