feat(monitor): 自 go-hua 迁入系统资源监控
- Start 后台每秒采集(CPU/内存/磁盘/网络/进程/连接/主机), GetInfo 取快照 - 基于 gopsutil, 附测试、examples/monitor 例程与包 README
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
package monitor
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/shirou/gopsutil/v3/load"
|
||||
)
|
||||
|
||||
// LoadInfo 系统负载信息(1/5/15 分钟均值)
|
||||
type LoadInfo struct {
|
||||
Load1 float64 `json:"load1"`
|
||||
Load5 float64 `json:"load5"`
|
||||
Load15 float64 `json:"load15"`
|
||||
}
|
||||
|
||||
// getLoadInfo 采集系统负载
|
||||
func getLoadInfo() (LoadInfo, error) {
|
||||
info := LoadInfo{}
|
||||
v, err := load.Avg()
|
||||
if err != nil {
|
||||
return info, err
|
||||
}
|
||||
info.Load1, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", v.Load1), 64)
|
||||
info.Load5, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", v.Load5), 64)
|
||||
info.Load15, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", v.Load15), 64)
|
||||
//fmt.Printf("%v\n", info)
|
||||
return info, nil
|
||||
}
|
||||
Reference in New Issue
Block a user