forked from rancher/agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpu_info.go
59 lines (51 loc) · 1.48 KB
/
cpu_info.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
// +build linux freebsd solaris openbsd darwin
package hostInfo
import (
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/load"
"strconv"
"time"
)
func (c CPUCollector) getCPUInfo() (map[string]interface{}, error) {
data := map[string]interface{}{}
cpuInfo, err := cpu.Info()
if err != nil {
return map[string]interface{}{}, err
}
counts, err := cpu.Counts(true)
if err != nil {
return map[string]interface{}{}, err
}
data["count"] = counts
if len(cpuInfo) > 0 {
data["modelName"] = cpuInfo[0].ModelName
data["mhz"] = cpuInfo[0].Mhz
}
return data, nil
}
func (c CPUCollector) getCPUPercentage() (map[string]interface{}, error) {
data := map[string]interface{}{}
cpuCoresPercentages := []string{}
percents, err := cpu.Percent(time.Second*1, true)
if err != nil {
return map[string]interface{}{}, err
}
for _, percent := range percents {
cpuCoresPercentages = append(cpuCoresPercentages, strconv.FormatFloat(percent, 'f', -1, 64))
}
data["cpuCoresPercentages"] = cpuCoresPercentages
return data, nil
}
func (c CPUCollector) getCPULoadAverage() map[string]interface{} {
loadData, err := load.Avg()
if err != nil {
return map[string]interface{}{}
}
loadAvg := []string{}
loadAvg = append(loadAvg, strconv.FormatFloat(loadData.Load1, 'f', -1, 64))
loadAvg = append(loadAvg, strconv.FormatFloat(loadData.Load5, 'f', -1, 64))
loadAvg = append(loadAvg, strconv.FormatFloat(loadData.Load15, 'f', -1, 64))
return map[string]interface{}{
"loadAvg": loadAvg,
}
}