行业知识
【Golang】gopsutil包常用监控资源信息API
【Golang】gopsutil包常用监控资源信息API
大家好 我是寸铁
总结了一篇【Golang】gopsutil包常用监控资源信息API✨
喜欢的小伙伴可以点点关注
前言
本文涵盖了最常用的系统资源监控函数,可以直接拿函数进行调用,非常方便
- 先安装gopsutil包
go get -u `github.com/shirou/gopsutil`
- 或者导入后用go mod引入
go mod tidy
CPU
获取Cpu相关的全部信息
// 获取Cpu相关的全部信息func getCpuAllInfo() {cpuInfo, _ := cpu.Info()fmt.Println("返回cpu的具体信息:", cpuInfo)}返回cpu执行不同操作所花费的时间
// 返回cpu执行不同操作所花费的时间func getCpuCountsTimes() {cpuCounts, _ := cpu.Times(false)fmt.Println("返回cpu执行不同操作所花费的时间 工作种类:", cpuCounts)//TimesStat 包含 CPU 执行不同操作所花费的时间 工作种类。时间单位以秒为单位。它基于 linux /proc/stat 文件。}返回CPU的使用率
// 返回CPU的使用率func getCpuPercent() {cpuPercent, _ := cpu.Percent(time.Second, false)fmt.Println("返回cpu的使用率:", cpuPercent)}内存
获取物理内存和交换区内存信息
// 获取物理内存和交换区内存信息func getVirtualMemory() {m1, _ := mem.VirtualMemory()fmt.Println("返回虚拟内存的信息:", m1)}返回交换内存的信息
// 返回交换内存的信息func getSwapMemory() {m2, _ := mem.SwapMemory()fmt.Println("返回交换内存的信息:", m2)}返回交换设备的信息
// 返回交换设备的信息func getSwapDevices() {devices, _ := mem.SwapDevices()fmt.Println("返回交换设备的信息:", devices)}磁盘
返回有关文件系统挂载点的信息
// 返回有关文件系统挂载点的信息func getDiskPartitions() {//可以通过psutil获取磁盘分区、磁盘使用率和磁盘IO信息d1, _ := disk.Partitions(true) //所有分区fmt.Println("有关文件系统挂载点的信息:", d1)}返回指定某路径的硬盘使用情况
// 返回指定某路径的硬盘使用情况func getDiskUsage(path string) {d2, _ := disk.Usage(path) //指定某路径的硬盘使用情况fmt.Println("指定E硬盘使用情况:", d2)}返回关于磁盘分区的输入/输出(I/O)统计信息
// 返回关于磁盘分区的输入/输出(I/O)统计信息func getDiskIOCounters() {d3, _ := disk.IOCounters() //所有硬盘的io信息fmt.Println("关于磁盘分区的输入/输出(I/O)统计信息:", d3)}文件
返回文件压缩情况
// 返回文件压缩情况func getFileFileCompression() {compression := disk.FileFileCompressionfmt.Println("返回文件压缩情况: ", compression)}返回只读文件卷的数量
// 返回只读文件卷的数量func getFileReadOnlyVolume() {volume := disk.FileReadOnlyVolumefmt.Println("返回只读文件卷的数量: ", volume)}网络
返回当前网络连接信息
// 返回当前网络连接信息func getNetConnections(kind string) {//获取当前网络连接信息n1, _ := net.Connections(kind) //可填入tcp、udp、tcp4、udp4等等fmt.Println("获取当前网络连接信息: ", n1)}获取网络读写字节/包的个数
// 获取网络读写字节/包的个数func getNetIOCounters() {//获取网络读写字节/包的个数n2, _ := net.IOCounters(false)fmt.Println("获取网络读写字节/包的个数: ", n2)}返回网络过滤器计数器
// 返回网络过滤器计数器func getNetFilterCounters() {n3, _ := net.FilterCounters()fmt.Println("过滤器计数器: ", n3)}返回网络的接口信息
// 返回网络的接口信息func getNetInterfaces() {interfaces, _ := net.Interfaces()fmt.Println("网络的接口信息: ", interfaces)}返回本机信息的详细信息
// 返回本机信息的详细信息func getHostInfo() {h, _ := host.Info()fmt.Println("本机信息: ", h)}demo
package mainimport ("fmt""github.com/shirou/gopsutil/cpu""github.com/shirou/gopsutil/disk""github.com/shirou/gopsutil/host""github.com/shirou/gopsutil/mem""github.com/shirou/gopsutil/net""time")func main() {fmt.Println("获取Cpu相关信息")//获取Cpu相关信息getCpuAllInfo()getCpuCountsTimes()getCpuPercent()fmt.Println("------------------------------------")fmt.Println("获取内存相关信息")//获取内存相关信息getVirtualMemory()getSwapMemory()getSwapDevices()fmt.Println("------------------------------------")fmt.Println("获取磁盘相关信息")//获取磁盘相关信息getDiskPartitions()//要查看磁盘信息的路径path := "E:"getDiskUsage(path)getDiskIOCounters()getFileFileCompression()getFileReadOnlyVolume()fmt.Println("------------------------------------")fmt.Println("获取网络相关信息")//获取当前网络连接信息kind := "tcp" //可填入tcp、udp、tcp4、udp4等等getNetConnections(kind)getNetIOCounters()getNetFilterCounters()getNetInterfaces()fmt.Println("------------------------------------")fmt.Println("获取主机相关信息")//获取主机相关信息getHostInfo()fmt.Println("------------------------------------")}// 获取Cpu相关的全部信息func getCpuAllInfo() {cpuInfo, _ := cpu.Info()fmt.Println("返回cpu的具体信息:", cpuInfo)}// 返回cpu执行不同操作所花费的时间func getCpuCountsTimes() {cpuCounts, _ := cpu.Times(false)fmt.Println("返回cpu执行不同操作所花费的时间 工作种类:", cpuCounts)//TimesStat 包含 CPU 执行不同操作所花费的时间 工作种类。时间单位以秒为单位。它基于 linux /proc/stat 文件。}// 返回CPU的使用率func getCpuPercent() {cpuPercent, _ := cpu.Percent(time.Second, false)fmt.Println("返回cpu的使用率:", cpuPercent)}// 获取物理内存和交换区内存信息func getVirtualMemory() {m1, _ := mem.VirtualMemory()fmt.Println("返回虚拟内存的信息:", m1)}// 返回交换内存的信息func getSwapMemory() {m2, _ := mem.SwapMemory()fmt.Println("返回交换内存的信息:", m2)}// 返回交换设备的信息func getSwapDevices() {devices, _ := mem.SwapDevices()fmt.Println("返回交换设备的信息:", devices)}// 返回有关文件系统挂载点的信息func getDiskPartitions() {//可以通过psutil获取磁盘分区、磁盘使用率和磁盘IO信息d1, _ := disk.Partitions(true) //所有分区fmt.Println("有关文件系统挂载点的信息:", d1)}// 返回指定某路径的硬盘使用情况func getDiskUsage(path string) {d2, _ := disk.Usage(path) //指定某路径的硬盘使用情况fmt.Println("指定E硬盘使用情况:", d2)}// 返回关于磁盘分区的输入/输出(I/O)统计信息func getDiskIOCounters() {d3, _ := disk.IOCounters() //所有硬盘的io信息fmt.Println("关于磁盘分区的输入/输出(I/O)统计信息:", d3)}// 返回文件压缩情况func getFileFileCompression() {compression := disk.FileFileCompressionfmt.Println("返回文件压缩情况: ", compression)}// 返回只读文件卷的数量func getFileReadOnlyVolume() {volume := disk.FileReadOnlyVolumefmt.Println("返回只读文件卷的数量: ", volume)}// 返回当前网络连接信息func getNetConnections(kind string) {//获取当前网络连接信息n1, _ := net.Connections(kind) //可填入tcp、udp、tcp4、udp4等等fmt.Println("获取当前网络连接信息: ", n1)}// 获取网络读写字节/包的个数func getNetIOCounters() {//获取网络读写字节/包的个数n2, _ := net.IOCounters(false)fmt.Println("获取网络读写字节/包的个数: ", n2)}// 返回网络过滤器计数器func getNetFilterCounters() {n3, _ := net.FilterCounters()fmt.Println("过滤器计数器: ", n3)}// 返回网络的接口信息func getNetInterfaces() {interfaces, _ := net.Interfaces()fmt.Println("网络的接口信息: ", interfaces)}// 返回本机信息的详细信息func getHostInfo() {h, _ := host.Info()fmt.Println("本机信息: ", h)}看到这里的小伙伴,恭喜你又掌握了一个技能
希望大家能取得胜利,坚持就是胜利
我是寸铁!我们下期再见
往期好文
保姆级教程
【保姆级教程】Windows11下go-zero的etcd安装与初步使用
【保姆级教程】Windows11安装go-zero代码生成工具goctl、protoc、go-zero
【Go-Zero】手把手带你在goland中创建api文件并设置高亮
报错解决
【Go-Zero】Error: user.api 27:9 syntax error: expected ‘:‘ | ‘IDENT‘ | ‘INT‘, got ‘(‘ 报错解决方案及api路由注意事项
【Go-Zero】Error: only one service expected goctl一键转换生成rpc服务错误解决方案
【Go-Zero】【error】 failed to initialize database, got error Error 1045 (28000):报错解决方案
【Go-Zero】Error 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)报错解决方案
【Go-Zero】type mismatch for field “Auth.AccessSecret“, expect “string“, actual “number“报错解决方案
【Go-Zero】Error: user.api 30:2 syntax error: expected ‘)‘ | ‘KEY‘, got ‘IDENT‘报错解决方案
【Go-Zero】Windows启动rpc服务报错panic:context deadline exceeded解决方案
Go面试向
【Go面试向】defer与time.sleep初探
【Go面试向】defer与return的执行顺序初探
【Go面试向】Go程序的执行顺序
【Go面试向】rune和byte类型的认识与使用
【Go面试向】实现map稳定的有序遍历的方式
- 或者导入后用go mod引入