From ce07f5f4f7f0ec1910b9e081d1a9be88b61bbccc Mon Sep 17 00:00:00 2001 From: omgold Date: Wed, 15 May 2019 21:21:55 +0200 Subject: [PATCH] Allow devices option to match against devlinks (#5817) --- plugins/inputs/diskio/diskio.go | 40 +++++++++++++++++----- plugins/inputs/diskio/diskio_linux_test.go | 3 +- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/plugins/inputs/diskio/diskio.go b/plugins/inputs/diskio/diskio.go index e0c6243bb83c0..053765b4e5173 100644 --- a/plugins/inputs/diskio/diskio.go +++ b/plugins/inputs/diskio/diskio.go @@ -103,15 +103,32 @@ func (s *DiskIO) Gather(acc telegraf.Accumulator) error { } for _, io := range diskio { - if s.deviceFilter != nil && !s.deviceFilter.Match(io.Name) { - continue + + match := false + if s.deviceFilter != nil && s.deviceFilter.Match(io.Name) { + match = true } tags := map[string]string{} - tags["name"] = s.diskName(io.Name) + var devLinks []string + tags["name"], devLinks = s.diskName(io.Name) + + if s.deviceFilter != nil && !match { + for _, devLink := range devLinks { + if s.deviceFilter.Match(devLink) { + match = true + break + } + } + if !match { + continue + } + } + for t, v := range s.diskTags(io.Name) { tags[t] = v } + if !s.SkipSerialNumber { if len(io.SerialNumber) != 0 { tags["serial"] = io.SerialNumber @@ -137,15 +154,20 @@ func (s *DiskIO) Gather(acc telegraf.Accumulator) error { return nil } -func (s *DiskIO) diskName(devName string) string { +func (s *DiskIO) diskName(devName string) (string, []string) { + di, err := s.diskInfo(devName) + devLinks := strings.Split(di["DEVLINKS"], " ") + for i, devLink := range devLinks { + devLinks[i] = strings.TrimPrefix(devLink, "/dev/") + } + if len(s.NameTemplates) == 0 { - return devName + return devName, devLinks } - di, err := s.diskInfo(devName) if err != nil { log.Printf("W! Error gathering disk info: %s", err) - return devName + return devName, devLinks } for _, nt := range s.NameTemplates { @@ -163,11 +185,11 @@ func (s *DiskIO) diskName(devName string) string { }) if !miss { - return name + return name, devLinks } } - return devName + return devName, devLinks } func (s *DiskIO) diskTags(devName string) map[string]string { diff --git a/plugins/inputs/diskio/diskio_linux_test.go b/plugins/inputs/diskio/diskio_linux_test.go index 9e79be1653ca9..1cb031bdce553 100644 --- a/plugins/inputs/diskio/diskio_linux_test.go +++ b/plugins/inputs/diskio/diskio_linux_test.go @@ -88,7 +88,8 @@ func TestDiskIOStats_diskName(t *testing.T) { s := DiskIO{ NameTemplates: tc.templates, } - assert.Equal(t, tc.expected, s.diskName("null"), "Templates: %#v", tc.templates) + name, _ := s.diskName("null") + assert.Equal(t, tc.expected, name, "Templates: %#v", tc.templates) } }