-
Notifications
You must be signed in to change notification settings - Fork 2
/
memory.go
75 lines (65 loc) · 1.36 KB
/
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
package main
import (
"fmt"
"log"
"os/exec"
"regexp"
"strconv"
"strings"
"time"
)
var match_mem = regexp.MustCompile(`Mem:\s+(.+)`)
type Memory struct {
val map[string]interface{}
}
func (k *Memory) value() map[string]interface{} {
return k.val
}
func memory_usage() element {
e := &Memory{}
go func() {
for {
if val, err := e.read(); err == nil {
e.val = val
} else {
log.Printf("could not read memory usage: %v", err)
}
time.Sleep(time.Second)
}
}()
return e
}
func (k *Memory) read() (map[string]interface{}, error) {
data, err := exec.Command("free", "-m").Output()
if err != nil {
return nil, fmt.Errorf("'free -m' command: %s", err)
}
m := match_mem.FindStringSubmatch(string(data))
if len(m) != 2 {
return nil, fmt.Errorf("number of matches was not expected for mem submatch")
}
parts := strings.Fields(m[1])
total, err := strconv.Atoi(parts[0])
if err != nil {
return nil, fmt.Errorf("read total memory: %s", err)
}
avail, err := strconv.Atoi(parts[5])
if err != nil {
return nil, fmt.Errorf("read available memory: %s", err)
}
used := total - avail
perc := 100 * used / total
var color string
switch {
case perc >= 90:
color = "#dc322f"
case perc >= 70:
color = "#b58900"
default:
color = "#859900"
}
return map[string]interface{}{
"color": color,
"perc": perc,
"icon": xbm("mem")}, nil
}