当前位置:网站首页>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/
边栏推荐
- 智能聊天机器人的优势在哪里?资深独立站卖家告诉你!
- Three development trends of enterprise application viewed from the third technological revolution
- taro3.*中使用 dva 入门级别的哦
- 11. application layer data transmission format / port number -bite
- 89.(cesium篇)cesium聚合图(自定义图片)
- 分页sql(rownum、row_number、dense_rank、rank)
- Stlink troubleshooting
- GWD: rotating target detection based on Gaussian Wasserstein distance | ICML 2021
- 关于 国产麒麟系统运行Qt,在命令行可以运行而双击无法运行(无反应) 的解决方法
- Alibaba cloud experience Award: use polardb-x and Flink to build a large real-time data screen
猜你喜欢

京东联盟API - 万能转链接口 - 京品库接口 - 接口定制

CVPR 2022 | greatly reduce the manual annotation required for zero sample learning. Mapuosuo and Beiyou proposed category semantic embedding rich in visual information

It is expected to significantly improve the computational performance of integrated photonic circuits. The Tsinghua team proposed a diffraction pattern neural network framework

Pre war minesweeping: five measures for vulnerability management

EasyGBS调用获取实时快照接口时,出现白色方块该如何解决?

《网络是怎么样连接的》读书笔记 - WEB服务端请求和响应(五)

File常用工具类, 流相关运用 (记录)

Business Intelligence BI and business management decision-making thinking No. 3: business quality analysis

Google 软件版本经历周期

发明了杀毒软件之后,他选择做一个极品混混
随机推荐
taro3.*中使用 dva 入门级别的哦
three. JS and Gaode map are combined to introduce obj format model - effect demonstration
微信公告号自动回复使用图灵机器人实现智能回复
Development and application of NFT chain Games: Six noteworthy NFT trends in 2022
DataKit 作为本地获取数据的 API 服务器
小程序判断数据为不为空
CVPR 2022 | 大幅减少零样本学习所需的人工标注,马普所和北邮提出富含视觉信息的类别语义嵌入
Swoole TCP distributed implementation
Introduction to radar antenna
wallys/m.2/Adapter card(one pcie1x to 4 x Mini PCIE)
Polarimetric SAR surface classification
关于 国产麒麟系统运行Qt,在命令行可以运行而双击无法运行(无反应) 的解决方法
Leetcode-234-palindrome linked list
2022 OpenVINO DevCon 大揭秘!英特尔携众多合作伙伴深化开发者生态建设,释放AI产业创新潜能
微博评论高可用高性能计算架构
C language homework - matching system
Kotlin annotation Statement and use
SSL V**技术原理
el-table-column行按钮防重控制loading
【力扣10天SQL入门】Day7+8 计算函数