-
Notifications
You must be signed in to change notification settings - Fork 8
/
ram.go
44 lines (38 loc) · 788 Bytes
/
ram.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
package main
import (
"bufio"
"bytes"
"io"
"log"
"os"
)
type Ram struct {
Free int `json:"free"`
Total int `json:"total"`
}
func ram() interface{} {
f, err := os.Open("/proc/meminfo")
if err != nil {
return "Unsupported"
}
defer f.Close()
bufReader := bufio.NewReader(f)
b := make([]byte, 0, 100)
var free, total int
for line, isPrefix, err := bufReader.ReadLine(); err != io.EOF; line, isPrefix, err = bufReader.ReadLine() {
if err != nil {
log.Fatal("bufReader.ReadLine: ", err)
}
b = append(b, line...)
if !isPrefix {
switch {
case bytes.Contains(b, []byte("MemFree")):
free = toInt(bytes.Fields(b)[1])
case bytes.Contains(b, []byte("MemTotal")):
total = toInt(bytes.Fields(b)[1])
}
b = b[:0]
}
}
return Ram{free, total}
}