Skip to content

Commit 12831f4

Browse files
omgolddanielnelson
authored andcommitted
Allow devices option to match against devlinks (influxdata#5817)
1 parent 3b91542 commit 12831f4

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

plugins/inputs/diskio/diskio.go

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,32 @@ func (s *DiskIO) Gather(acc telegraf.Accumulator) error {
103103
}
104104

105105
for _, io := range diskio {
106-
if s.deviceFilter != nil && !s.deviceFilter.Match(io.Name) {
107-
continue
106+
107+
match := false
108+
if s.deviceFilter != nil && s.deviceFilter.Match(io.Name) {
109+
match = true
108110
}
109111

110112
tags := map[string]string{}
111-
tags["name"] = s.diskName(io.Name)
113+
var devLinks []string
114+
tags["name"], devLinks = s.diskName(io.Name)
115+
116+
if s.deviceFilter != nil && !match {
117+
for _, devLink := range devLinks {
118+
if s.deviceFilter.Match(devLink) {
119+
match = true
120+
break
121+
}
122+
}
123+
if !match {
124+
continue
125+
}
126+
}
127+
112128
for t, v := range s.diskTags(io.Name) {
113129
tags[t] = v
114130
}
131+
115132
if !s.SkipSerialNumber {
116133
if len(io.SerialNumber) != 0 {
117134
tags["serial"] = io.SerialNumber
@@ -137,15 +154,20 @@ func (s *DiskIO) Gather(acc telegraf.Accumulator) error {
137154
return nil
138155
}
139156

140-
func (s *DiskIO) diskName(devName string) string {
157+
func (s *DiskIO) diskName(devName string) (string, []string) {
158+
di, err := s.diskInfo(devName)
159+
devLinks := strings.Split(di["DEVLINKS"], " ")
160+
for i, devLink := range devLinks {
161+
devLinks[i] = strings.TrimPrefix(devLink, "/dev/")
162+
}
163+
141164
if len(s.NameTemplates) == 0 {
142-
return devName
165+
return devName, devLinks
143166
}
144167

145-
di, err := s.diskInfo(devName)
146168
if err != nil {
147169
log.Printf("W! Error gathering disk info: %s", err)
148-
return devName
170+
return devName, devLinks
149171
}
150172

151173
for _, nt := range s.NameTemplates {
@@ -163,11 +185,11 @@ func (s *DiskIO) diskName(devName string) string {
163185
})
164186

165187
if !miss {
166-
return name
188+
return name, devLinks
167189
}
168190
}
169191

170-
return devName
192+
return devName, devLinks
171193
}
172194

173195
func (s *DiskIO) diskTags(devName string) map[string]string {

plugins/inputs/diskio/diskio_linux_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ func TestDiskIOStats_diskName(t *testing.T) {
8888
s := DiskIO{
8989
NameTemplates: tc.templates,
9090
}
91-
assert.Equal(t, tc.expected, s.diskName("null"), "Templates: %#v", tc.templates)
91+
name, _ := s.diskName("null")
92+
assert.Equal(t, tc.expected, name, "Templates: %#v", tc.templates)
9293
}
9394
}
9495

0 commit comments

Comments
 (0)