-
Notifications
You must be signed in to change notification settings - Fork 13
/
cpu.go
85 lines (63 loc) · 1.65 KB
/
cpu.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
76
77
78
79
80
81
82
83
84
85
package goshin
import (
"fmt"
"github.com/tjgq/broadcast"
"os/exec"
)
import linuxproc "github.com/c9s/goprocinfo/linux"
type CPUTime struct {
last, actual linuxproc.CPUStat
}
func (c *CPUTime) Store() {
c.last = c.actual
stat, _ := linuxproc.ReadStat("/proc/stat")
c.actual = stat.CPUStatAll
}
func (c *CPUTime) Used() uint64 {
var used = (c.actual.User + c.actual.Nice + c.actual.System) - (c.last.User + c.last.Nice + c.last.System)
return used
}
func (c *CPUTime) IOWait() uint64 {
return c.actual.IOWait - c.last.IOWait
}
func (c *CPUTime) Total() uint64 {
// should IOWait be considered as idle?
return c.Used() + (c.actual.Idle + c.actual.IOWait) - (c.last.Idle + c.actual.IOWait)
}
func (c *CPUTime) Usage() float64 {
var fraction float64 = float64(c.Used()) / float64(c.Total())
return fraction
}
func (c *CPUTime) IOWaitUsage() float64 {
var fraction float64 = float64(c.IOWait()) / float64(c.Total())
return fraction
}
func (c *CPUTime) Ranking() string {
out, _ := exec.Command("sh", "-c", "ps -eo pcpu,pid,comm | sort -nrb -k1 | head -10").Output()
s := string(out[:])
return fmt.Sprint("user+nice+system\n\n", s)
}
func (c *CPUTime) Collect(queue chan *Metric, listener *broadcast.Listener) {
for {
<-listener.Ch
c.Store()
if c.last.User == 0 {
// nothing stored yet
// so no metric to send
continue
}
cpu := NewMetric()
cpu.Service = "cpu"
cpu.Value = c.Usage()
cpu.Description = c.Ranking()
queue <- cpu
cpuwait := NewMetric()
cpuwait.Service = "cpuwait"
cpuwait.Value = c.IOWaitUsage()
cpuwait.Description = c.Ranking()
queue <- cpuwait
}
}
func NewCPUTime() *CPUTime {
return &CPUTime{}
}