Skip to content

Commit 074e6d1

Browse files
phemmersparrc
authored andcommitted
add support for diskio name templates & udev tags
closes #1453 closes #1386 closes #1428
1 parent 1d864eb commit 074e6d1

File tree

5 files changed

+261
-1
lines changed

5 files changed

+261
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ It is highly recommended that all users migrate to the new riemann output plugin
1515
- [#2179](https://github.com/influxdata/telegraf/pull/2179): Added more InnoDB metric to MySQL plugin.
1616
- [#2251](https://github.com/influxdata/telegraf/pull/2251): InfluxDB output: use own client for improved through-put and less allocations.
1717
- [#1900](https://github.com/influxdata/telegraf/pull/1900): Riemann plugin rewrite.
18+
- [#1453](https://github.com/influxdata/telegraf/pull/1453): diskio: add support for name templates and udev tags.
1819

1920
### Bugfixes
2021

plugins/inputs/system/disk.go

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package system
22

33
import (
44
"fmt"
5+
"regexp"
56
"strings"
67

78
"github.com/influxdata/telegraf"
@@ -82,7 +83,11 @@ type DiskIOStats struct {
8283
ps PS
8384

8485
Devices []string
86+
DeviceTags []string
87+
NameTemplates []string
8588
SkipSerialNumber bool
89+
90+
infoCache map[string]diskInfoCache
8691
}
8792

8893
func (_ *DiskIOStats) Description() string {
@@ -96,6 +101,23 @@ var diskIoSampleConfig = `
96101
# devices = ["sda", "sdb"]
97102
## Uncomment the following line if you need disk serial numbers.
98103
# skip_serial_number = false
104+
#
105+
## On systems which support it, device metadata can be added in the form of
106+
## tags.
107+
## Currently only Linux is supported via udev properties. You can view
108+
## available properties for a device by running:
109+
## 'udevadm info -q property -n /dev/sda'
110+
# device_tags = ["ID_FS_TYPE", "ID_FS_USAGE"]
111+
#
112+
## Using the same metadata source as device_tags, you can also customize the
113+
## name of the device via templates.
114+
## The 'name_templates' parameter is a list of templates to try and apply to
115+
## the device. The template may contain variables in the form of '$PROPERTY' or
116+
## '${PROPERTY}'. The first template which does not contain any variables not
117+
## present for the device is used as the device name tag.
118+
## The typical use case is for LVM volumes, to get the VG/LV name instead of
119+
## the near-meaningless DM-0 name.
120+
# name_templates = ["$ID_FS_LABEL","$DM_VG_NAME/$DM_LV_NAME"]
99121
`
100122

101123
func (_ *DiskIOStats) SampleConfig() string {
@@ -123,7 +145,10 @@ func (s *DiskIOStats) Gather(acc telegraf.Accumulator) error {
123145
continue
124146
}
125147
tags := map[string]string{}
126-
tags["name"] = io.Name
148+
tags["name"] = s.diskName(io.Name)
149+
for t, v := range s.diskTags(io.Name) {
150+
tags[t] = v
151+
}
127152
if !s.SkipSerialNumber {
128153
if len(io.SerialNumber) != 0 {
129154
tags["serial"] = io.SerialNumber
@@ -148,6 +173,64 @@ func (s *DiskIOStats) Gather(acc telegraf.Accumulator) error {
148173
return nil
149174
}
150175

176+
var varRegex = regexp.MustCompile(`\$(?:\w+|\{\w+\})`)
177+
178+
func (s *DiskIOStats) diskName(devName string) string {
179+
di, err := s.diskInfo(devName)
180+
if err != nil {
181+
// discard error :-(
182+
// We can't return error because it's non-fatal to the Gather().
183+
// And we have no logger, so we can't log it.
184+
return devName
185+
}
186+
if di == nil {
187+
return devName
188+
}
189+
190+
for _, nt := range s.NameTemplates {
191+
miss := false
192+
name := varRegex.ReplaceAllStringFunc(nt, func(sub string) string {
193+
sub = sub[1:] // strip leading '$'
194+
if sub[0] == '{' {
195+
sub = sub[1 : len(sub)-1] // strip leading & trailing '{' '}'
196+
}
197+
if v, ok := di[sub]; ok {
198+
return v
199+
}
200+
miss = true
201+
return ""
202+
})
203+
204+
if !miss {
205+
return name
206+
}
207+
}
208+
209+
return devName
210+
}
211+
212+
func (s *DiskIOStats) diskTags(devName string) map[string]string {
213+
di, err := s.diskInfo(devName)
214+
if err != nil {
215+
// discard error :-(
216+
// We can't return error because it's non-fatal to the Gather().
217+
// And we have no logger, so we can't log it.
218+
return nil
219+
}
220+
if di == nil {
221+
return nil
222+
}
223+
224+
tags := map[string]string{}
225+
for _, dt := range s.DeviceTags {
226+
if v, ok := di[dt]; ok {
227+
tags[dt] = v
228+
}
229+
}
230+
231+
return tags
232+
}
233+
151234
func init() {
152235
inputs.Add("disk", func() telegraf.Input {
153236
return &DiskStats{ps: &systemPS{}}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package system
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"os"
7+
"strings"
8+
"syscall"
9+
)
10+
11+
type diskInfoCache struct {
12+
stat syscall.Stat_t
13+
values map[string]string
14+
}
15+
16+
var udevPath = "/run/udev/data"
17+
18+
func (s *DiskIOStats) diskInfo(devName string) (map[string]string, error) {
19+
fi, err := os.Stat("/dev/" + devName)
20+
if err != nil {
21+
return nil, err
22+
}
23+
stat, ok := fi.Sys().(*syscall.Stat_t)
24+
if !ok {
25+
return nil, nil
26+
}
27+
28+
if s.infoCache == nil {
29+
s.infoCache = map[string]diskInfoCache{}
30+
}
31+
ic, ok := s.infoCache[devName]
32+
if ok {
33+
return ic.values, nil
34+
} else {
35+
ic = diskInfoCache{
36+
stat: *stat,
37+
values: map[string]string{},
38+
}
39+
s.infoCache[devName] = ic
40+
}
41+
di := ic.values
42+
43+
major := stat.Rdev >> 8 & 0xff
44+
minor := stat.Rdev & 0xff
45+
46+
f, err := os.Open(fmt.Sprintf("%s/b%d:%d", udevPath, major, minor))
47+
if err != nil {
48+
return nil, err
49+
}
50+
defer f.Close()
51+
scnr := bufio.NewScanner(f)
52+
53+
for scnr.Scan() {
54+
l := scnr.Text()
55+
if len(l) < 4 || l[:2] != "E:" {
56+
continue
57+
}
58+
kv := strings.SplitN(l[2:], "=", 2)
59+
if len(kv) < 2 {
60+
continue
61+
}
62+
di[kv[0]] = kv[1]
63+
}
64+
65+
return di, nil
66+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// +build linux
2+
3+
package system
4+
5+
import (
6+
"io/ioutil"
7+
"os"
8+
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
"github.com/stretchr/testify/require"
12+
)
13+
14+
var nullDiskInfo = []byte(`
15+
E:MY_PARAM_1=myval1
16+
E:MY_PARAM_2=myval2
17+
`)
18+
19+
// setupNullDisk sets up fake udev info as if /dev/null were a disk.
20+
func setupNullDisk(t *testing.T) func() error {
21+
td, err := ioutil.TempDir("", ".telegraf.TestDiskInfo")
22+
require.NoError(t, err)
23+
24+
origUdevPath := udevPath
25+
26+
cleanFunc := func() error {
27+
udevPath = origUdevPath
28+
return os.RemoveAll(td)
29+
}
30+
31+
udevPath = td
32+
err = ioutil.WriteFile(td+"/b1:3", nullDiskInfo, 0644) // 1:3 is the 'null' device
33+
if err != nil {
34+
cleanFunc()
35+
t.Fatal(err)
36+
}
37+
38+
return cleanFunc
39+
}
40+
41+
func TestDiskInfo(t *testing.T) {
42+
clean := setupNullDisk(t)
43+
defer clean()
44+
45+
s := &DiskIOStats{}
46+
di, err := s.diskInfo("null")
47+
require.NoError(t, err)
48+
assert.Equal(t, "myval1", di["MY_PARAM_1"])
49+
assert.Equal(t, "myval2", di["MY_PARAM_2"])
50+
51+
// test that data is cached
52+
err = clean()
53+
require.NoError(t, err)
54+
55+
di, err = s.diskInfo("null")
56+
require.NoError(t, err)
57+
assert.Equal(t, "myval1", di["MY_PARAM_1"])
58+
assert.Equal(t, "myval2", di["MY_PARAM_2"])
59+
60+
// unfortunately we can't adjust mtime on /dev/null to test cache invalidation
61+
}
62+
63+
// DiskIOStats.diskName isn't a linux specific function, but dependent
64+
// functions are a no-op on non-Linux.
65+
func TestDiskIOStats_diskName(t *testing.T) {
66+
defer setupNullDisk(t)()
67+
68+
tests := []struct {
69+
templates []string
70+
expected string
71+
}{
72+
{[]string{"$MY_PARAM_1"}, "myval1"},
73+
{[]string{"${MY_PARAM_1}"}, "myval1"},
74+
{[]string{"x$MY_PARAM_1"}, "xmyval1"},
75+
{[]string{"x${MY_PARAM_1}x"}, "xmyval1x"},
76+
{[]string{"$MISSING", "$MY_PARAM_1"}, "myval1"},
77+
{[]string{"$MY_PARAM_1", "$MY_PARAM_2"}, "myval1"},
78+
{[]string{"$MISSING"}, "null"},
79+
{[]string{"$MY_PARAM_1/$MY_PARAM_2"}, "myval1/myval2"},
80+
{[]string{"$MY_PARAM_2/$MISSING"}, "null"},
81+
}
82+
83+
for _, tc := range tests {
84+
s := DiskIOStats{
85+
NameTemplates: tc.templates,
86+
}
87+
assert.Equal(t, tc.expected, s.diskName("null"), "Templates: %#v", tc.templates)
88+
}
89+
}
90+
91+
// DiskIOStats.diskTags isn't a linux specific function, but dependent
92+
// functions are a no-op on non-Linux.
93+
func TestDiskIOStats_diskTags(t *testing.T) {
94+
defer setupNullDisk(t)()
95+
96+
s := &DiskIOStats{
97+
DeviceTags: []string{"MY_PARAM_2"},
98+
}
99+
dt := s.diskTags("null")
100+
assert.Equal(t, map[string]string{"MY_PARAM_2": "myval2"}, dt)
101+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// +build !linux
2+
3+
package system
4+
5+
type diskInfoCache struct{}
6+
7+
func (s *DiskIOStats) diskInfo(devName string) (map[string]string, error) {
8+
return nil, nil
9+
}

0 commit comments

Comments
 (0)