2020-02-26 04:04:55 +08:00
|
|
|
package devices
|
|
|
|
|
2020-03-03 19:47:52 +08:00
|
|
|
// TODO: https://github.com/elastic/go-sysinfo
|
2020-05-20 02:49:19 +08:00
|
|
|
// TODO: https://github.com/mackerelio/go-osstat
|
|
|
|
// TODO: https://github.com/akhenakh/statgo
|
|
|
|
// TODO: https://github.com/jaypipes/ghw
|
2020-03-03 19:47:52 +08:00
|
|
|
|
2020-02-26 04:04:55 +08:00
|
|
|
import (
|
2020-02-28 08:51:28 +08:00
|
|
|
"log"
|
2020-02-26 04:04:55 +08:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2020-05-02 01:41:18 +08:00
|
|
|
var cpuFuncs []func(map[string]int, bool) map[string]error
|
2020-02-26 04:04:55 +08:00
|
|
|
|
2020-02-28 08:51:28 +08:00
|
|
|
// RegisterCPU adds a new CPU device to the CPU widget. labels returns the
|
|
|
|
// names of the devices; they should be as short as possible, and the indexes
|
|
|
|
// of the returned slice should align with the values returned by the percents
|
|
|
|
// function. The percents function should return the percent CPU usage of the
|
|
|
|
// device(s), sliced over the time duration supplied. If the bool argument to
|
|
|
|
// percents is true, it is expected that the return slice
|
2020-02-26 04:04:55 +08:00
|
|
|
//
|
2020-02-28 08:51:28 +08:00
|
|
|
// labels may be called once and the value cached. This means the number of
|
|
|
|
// cores should not change dynamically.
|
2020-05-02 01:41:18 +08:00
|
|
|
func RegisterCPU(f func(map[string]int, bool) map[string]error) {
|
2020-02-28 08:51:28 +08:00
|
|
|
cpuFuncs = append(cpuFuncs, f)
|
2020-02-26 04:04:55 +08:00
|
|
|
}
|
|
|
|
|
2020-02-28 08:51:28 +08:00
|
|
|
// CPUPercent calculates the percentage of cpu used either per CPU or combined.
|
2020-02-26 04:04:55 +08:00
|
|
|
// Returns one value per cpu, or a single value if percpu is set to false.
|
2020-02-29 00:03:41 +08:00
|
|
|
func UpdateCPU(cpus map[string]int, interval time.Duration, logical bool) {
|
2020-02-28 08:51:28 +08:00
|
|
|
for _, f := range cpuFuncs {
|
2020-05-02 01:41:18 +08:00
|
|
|
errs := f(cpus, logical)
|
2020-02-28 08:51:28 +08:00
|
|
|
if errs != nil {
|
|
|
|
for k, e := range errs {
|
|
|
|
log.Printf("%s: %s", k, e)
|
|
|
|
}
|
2020-02-26 04:04:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|