2020-02-26 04:04:55 +08:00
|
|
|
package devices
|
|
|
|
|
|
|
|
import (
|
2020-02-28 08:51:28 +08:00
|
|
|
"fmt"
|
|
|
|
|
2020-02-26 04:04:55 +08:00
|
|
|
psCpu "github.com/shirou/gopsutil/cpu"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2020-05-02 01:41:18 +08:00
|
|
|
f := func(cpus map[string]int, l bool) map[string]error {
|
2020-06-27 00:50:15 +08:00
|
|
|
cpuCount, err := CpuCount()
|
2020-02-28 08:51:28 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
formatString := "CPU%1d"
|
|
|
|
if cpuCount > 10 {
|
|
|
|
formatString = "CPU%02d"
|
|
|
|
}
|
2020-05-02 01:41:18 +08:00
|
|
|
vals, err := psCpu.Percent(0, l)
|
2020-02-28 08:51:28 +08:00
|
|
|
if err != nil {
|
|
|
|
return map[string]error{"gopsutil": err}
|
|
|
|
}
|
|
|
|
for i := 0; i < len(vals); i++ {
|
|
|
|
key := fmt.Sprintf(formatString, i)
|
2020-03-03 20:36:42 +08:00
|
|
|
v := vals[i]
|
|
|
|
if v > 100 {
|
|
|
|
v = 100
|
|
|
|
}
|
|
|
|
cpus[key] = int(v)
|
2020-02-28 08:51:28 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
RegisterCPU(f)
|
2020-02-26 04:04:55 +08:00
|
|
|
}
|