gopsutil
-
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信息
{
"cpu": 0,
"vendorId": "GenuineIntel",
"family": "205",
"model": "",
"stepping": 0,
"physicalId": "BFEBFBFF000906EA",
"coreId": "",
"cores": 8,
"modelName": "Intel(R) Core(TM) i5-8300H CPU @ 2.30GHz",
"mhz": 2304,
"cacheSize": 0,
"flags": [],
"microcode": ""
}cpu使用率
cpu percent:[9.807692307692308]
cpu percent:[8.846153846153847]
cpu percent:[14.836223506743737]
cpu percent:[6.0546875]
cpu percent:[7.6923076923076925]
cpu percent:[9.375]
cpu percent:[4.807692307692308]
cpu percent:[5.078125]获取CPU负载信息:
import "github.com/shirou/gopsutil/load" // 获取CPU负载信息 func getCpuLoad() { info, _ := load.Avg() fmt.Printf("%v\n", info) }
{"load1":0,"load5":0,"load15":0}
Memory
import "github.com/shirou/gopsutil/mem" // mem info func getMemInfo() { memInfo, _ := mem.VirtualMemory() fmt.Printf("mem info:%v\n", memInfo) }
{
"total": 17033486336,
"available": 5740814336,
"used": 11292672000,
"usedPercent": 66,
"free": 5740814336,
"active": 0,
"inactive": 0,
"wired": 0,
"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
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) }
host info:{"hostname":"DESKTOP-TIJ4BO9","uptime":292327,"bootTime":1631545932,"procs":283,"os":"windows","platform":"Microsoft Windows 10 Home China","platformFamily":"Standalone Workstation","platformVersion":"10.0.19042 Build 19042","kernelVersion":"10.0.19042 Build 19042","kernelArch":"x86_64","virtualizationSystem":"","virtualizationRole":"","hostid":"b2ac9e04-311f-4b55-a4e1-e493042934ef"} uptime:292327 boottime:1631545932
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" // net info 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() }