-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitor_memory.go
71 lines (64 loc) · 1.4 KB
/
monitor_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
package main
import (
"encoding/json"
"io/ioutil"
"log"
"strconv"
"strings"
)
func init() {
AddMonitorDriver("memory", func(options *json.RawMessage) Monitor {
m := &MemoryMonitor{}
json.Unmarshal(*options, &m)
if m.File == "" {
m.File = "/proc/meminfo"
}
m.Start()
return m
})
}
type MemoryMonitor struct {
File string
}
func (m *MemoryMonitor) Start() {
}
func (m *MemoryMonitor) GetVariables() []string {
data, err := ioutil.ReadFile(m.File)
if err != nil {
log.Printf("Error getting memory variables: %s", err)
return []string{}
}
memstring := string(data)
lines := strings.Split(memstring, "\n")
variables := []string{}
for _, line := range lines {
name := strings.Split(line, ":")[0]
variables = append(variables, name)
}
return variables
}
func (m *MemoryMonitor) GetValues(names []string) (values map[string]interface{}) {
values = make(map[string]interface{})
data, err := ioutil.ReadFile(m.File)
if err != nil {
log.Printf("Error getting memory variables: %s", err)
return
}
memstring := string(data)
lines := strings.Split(memstring, "\n")
for _, line := range lines {
parts := strings.Split(line, ":")
found := false
for _, name := range names {
if name == parts[0] {
found = true
}
}
if !found {
continue
}
value := strings.Split(strings.Trim(parts[1], " "), " ")[0]
values[parts[0]], _ = strconv.ParseUint(value, 10, 64)
}
return
}