Skip to content

Commit

Permalink
Strip rootfs prefix for run in docker
Browse files Browse the repository at this point in the history
- relates to prometheus#66

Signed-off-by: Ivan Mikheykin <ivan.mikheykin@flant.com>
  • Loading branch information
diafour authored and marcbachmann committed Jul 9, 2018
1 parent ac5a981 commit 34d2078
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,23 @@ 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. You also need bind-mount host proc and host sys
with `path.procfs` and `path.sysfs` arguments.
Also you need to use `bind-propagation=rslave` option for bind-mount host rootfs to
propagate host mounts changes to container (option available since 17.05.0).

```bash
docker run -d \
--net="host" \
--pid="host" \
quay.io/prometheus/node-exporter
-v "/proc:/host/proc:ro" -v "/sys:/host/sys:ro" \
--mount "type=bind,source=/,target=/rootfs,readonly,bind-propagation=rslave" \
quay.io/prometheus/node-exporter \
--path.procfs /host/proc --path.sysfs /host/sys \
--path.rootfs /rootfs
```

## Using a third-party repository for RHEL/CentOS/Fedora
Expand Down
10 changes: 7 additions & 3 deletions collector/filesystem_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ func (c *filesystemCollector) GetStats() ([]filesystemStats, error) {
}

buf := new(syscall.Statfs_t)
err := syscall.Statfs(labels.mountPoint, buf)
err := syscall.Statfs(rootfsFilePath(labels.mountPoint), buf)
if err != nil {
stats = append(stats, filesystemStats{
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 @@ -93,9 +93,13 @@ func mountPointDetails() ([]filesystemLabels, error) {
parts[1] = strings.Replace(parts[1], "\\040", " ", -1)
parts[1] = strings.Replace(parts[1], "\\011", "\t", -1)

// skip non rootfs paths if rootfsPath defined
if !rootfsPathDetect(parts[1]) {
continue
}
filesystems = append(filesystems, filesystemLabels{
device: parts[0],
mountPoint: parts[1],
mountPoint: rootfsStripPrefix(parts[1]),
fsType: parts[2],
})
}
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
}

0 comments on commit 34d2078

Please sign in to comment.