feat(monitor): 自 go-hua 迁入系统资源监控
- Start 后台每秒采集(CPU/内存/磁盘/网络/进程/连接/主机), GetInfo 取快照 - 基于 gopsutil, 附测试、examples/monitor 例程与包 README
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
# monitor
|
||||
|
||||
系统资源监控:CPU/内存/磁盘/网络/进程/TCP·UDP 连接/主机信息,基于 `gopsutil`。
|
||||
`Start` 后台每秒采集(主机/连接每 5 秒),`GetInfo` 随时取最新快照。
|
||||
|
||||
> 迁移自 go-hua/monitor,代码保持原样。
|
||||
|
||||
## 用法
|
||||
|
||||
```go
|
||||
import (
|
||||
"time"
|
||||
|
||||
"git.zeroonesoft.cn/golib/zogo/monitor"
|
||||
)
|
||||
|
||||
// 启动后台采集
|
||||
monitor.Start("1.0.0")
|
||||
time.Sleep(6 * time.Second) // 等采集几轮
|
||||
|
||||
info := monitor.GetInfo() // MonInfo 全量快照
|
||||
fmt.Println(info.Cpu.UsedPercent, info.Mem.UsedPercent)
|
||||
|
||||
// 精简版快照(面向上报的轻量结构)
|
||||
lite := monitor.ToLiteInfo(info)
|
||||
|
||||
// 单项直查(不经后台采集)
|
||||
procs, err := monitor.GetProcessInfo()
|
||||
```
|
||||
|
||||
完整可运行例程:[examples/monitor/main.go](../examples/monitor/main.go)
|
||||
|
||||
## 注意
|
||||
|
||||
- `Start` 起后台 goroutine,进程退出无需显式停止;重复调用会起多份采集。
|
||||
- CPU 使用率首次采样为 0(需两个采样窗口),取值前至少等一个采集周期。
|
||||
Reference in New Issue
Block a user