forked from kavanahuang/system
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hardware.go
156 lines (129 loc) · 3.17 KB
/
hardware.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/**
* Created by Kernel.Huang
* User: kernelman@live.com
* Date: 2021/3/23
* Time: 16:03
*/
package system
import (
"github.com/jucci1887/logs"
"github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/disk"
"github.com/shirou/gopsutil/v3/load"
"github.com/shirou/gopsutil/v3/mem"
"runtime"
)
type HardwareInfo struct {
CPU float64 `json:"cpu"`
DiskFree uint64 `json:"diskFree"`
DiskUsedPercent float64 `json:"diskUsedPercent"`
MemUsedPercent float64 `json:"memUsedPercent"`
GoMemory int64 `json:"goMemory"`
GoRoutines int `json:"goRoutines"`
DirPath string
}
var Hardware = new(HardwareInfo)
func init() {
Hardware.DirPath = ""
}
// 检查磁盘空间是否已满
func (info *HardwareInfo) IsDiskFull(dir string) bool {
Hardware.DirPath = dir
if Hardware.DiskFreeSpace() < 10*1024*1024 {
logs.Error("Disk is full.")
return true
}
return false
}
func (info *HardwareInfo) GetCpuPercent() (cpuInfo float64) {
percent, err := cpu.Percent(0, false)
if err == nil {
cpuInfo = percent[0]
}
return
}
// 获取系统运行时的硬件状态
func (info *HardwareInfo) GetHardwareState() {
info.CPU = info.GetCpuPercent()
info.DiskUsedPercent = info.GetDiskUsedPercent()
info.DiskFree = info.DiskFreeSpace()
info.MemUsedPercent = info.RamUsed()
info.GoMemory = info.CountGoUseRam()
info.GoRoutines = info.CountGoroutine()
}
// 计算Golang运行时分配的内存总字节数
func (info *HardwareInfo) CountGoUseRam() int64 {
ram := runtime.MemStats{}
runtime.ReadMemStats(&ram)
return int64(ram.Sys)
}
// 计算当前Goroutine的数量
func (info *HardwareInfo) CountGoroutine() int {
return runtime.NumGoroutine()
}
// 获取CPU信息
func (info *HardwareInfo) CpuInfo() []cpu.InfoStat {
cpuInfo, err := cpu.Info()
if err != nil {
logs.Error("Get cpu info error: ", err)
}
return cpuInfo
}
// 获取CPU平均负载
func (info *HardwareInfo) CpuLoad() *load.AvgStat {
avg, err := load.Avg()
if err != nil {
logs.Error("Get cpu load avg error: ", err)
}
return avg
}
// 获取硬盘状态
func (info *HardwareInfo) DiskState() (diskFree uint64, diskUsed float64) {
if info.DirPath == "" {
info.DirPath = "/"
}
stat, err := disk.Usage(info.DirPath)
if err == nil {
diskFree = stat.Free
diskUsed = stat.UsedPercent
return
}
logs.Error("Get hard disk stat error: ", err)
return
}
// 获取硬盘已使用空间
func (info *HardwareInfo) DiskFreeSpace() (diskFree uint64) {
if info.DirPath == "" {
info.DirPath = "/"
}
stat, err := disk.Usage(info.DirPath)
if err == nil {
diskFree = stat.Free
return
}
logs.Error("Get hard disk free space error: ", err)
return
}
// 获取硬盘使用百分比
func (info *HardwareInfo) GetDiskUsedPercent() (diskUsed float64) {
if info.DirPath == "" {
info.DirPath = "/"
}
stat, err := disk.Usage(info.DirPath)
if err == nil {
diskUsed = stat.UsedPercent
return
}
logs.Error("Get hard disk stat error: ", err)
return
}
// 获取内存使用率
func (info *HardwareInfo) RamUsed() (used float64) {
stat, err := mem.VirtualMemory()
if err == nil {
used = stat.UsedPercent
return
}
logs.Error("Get memory usage error: ", err)
return
}