From 0b9b7bddd7ef2ececcc024042b97fc31301fb57d Mon Sep 17 00:00:00 2001
From: John Engelman <john.r.engelman@gmail.com>
Date: Thu, 28 Apr 2016 14:24:09 -0500
Subject: [PATCH] Specify host mount prefix with envvar.

closes #1120
---
 CHANGELOG.md                         | 3 +++
 plugins/inputs/system/DISK_README.md | 8 ++++++++
 plugins/inputs/system/ps.go          | 6 ++++--
 3 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index aae65b99..e4fac45e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -49,6 +49,9 @@ based on _prefix_ in addition to globs. This means that a filter like
   - rethinkdb: `host -> rethinkdb_host`
 
 - **Breaking Change**: The `win_perf_counters` input has been changed to sanitize field names, replacing `/Sec` and `/sec` with `_persec`, as well as spaces with underscores. This is needed because Graphite doesn't like slashes and spaces, and was failing to accept metrics that had them. The `/[sS]ec` -> `_persec` is just to make things clearer and uniform.
+- The `disk` input plugin can now be configured with the `HOST_MOUNT_PREFIX` environment variable.
+This value is prepended to any mountpaths discovered before retrieving stats.
+It is not included on the report path. This is necessary for reporting host disk stats when running from within a container.
 
 ### Features
 
diff --git a/plugins/inputs/system/DISK_README.md b/plugins/inputs/system/DISK_README.md
index c89007b4..c510615e 100644
--- a/plugins/inputs/system/DISK_README.md
+++ b/plugins/inputs/system/DISK_README.md
@@ -16,6 +16,14 @@ https://en.wikipedia.org/wiki/Df_(Unix) for more details.
   # mount_points = ["/"]
 ```
 
+Additionally, the behavior of resolving the `mount_points` can be configured by using the `HOST_MOUNT_PREFIX` environment variable.
+When present, this variable is prepended to the mountpoints discovered by the plugin before retrieving stats.
+The prefix is stripped from the reported `path` in the measurement.
+This settings is useful when running `telegraf` inside a docker container to report host machine metrics.
+In this case, the host's root volume should be mounted into the container and the `HOST_MOUNT_PREFIX` and `HOST_ETC` environment variables set.
+
+`docker run -v /:/hostfs:ro -e HOST_MOUNT_PREFIX=/hostfs -e HOST_ETC=/hostfs/etc telegraf-docker`
+
 ### Measurements & Fields:
 
 - disk
diff --git a/plugins/inputs/system/ps.go b/plugins/inputs/system/ps.go
index f1a1b27d..9602acd5 100644
--- a/plugins/inputs/system/ps.go
+++ b/plugins/inputs/system/ps.go
@@ -81,8 +81,10 @@ func (s *systemPS) DiskUsage(
 				continue
 			}
 		}
-		if _, err := os.Stat(p.Mountpoint); err == nil {
-			du, err := disk.DiskUsage(p.Mountpoint)
+		mountpoint := os.Getenv("HOST_MOUNT_PREFIX") + p.Mountpoint
+		if _, err := os.Stat(mountpoint); err == nil {
+			du, err := disk.DiskUsage(mountpoint)
+			du.Path = p.Mountpoint
 			if err != nil {
 				return nil, err
 			}
-- 
GitLab