forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
disk_linux.go
66 lines (57 loc) · 1.07 KB
/
disk_linux.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package system
import (
"bufio"
"fmt"
"os"
"strings"
"syscall"
)
type diskInfoCache struct {
stat syscall.Stat_t
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)
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{}
}
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
f, err := os.Open(fmt.Sprintf("%s/b%d:%d", udevPath, major, minor))
if err != nil {
return nil, err
}
defer f.Close()
scnr := bufio.NewScanner(f)
for scnr.Scan() {
l := scnr.Text()
if len(l) < 4 || l[:2] != "E:" {
continue
}
kv := strings.SplitN(l[2:], "=", 2)
if len(kv) < 2 {
continue
}
di[kv[0]] = kv[1]
}
return di, nil
}