Skip to content

Commit

Permalink
fix(inputs.disk): Add inodes_used_percent field (#14267)
Browse files Browse the repository at this point in the history
(cherry picked from commit dce3bbd)
  • Loading branch information
powersj committed Nov 13, 2023
1 parent ebaeeff commit cf5d6b4
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 104 deletions.
11 changes: 6 additions & 5 deletions plugins/inputs/disk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
21 changes: 14 additions & 7 deletions plugins/inputs/disk/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
196 changes: 104 additions & 92 deletions plugins/inputs/disk/disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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)
Expand All @@ -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) {
Expand Down Expand Up @@ -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),
},
},
{
Expand All @@ -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),
},
},
{
Expand All @@ -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),
},
},
}
Expand Down Expand Up @@ -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{
Expand All @@ -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)
Expand All @@ -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) {
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit cf5d6b4

Please sign in to comment.