Skip to content

Commit c095876

Browse files
committed
Fix HOST_MOUNT_PREFIX in docker with disk input (#3529)
(cherry picked from commit 7f66863)
1 parent 809ed51 commit c095876

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

plugins/inputs/system/DISK_README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ Additionally, the behavior of resolving the `mount_points` can be configured by
2020
When present, this variable is prepended to the mountpoints discovered by the plugin before retrieving stats.
2121
The prefix is stripped from the reported `path` in the measurement.
2222
This settings is useful when running `telegraf` inside a docker container to report host machine metrics.
23-
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.
23+
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.
2424

25-
`docker run -v /:/hostfs:ro -e HOST_MOUNT_PREFIX=/hostfs -e HOST_ETC=/hostfs/etc telegraf-docker`
25+
```
26+
docker run -v /:/hostfs:ro -e HOST_MOUNT_PREFIX=/hostfs -e HOST_PROC=/hostfs/proc telegraf
27+
```
2628

2729
### Measurements & Fields:
2830

plugins/inputs/system/disk_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ func TestDiskUsage(t *testing.T) {
6262

6363
mps.On("Partitions", true).Return(psAll, nil)
6464
mps.On("OSGetenv", "HOST_MOUNT_PREFIX").Return("")
65-
mps.On("OSStat", "/").Return(MockFileInfo{}, nil)
66-
mps.On("OSStat", "/home").Return(MockFileInfo{}, nil)
6765
mps.On("PSDiskUsage", "/").Return(&duAll[0], nil)
6866
mps.On("PSDiskUsage", "/home").Return(&duAll[1], nil)
6967

plugins/inputs/system/ps.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package system
22

33
import (
44
"os"
5+
"strings"
56

67
"github.com/influxdata/telegraf"
78
"github.com/influxdata/telegraf/internal"
@@ -84,9 +85,14 @@ func (s *systemPS) DiskUsage(
8485
for _, filter := range fstypeExclude {
8586
fstypeExcludeSet[filter] = true
8687
}
88+
paths := make(map[string]bool)
89+
for _, part := range parts {
90+
paths[part.Mountpoint] = true
91+
}
8792

8893
var usage []*disk.UsageStat
8994
var partitions []*disk.PartitionStat
95+
hostMountPrefix := s.OSGetenv("HOST_MOUNT_PREFIX")
9096

9197
for i := range parts {
9298
p := parts[i]
@@ -105,15 +111,20 @@ func (s *systemPS) DiskUsage(
105111
continue
106112
}
107113

108-
mountpoint := s.OSGetenv("HOST_MOUNT_PREFIX") + p.Mountpoint
109-
if _, err := s.OSStat(mountpoint); err != nil {
114+
// If there's a host mount prefix, exclude any paths which conflict
115+
// with the prefix.
116+
if len(hostMountPrefix) > 0 &&
117+
!strings.HasPrefix(p.Mountpoint, hostMountPrefix) &&
118+
paths[hostMountPrefix+p.Mountpoint] {
110119
continue
111120
}
112-
du, err := s.PSDiskUsage(mountpoint)
121+
122+
du, err := s.PSDiskUsage(p.Mountpoint)
113123
if err != nil {
114124
continue
115125
}
116-
du.Path = p.Mountpoint
126+
127+
du.Path = strings.TrimPrefix(p.Mountpoint, hostMountPrefix)
117128
du.Fstype = p.Fstype
118129
usage = append(usage, du)
119130
partitions = append(partitions, &p)

0 commit comments

Comments
 (0)