当前位置:网站首页>golang gopsutil库的使用:进程和系统资源监控(CPU 内存 磁盘等)
golang gopsutil库的使用:进程和系统资源监控(CPU 内存 磁盘等)
2022-06-29 15:36:00 【学亮编程手记】
golang gopsutil库的使用:进程和系统资源监控(CPU 内存 磁盘等)
| Golang
psutil是一个跨平台进程和系统监控的Python库,而gopsutil是其Go语言版本的实现。本文介绍了它的基本使用。
Go语言部署简单、性能好的特点非常适合做一些诸如采集系统信息和监控的服务,本文介绍的gopsutil库是知名Python库:psutil的一个Go语言版本的实现。
安装
go get github.com/shirou/gopsutil
使用
CPU
采集CPU相关信息。
import "github.com/shirou/gopsutil/cpu"
// cpu info
func getCpuInfo() {
cpuInfos, err := cpu.Info()
if err != nil {
fmt.Printf("get cpu info failed, err:%v", err)
}
for _, ci := range cpuInfos {
fmt.Println(ci)
}
// CPU使用率
for {
percent, _ := cpu.Percent(time.Second, false)
fmt.Printf("cpu percent:%v\n", percent)
}
}
获取CPU负载信息:
import "github.com/shirou/gopsutil/load"
func getCpuLoad() {
info, _ := load.Avg()
fmt.Printf("%v\n", info)
}
Memory
import "github.com/shirou/gopsutil/mem"
// mem info
func getMemInfo() {
memInfo, _ := mem.VirtualMemory()
fmt.Printf("mem info:%v\n", memInfo)
}
Host
import "github.com/shirou/gopsutil/host"
// host info
func getHostInfo() {
hInfo, _ := host.Info()
fmt.Printf("host info:%v uptime:%v boottime:%v\n", hInfo, hInfo.Uptime, hInfo.BootTime)
}
Disk
import "github.com/shirou/gopsutil/disk"
// disk info
func getDiskInfo() {
parts, err := disk.Partitions(true)
if err != nil {
fmt.Printf("get Partitions failed, err:%v\n", err)
return
}
for _, part := range parts {
fmt.Printf("part:%v\n", part.String())
diskInfo, _ := disk.Usage(part.Mountpoint)
fmt.Printf("disk info:used:%v free:%v\n", diskInfo.UsedPercent, diskInfo.Free)
}
ioStat, _ := disk.IOCounters()
for k, v := range ioStat {
fmt.Printf("%v:%v\n", k, v)
}
}
net IO
import "github.com/shirou/gopsutil/net"
func getNetInfo() {
info, _ := net.IOCounters(true)
for index, v := range info {
fmt.Printf("%v:%v send:%v recv:%v\n", index, v, v.BytesSent, v.BytesRecv)
}
}
net
获取本机IP的两种方式
func GetLocalIP() (ip string, err error) {
addrs, err := net.InterfaceAddrs()
if err != nil {
return
}
for _, addr := range addrs {
ipAddr, ok := addr.(*net.IPNet)
if !ok {
continue
}
if ipAddr.IP.IsLoopback() {
continue
}
if !ipAddr.IP.IsGlobalUnicast() {
continue
}
return ipAddr.IP.String(), nil
}
return
}
或:
// Get preferred outbound ip of this machine
func GetOutboundIP() string {
conn, err := net.Dial("udp", "8.8.8.8:80")
if err != nil {
log.Fatal(err)
}
defer conn.Close()
localAddr := conn.LocalAddr().(*net.UDPAddr)
fmt.Println(localAddr.String())
return localAddr.IP.String()
}
参考链接:https://www.liwenzhou.com/posts/Go/go_gopsutil/
边栏推荐
- C language homework - matching system
- 从第三次技术革命看企业应用三大开发趋势
- kotlin 注解声明与使用
- Is there any lack of dependence? An error is reported when flinksql is packaged and running, but there is no problem when the local idea runs. Solve it. Thanks
- Andorid Jetpack Hilt
- Taro 小程序开启wxml代码压缩
- el-table-column行按钮防重控制loading
- 如何用好数据科学?
- C#学习一:值类型与引用类型
- Interviewer: tell me about the MySQL transaction isolation level?
猜你喜欢

Interviewer: tell me about the MySQL transaction isolation level?

发明了杀毒软件之后,他选择做一个极品混混

架构实战营模块五作业

Leetcode-64- minimum path sum

Google 软件版本经历周期

cmake学习-2

STM32与GD32笔记

Stlink troubleshooting
[data analysis] five common questions about learning SQL?

What are the advantages of intelligent chat robots? Senior independent station sellers tell you!
随机推荐
Stlink troubleshooting
Volcano engine was selected into the first "panorama of edge computing industry" in China
Why MySQL chooses b+ tree to store indexes
动作捕捉系统用于苹果采摘机器人
2022-06-29日报: 李飞飞划重点的「具身智能」,走到哪一步了?
2022年第一季度保险服务数字化跟踪分析
商业智能BI与业务管理决策思维之三:业务质量分析
Three development trends of enterprise application viewed from the third technological revolution
Alibaba cloud experience Award: use polardb-x and Flink to build a large real-time data screen
About sql+nosql: newsql database
Mysql database naming conventions PDF
LeetCode-64-最小路径和
面试官:说一下MySQL事务隔离级别?
Training mode of deep learning network
Polarimetric SAR surface classification
华为云AOM 2.0版本发布
Summary of recent work
Is there any lack of dependence? An error is reported when flinksql is packaged and running, but there is no problem when the local idea runs. Solve it. Thanks
mysql XA 分布式事务
ROS2机器人f1tenth之CLI工具基础