From cf5d6b4414a692ae5629cb3f9ae073622e9a09a4 Mon Sep 17 00:00:00 2001 From: Joshua Powers Date: Fri, 10 Nov 2023 09:24:35 -0700 Subject: [PATCH] fix(inputs.disk): Add inodes_used_percent field (#14267) (cherry picked from commit dce3bbd6791ea3b6779860314127f10f97cd7679) --- plugins/inputs/disk/README.md | 11 +- plugins/inputs/disk/disk.go | 21 ++-- plugins/inputs/disk/disk_test.go | 196 ++++++++++++++++--------------- 3 files changed, 124 insertions(+), 104 deletions(-) diff --git a/plugins/inputs/disk/README.md b/plugins/inputs/disk/README.md index f8b27cc6f1e47..8a2bb0bec3785 100644 --- a/plugins/inputs/disk/README.md +++ b/plugins/inputs/disk/README.md @@ -63,6 +63,7 @@ docker run -v /:/hostfs:ro -e HOST_MOUNT_PREFIX=/hostfs -e HOST_PROC=/hostfs/pro - inodes_free (integer, files) - inodes_total (integer, files) - inodes_used (integer, files) + - inodes_used_percent (float, percent) ## Troubleshooting @@ -85,11 +86,11 @@ sudo setfacl -R -m u:telegraf:X /var/lib/docker/volumes/ ## Example Output ```text -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=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 +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,inodes_used_percent=20.1729894 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,inodes_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,inodes_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,inodes_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,inodes_used_percent=0.0017530778 1677001387000000000 ``` [statfs]: http://man7.org/linux/man-pages/man2/statfs.2.html diff --git a/plugins/inputs/disk/disk.go b/plugins/inputs/disk/disk.go index 71405840d3e75..616febc2f465d 100644 --- a/plugins/inputs/disk/disk.go +++ b/plugins/inputs/disk/disk.go @@ -75,14 +75,21 @@ func (ds *DiskStats) Gather(acc telegraf.Accumulator) error { (float64(du.Used) + float64(du.Free)) * 100 } + var inodesUsedPercent float64 + if du.InodesUsed+du.InodesFree > 0 { + inodesUsedPercent = float64(du.InodesUsed) / + (float64(du.InodesUsed) + float64(du.InodesFree)) * 100 + } + fields := map[string]interface{}{ - "total": du.Total, - "free": du.Free, - "used": du.Used, - "used_percent": usedPercent, - "inodes_total": du.InodesTotal, - "inodes_free": du.InodesFree, - "inodes_used": du.InodesUsed, + "total": du.Total, + "free": du.Free, + "used": du.Used, + "used_percent": usedPercent, + "inodes_total": du.InodesTotal, + "inodes_free": du.InodesFree, + "inodes_used": du.InodesUsed, + "inodes_used_percent": inodesUsedPercent, } acc.AddGauge("disk", fields, tags) } diff --git a/plugins/inputs/disk/disk_test.go b/plugins/inputs/disk/disk_test.go index e47bebb326921..975fd58c685e6 100644 --- a/plugins/inputs/disk/disk_test.go +++ b/plugins/inputs/disk/disk_test.go @@ -93,7 +93,7 @@ func TestDiskUsage(t *testing.T) { require.NoError(t, err) numDiskMetrics := acc.NFields() - expectedAllDiskMetrics := 21 + expectedAllDiskMetrics := 24 require.Equal(t, expectedAllDiskMetrics, numDiskMetrics) tags1 := map[string]string{ @@ -116,31 +116,34 @@ func TestDiskUsage(t *testing.T) { } fields1 := map[string]interface{}{ - "total": uint64(128), - "used": uint64(100), - "free": uint64(23), - "inodes_total": uint64(1234), - "inodes_free": uint64(234), - "inodes_used": uint64(1000), - "used_percent": float64(81.30081300813008), + "total": uint64(128), + "used": uint64(100), + "free": uint64(23), + "inodes_total": uint64(1234), + "inodes_free": uint64(234), + "inodes_used": uint64(1000), + "used_percent": float64(81.30081300813008), + "inodes_used_percent": float64(81.03727714748784), } fields2 := map[string]interface{}{ - "total": uint64(256), - "used": uint64(200), - "free": uint64(46), - "inodes_total": uint64(2468), - "inodes_free": uint64(468), - "inodes_used": uint64(2000), - "used_percent": float64(81.30081300813008), + "total": uint64(256), + "used": uint64(200), + "free": uint64(46), + "inodes_total": uint64(2468), + "inodes_free": uint64(468), + "inodes_used": uint64(2000), + "used_percent": float64(81.30081300813008), + "inodes_used_percent": float64(81.03727714748784), } fields3 := map[string]interface{}{ - "total": uint64(128), - "used": uint64(100), - "free": uint64(23), - "inodes_total": uint64(1234), - "inodes_free": uint64(234), - "inodes_used": uint64(1000), - "used_percent": float64(81.30081300813008), + "total": uint64(128), + "used": uint64(100), + "free": uint64(23), + "inodes_total": uint64(1234), + "inodes_free": uint64(234), + "inodes_used": uint64(1000), + "used_percent": float64(81.30081300813008), + "inodes_used_percent": float64(81.03727714748784), } acc.AssertContainsTaggedFields(t, "disk", fields1, tags1) acc.AssertContainsTaggedFields(t, "disk", fields2, tags2) @@ -150,18 +153,18 @@ func TestDiskUsage(t *testing.T) { // and /home not matching the /dev in MountPoints err = (&DiskStats{ps: &mps, MountPoints: []string{"/", "/dev"}}).Gather(&acc) require.NoError(t, err) - require.Equal(t, expectedAllDiskMetrics+7, acc.NFields()) + require.Equal(t, expectedAllDiskMetrics+8, acc.NFields()) // We should see all the diskpoints as MountPoints includes both // /, /home, and /var/rootbind err = (&DiskStats{ps: &mps, MountPoints: []string{"/", "/home", "/var/rootbind"}}).Gather(&acc) require.NoError(t, err) - require.Equal(t, expectedAllDiskMetrics+7*4, acc.NFields()) + require.Equal(t, expectedAllDiskMetrics+8*4, acc.NFields()) // We should see all the mounts as MountPoints except the bind mound err = (&DiskStats{ps: &mps, IgnoreMountOpts: []string{"bind"}}).Gather(&acc) require.NoError(t, err) - require.Equal(t, expectedAllDiskMetrics+7*6, acc.NFields()) + require.Equal(t, expectedAllDiskMetrics+8*6, acc.NFields()) } func TestDiskUsageHostMountPrefix(t *testing.T) { @@ -196,13 +199,14 @@ func TestDiskUsageHostMountPrefix(t *testing.T) { "mode": "ro", }, expectedFields: map[string]interface{}{ - "total": uint64(42), - "used": uint64(0), - "free": uint64(0), - "inodes_total": uint64(0), - "inodes_free": uint64(0), - "inodes_used": uint64(0), - "used_percent": float64(0), + "total": uint64(42), + "used": uint64(0), + "free": uint64(0), + "inodes_total": uint64(0), + "inodes_free": uint64(0), + "inodes_used": uint64(0), + "used_percent": float64(0), + "inodes_used_percent": float64(0), }, }, { @@ -229,13 +233,14 @@ func TestDiskUsageHostMountPrefix(t *testing.T) { "mode": "ro", }, expectedFields: map[string]interface{}{ - "total": uint64(42), - "used": uint64(0), - "free": uint64(0), - "inodes_total": uint64(0), - "inodes_free": uint64(0), - "inodes_used": uint64(0), - "used_percent": float64(0), + "total": uint64(42), + "used": uint64(0), + "free": uint64(0), + "inodes_total": uint64(0), + "inodes_free": uint64(0), + "inodes_used": uint64(0), + "used_percent": float64(0), + "inodes_used_percent": float64(0), }, }, { @@ -262,13 +267,14 @@ func TestDiskUsageHostMountPrefix(t *testing.T) { "mode": "ro", }, expectedFields: map[string]interface{}{ - "total": uint64(42), - "used": uint64(0), - "free": uint64(0), - "inodes_total": uint64(0), - "inodes_free": uint64(0), - "inodes_used": uint64(0), - "used_percent": float64(0), + "total": uint64(42), + "used": uint64(0), + "free": uint64(0), + "inodes_total": uint64(0), + "inodes_free": uint64(0), + "inodes_used": uint64(0), + "used_percent": float64(0), + "inodes_used_percent": float64(0), }, }, } @@ -424,7 +430,7 @@ func TestDiskStats(t *testing.T) { require.NoError(t, err) numDiskMetrics := acc.NFields() - expectedAllDiskMetrics := 21 + expectedAllDiskMetrics := 24 require.Equal(t, expectedAllDiskMetrics, numDiskMetrics) tags1 := map[string]string{ @@ -441,22 +447,24 @@ func TestDiskStats(t *testing.T) { } fields1 := map[string]interface{}{ - "total": uint64(128), - "used": uint64(100), - "free": uint64(23), - "inodes_total": uint64(1234), - "inodes_free": uint64(234), - "inodes_used": uint64(1000), - "used_percent": float64(81.30081300813008), + "total": uint64(128), + "used": uint64(100), + "free": uint64(23), + "inodes_total": uint64(1234), + "inodes_free": uint64(234), + "inodes_used": uint64(1000), + "used_percent": float64(81.30081300813008), + "inodes_used_percent": float64(81.03727714748784), } fields2 := map[string]interface{}{ - "total": uint64(256), - "used": uint64(200), - "free": uint64(46), - "inodes_total": uint64(2468), - "inodes_free": uint64(468), - "inodes_used": uint64(2000), - "used_percent": float64(81.30081300813008), + "total": uint64(256), + "used": uint64(200), + "free": uint64(46), + "inodes_total": uint64(2468), + "inodes_free": uint64(468), + "inodes_used": uint64(2000), + "used_percent": float64(81.30081300813008), + "inodes_used_percent": float64(81.03727714748784), } acc.AssertContainsTaggedFields(t, "disk", fields1, tags1) acc.AssertContainsTaggedFields(t, "disk", fields2, tags2) @@ -465,18 +473,18 @@ func TestDiskStats(t *testing.T) { // and /home and /var/rootbind not matching the /dev in MountPoints err = (&DiskStats{ps: &mps, MountPoints: []string{"/", "/dev"}}).Gather(&acc) require.NoError(t, err) - require.Equal(t, expectedAllDiskMetrics+7, acc.NFields()) + require.Equal(t, expectedAllDiskMetrics+8, acc.NFields()) // We should see all the diskpoints as MountPoints includes both // /, /home, and /var/rootbind err = (&DiskStats{ps: &mps, MountPoints: []string{"/", "/home", "/var/rootbind"}}).Gather(&acc) require.NoError(t, err) - require.Equal(t, expectedAllDiskMetrics+7*4, acc.NFields()) + require.Equal(t, expectedAllDiskMetrics+8*4, acc.NFields()) // We should see all the mounts as MountPoints except the bind mound err = (&DiskStats{ps: &mps, IgnoreMountOpts: []string{"bind"}}).Gather(&acc) require.NoError(t, err) - require.Equal(t, expectedAllDiskMetrics+7*6, acc.NFields()) + require.Equal(t, expectedAllDiskMetrics+8*6, acc.NFields()) } func TestDiskUsageIssues(t *testing.T) { @@ -511,13 +519,14 @@ func TestDiskUsageIssues(t *testing.T) { "path": "/tmp", }, map[string]interface{}{ - "total": uint64(256), - "used": uint64(200), - "free": uint64(46), - "inodes_total": uint64(2468), - "inodes_free": uint64(468), - "inodes_used": uint64(2000), - "used_percent": float64(81.30081300813008), + "total": uint64(256), + "used": uint64(200), + "free": uint64(46), + "inodes_total": uint64(2468), + "inodes_free": uint64(468), + "inodes_used": uint64(2000), + "used_percent": float64(81.30081300813008), + "inodes_used_percent": float64(81.03727714748784), }, time.Unix(0, 0), telegraf.Gauge, @@ -531,13 +540,14 @@ func TestDiskUsageIssues(t *testing.T) { "path": "/", }, map[string]interface{}{ - "total": uint64(256), - "used": uint64(200), - "free": uint64(46), - "inodes_total": uint64(2468), - "inodes_free": uint64(468), - "inodes_used": uint64(2000), - "used_percent": float64(81.30081300813008), + "total": uint64(256), + "used": uint64(200), + "free": uint64(46), + "inodes_total": uint64(2468), + "inodes_free": uint64(468), + "inodes_used": uint64(2000), + "used_percent": float64(81.30081300813008), + "inodes_used_percent": float64(81.03727714748784), }, time.Unix(0, 0), telegraf.Gauge, @@ -565,13 +575,14 @@ func TestDiskUsageIssues(t *testing.T) { "path": "/", }, map[string]interface{}{ - "total": uint64(256), - "used": uint64(200), - "free": uint64(46), - "inodes_total": uint64(2468), - "inodes_free": uint64(468), - "inodes_used": uint64(2000), - "used_percent": float64(81.30081300813008), + "total": uint64(256), + "used": uint64(200), + "free": uint64(46), + "inodes_total": uint64(2468), + "inodes_free": uint64(468), + "inodes_used": uint64(2000), + "used_percent": float64(81.30081300813008), + "inodes_used_percent": float64(81.03727714748784), }, time.Unix(0, 0), telegraf.Gauge, @@ -585,13 +596,14 @@ func TestDiskUsageIssues(t *testing.T) { "path": "/mnt/storage", }, map[string]interface{}{ - "total": uint64(256), - "used": uint64(200), - "free": uint64(46), - "inodes_total": uint64(2468), - "inodes_free": uint64(468), - "inodes_used": uint64(2000), - "used_percent": float64(81.30081300813008), + "total": uint64(256), + "used": uint64(200), + "free": uint64(46), + "inodes_total": uint64(2468), + "inodes_free": uint64(468), + "inodes_used": uint64(2000), + "used_percent": float64(81.30081300813008), + "inodes_used_percent": float64(81.03727714748784), }, time.Unix(0, 0), telegraf.Gauge,