-
Notifications
You must be signed in to change notification settings - Fork 2
/
cpu_load.go
59 lines (53 loc) · 1.04 KB
/
cpu_load.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
package main
import (
"fmt"
"io/ioutil"
"log"
"strconv"
"strings"
"time"
)
type CpuLoad struct {
val map[string]interface{}
}
func (k *CpuLoad) value() map[string]interface{} {
return k.val
}
func cpu_load() element {
e := &CpuLoad{}
go func() {
for {
if val, err := e.read(); err == nil {
e.val = val
} else {
log.Printf("could not read cpu load: %v", err)
}
time.Sleep(time.Second)
}
}()
return e
}
func (k *CpuLoad) read() (map[string]interface{}, error) {
data, err := ioutil.ReadFile("/proc/loadavg")
if err != nil {
return nil, fmt.Errorf("read cpu load from %s - %s", "/proc/loadavg", err)
}
parts := strings.Split(strings.TrimSpace(string(data)), " ")
load, err := strconv.ParseFloat(parts[0], 64)
if err != nil {
return nil, fmt.Errorf("parse cpu average load: %s", err)
}
var color string
switch {
case load >= 10:
color = "#dc322f"
case load >= 4:
color = "#b58900"
default:
color = "#6c71c4"
}
return map[string]interface{}{
"color": color,
"load": load,
"icon": xbm("load")}, nil
}