-
Notifications
You must be signed in to change notification settings - Fork 2
/
diskusage.go
129 lines (106 loc) · 3.59 KB
/
diskusage.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"fmt"
"io/ioutil"
"strconv"
"strings"
"time"
)
type DiskStats struct {
Reads float64
ReadsMerged float64
ReadsSectors float64
ReadsMilliseconds float64
Writes float64
WritesMerged float64
WritesSectors float64
WritesMilliseconds float64
IOInProgress float64
IOMilliseconds float64
IOMillisecondsWeighted float64
}
const DiskusageModuleName = "diskusage"
type DiskusageInputModule struct {
previousDiskStats map[string]DiskStats
previousTime time.Time
}
func (m *DiskusageInputModule) Name() string {
return DiskusageModuleName
}
func (m *DiskusageInputModule) Init(config *Config, moduleConfig *ModuleConfig) error {
return nil
}
func (m *DiskusageInputModule) TearDown() error {
return nil
}
func (m *DiskusageInputModule) GetMetrics() (*ModuleMetrics, error) {
metrics := make([]Metric, 0, 50)
now := time.Now()
timeDiff := time.Since(m.previousTime).Seconds()
allStats, err := GetDiskStats("/proc/diskstats")
if err != nil {
return nil, err
}
if m.previousDiskStats != nil {
for device, stats := range allStats {
previous, ok := m.previousDiskStats[device]
if ok {
readsPerSecond := (stats.Reads - previous.Reads) / timeDiff
writesPerSecond := (stats.Writes - previous.Writes) / timeDiff
readsMergedPerSecond := (stats.ReadsMerged - previous.ReadsMerged) / timeDiff
writesMergedPerSecond := (stats.WritesMerged - previous.WritesMerged) / timeDiff
readBytesPerSecond := ((stats.ReadsSectors - previous.ReadsSectors) * 512) / timeDiff
writeBytesPerSecond := ((stats.WritesSectors - previous.WritesSectors) * 512) / timeDiff
metrics = append(metrics, NewMetric(fmt.Sprintf("%s.reads", device), readsPerSecond))
metrics = append(metrics, NewMetric(fmt.Sprintf("%s.writes", device), writesPerSecond))
metrics = append(metrics, NewMetric(fmt.Sprintf("%s.reads_merged", device), readsMergedPerSecond))
metrics = append(metrics, NewMetric(fmt.Sprintf("%s.writes_merged", device), writesMergedPerSecond))
metrics = append(metrics, NewMetric(fmt.Sprintf("%s.read_bytes", device), readBytesPerSecond))
metrics = append(metrics, NewMetric(fmt.Sprintf("%s.write_bytes", device), writeBytesPerSecond))
}
}
}
m.previousDiskStats = allStats
m.previousTime = now
return &ModuleMetrics{Module: m.Name(), Metrics: metrics}, nil
}
func GetDiskStats(path string) (map[string]DiskStats, error) {
b, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
content := string(b)
lines := strings.Split(content, "\n")
stats := make(map[string]DiskStats)
for _, line := range lines {
fields := strings.Fields(line)
if len(fields) < 14 || len(fields) == 0 {
continue
}
device := fields[2]
if strings.HasPrefix(device, "ram") || strings.HasPrefix(device, "loop") {
continue
}
stats[device] = DiskStats{
Reads: fieldValue(fields[3]),
ReadsMerged: fieldValue(fields[4]),
ReadsSectors: fieldValue(fields[5]),
ReadsMilliseconds: fieldValue(fields[6]),
Writes: fieldValue(fields[7]),
WritesMerged: fieldValue(fields[8]),
WritesSectors: fieldValue(fields[9]),
WritesMilliseconds: fieldValue(fields[10]),
IOInProgress: fieldValue(fields[11]),
IOMilliseconds: fieldValue(fields[12]),
IOMillisecondsWeighted: fieldValue(fields[13]),
}
}
return stats, nil
}
func fieldValue(field string) float64 {
value, err := strconv.ParseFloat(field, 64)
if err != nil {
value = 0
}
return value
}