From f685837519ea1a28ca936d48b92b896a51fffc21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Benencia?= Date: Wed, 6 Sep 2017 18:28:11 -0300 Subject: [PATCH] Include mount mode option in disk metrics (#3027) --- plugins/inputs/system/DISK_README.md | 9 +++++---- plugins/inputs/system/disk.go | 27 +++++++++++++++++++++++++++ plugins/inputs/system/disk_test.go | 14 +++++++++----- 3 files changed, 41 insertions(+), 9 deletions(-) diff --git a/plugins/inputs/system/DISK_README.md b/plugins/inputs/system/DISK_README.md index bbb78961d..b3b6a52fa 100644 --- a/plugins/inputs/system/DISK_README.md +++ b/plugins/inputs/system/DISK_README.md @@ -40,16 +40,17 @@ In this case, the host's root volume should be mounted into the container and th - All measurements have the following tags: - fstype (filesystem type) - path (mount point path) + - mode (whether the mount is rw or ro) ### Example Output: ``` % ./telegraf --config ~/ws/telegraf.conf --input-filter disk --test * Plugin: disk, Collection 1 -> disk,fstype=hfs,path=/ free=398407520256i,inodes_free=97267461i,inodes_total=121847806i,inodes_used=24580345i,total=499088621568i,used=100418957312i,used_percent=20.131039916242397 1453832006274071563 -> disk,fstype=devfs,path=/dev free=0i,inodes_free=0i,inodes_total=628i,inodes_used=628i,total=185856i,used=185856i,used_percent=100 1453832006274137913 -> disk,fstype=autofs,path=/net free=0i,inodes_free=0i,inodes_total=0i,inodes_used=0i,total=0i,used=0i,used_percent=0 1453832006274157077 -> disk,fstype=autofs,path=/home free=0i,inodes_free=0i,inodes_total=0i,inodes_used=0i,total=0i,used=0i,used_percent=0 1453832006274169688 +> 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 ``` diff --git a/plugins/inputs/system/disk.go b/plugins/inputs/system/disk.go index f2d484273..ddacbab46 100644 --- a/plugins/inputs/system/disk.go +++ b/plugins/inputs/system/disk.go @@ -53,10 +53,12 @@ func (s *DiskStats) Gather(acc telegraf.Accumulator) error { // Skip dummy filesystem (procfs, cgroupfs, ...) continue } + mountOpts := parseOptions(partitions[i].Opts) tags := map[string]string{ "path": du.Path, "device": strings.Replace(partitions[i].Device, "/dev/", "", -1), "fstype": du.Fstype, + "mode": mountOpts.Mode(), } var used_percent float64 if du.Used+du.Free > 0 { @@ -219,6 +221,31 @@ func (s *DiskIOStats) diskTags(devName string) map[string]string { return tags } +type MountOptions []string + +func (opts MountOptions) Mode() string { + if opts.exists("rw") { + return "rw" + } else if opts.exists("ro") { + return "ro" + } else { + return "unknown" + } +} + +func (opts MountOptions) exists(opt string) bool { + for _, o := range opts { + if o == opt { + return true + } + } + return false +} + +func parseOptions(opts string) MountOptions { + return strings.Split(opts, ",") +} + func init() { ps := newSystemPS() inputs.Add("disk", func() telegraf.Input { diff --git a/plugins/inputs/system/disk_test.go b/plugins/inputs/system/disk_test.go index 5ba4d041f..28a433fe9 100644 --- a/plugins/inputs/system/disk_test.go +++ b/plugins/inputs/system/disk_test.go @@ -28,13 +28,13 @@ func TestDiskUsage(t *testing.T) { Device: "/dev/sda", Mountpoint: "/", Fstype: "ext4", - Opts: "", + Opts: "ro,noatime,nodiratime", }, { Device: "/dev/sdb", Mountpoint: "/home", Fstype: "ext4", - Opts: "", + Opts: "rw,noatime,nodiratime,errors=remount-ro", }, } duAll := []disk.UsageStat{ @@ -78,11 +78,13 @@ func TestDiskUsage(t *testing.T) { "path": "/", "fstype": "ext4", "device": "sda", + "mode": "ro", } tags2 := map[string]string{ "path": "/home", "fstype": "ext4", "device": "sdb", + "mode": "rw", } fields1 := map[string]interface{}{ @@ -163,13 +165,13 @@ func TestDiskStats(t *testing.T) { Device: "/dev/sda", Mountpoint: "/", Fstype: "ext4", - Opts: "", + Opts: "ro,noatime,nodiratime", }, { Device: "/dev/sdb", Mountpoint: "/home", Fstype: "ext4", - Opts: "", + Opts: "rw,noatime,nodiratime,errors=remount-ro", }, } @@ -178,7 +180,7 @@ func TestDiskStats(t *testing.T) { Device: "/dev/sda", Mountpoint: "/", Fstype: "ext4", - Opts: "", + Opts: "ro,noatime,nodiratime", }, } @@ -197,11 +199,13 @@ func TestDiskStats(t *testing.T) { "path": "/", "fstype": "ext4", "device": "sda", + "mode": "ro", } tags2 := map[string]string{ "path": "/home", "fstype": "ext4", "device": "sdb", + "mode": "rw", } fields1 := map[string]interface{}{