Use golang.org/x/sys/unix instead of syscall in diskio (#3384)

This commit is contained in:
Daniel Nelson 2017-10-24 16:22:31 -07:00 committed by GitHub
parent db9c0643de
commit 71480f8cee
2 changed files with 30 additions and 29 deletions

View File

@ -2,6 +2,7 @@ package system
import ( import (
"fmt" "fmt"
"log"
"regexp" "regexp"
"strings" "strings"
@ -166,14 +167,13 @@ func (s *DiskIOStats) Gather(acc telegraf.Accumulator) error {
var varRegex = regexp.MustCompile(`\$(?:\w+|\{\w+\})`) var varRegex = regexp.MustCompile(`\$(?:\w+|\{\w+\})`)
func (s *DiskIOStats) diskName(devName string) string { func (s *DiskIOStats) diskName(devName string) string {
di, err := s.diskInfo(devName) if len(s.NameTemplates) == 0 {
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.
return devName return devName
} }
if di == nil {
di, err := s.diskInfo(devName)
if err != nil {
log.Printf("W! Error gathering disk info: %s", err)
return devName return devName
} }
@ -200,14 +200,13 @@ func (s *DiskIOStats) diskName(devName string) string {
} }
func (s *DiskIOStats) diskTags(devName string) map[string]string { func (s *DiskIOStats) diskTags(devName string) map[string]string {
di, err := s.diskInfo(devName) if len(s.DeviceTags) == 0 {
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.
return nil return nil
} }
if di == nil {
di, err := s.diskInfo(devName)
if err != nil {
log.Printf("W! Error gathering disk info: %s", err)
return nil return nil
} }

View File

@ -5,25 +5,26 @@ import (
"fmt" "fmt"
"os" "os"
"strings" "strings"
"syscall"
"golang.org/x/sys/unix"
) )
type diskInfoCache struct { type diskInfoCache struct {
stat syscall.Stat_t udevDataPath string
values map[string]string values map[string]string
} }
var udevPath = "/run/udev/data" var udevPath = "/run/udev/data"
func (s *DiskIOStats) diskInfo(devName string) (map[string]string, error) { 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 { if err != nil {
return nil, err return nil, err
} }
stat, ok := fi.Sys().(*syscall.Stat_t)
if !ok {
return nil, nil
}
if s.infoCache == nil { if s.infoCache == nil {
s.infoCache = map[string]diskInfoCache{} s.infoCache = map[string]diskInfoCache{}
@ -31,25 +32,26 @@ func (s *DiskIOStats) diskInfo(devName string) (map[string]string, error) {
ic, ok := s.infoCache[devName] ic, ok := s.infoCache[devName]
if ok { if ok {
return ic.values, nil 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 major := stat.Rdev >> 8 & 0xff
minor := stat.Rdev & 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 { if err != nil {
return nil, err return nil, err
} }
defer f.Close() defer f.Close()
scnr := bufio.NewScanner(f)
scnr := bufio.NewScanner(f)
for scnr.Scan() { for scnr.Scan() {
l := scnr.Text() l := scnr.Text()
if len(l) < 4 || l[:2] != "E:" { if len(l) < 4 || l[:2] != "E:" {