xmtop/devices/cpu_cpu.go
Sean E. Russell 231b0d03fe Extensions.
2020-02-28 10:06:05 -06:00

32 lines
569 B
Go

package devices
import (
"fmt"
"time"
psCpu "github.com/shirou/gopsutil/cpu"
)
func init() {
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)
cpus[key] = int(vals[i])
}
return nil
}
RegisterCPU(f)
}