/
data_std_cpu.go
56 lines (46 loc) · 1.11 KB
/
data_std_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
package main
import (
"fmt"
"regexp"
"strconv"
"strings"
)
func GetDataCPU(data *Data) error {
psStat, err := ReadProcFSFile("stat")
if err != nil {
return err
}
expr := regexp.MustCompile(`^cpu(\d+)`)
for i, l := range psStat.GetAsLines() {
if i > 0 {
m := expr.FindStringSubmatch(l)
if len(m) == 2 {
if cpuN, err := strconv.ParseInt(m[1], 10, 32); err == nil && cpuN < 64 {
r := strings.Split(l, " ")
if len(r) >= 8 {
user, _ := strconv.ParseUint(r[1], 10, 64)
nice, _ := strconv.ParseUint(r[2], 10, 64)
system, _ := strconv.ParseUint(r[3], 10, 64)
wait, _ := strconv.ParseUint(r[5], 10, 64)
prefix := fmt.Sprintf("CPU.cpu%d.", cpuN)
data.Longterm[prefix+"user"] = user + nice
data.Longterm[prefix+"system"] = system
data.Longterm[prefix+"wait"] = wait
}
}
} else {
break
}
}
}
psLoadAvg, err := ReadProcFSFile("loadavg")
if err != nil {
return err
}
line1 := strings.Split(psLoadAvg.GetAsString(), " ")
if len(line1) > 1 {
load, _ := strconv.ParseFloat(line1[0], 64)
data.Longterm["Load"] = load
}
return nil
}