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

Docker host mount prefix #3529

Merged
merged 6 commits into from
Dec 1, 2017
Merged
Show file tree
Hide file tree
Changes from all 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
19 changes: 19 additions & 0 deletions docs/FAQ.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
# Frequently Asked Questions

### Q: How can I monitor the Docker Engine Host from within a container?

You will need to setup several volume mounts as well as some environment
variables:
```
docker run --name telegraf
-v /:/hostfs:ro
-v /etc:/hostfs/etc:ro
-v /proc:/hostfs/proc:ro
-v /sys:/hostfs/sys:ro
-v /var/run/utmp:/var/run/utmp:ro
-e HOST_ETC=/hostfs/etc
-e HOST_PROC=/hostfs/proc
-e HOST_SYS=/hostfs/sys
-e HOST_MOUNT_PREFIX=/hostfs
telegraf
```


### Q: Why do I get a "no such host" error resolving hostnames that other
programs can resolve?

Expand Down
6 changes: 4 additions & 2 deletions plugins/inputs/system/DISK_README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ Additionally, the behavior of resolving the `mount_points` can be configured by
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.
In this case, the host's root volume should be mounted into the container and the `HOST_MOUNT_PREFIX` and `HOST_PROC` environment variables set.

`docker run -v /:/hostfs:ro -e HOST_MOUNT_PREFIX=/hostfs -e HOST_ETC=/hostfs/etc telegraf`
```
docker run -v /:/hostfs:ro -e HOST_MOUNT_PREFIX=/hostfs -e HOST_PROC=/hostfs/proc telegraf
```

### Measurements & Fields:

Expand Down
2 changes: 0 additions & 2 deletions plugins/inputs/system/disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ func TestDiskUsage(t *testing.T) {

mps.On("Partitions", true).Return(psAll, nil)
mps.On("OSGetenv", "HOST_MOUNT_PREFIX").Return("")
mps.On("OSStat", "/").Return(MockFileInfo{}, nil)
mps.On("OSStat", "/home").Return(MockFileInfo{}, nil)
mps.On("PSDiskUsage", "/").Return(&duAll[0], nil)
mps.On("PSDiskUsage", "/home").Return(&duAll[1], nil)

Expand Down
19 changes: 15 additions & 4 deletions plugins/inputs/system/ps.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package system

import (
"os"
"strings"

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal"
Expand Down Expand Up @@ -84,6 +85,10 @@ func (s *systemPS) DiskUsage(
for _, filter := range fstypeExclude {
fstypeExcludeSet[filter] = true
}
paths := make(map[string]bool)
for _, part := range parts {
paths[part.Mountpoint] = true
}

// Autofs mounts indicate a potential mount, the partition will also be
// listed with the actual filesystem when mounted. Ignore the autofs
Expand All @@ -92,6 +97,7 @@ func (s *systemPS) DiskUsage(

var usage []*disk.UsageStat
var partitions []*disk.PartitionStat
hostMountPrefix := s.OSGetenv("HOST_MOUNT_PREFIX")

for i := range parts {
p := parts[i]
Expand All @@ -110,15 +116,20 @@ func (s *systemPS) DiskUsage(
continue
}

mountpoint := s.OSGetenv("HOST_MOUNT_PREFIX") + p.Mountpoint
if _, err := s.OSStat(mountpoint); err != nil {
// If there's a host mount prefix, exclude any paths which conflict
// with the prefix.
if len(hostMountPrefix) > 0 &&
!strings.HasPrefix(p.Mountpoint, hostMountPrefix) &&
paths[hostMountPrefix+p.Mountpoint] {
continue
}
du, err := s.PSDiskUsage(mountpoint)

du, err := s.PSDiskUsage(p.Mountpoint)
if err != nil {
continue
}
du.Path = p.Mountpoint

du.Path = strings.TrimPrefix(p.Mountpoint, hostMountPrefix)
du.Fstype = p.Fstype
usage = append(usage, du)
partitions = append(partitions, &p)
Expand Down