Skip to content

Instantly share code, notes, and snippets.

@kgersen
Created October 27, 2021 22:00
Show Gist options
  • Save kgersen/ca265719c76ed1a41e98c2801135b3b8 to your computer and use it in GitHub Desktop.
Save kgersen/ca265719c76ed1a41e98c2801135b3b8 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"math"
"time"
"github.com/shirou/gopsutil/v3/cpu"
)
func GetCPULoad(interval time.Duration) (string, error) {
pcts, err := cpu.Percent(interval, true)
if err != nil {
return "", err
}
cpuload := "|"
for _, pc := range pcts {
v := math.Round(pc)
cpuload += fmt.Sprintf("%3v|", v)
}
return cpuload, nil
}
func main() {
for {
load, err := GetCPULoad(0)
if err != nil {
fmt.Printf("%v\n", err)
}
fmt.Printf("%s\n", load)
time.Sleep(time.Second * 1)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment