-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathi_memory.go
86 lines (69 loc) · 1.85 KB
/
i_memory.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
package main
import (
"regexp"
"os/exec"
"strconv"
"errors"
"time"
"strings"
"fmt"
)
type MemStats struct {
ps PS
}
func (_ *MemStats) Description() string {
return "Read metrics about memory usage"
}
func (_ *MemStats) SampleConfig() string { return "" }
var globalZoneMemoryCapacityMatch = regexp.MustCompile(`Memory size: ([\d]+) Megabytes`)
func globalZoneMemoryCapacity() (uint64, error) {
prtconf, err := exec.LookPath("/usr/sbin/prtconf")
if err != nil {
return 0, err
}
out, err := exec.Command(prtconf).CombinedOutput()
if err != nil {
return 0, err
}
match := globalZoneMemoryCapacityMatch.FindAllStringSubmatch(string(out), -1)
if len(match) != 1 {
return 0, errors.New("memory size not contained in output of /usr/sbin/prtconf")
}
totalMB, err := strconv.ParseUint(match[0][1], 10, 64)
if err != nil {
return 0, err
}
return totalMB * 1024 * 1024, nil
}
func (s *MemStats) Gather(acc Accumulator) error {
now := time.Now()
total, err := globalZoneMemoryCapacity()
if err != nil {
return err
}
output, err := exec.Command("vmstat", "-S").CombinedOutput()
if err != nil {
return fmt.Errorf("error getting Memory info: %s", err.Error())
}
stats := string(output)
rows := strings.Split(stats, "\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
}
free := data["free"] * 1024
fields := map[string]interface{}{
"total": total,
"available": free,
"free": free,
"used": total - free,
"available_percent": 100 * float64(free) / float64(total),
"used_percent": 100 * float64(total-free) / float64(total),
}
acc.AddCounter("mem", fields, nil, now)
return nil
}