-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload.go
43 lines (36 loc) · 785 Bytes
/
load.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
package util
import (
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/load"
"github.com/shirou/gopsutil/mem"
)
var (
cpuLogicCount int = 1
)
func init() {
cpuLogicCount, _ = cpu.Counts(true)
}
type CpuLoadInfo struct {
CpuNum int `json:"cpu_num"`
LoadAvg []float64 `json:"load_avg"`
}
type MemInfo struct {
MemorySum uint64 `json:"memory_sum"`
MemoryUseRate float64 `json:"memory_use_rate"`
}
func GetCpuLoadInfo() *CpuLoadInfo {
avgStat, _ := load.Avg()
info := &CpuLoadInfo{
CpuNum: cpuLogicCount,
LoadAvg: []float64{avgStat.Load1, avgStat.Load5, avgStat.Load15},
}
return info
}
func GetMemInfo() *MemInfo {
v, _ := mem.VirtualMemory()
info := &MemInfo{
MemorySum: v.Total,
MemoryUseRate: v.UsedPercent,
}
return info
}