简介
psutil
是一个跨平台进程和系统监控的Python库,而gopsutil
是其Go语言版本的实现。
Go语言部署简单、性能好的特点非常适合做一些诸如采集系统信息和监控的服务,本文介绍的gopsutil库是知名Python库:psutil的一个Go语言版本的实现。
特点
gopsutils为我们屏蔽了各个系统之间的差异, 具有非常强悍的可移植性, 有了gopsutil, 我们不再需要针对不同的系统使用syscall调用对应的系统方法, 更棒的是gopsutil的实现中没有任何cgo的代码, 使得交叉编译成为可能.
安装
go get github.com/shirou/gopsutil
功能
gopsutil将不同的功能划分到不同的子包中
/*
cpu: CPU相关
disk: 磁盘相关
docker: docker相关
host: 主机相关
mem: 内存相关
net: 网络相关
process: 进程相关
*/
CPU
采集CPU相关信息
package main
import (
"fmt"
"github.com/shirou/gopsutil/cpu"
"time"
)
// 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)
}
}
func main() {
getCpuInfo()
}
go run ps.go
{"cpu":0,"vendorId":"GenuineIntel","family":"6","model":"142","stepping":10,"physicalId":"","coreId":"","cores":4,"modelName":"Intel(R) Core(TM) i5-8257U CPU @ 1.40GHz","mhz":1400,"cacheSize":256,"flags":["fpu","vme","de","pse","tsc","msr","pae","mce","cx8","apic","sep","mtrr","pge","mca","cmov","pat","pse36","clfsh","ds","acpi","mmx","fxsr","sse","sse2","ss","htt","tm","pbe","sse3","pclmulqdq","dtes64","mon","dscpl","vmx","est","tm2","ssse3","fma","cx16","tpr","pdcm","sse4.1","sse4.2","x2apic","movbe","popcnt","aes","pcid","xsave","osxsave","seglim64","tsctmr","avx1.0","rdrand","f16c","rdwrfsgs","tsc_thread_offset","sgx","bmi1","avx2","smep","bmi2","erms","invpcid","fpu_csds","mpx","rdseed","adx","smap","clfsopt","ipt","mdclear","tsxfa","ibrs","stibp","l1df","ssbd","syscall","xd","1gbpage","em64t","lahf","lzcnt","prefetchw","rdtscp","tsci"],"microcode":""}
cpu percent:[10.099750623570355]
cpu percent:[7.615480649005255]
获取CPU负载消息
package main
import (
"fmt"
"github.com/shirou/gopsutil/load"
)
func getCpuLoad() {
info, _ := load.Avg()
fmt.Printf("%v\n",info)
}
func main() {
getCpuLoad()
}
go run ps.go
{"load1":2.591796875,"load5":2.86572265625,"load15":2.7578125}
**
Memory
package main
import (
"fmt"
"github.com/shirou/gopsutil/mem"
)
func getMemInfo() {
memInfo, _ := mem.VirtualMemory()
fmt.Printf("mem info:%v\n",memInfo)
}
func main() {
getMemInfo()
}
go run ps.go
mem info:{"total":8589934592,"available":1984147456,"used":6605787136,"usedPercent":76.9014835357666,"free":122396672,"active":1884549120,"inactive":1861750784,"wired":2657144832,"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,"hugepagesize":0}
Host
package main
import (
"fmt"
"github.com/shirou/gopsutil/host"
)
func getHostInfo() {
hInfo, _ := host.Info()
fmt.Printf("host info:%v uptime:%v boottime:%v\n", hInfo, hInfo.Uptime, hInfo.BootTime)
}
func main() {
getHostInfo()
}
go run ps.go
host info:{"hostname":"youmendeMacBook-Pro.local","uptime":314216,"bootTime":1609206164,"procs":525,"os":"darwin","platform":"darwin","platformFamily":"Standalone Workstation","platformVersion":"11.1","kernelVersion":"20.2.0","kernelArch":"x86_64","virtualizationSystem":"","virtualizationRole":"","hostid":"82e2050c-5936-3d24-ad3b-ec4ec5c09e11"} uptime:314216 boottime:1609206164
Disk
package main
import (
"fmt"
"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)
}
}
func main() {
getDiskInfo()
}
go run ps.go
part:{"device":"/dev/disk1s2s1","mountpoint":"/","fstype":"apfs","opts":"ro,journaled,multilabel"}
disk info:used:21.590916437596942 free:54644285440
part:{"device":"devfs","mountpoint":"/dev","fstype":"devfs","opts":"rw,nobrowse,multilabel"}
disk info:used:100 free:0
part:{"device":"/dev/disk1s5","mountpoint":"/System/Volumes/VM","fstype":"apfs","opts":"rw,noexec,nobrowse,journaled,multilabel,noatime"}
disk info:used:3.781419239013729 free:54644285440
part:{"device":"/dev/disk1s3","mountpoint":"/System/Volumes/Preboot","fstype":"apfs","opts":"rw,nobrowse,journaled,multilabel"}
disk info:used:0.5894192171529189 free:54644285440
part:{"device":"/dev/disk1s6","mountpoint":"/System/Volumes/Update","fstype":"apfs","opts":"rw,nobrowse,journaled,multilabel"}
disk info:used:0.0014916323547500506 free:54644285440
part:{"device":"/dev/disk1s1","mountpoint":"/System/Volumes/Data","fstype":"apfs","opts":"rw,nobrowse,journaled,multilabel"}
disk info:used:76.48839834497826 free:54644285440
part:{"device":"map auto_home","mountpoint":"/System/Volumes/Data/home","fstype":"autofs","opts":"rw,nobrowse,automounted,multilabel"}
disk info:used:0 free:0
disk0:{"readCount":7171236,"mergedReadCount":0,"writeCount":3480390,"mergedWriteCount":0,"readBytes":157156835328,"writeBytes":130453229568,"readTime":6791329,"writeTime":2234950,"iopsInProgress":0,"ioTime":9026279,"weightedIO":0,"name":"disk0","serialNumber":"","label":""}
IO
package main
import (
"fmt"
"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)
}
}
func main() {
getNetInfo()
}
go run ps.go
0:{"name":"lo0","bytesSent":121344185,"bytesRecv":121344185,"packetsSent":433070,"packetsRecv":433070,"errin":0,"errout":0,"dropin":0,"dropout":0,"fifoin":0,"fifoout":0} send:121344185 recv:121344185
1:{"name":"gif0","bytesSent":0,"bytesRecv":0,"packetsSent":0,"packetsRecv":0,"errin":0,"errout":0,"dropin":0,"dropout":0,"fifoin":0,"fifoout":0} send:0 recv:0
2:{"name":"stf0","bytesSent":0,"bytesRecv":0,"packetsSent":0,"packetsRecv":0,"errin":0,"errout":0,"dropin":0,"dropout":0,"fifoin":0,"fifoout":0} send:0 recv:0
3:{"name":"en3","bytesSent":14748522,"bytesRecv":8111958,"packetsSent":61122,"packetsRecv":61250,"errin":0,"errout":1394,"dropin":0,"dropout":369,"fifoin":0,"fifoout":0} send:14748522 recv:8111958
4:{"name":"ap1","bytesSent":0,"bytesRecv":0,"packetsSent":0,"packetsRecv":0,"errin":0,"errout":0,"dropin":0,"dropout":0,"fifoin":0,"fifoout":0} send:0 recv:0
5:{"name":"en0","bytesSent":2744132038,"bytesRecv":12672282198,"packetsSent":7843906,"packetsRecv":9697152,"errin":0,"errout":1194,"dropin":0,"dropout":666,"fifoin":0,"fifoout":0} send:2744132038 recv:12672282198
6:{"name":"en1","bytesSent":0,"bytesRecv":0,"packetsSent":0,"packetsRecv":0,"errin":0,"errout":0,"dropin":0,"dropout":0,"fifoin":0,"fifoout":0} send:0 recv:0
7:{"name":"en2","bytesSent":0,"bytesRecv":0,"packetsSent":0,"packetsRecv":0,"errin":0,"errout":0,"dropin":0,"dropout":0,"fifoin":0,"fifoout":0} send:0 recv:0
8:{"name":"awdl0","bytesSent":241402,"bytesRecv":0,"packetsSent":1961,"packetsRecv":0,"errin":0,"errout":0,"dropin":0,"dropout":0,"fifoin":0,"fifoout":0} send:241402 recv:0
9:{"name":"llw0","bytesSent":0,"bytesRecv":0,"packetsSent":0,"packetsRecv":0,"errin":0,"errout":0,"dropin":0,"dropout":0,"fifoin":0,"fifoout":0} send:0 recv:0
10:{"name":"utun0","bytesSent":187730,"bytesRecv":0,"packetsSent":1629,"packetsRecv":0,"errin":0,"errout":0,"dropin":0,"dropout":0,"fifoin":0,"fifoout":0} send:187730 recv:0
11:{"name":"utun1","bytesSent":187730,"bytesRecv":0,"packetsSent":1629,"packetsRecv":0,"errin":0,"errout":0,"dropin":0,"dropout":0,"fifoin":0,"fifoout":0} send:187730 recv:0
12:{"name":"en6","bytesSent":20906,"bytesRecv":6600,"packetsSent":149,"packetsRecv":60,"errin":0,"errout":0,"dropin":0,"dropout":0,"fifoin":0,"fifoout":0} send:20906 recv:6600
13:{"name":"en7","bytesSent":20906,"bytesRecv":6600,"packetsSent":149,"packetsRecv":60,"errin":0,"errout":0,"dropin":0,"dropout":0,"fifoin":0,"fifoout":0} send:20906 recv:6600
14:{"name":"bridg","bytesSent":0,"bytesRecv":0,"packetsSent":0,"packetsRecv":0,"errin":0,"errout":0,"dropin":0,"dropout":0,"fifoin":0,"fifoout":0} send:0 recv:0
15:{"name":"bridg","bytesSent":41812,"bytesRecv":12360,"packetsSent":298,"packetsRecv":120,"errin":0,"errout":0,"dropin":0,"dropout":0,"fifoin":0,"fifoout":0} send:41812 recv:12360
16:{"name":"bridg","bytesSent":41812,"bytesRecv":12360,"packetsSent":298,"packetsRecv":120,"errin":0,"errout":0,"dropin":0,"dropout":0,"fifoin":0,"fifoout":0} send:41812 recv:12360