2024-10-26
Go
00

目录

第一部分 概述
第二部分 常用操作
2.1 CPU
2.1.1 CPU信息
2.1.2 CPU核心数
2.1.3 CPU使用率
2.2 DISK
2.2.1 分区信息
2.2.2 IO计数
2.3 HOST
2.3.1 主机信息
2.4 LOAD
2.4.1 负载
2.5 MEM
2.5.1 虚拟内存
2.5.2 交换内存
2.6 NET
2.6.1 网络计数
2.6.2 网络连接
2.7 PROCESS
2.7.1 进程列表

第一部分 概述

gopsutil是一个用于获取系统和进程信息的Go语言库。它可以帮助开发者轻松地访问系统级别的资源信息,包括 CPU、内存、磁盘、网络等。这些信息对于监控系统性能、分析应用程序行为以及故障排查非常有用。

主要特点

  • 跨平台支持:支持多种操作系统,包括 Windows、Linux 和 macOS。
  • 简单易用:提供了简洁的 API,方便快速集成到 Go 应用中。
  • 实时监控:可以获取实时的系统资源使用情况,适合做系统监控和性能分析。

常用模块

  • CPU:获取 CPU 使用率、核心信息等。
  • 内存:获取物理内存和虚拟内存的使用情况。
  • 磁盘:获取磁盘使用情况,包括读写速率和使用百分比。
  • 网络:获取网络流量信息和网络接口统计。
  • 进程:获取系统中运行的进程信息,如 PID、名称、状态等。

应用场景

  • 系统监控工具
  • 性能分析和调优
  • 自动化运维脚本
  • 故障排查和诊断工具

项目地址:https://github.com/shirou/gopsutil

第二部分 常用操作

2.1 CPU

2.1.1 CPU信息

代码片段:

go
info, _ := 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": "" } ]

2.1.2 CPU核心数

代码片段:

go
cpuNumber, _ := cpu.Counts(true) println("CPU逻辑数量: ", cpuNumber) cpuNumber, _ = cpu.Counts(false) println("CPU物理核心: ", cpuNumber)

运行结果:

CPU逻辑数量: 8 CPU物理核心: 4

2.1.3 CPU使用率

代码片段:

go
cpuPercent, _ := cpu.Percent(time.Second, false) println("CPU使用率: ", cpuPercent[0])

运行结果:

CPU使用率: +1.523438e+001

2.2 DISK

2.2.1 分区信息

代码片段:

go
diskPart, _ := 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%

2.2.2 IO计数

代码片段:

go
counters, _ := 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": "" }

2.3 HOST

2.3.1 主机信息

代码片段:

go
info, _ := 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

2.4 LOAD

2.4.1 负载

代码片段:

go
avg, _ := 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 }

2.5 MEM

2.5.1 虚拟内存

代码片段:

go
virtualMemory, _ := 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 }

2.5.2 交换内存

代码片段:

go
swapMemory, _ := 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 }

2.6 NET

2.6.1 网络计数

代码片段:

go
counters, _ := 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 } ]

2.6.2 网络连接

代码片段:

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 } ]

2.7 PROCESS

2.7.1 进程列表

代码片段:

go
processes, _ := 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
如果对你有用的话,可以打赏哦
打赏
ali pay
wechat pay

本文作者:蒋固金

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!