summaryrefslogtreecommitdiffstats
path: root/devices/cpu_cpu.go
blob: 9847572bed934b28f18dd54cff85fb6695102c5c (plain)
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
package devices

import (
	"fmt"

	psCpu "github.com/shirou/gopsutil/cpu"
)

func init() {
	f := func(cpus map[string]int, l bool) map[string]error {
		cpuCount, err := CpuCount()
		if err != nil {
			return nil
		}
		formatString := "CPU%1d"
		if cpuCount > 10 {
			formatString = "CPU%02d"
		}
		vals, err := psCpu.Percent(0, l)
		if err != nil {
			return map[string]error{"gopsutil": err}
		}
		for i := 0; i < len(vals); i++ {
			key := fmt.Sprintf(formatString, i)
			v := vals[i]
			if v > 100 {
				v = 100
			}
			cpus[key] = int(v)
		}
		return nil
	}
	RegisterCPU(f)
}