-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathfree.go
68 lines (59 loc) · 1.58 KB
/
free.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
package main
import (
"fmt"
"os/exec"
"regexp"
)
const (
FREE = "free"
FREE_MEM_TOTAL = "mem.total"
FREE_MEM_USED = "mem.used"
FREE_MEM_FREE = "mem.free"
FREE_MEM_BUFFERS = "mem.buffers"
FREE_MEM_CACHED = "mem.cached"
FREE_SWAP_TOTAL = "swap.total"
FREE_SWAP_USED = "swap.used"
FREE_SWAP_FREE = "swap.free"
)
var FREE_MEMORY_REGEXP = regexp.MustCompile("Mem:\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)")
var FREE_SWAP_REGEXP = regexp.MustCompile("Swap:\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)")
func init() {
parser.Add(FREE, "true", "Collect free metrics")
}
type Free struct {
RawStatus []byte
}
func (free *Free) fetch() (b []byte, e error) {
if len(free.RawStatus) == 0 {
free.RawStatus, _ = exec.Command("free").Output()
if len(free.RawStatus) == 0 {
return b, fmt.Errorf("free returned empty output")
}
}
return free.RawStatus, nil
}
func (free *Free) Collect(c *MetricsCollection) error {
s, e := free.fetch()
if e != nil {
return e
}
raw := string(s)
parts := FREE_MEMORY_REGEXP.FindStringSubmatch(raw)
if len(parts) > 0 {
c.Add(FREE_MEM_TOTAL, parseInt64(parts[1]))
c.Add(FREE_MEM_USED, parseInt64(parts[2]))
c.Add(FREE_MEM_FREE, parseInt64(parts[3]))
c.Add(FREE_MEM_BUFFERS, parseInt64(parts[5]))
c.Add(FREE_MEM_CACHED, parseInt64(parts[6]))
}
parts = FREE_SWAP_REGEXP.FindStringSubmatch(raw)
if len(parts) > 0 {
c.Add(FREE_SWAP_TOTAL, parseInt64(parts[1]))
c.Add(FREE_SWAP_USED, parseInt64(parts[2]))
c.Add(FREE_SWAP_FREE, parseInt64(parts[3]))
}
return nil
}
func (free *Free) Prefix() string {
return "free"
}