feat(monitor): 自 go-hua 迁入系统资源监控

- Start 后台每秒采集(CPU/内存/磁盘/网络/进程/连接/主机), GetInfo 取快照
- 基于 gopsutil, 附测试、examples/monitor 例程与包 README
This commit is contained in:
2026-09-20 12:29:37 +08:00
parent 138c929bd4
commit 30a0889ffa
11 changed files with 963 additions and 0 deletions
+36
View File
@@ -0,0 +1,36 @@
// monitor 包示例:系统资源监控(CPU/内存/磁盘/网络/进程/连接),Start 后台采集,GetInfo 随时取最新快照
package main
import (
"encoding/json"
"fmt"
"time"
"git.zeroonesoft.cn/golib/zogo/monitor"
)
func main() {
// 启动后台采集(每秒刷新;主机/连接信息每 5 秒刷新一次)
monitor.Start("1.0.0")
// 等待采集几轮数据
time.Sleep(6 * time.Second)
info := monitor.GetInfo()
fmt.Printf("主机: %s 系统: %s 开机时长: %d 秒\n", info.Host.HostName, info.Host.Platform, info.Host.Uptime)
fmt.Printf("CPU: 物理核心 %d 逻辑核心 %d 总使用率 %.1f%%\n", info.Cpu.Core, info.Cpu.Logic, info.Cpu.UsedPercent)
fmt.Printf("内存: 总计 %d MB 已用 %.1f%%\n", info.Mem.Total/1024/1024, info.Mem.UsedPercent)
fmt.Printf("TCP 连接: %d UDP 连接: %d 监听端口: %d 个\n", info.Conn.Tcp, info.Conn.Udp, len(info.Conn.Listen))
fmt.Printf("磁盘分区: %d 个 网卡: %d 个 进程: %d 个\n", len(info.Disk), len(info.Net), len(info.Process))
for _, d := range info.Disk {
if d.Total > 0 {
fmt.Printf(" 磁盘 %s 挂载 %s 已用 %.1f%% (%s)\n", d.Device, d.MountPoint, d.UsedPercent, d.UsedVolume)
}
}
// 精简版信息(适合定时上报),JSON 序列化输出
lite := monitor.ToLiteInfo(info)
buf, _ := json.Marshal(lite)
fmt.Printf("Lite JSON: %d 字节\n", len(buf))
}