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 f64cf89db1
commit 9a062498e7
2 changed files with 30 additions and 29 deletions

View File

@ -2,6 +2,7 @@ package system
import (
"fmt"
"log"
"regexp"
"strings"
@ -166,14 +167,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
}
@ -200,14 +200,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
}

View File

@ -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:" {