-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathi_swap.go
87 lines (67 loc) · 1.92 KB
/
i_swap.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
package main
import (
"os/exec"
"strings"
"strconv"
"fmt"
)
type SwapStats struct {
ps PS
}
func (_ *SwapStats) Description() string {
return "Read metrics about swap memory usage"
}
func (_ *SwapStats) SampleConfig() string { return "" }
func (s *SwapStats) Gather(acc Accumulator) error {
output, err := exec.Command("swap", "-s").CombinedOutput()
if err != nil {
return fmt.Errorf("error getting Swap info: %s", err.Error())
}
sout := string(output)
if sout != "" {
swapOutput := strings.Split(sout, "\n")
swapOutput = swapOutput[0: len(swapOutput)-1]
s := strings.Split(swapOutput[0], ",")
if len(s) == 2 {
kUsed := strings.Trim(strings.Replace(strings.Split(s[0], "=")[1], "k used", "", 1), " ")
kAvailable := strings.Trim(strings.Replace(s[1], "k available", "", 1), " ")
used, _ := strconv.ParseUint(kUsed, 10, 0)
avail, _ := strconv.ParseUint(kAvailable, 10, 0)
used = used * 1024
avail = avail * 1024
total := used + avail
free := total - used
var usedPercent float64
if total != 0 {
usedPercent = float64(used) / float64(total) * 100.0
}
fieldsG := map[string]interface{}{
"total": total,
"used": used,
"free": free,
"used_percent": usedPercent,
}
acc.AddGauge("swap", fieldsG, nil)
output, err = exec.Command("vmstat", "-S").CombinedOutput()
if err != nil {
return fmt.Errorf("error getting Swap Memory info: %s", err.Error())
}
vmstats := string(output)
rows := strings.Split(vmstats, "\n")
rows = rows[1:]
data := make(map[string]uint64)
headers := strings.Fields(rows[0])
values := strings.Fields(rows[1])
for count := 0; count < len(headers); count++ {
v, _ := strconv.ParseUint(values[count], 10, 0)
data[headers[count]] = v
}
fieldsC := map[string]interface{}{
"in": data["si"],
"out": data["so"],
}
acc.AddCounter("swap", fieldsC, nil)
}
}
return nil
}