forked from dynport/metrix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisk.go
56 lines (49 loc) · 1.13 KB
/
disk.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
package main
import (
"regexp"
"strconv"
"strings"
)
const DISK = "disk"
func init() {
parser.Add(DISK, "true", "Collect disk usage metrics")
}
var diskStatFields = map[int]string{
0: "ReadsCompleted",
1: "ReadsMerged",
2: "SectorsRead",
3: "MillisecondsRead",
4: "WritesCompleted",
5: "WritesMerged",
6: "SectorsWritten",
7: "MillisecondsWritten",
8: "IosInProgress",
9: "MillisecondsIO",
10: "WeightedMillisecondsIO",
}
type Disk struct {
}
func (self *Disk) Prefix() string {
return "disk"
}
func (self *Disk) Collect(c *MetricsCollection) (e error) {
str := ReadProcFile("diskstats")
re := regexp.MustCompile("\\d+\\s+\\d+ (\\w+) (\\d+.*)")
matches := re.FindAllStringSubmatch(str, -1)
for _, m1 := range matches {
name := m1[1]
tags := map[string]string{"name": name}
if strings.HasPrefix(name, "ram") || strings.HasPrefix(name, "loop") || strings.HasPrefix(name, "sr") {
continue
}
chunks := strings.Split(m1[2], " ")
for idx, v := range chunks {
if i, e := strconv.ParseInt(v, 10, 64); e == nil {
if k, ok := diskStatFields[idx]; ok {
c.AddWithTags(k, i, tags)
}
}
}
}
return
}