Skip to content

Commit

Permalink
Allow devices option to match against devlinks (influxdata#5817)
Browse files Browse the repository at this point in the history
  • Loading branch information
omgold authored and Mathieu Lecarme committed Apr 17, 2020
1 parent ba9275d commit ae03889
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
40 changes: 31 additions & 9 deletions plugins/inputs/diskio/diskio.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion plugins/inputs/diskio/diskio_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down

0 comments on commit ae03889

Please sign in to comment.