-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathai_false.go
63 lines (55 loc) · 1.28 KB
/
ai_false.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
package main
import (
"fmt"
"math"
"time"
"github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/disk"
"github.com/shirou/gopsutil/v3/mem"
zero "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/message"
)
func init() { // 插件主体
zero.OnFullMatchGroup([]string{"检查身体", "自检", "启动自检", "系统状态"}, zero.AdminPermission).SetBlock(true).
Handle(func(ctx *zero.Ctx) {
ctx.SendChain(message.Text(
"* CPU占用: ", cpuPercent(), "%\n",
"* RAM占用: ", memPercent(), "%\n",
"* 硬盘使用: ", diskPercent(),
),
)
})
}
func cpuPercent() float64 {
percent, err := cpu.Percent(time.Second, false)
if err != nil {
return -1
}
return math.Round(percent[0])
}
func memPercent() float64 {
memInfo, err := mem.VirtualMemory()
if err != nil {
return -1
}
return math.Round(memInfo.UsedPercent)
}
func diskPercent() string {
parts, err := disk.Partitions(true)
if err != nil {
return err.Error()
}
msg := ""
for _, p := range parts {
diskInfo, err := disk.Usage(p.Mountpoint)
if err != nil {
msg += "\n - " + err.Error()
continue
}
pc := uint(math.Round(diskInfo.UsedPercent))
if pc > 0 {
msg += fmt.Sprintf("\n - %s(%dM) %d%%", p.Mountpoint, diskInfo.Total/1024/1024, pc)
}
}
return msg
}