Files
4566704 30a0889ffa feat(monitor): 自 go-hua 迁入系统资源监控
- Start 后台每秒采集(CPU/内存/磁盘/网络/进程/连接/主机), GetInfo 取快照
- 基于 gopsutil, 附测试、examples/monitor 例程与包 README
2026-09-20 12:29:37 +08:00

35 lines
771 B
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package monitor
import (
"strings"
"github.com/shirou/gopsutil/v3/host"
)
// HostInfo 主机信息(主机名/系统/开机时长等,gopsutil host.InfoStat)
type HostInfo struct {
HostName string `json:"hostName"`
Uptime uint64 `json:"uptime"`
BootTime uint64 `json:"bootTime"`
Procs uint64 `json:"procs"`
Os string `json:"os"`
Platform string `json:"platform"`
}
// getHostInfo 采集主机信息
func getHostInfo() (HostInfo, error) {
info := HostInfo{}
h, err := host.Info()
if err != nil {
return info, err
}
info.HostName = h.Hostname
info.Uptime = h.Uptime
info.BootTime = h.BootTime
info.Procs = h.Procs
info.Os = h.OS
info.Platform = strings.Trim(strings.Replace(h.Platform, "Microsoft", "", -1), " ")
return info, nil
}