gopsutil
是一个用于获取系统和进程信息的Go
语言库。它可以帮助开发者轻松地访问系统级别的资源信息,包括 CPU、内存、磁盘、网络等。这些信息对于监控系统性能、分析应用程序行为以及故障排查非常有用。
主要特点
常用模块
应用场景
项目地址:https://github.com/shirou/gopsutil
代码片段:
goinfo, _ := cpu.Info()
pretty, _ := json.MarshalIndent(info, "", " ")
println("Info:", string(pretty))
运行结果:
Info: [ { "cpu": 0, "vendorId": "GenuineIntel", "family": "198", "model": "", "stepping": 0, "physicalId": "BFEBFBFF000806EA", "coreId": "", "cores": 8, "modelName": "Intel(R) Core(TM) i7-8650U CPU @ 1.90GHz", "mhz": 2112, "cacheSize": 0, "flags": [], "microcode": "" } ]
代码片段:
gocpuNumber, _ := cpu.Counts(true)
println("CPU逻辑数量: ", cpuNumber)
cpuNumber, _ = cpu.Counts(false)
println("CPU物理核心: ", cpuNumber)
运行结果:
CPU逻辑数量: 8 CPU物理核心: 4
代码片段:
gocpuPercent, _ := cpu.Percent(time.Second, false)
println("CPU使用率: ", cpuPercent[0])
运行结果:
CPU使用率: +1.523438e+001
代码片段:
godiskPart, _ := disk.Partitions(false)
for _, dp := range diskPart {
diskUsed, _ := disk.Usage(dp.Mountpoint)
fmt.Printf("路径: %s, 分区总大小: %d, 剩余大小: %d, 文件系统类型: %s, 分区使用率: %.3f%%\n",
diskUsed.Path,
diskUsed.Total,
diskUsed.Free,
dp.Fstype,
diskUsed.UsedPercent,
)
}
运行结果:
路径: C:, 分区总大小: 254792429568, 剩余大小: 16384618496, 文件系统类型: NTFS, 分区使用率: 93.569% 路径: D:, 分区总大小: 124688269312, 剩余大小: 113836687360, 文件系统类型: exFAT, 分区使用率: 8.703%
代码片段:
gocounters, _ := disk.IOCounters()
for _, counter := range counters {
pretty, _ := json.MarshalIndent(counter, "", " ")
println(string(pretty))
}
运行结果:
{ "readCount": 300824488, "mergedReadCount": 0, "writeCount": 108395343, "mergedWriteCount": 0, "readBytes": 9078301635072, "writeBytes": 4770640154624, "readTime": 184401, "writeTime": 78903, "iopsInProgress": 0, "ioTime": 0, "weightedIO": 0, "name": "C:", "serialNumber": "", "label": "" }
代码片段:
goinfo, _ := host.Info()
pretty, _ := json.MarshalIndent(info, "", " ")
println("Info:", string(pretty))
te := time.Unix(int64(info.BootTime), 0)
println("BootTime:", te.Format(time.DateTime))
println("Uptime:", info.Uptime)
运行结果:
Info: { "hostname": "DESKTOP-5NLBCER", "uptime": 15969905, "bootTime": 1713940979, "procs": 220, "os": "windows", "platform": "Microsoft Windows 10 Home", "platformFamily": "Standalone Workstation", "platformVersion": "21H1", "kernelVersion": "10.0.19043.1645 Build 19043.1645", "kernelArch": "x86_64", "virtualizationSystem": "", "virtualizationRole": "", "hostId": "c9667c87-395d-4cd8-a4b7-c00bd6ee35df" } BootTime: 2024-04-24 14:42:59 Uptime: 15969905
代码片段:
goavg, _ := load.Avg()
pretty, _ := json.MarshalIndent(avg, "", " ")
println("Avg:", string(pretty))
misc, _ := load.Misc()
pretty, _ = json.MarshalIndent(misc, "", " ")
println("Misc:", string(pretty))
运行结果:
Avg: { "load1": 0, "load5": 0, "load15": 0 } Misc: { "procsTotal": 0, "procsCreated": 0, "procsRunning": 0, "procsBlocked": 0, "ctxt": 0 }
代码片段:
govirtualMemory, _ := mem.VirtualMemory()
pretty, _ := json.MarshalIndent(virtualMemory, "", " ")
println("VirtualMemory:", string(pretty))
运行结果:
VirtualMemory: { "total": 8502890496, "available": 796385280, "used": 7706505216, "usedPercent": 90, "free": 796385280, "active": 0, "inactive": 0, "wired": 0, "laundry": 0, "buffers": 0, "cached": 0, "writeBack": 0, "dirty": 0, "writeBackTmp": 0, "shared": 0, "slab": 0, "sreclaimable": 0, "sunreclaim": 0, "pageTables": 0, "swapCached": 0, "commitLimit": 0, "committedAS": 0, "highTotal": 0, "highFree": 0, "lowTotal": 0, "lowFree": 0, "swapTotal": 0, "swapFree": 0, "mapped": 0, "vmallocTotal": 0, "vmallocUsed": 0, "vmallocChunk": 0, "hugePagesTotal": 0, "hugePagesFree": 0, "hugePagesRsvd": 0, "hugePagesSurp": 0, "hugePageSize": 0, "anonHugePages": 0 }
代码片段:
goswapMemory, _ := mem.SwapMemory()
pretty, _ := json.MarshalIndent(swapMemory, "", " ")
println("SwapMemory:", string(pretty))
运行结果:
SwapMemory: { "total": 22425178112, "used": 4906590208, "free": 17518587904, "usedPercent": 21.9, "sin": 0, "sout": 0, "pgIn": 0, "pgOut": 0, "pgFault": 0, "pgMajFault": 0 }
代码片段:
gocounters, _ := net.IOCounters(true)
pretty, _ := json.MarshalIndent(counters, "", " ")
println(string(pretty))
运行结果:
[ { "name": "以太网 3", "bytesSent": 435026, "bytesRecv": 52149, "packetsSent": 807, "packetsRecv": 467, "errin": 0, "errout": 0, "dropin": 0, "dropout": 0, "fifoin": 0, "fifoout": 0 }, { "name": "本地连接", "bytesSent": 0, "bytesRecv": 0, "packetsSent": 0, "packetsRecv": 0, "errin": 0, "errout": 0, "dropin": 0, "dropout": 0, "fifoin": 0, "fifoout": 0 } ]
代码片段:
go// 可填入all、tcp、udp、tcp4、udp4
info, _ := net.Connections("tcp")
pretty, _ = json.MarshalIndent(info, "", " ")
println(string(pretty))
运行结果:
[ { "fd": 0, "family": 2, "type": 1, "localaddr": { "ip": "0.0.0.0", "port": 135 }, "remoteaddr": { "ip": "0.0.0.0", "port": 0 }, "status": "LISTEN", "uids": null, "pid": 1448 } ]
代码片段:
goprocesses, _ := process.Processes()
for _, pro := range processes {
name, _ := pro.Name()
println(pro.Pid, name)
}
运行结果:
27788 wpscloudsvr.exe 27832 fsnotifier64.exe 27940 WeChatAppEx.exe 27960 svchost.exe 28316 WeChatAppEx.exe 28332 RuntimeBroker.exe 28660 WeChatAppEx.exe 28828 chrome.exe 29020 SearchApp.exe
本文作者:蒋固金
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!