Include DEVLINKS in available diskio udev properties (#5116)

This commit is contained in:
dbergmanskytap 2018-12-13 11:49:19 -08:00 committed by Daniel Nelson
parent dbea6dca30
commit 6b5ddbbf66
3 changed files with 26 additions and 1 deletions

View File

@ -19,6 +19,10 @@ The diskio input plugin gathers metrics about disk traffic and timing.
## Currently only Linux is supported via udev properties. You can view ## Currently only Linux is supported via udev properties. You can view
## available properties for a device by running: ## available properties for a device by running:
## 'udevadm info -q property -n /dev/sda' ## 'udevadm info -q property -n /dev/sda'
## Note: Most, but not all, udev properties can be accessed this way. Properties
## that are currently inaccessible include DEVTYPE, DEVNAME, and DEVPATH.
## DEVLINKS, however, can be used as a tag as of Telegraf 1.10
## For more info see https://github.com/influxdata/telegraf/issues/3663
# device_tags = ["ID_FS_TYPE", "ID_FS_USAGE"] # device_tags = ["ID_FS_TYPE", "ID_FS_USAGE"]
# #
## Using the same metadata source as device_tags, you can also customize the ## Using the same metadata source as device_tags, you can also customize the

View File

@ -2,6 +2,7 @@ package diskio
import ( import (
"bufio" "bufio"
"bytes"
"fmt" "fmt"
"os" "os"
"strings" "strings"
@ -52,9 +53,21 @@ func (s *DiskIO) diskInfo(devName string) (map[string]string, error) {
defer f.Close() defer f.Close()
scnr := bufio.NewScanner(f) scnr := bufio.NewScanner(f)
var devlinks bytes.Buffer
for scnr.Scan() { for scnr.Scan() {
l := scnr.Text() l := scnr.Text()
if len(l) < 4 || l[:2] != "E:" { if len(l) < 4 {
continue
}
if l[:2] == "S:" {
if devlinks.Len() > 0 {
devlinks.WriteString(" ")
}
devlinks.WriteString("/dev/")
devlinks.WriteString(l[2:])
continue
}
if l[:2] != "E:" {
continue continue
} }
kv := strings.SplitN(l[2:], "=", 2) kv := strings.SplitN(l[2:], "=", 2)
@ -64,5 +77,9 @@ func (s *DiskIO) diskInfo(devName string) (map[string]string, error) {
di[kv[0]] = kv[1] di[kv[0]] = kv[1]
} }
if devlinks.Len() > 0 {
di["DEVLINKS"] = devlinks.String()
}
return di, nil return di, nil
} }

View File

@ -14,6 +14,8 @@ import (
var nullDiskInfo = []byte(` var nullDiskInfo = []byte(`
E:MY_PARAM_1=myval1 E:MY_PARAM_1=myval1
E:MY_PARAM_2=myval2 E:MY_PARAM_2=myval2
S:foo/bar/devlink
S:foo/bar/devlink1
`) `)
// setupNullDisk sets up fake udev info as if /dev/null were a disk. // setupNullDisk sets up fake udev info as if /dev/null were a disk.
@ -47,6 +49,7 @@ func TestDiskInfo(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, "myval1", di["MY_PARAM_1"]) assert.Equal(t, "myval1", di["MY_PARAM_1"])
assert.Equal(t, "myval2", di["MY_PARAM_2"]) assert.Equal(t, "myval2", di["MY_PARAM_2"])
assert.Equal(t, "/dev/foo/bar/devlink /dev/foo/bar/devlink1", di["DEVLINKS"])
// test that data is cached // test that data is cached
err = clean() err = clean()
@ -56,6 +59,7 @@ func TestDiskInfo(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, "myval1", di["MY_PARAM_1"]) assert.Equal(t, "myval1", di["MY_PARAM_1"])
assert.Equal(t, "myval2", di["MY_PARAM_2"]) assert.Equal(t, "myval2", di["MY_PARAM_2"])
assert.Equal(t, "/dev/foo/bar/devlink /dev/foo/bar/devlink1", di["DEVLINKS"])
// unfortunately we can't adjust mtime on /dev/null to test cache invalidation // unfortunately we can't adjust mtime on /dev/null to test cache invalidation
} }