feat(inputs.disk): Add label as tag (#12696)

This commit is contained in:
Joshua Powers 2023-02-22 07:55:55 -07:00 committed by GitHub
parent 29916dfee0
commit caf14adb17
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 1 deletions

View File

@ -54,6 +54,7 @@ docker run -v /:/hostfs:ro -e HOST_MOUNT_PREFIX=/hostfs -e HOST_PROC=/hostfs/pro
- device (device file)
- path (mount point path)
- mode (whether the mount is rw or ro)
- label (devicemapper labels, only if present)
- fields:
- free (integer, bytes)
- total (integer, bytes)
@ -88,6 +89,7 @@ disk,fstype=hfs,mode=ro,path=/ free=398407520256i,inodes_free=97267461i,inodes_t
disk,fstype=devfs,mode=rw,path=/dev free=0i,inodes_free=0i,inodes_total=628i,inodes_used=628i,total=185856i,used=185856i,used_percent=100 1453832006274137913
disk,fstype=autofs,mode=rw,path=/net free=0i,inodes_free=0i,inodes_total=0i,inodes_used=0i,total=0i,used=0i,used_percent=0 1453832006274157077
disk,fstype=autofs,mode=rw,path=/home free=0i,inodes_free=0i,inodes_total=0i,inodes_used=0i,total=0i,used=0i,used_percent=0 1453832006274169688
disk,device=dm-1,fstype=xfs,label=lvg-lv,mode=rw,path=/mnt inodes_free=8388605i,inodes_used=3i,total=17112760320i,free=16959598592i,used=153161728i,used_percent=0.8950147441789215,inodes_total=8388608i 1677001387000000000
```
[statfs]: http://man7.org/linux/man-pages/man2/statfs.2.html

View File

@ -9,6 +9,7 @@ import (
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/inputs"
"github.com/influxdata/telegraf/plugins/inputs/system"
"github.com/shirou/gopsutil/v3/disk"
)
//go:embed sample.conf
@ -53,13 +54,21 @@ func (ds *DiskStats) Gather(acc telegraf.Accumulator) error {
// Skip dummy filesystem (procfs, cgroupfs, ...)
continue
}
device := partitions[i].Device
mountOpts := MountOptions(partitions[i].Opts)
tags := map[string]string{
"path": du.Path,
"device": strings.ReplaceAll(partitions[i].Device, "/dev/", ""),
"device": strings.ReplaceAll(device, "/dev/", ""),
"fstype": du.Fstype,
"mode": mountOpts.Mode(),
}
label, err := disk.Label(strings.TrimPrefix(device, "/dev/"))
if err == nil && label != "" {
tags["label"] = label
}
var usedPercent float64
if du.Used+du.Free > 0 {
usedPercent = float64(du.Used) /