-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmem.go
45 lines (32 loc) · 960 Bytes
/
mem.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
package goshin
import "fmt"
import "os/exec"
import linuxproc "github.com/c9s/goprocinfo/linux"
import "github.com/tjgq/broadcast"
type MemoryUsage struct {
total, free, buffers, cache, swap uint64
}
func (m *MemoryUsage) Usage() float64 {
memInfo, _ := linuxproc.ReadMemInfo("/proc/meminfo")
m.free = memInfo.MemFree + memInfo.Buffers + memInfo.Cached
m.total = memInfo.MemTotal
return float64(1 - float64(m.free)/float64(m.total))
}
func (m *MemoryUsage) Ranking() string {
out, _ := exec.Command("sh", "-c", "ps -eo pmem,pid,comm | sed 1d | sort -nrb -k1 | head -10").Output()
s := string(out[:])
return fmt.Sprint("used\n\n", s)
}
func (m *MemoryUsage) Collect(queue chan *Metric, listener *broadcast.Listener) {
for {
<-listener.Ch
metric := NewMetric()
metric.Service = "memory"
metric.Value = m.Usage()
metric.Description = m.Ranking()
queue <- metric
}
}
func NewMemoryUsage() *MemoryUsage {
return &MemoryUsage{}
}