Skip to content

Commit f685837

Browse files
ruldanielnelson
authored andcommitted
Include mount mode option in disk metrics (#3027)
1 parent 37bad29 commit f685837

File tree

3 files changed

+41
-9
lines changed

3 files changed

+41
-9
lines changed

plugins/inputs/system/DISK_README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,17 @@ In this case, the host's root volume should be mounted into the container and th
4040
- All measurements have the following tags:
4141
- fstype (filesystem type)
4242
- path (mount point path)
43+
- mode (whether the mount is rw or ro)
4344

4445
### Example Output:
4546

4647
```
4748
% ./telegraf --config ~/ws/telegraf.conf --input-filter disk --test
4849
* Plugin: disk, Collection 1
49-
> disk,fstype=hfs,path=/ free=398407520256i,inodes_free=97267461i,inodes_total=121847806i,inodes_used=24580345i,total=499088621568i,used=100418957312i,used_percent=20.131039916242397 1453832006274071563
50-
> disk,fstype=devfs,path=/dev free=0i,inodes_free=0i,inodes_total=628i,inodes_used=628i,total=185856i,used=185856i,used_percent=100 1453832006274137913
51-
> disk,fstype=autofs,path=/net free=0i,inodes_free=0i,inodes_total=0i,inodes_used=0i,total=0i,used=0i,used_percent=0 1453832006274157077
52-
> disk,fstype=autofs,path=/home free=0i,inodes_free=0i,inodes_total=0i,inodes_used=0i,total=0i,used=0i,used_percent=0 1453832006274169688
50+
> 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
51+
> 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
52+
> 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
53+
> 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
5354
```
5455

5556

plugins/inputs/system/disk.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,12 @@ func (s *DiskStats) Gather(acc telegraf.Accumulator) error {
5353
// Skip dummy filesystem (procfs, cgroupfs, ...)
5454
continue
5555
}
56+
mountOpts := parseOptions(partitions[i].Opts)
5657
tags := map[string]string{
5758
"path": du.Path,
5859
"device": strings.Replace(partitions[i].Device, "/dev/", "", -1),
5960
"fstype": du.Fstype,
61+
"mode": mountOpts.Mode(),
6062
}
6163
var used_percent float64
6264
if du.Used+du.Free > 0 {
@@ -219,6 +221,31 @@ func (s *DiskIOStats) diskTags(devName string) map[string]string {
219221
return tags
220222
}
221223

224+
type MountOptions []string
225+
226+
func (opts MountOptions) Mode() string {
227+
if opts.exists("rw") {
228+
return "rw"
229+
} else if opts.exists("ro") {
230+
return "ro"
231+
} else {
232+
return "unknown"
233+
}
234+
}
235+
236+
func (opts MountOptions) exists(opt string) bool {
237+
for _, o := range opts {
238+
if o == opt {
239+
return true
240+
}
241+
}
242+
return false
243+
}
244+
245+
func parseOptions(opts string) MountOptions {
246+
return strings.Split(opts, ",")
247+
}
248+
222249
func init() {
223250
ps := newSystemPS()
224251
inputs.Add("disk", func() telegraf.Input {

plugins/inputs/system/disk_test.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ func TestDiskUsage(t *testing.T) {
2828
Device: "/dev/sda",
2929
Mountpoint: "/",
3030
Fstype: "ext4",
31-
Opts: "",
31+
Opts: "ro,noatime,nodiratime",
3232
},
3333
{
3434
Device: "/dev/sdb",
3535
Mountpoint: "/home",
3636
Fstype: "ext4",
37-
Opts: "",
37+
Opts: "rw,noatime,nodiratime,errors=remount-ro",
3838
},
3939
}
4040
duAll := []disk.UsageStat{
@@ -78,11 +78,13 @@ func TestDiskUsage(t *testing.T) {
7878
"path": "/",
7979
"fstype": "ext4",
8080
"device": "sda",
81+
"mode": "ro",
8182
}
8283
tags2 := map[string]string{
8384
"path": "/home",
8485
"fstype": "ext4",
8586
"device": "sdb",
87+
"mode": "rw",
8688
}
8789

8890
fields1 := map[string]interface{}{
@@ -163,13 +165,13 @@ func TestDiskStats(t *testing.T) {
163165
Device: "/dev/sda",
164166
Mountpoint: "/",
165167
Fstype: "ext4",
166-
Opts: "",
168+
Opts: "ro,noatime,nodiratime",
167169
},
168170
{
169171
Device: "/dev/sdb",
170172
Mountpoint: "/home",
171173
Fstype: "ext4",
172-
Opts: "",
174+
Opts: "rw,noatime,nodiratime,errors=remount-ro",
173175
},
174176
}
175177

@@ -178,7 +180,7 @@ func TestDiskStats(t *testing.T) {
178180
Device: "/dev/sda",
179181
Mountpoint: "/",
180182
Fstype: "ext4",
181-
Opts: "",
183+
Opts: "ro,noatime,nodiratime",
182184
},
183185
}
184186

@@ -197,11 +199,13 @@ func TestDiskStats(t *testing.T) {
197199
"path": "/",
198200
"fstype": "ext4",
199201
"device": "sda",
202+
"mode": "ro",
200203
}
201204
tags2 := map[string]string{
202205
"path": "/home",
203206
"fstype": "ext4",
204207
"device": "sdb",
208+
"mode": "rw",
205209
}
206210

207211
fields1 := map[string]interface{}{

0 commit comments

Comments
 (0)