Created
October 27, 2021 22:00
-
-
Save kgersen/ca265719c76ed1a41e98c2801135b3b8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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