-
Notifications
You must be signed in to change notification settings - Fork 0
/
getcpuload.go
55 lines (43 loc) · 1.06 KB
/
getcpuload.go
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main
import (
"fmt"
"log"
"math"
"time"
"github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/mem"
)
const REFRESH_TIME = 3 * time.Second
func main() {
// Show an initial result quickly
fmt.Println(get(1 * time.Second))
for {
fmt.Println(get(REFRESH_TIME))
}
}
func get(refreshTime time.Duration) string {
curCPU := getCPU(refreshTime)
curMem := getMem()
return fmt.Sprintf("%s %s", curCPU, curMem)
}
func getCPU(refreshTime time.Duration) string {
c, err := cpu.Percent(refreshTime, false)
if err != nil {
log.Fatalln("Cannot get CPU percentage:", err)
}
return percentString(c[0])
}
func getMem() string {
m, err := mem.VirtualMemory()
if err != nil {
log.Fatalln("Cannot get memory info:", err)
}
return fmt.Sprintf("%s(%dG)", percentString(m.UsedPercent), bytesToGB(m.Used))
}
func bytesToGB(b uint64) int {
return int(math.Round(float64(b) / 1024 / 1024 / 1024))
}
// Returns a string with a rounded integer and a percent sign
func percentString(f float64) string {
return fmt.Sprintf("%d%%", int(math.Round(f)))
}