当前位置:网站首页>Use of golang gopsutil Library: process and system resource monitoring (CPU, memory, disk, etc.)
Use of golang gopsutil Library: process and system resource monitoring (CPU, memory, disk, etc.)
2022-06-29 21:35:00 【Learn programming notes】
golang gopsutil Library usage : Process and system resource monitoring (CPU Memory Disks, etc )
| Golang
psutil Is a cross platform process and system monitoring Python library , and gopsutil It is its Go Implementation of language version . This paper introduces its basic use .
Go Language deployment is simple 、 Good performance is very suitable for some services such as collecting system information and monitoring , This paper introduces gopsutil The library is well known Python library :psutil One of the Go Implementation of language version .
install
go get github.com/shirou/gopsutil
Use
CPU
collection CPU Related information .
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 Usage rate
for {
percent, _ := cpu.Percent(time.Second, false)
fmt.Printf("cpu percent:%v\n", percent)
}
}
obtain CPU Load information :
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
Get local IP Two ways
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
}
or :
// 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()
}
Reference link :https://www.liwenzhou.com/posts/Go/go_gopsutil/
边栏推荐
- 阿里巴巴商品详情API接口(item_get-获得商品详情接口),阿里巴巴API接口
- Verilog implements DDS waveform generator module, which can realize adjustable frequency and phase, three waveforms
- Jump to open a new window
- Knowledge distilling learning notes
- Win10 add SSH public key
- Amazon Keyword Search API interface (item_search- Amazon product search interface by keyword), Amazon API interface
- STM32最小系统搭建(原理图)
- Explain PBR texture maps
- 一次 Keepalived 高可用的事故,让我重学了一遍它!
- Alibaba keyword search commodity API interface (item_search- commodity search interface by keyword), Alibaba Search API interface
猜你喜欢

Threejs basic introduction

Star ring technology data security management platform defender heavy release

报表交付工程师

Application of VoIP push in overseas audio and video services
![[advanced ROS chapter] Lecture 4: duplicate names in ROS (nodes, topics and parameters)](/img/25/85e8c55605f5cc999a8e85f0a05f93.jpg)
[advanced ROS chapter] Lecture 4: duplicate names in ROS (nodes, topics and parameters)

leetcode:370. 区间加法

leetcode:307. Area and retrieval - array modifiable

数字密码锁verilog设计+仿真+上板验证

习近平在湖北武汉考察时强调 把科技的命脉牢牢掌握在自己手中 不断提升我国发展独立性自主性安全性

广东二级造价工程师《造价管理》真题解析
随机推荐
BUAA OO unit 4 HW16 unit 4 Summary and course review
STL tutorial 6-deque, stack, queue, list container
Analysis of the factors affecting the transmission signal of the conductive slip ring
leetcode:370. 区间加法
双目立体视觉摄像头的标定、矫正、世界坐标计算(opencv)
GoAhead WebServer移植
How do new shareholders open accounts online? Is it safe to open an account online?
Varnish 503 no backend connection – varnish health check
leetcode:370. Interval addition
Vipshop Keyword Search API interface (item_search- search vipshop commodity API interface by keyword), vipshop API interface
CORDIC based Signal Processor desgn
【ROS进阶篇】第四讲 ROS中的重名问题(节点、话题与参数)
导航【微机原理】
How can colleges and universities build future oriented smart campus based on cloud native? Full stack cloud native vs traditional technology architecture
我的创作纪念日
[advanced ROS chapter] lesson 2 custom header and source file encapsulation
Recruit | DBA Data Engineer every week with an annual salary of 35+. Dream of Kyushu and bright stars!
VHDL电话计费系统设计
Is it safe to open a securities account in Caixue school?
HAproxy + Keepalive实现LDAP代理服务