diff --git a/plugins/inputs/system/disk.go b/plugins/inputs/system/disk.go index f2d484273..b6bf5f7d4 100644 --- a/plugins/inputs/system/disk.go +++ b/plugins/inputs/system/disk.go @@ -2,6 +2,7 @@ package system import ( "fmt" + "log" "regexp" "strings" @@ -164,14 +165,13 @@ func (s *DiskIOStats) Gather(acc telegraf.Accumulator) error { var varRegex = regexp.MustCompile(`\$(?:\w+|\{\w+\})`) func (s *DiskIOStats) diskName(devName string) string { - di, err := s.diskInfo(devName) - if err != nil { - // discard error :-( - // We can't return error because it's non-fatal to the Gather(). - // And we have no logger, so we can't log it. + if len(s.NameTemplates) == 0 { return devName } - if di == nil { + + di, err := s.diskInfo(devName) + if err != nil { + log.Printf("W! Error gathering disk info: %s", err) return devName } @@ -198,14 +198,13 @@ func (s *DiskIOStats) diskName(devName string) string { } func (s *DiskIOStats) diskTags(devName string) map[string]string { - di, err := s.diskInfo(devName) - if err != nil { - // discard error :-( - // We can't return error because it's non-fatal to the Gather(). - // And we have no logger, so we can't log it. + if len(s.DeviceTags) == 0 { return nil } - if di == nil { + + di, err := s.diskInfo(devName) + if err != nil { + log.Printf("W! Error gathering disk info: %s", err) return nil } diff --git a/plugins/inputs/system/disk_linux.go b/plugins/inputs/system/disk_linux.go index e5a0cff55..d3fd691c4 100644 --- a/plugins/inputs/system/disk_linux.go +++ b/plugins/inputs/system/disk_linux.go @@ -5,25 +5,26 @@ import ( "fmt" "os" "strings" - "syscall" + + "golang.org/x/sys/unix" ) type diskInfoCache struct { - stat syscall.Stat_t - values map[string]string + udevDataPath string + values map[string]string } var udevPath = "/run/udev/data" func (s *DiskIOStats) diskInfo(devName string) (map[string]string, error) { - fi, err := os.Stat("/dev/" + devName) + var err error + var stat unix.Stat_t + + path := "/dev/" + devName + err = unix.Stat(path, &stat) if err != nil { return nil, err } - stat, ok := fi.Sys().(*syscall.Stat_t) - if !ok { - return nil, nil - } if s.infoCache == nil { s.infoCache = map[string]diskInfoCache{} @@ -31,25 +32,26 @@ func (s *DiskIOStats) diskInfo(devName string) (map[string]string, error) { ic, ok := s.infoCache[devName] if ok { return ic.values, nil - } else { - ic = diskInfoCache{ - stat: *stat, - values: map[string]string{}, - } - s.infoCache[devName] = ic } - di := ic.values major := stat.Rdev >> 8 & 0xff minor := stat.Rdev & 0xff + udevDataPath := fmt.Sprintf("%s/b%d:%d", udevPath, major, minor) - f, err := os.Open(fmt.Sprintf("%s/b%d:%d", udevPath, major, minor)) + di := map[string]string{} + + s.infoCache[devName] = diskInfoCache{ + udevDataPath: udevDataPath, + values: di, + } + + f, err := os.Open(udevDataPath) if err != nil { return nil, err } defer f.Close() - scnr := bufio.NewScanner(f) + scnr := bufio.NewScanner(f) for scnr.Scan() { l := scnr.Text() if len(l) < 4 || l[:2] != "E:" {