forked from robertw07/boomer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresource_st.go
81 lines (72 loc) · 1.7 KB
/
resource_st.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package boomer
import (
"fmt"
"strconv"
"time"
"github.com/shirou/gopsutil/mem"
"github.com/shirou/gopsutil/v3/cpu"
)
type Storage struct {
Name string
FileSystem string
Total uint64
Free uint64
}
type storageInfo struct {
Name string
Size uint64
FreeSpace uint64
FileSystem string
}
//func getStorageInfo() {
// var storageinfo []storageInfo
// var loaclStorages []Storage
// err := wmi.Query("Select * from Win32_LogicalDisk", &storageinfo)
// if err != nil {
// return
// }
//
// for _, storage := range storageinfo {
// info := Storage{
// Name: storage.Name,
// FileSystem: storage.FileSystem,
// Total: storage.Size / 1024 / 1024 / 1024,
// Free: storage.FreeSpace / 1024 / 1024 / 1024,
// }
// if info.Total >= 1 {
// fmt.Printf("%s总大小%dG,可用%dG\n", info.Name, info.Total, info.Free)
// loaclStorages = append(loaclStorages, info)
// }
// }
// //fmt.Printf("localStorages:= %v\n", loaclStorages)
//}
type ComputerMonitor struct {
CPU float64 `json:"cpu"`
Mem float64 `json:"mem"`
}
// GetCPUPercent 获取CPU使用率
func GetCPUPercent() float64 {
percent, err := cpu.Percent(time.Second, false)
if err != nil {
fmt.Println(err.Error())
return -1
}
result, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", percent[0]), 64)
return result
}
// GetMemPercent 获取内存使用率
func GetMemPercent() float64 {
memInfo, err := mem.VirtualMemory()
if err != nil {
fmt.Println(err.Error())
return -1
}
result, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", memInfo.UsedPercent), 64)
return result
}
func GetCpuMem() ComputerMonitor {
var res ComputerMonitor
res.CPU = GetCPUPercent()
res.Mem = GetMemPercent()
return res
}