From e707514b9377f9493dac55aaf41d5e2373a90262 Mon Sep 17 00:00:00 2001 From: Dan Cleinmark Date: Mon, 21 Mar 2016 11:27:20 -0500 Subject: [PATCH] Add stripped-mount-point-paths argument * accepts a regex pattern to be removed from the mountpoints exported by the filesystem collector. * addresses issue #66 --- README.md | 9 +++++++++ collector/filesystem_common.go | 27 ++++++++++++++++++--------- collector/filesystem_linux.go | 18 +++++++++++++++--- 3 files changed, 42 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index c53d33d08e..80ab13a821 100644 --- a/README.md +++ b/README.md @@ -99,3 +99,12 @@ docker pull prom/node-exporter docker run -d -p 9100:9100 --net="host" prom/node-exporter ``` + +Note that Docker (and containers in general) is designed to separate a container's runtime from the +host. If you are running the node_exporter inside of Docker to export statistics about the host, +use a command similar to: + + docker run -d -p 9100:9100 -v "/proc:/host/proc" -v "/sys:/host/sys" -v "/:/rootfs" --net="host" prom/node-exporter -collector.procfs /host/proc -collector.sysfs /host/proc -collector.filesystem.ignored-mount-points "^/(sys|proc|dev|host|etc|rootfs/sys|rootfs/var/lib|)($|/)" -collector.filesystem.stripped-mount-point-paths "^/rootfs" + +See [Setting up the Node Exporter (in Docker)](https://www.digitalocean.com/community/tutorials/how-to-install-prometheus-using-docker-on-ubuntu-14-04#step-2-—-setting-up-node-exporter) +for more details. diff --git a/collector/filesystem_common.go b/collector/filesystem_common.go index b92f212349..75080816a6 100644 --- a/collector/filesystem_common.go +++ b/collector/filesystem_common.go @@ -25,6 +25,7 @@ import ( // Arch-dependent implementation must define: // * defIgnoredMountPoints +// * defStrippedMountPointPaths // * filesystemLabelNames // * filesystemCollector.GetStats @@ -34,11 +35,17 @@ var ( defIgnoredMountPoints, "Regexp of mount points to ignore for filesystem collector.") + strippedMountPointPaths = flag.String( + "collector.filesystem.stripped-mount-point-paths", + defStrippedMountPointPaths, + "Regexp of mount point paths to remove for filesystem collector.") + filesystemLabelNames = []string{"device", "mountpoint", "fstype"} ) type filesystemCollector struct { - ignoredMountPointsPattern *regexp.Regexp + ignoredMountPointsPattern *regexp.Regexp + strippedMountPointPathsPattern *regexp.Regexp sizeDesc, freeDesc, availDesc, filesDesc, filesFreeDesc, roDesc *prometheus.Desc } @@ -56,7 +63,8 @@ func init() { // Filesystems stats. func NewFilesystemCollector() (Collector, error) { subsystem := "filesystem" - pattern := regexp.MustCompile(*ignoredMountPoints) + ignoredMountPointsPattern := regexp.MustCompile(*ignoredMountPoints) + strippedMountPointPathsPattern := regexp.MustCompile(*strippedMountPointPaths) sizeDesc := prometheus.NewDesc( prometheus.BuildFQName(Namespace, subsystem, "size"), @@ -95,13 +103,14 @@ func NewFilesystemCollector() (Collector, error) { ) return &filesystemCollector{ - ignoredMountPointsPattern: pattern, - sizeDesc: sizeDesc, - freeDesc: freeDesc, - availDesc: availDesc, - filesDesc: filesDesc, - filesFreeDesc: filesFreeDesc, - roDesc: roDesc, + ignoredMountPointsPattern: ignoredMountPointsPattern, + strippedMountPointPathsPattern: strippedMountPointPathsPattern, + sizeDesc: sizeDesc, + freeDesc: freeDesc, + availDesc: availDesc, + filesDesc: filesDesc, + filesFreeDesc: filesFreeDesc, + roDesc: roDesc, }, nil } diff --git a/collector/filesystem_linux.go b/collector/filesystem_linux.go index 02f2c4a1b6..ffb6adc256 100644 --- a/collector/filesystem_linux.go +++ b/collector/filesystem_linux.go @@ -18,6 +18,7 @@ package collector import ( "bufio" "os" + "regexp" "strings" "syscall" @@ -25,8 +26,13 @@ import ( ) const ( - defIgnoredMountPoints = "^/(sys|proc|dev)($|/)" - ST_RDONLY = 0x1 + defIgnoredMountPoints = "^/(sys|proc|dev)($|/)" + defStrippedMountPointPaths = "" + ST_RDONLY = 0x1 +) + +var ( + rDoubleSlash = regexp.MustCompile("//") ) type filesystemDetails struct { @@ -60,7 +66,13 @@ func (c *filesystemCollector) GetStats() (stats []filesystemStats, err error) { ro = 1 } - labelValues := []string{mpd.device, mpd.mountPoint, mpd.fsType} + mountPoint := rDoubleSlash.ReplaceAllString( + c.strippedMountPointPathsPattern.ReplaceAllString(mpd.mountPoint, "/"), + "/") + labelValues := []string{ + mpd.device, + mountPoint, + mpd.fsType} stats = append(stats, filesystemStats{ labelValues: labelValues, size: float64(buf.Blocks) * float64(buf.Bsize),