xmtop/devices/cpu_cpu.go

32 lines
569 B
Go
Raw Normal View History

2020-02-26 04:04:55 +08:00
package devices
import (
"fmt"
"time"
2020-02-26 04:04:55 +08:00
psCpu "github.com/shirou/gopsutil/cpu"
)
func init() {
2020-02-29 00:03:41 +08:00
f := func(cpus map[string]int, iv time.Duration, l bool) map[string]error {
cpuCount, err := psCpu.Counts(l)
if err != nil {
return nil
}
formatString := "CPU%1d"
if cpuCount > 10 {
formatString = "CPU%02d"
}
vals, err := psCpu.Percent(iv, l)
if err != nil {
return map[string]error{"gopsutil": err}
}
for i := 0; i < len(vals); i++ {
key := fmt.Sprintf(formatString, i)
2020-02-29 00:03:41 +08:00
cpus[key] = int(vals[i])
}
return nil
}
RegisterCPU(f)
2020-02-26 04:04:55 +08:00
}