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

feat: add disk label tag to disk plugin #12594 #12620

Closed
wants to merge 2 commits into from
Closed
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
3 changes: 2 additions & 1 deletion plugins/inputs/disk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 (disk label if present)
- fields:
- free (integer, bytes)
- total (integer, bytes)
Expand Down Expand Up @@ -84,7 +85,7 @@ sudo setfacl -R -m u:telegraf:X /var/lib/docker/volumes/
## Example Output

```shell
disk,fstype=hfs,mode=ro,path=/ free=398407520256i,inodes_free=97267461i,inodes_total=121847806i,inodes_used=24580345i,total=499088621568i,used=100418957312i,used_percent=20.131039916242397 1453832006274071563
disk,fstype=hfs,label=root,mode=ro,path=/ free=398407520256i,inodes_free=97267461i,inodes_total=121847806i,inodes_used=24580345i,total=499088621568i,used=100418957312i,used_percent=20.131039916242397 1453832006274071563
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
Expand Down
1 change: 1 addition & 0 deletions plugins/inputs/disk/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func (ds *DiskStats) Gather(acc telegraf.Accumulator) error {
tags := map[string]string{
"path": du.Path,
"device": strings.ReplaceAll(partitions[i].Device, "/dev/", ""),
"label": partitions[i].Label,
"fstype": du.Fstype,
"mode": mountOpts.Mode(),
}
Expand Down
22 changes: 18 additions & 4 deletions plugins/inputs/system/ps.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

type PS interface {
CPUTimes(perCPU, totalCPU bool) ([]cpu.TimesStat, error)
DiskUsage(mountPointFilter []string, mountOptsExclude []string, fstypeExclude []string) ([]*disk.UsageStat, []*disk.PartitionStat, error)
DiskUsage(mountPointFilter []string, mountOptsExclude []string, fstypeExclude []string) ([]*disk.UsageStat, []*PartitionStatExtended, error)
NetIO() ([]net.IOCountersStat, error)
NetProto() ([]net.ProtoCountersStat, error)
DiskIO(names []string) (map[string]disk.IOCountersStat, error)
Expand Down Expand Up @@ -46,6 +46,14 @@ type SystemPS struct {

type SystemPSDisk struct{}

type PartitionStatExtended struct {
Device string `json:"device"`
Mountpoint string `json:"mountpoint"`
Fstype string `json:"fstype"`
Opts []string `json:"opts"`
Label string `json:"label"`
}

func (s *SystemPS) CPUTimes(perCPU, totalCPU bool) ([]cpu.TimesStat, error) {
var cpuTimes []cpu.TimesStat
if perCPU {
Expand Down Expand Up @@ -94,7 +102,7 @@ func (s *SystemPS) DiskUsage(
mountPointFilter []string,
mountOptsExclude []string,
fstypeExclude []string,
) ([]*disk.UsageStat, []*disk.PartitionStat, error) {
) ([]*disk.UsageStat, []*PartitionStatExtended, error) {
parts, err := s.Partitions(true)
if err != nil {
return nil, nil, err
Expand Down Expand Up @@ -123,7 +131,7 @@ func (s *SystemPS) DiskUsage(
fstypeExcludeSet.add("autofs")

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

partitionRange:
Expand Down Expand Up @@ -174,7 +182,13 @@ partitionRange:
du.Path = filepath.Join("/", strings.TrimPrefix(p.Mountpoint, hostMountPrefix))
du.Fstype = p.Fstype
usage = append(usage, du)
partitions = append(partitions, &p)
e := PartitionStatExtended{Device: p.Device, Mountpoint: p.Mountpoint, Fstype: p.Fstype, Opts: p.Opts}
m := strings.Count(p.Device, "/dev/")
if (m > 0) {
label, _ := disk.Label(strings.Replace(p.Device, "/dev/", "", 1))
e.Label = label
}
partitions = append(partitions, &e)
}

return usage, partitions, nil
Expand Down