Fix HOST_MOUNT_PREFIX in docker with disk input (#3529)
(cherry picked from commit 7f66863b87
)
This commit is contained in:
parent
809ed511dd
commit
c095876442
|
@ -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.
|
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.
|
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.
|
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`
|
```
|
||||||
|
docker run -v /:/hostfs:ro -e HOST_MOUNT_PREFIX=/hostfs -e HOST_PROC=/hostfs/proc telegraf
|
||||||
|
```
|
||||||
|
|
||||||
### Measurements & Fields:
|
### Measurements & Fields:
|
||||||
|
|
||||||
|
|
|
@ -62,8 +62,6 @@ func TestDiskUsage(t *testing.T) {
|
||||||
|
|
||||||
mps.On("Partitions", true).Return(psAll, nil)
|
mps.On("Partitions", true).Return(psAll, nil)
|
||||||
mps.On("OSGetenv", "HOST_MOUNT_PREFIX").Return("")
|
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", "/").Return(&duAll[0], nil)
|
||||||
mps.On("PSDiskUsage", "/home").Return(&duAll[1], nil)
|
mps.On("PSDiskUsage", "/home").Return(&duAll[1], nil)
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@ package system
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/influxdata/telegraf"
|
"github.com/influxdata/telegraf"
|
||||||
"github.com/influxdata/telegraf/internal"
|
"github.com/influxdata/telegraf/internal"
|
||||||
|
@ -84,9 +85,14 @@ func (s *systemPS) DiskUsage(
|
||||||
for _, filter := range fstypeExclude {
|
for _, filter := range fstypeExclude {
|
||||||
fstypeExcludeSet[filter] = true
|
fstypeExcludeSet[filter] = true
|
||||||
}
|
}
|
||||||
|
paths := make(map[string]bool)
|
||||||
|
for _, part := range parts {
|
||||||
|
paths[part.Mountpoint] = true
|
||||||
|
}
|
||||||
|
|
||||||
var usage []*disk.UsageStat
|
var usage []*disk.UsageStat
|
||||||
var partitions []*disk.PartitionStat
|
var partitions []*disk.PartitionStat
|
||||||
|
hostMountPrefix := s.OSGetenv("HOST_MOUNT_PREFIX")
|
||||||
|
|
||||||
for i := range parts {
|
for i := range parts {
|
||||||
p := parts[i]
|
p := parts[i]
|
||||||
|
@ -105,15 +111,20 @@ func (s *systemPS) DiskUsage(
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
mountpoint := s.OSGetenv("HOST_MOUNT_PREFIX") + p.Mountpoint
|
// If there's a host mount prefix, exclude any paths which conflict
|
||||||
if _, err := s.OSStat(mountpoint); err != nil {
|
// with the prefix.
|
||||||
|
if len(hostMountPrefix) > 0 &&
|
||||||
|
!strings.HasPrefix(p.Mountpoint, hostMountPrefix) &&
|
||||||
|
paths[hostMountPrefix+p.Mountpoint] {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
du, err := s.PSDiskUsage(mountpoint)
|
|
||||||
|
du, err := s.PSDiskUsage(p.Mountpoint)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
du.Path = p.Mountpoint
|
|
||||||
|
du.Path = strings.TrimPrefix(p.Mountpoint, hostMountPrefix)
|
||||||
du.Fstype = p.Fstype
|
du.Fstype = p.Fstype
|
||||||
usage = append(usage, du)
|
usage = append(usage, du)
|
||||||
partitions = append(partitions, &p)
|
partitions = append(partitions, &p)
|
||||||
|
|
Loading…
Reference in New Issue