上面的方式能夠完成需求,但是我們大不必重複造輪子,因為已經有相當完善的三方庫為我們實現了這些採集需求,它就是gopsutil。
psutil (process and system utilities,)是一個跨平台庫,用於在Python 中獲取進程和系統利用率(CPU、記憶體、磁碟、網路、感測器)的訊息,而gopsutil 是它的Go 語言版本。
gopsutil 為我們屏蔽了各系統差異,具有良好的移植性。
已支援清單
##部分支援清單
CPU on DragonFly BSD
##host on Linux RISC- V
另外,該專案透過將C 結構移植到Go 結構,它的實作中沒有cgo 的程式碼,這樣就更有利於交叉編譯了。
使用
gopsutil 現有v3 和v2 版本,且沒有向後相容性保證,因此有兩種使用方式
import (
// "github.com/shirou/gopsutil/v3/mem" // to use v3
"github.com/shirou/gopsutil/mem"
)
package main
import (
"fmt"
"github.com/shirou/gopsutil/v3/mem"
// "github.com/shirou/gopsutil/mem" // to use v2
)
func main() {
v, _ := mem.VirtualMemory()
// almost every return value is a struct
fmt.Printf("Total: %v, Free:%v, UsedPercent:%f%%\n", v.Total, v.Free, v.UsedPercent)
// convert to JSON. String() is also implemented
fmt.Println(v)
}
Total: 8589934592, Free:138248192, UsedPercent:76.416254% {"total":8589934592,"available":2025828352,"used":6564106240,"usedPercent":76.4162540435791,"free":138248192,"active":1949327360,"inactive":1887580160,"wired":2214510592,"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}### ###gopsutil 套件有一點比較友好的是,絕大多數的採集函數返回的都是一個結構體對象,它們都實現了 fmt.Stringer 接口,因此在打印時它們將會以json 格式進行輸出。 ############例如,上例中 mem.VirtualMemory 傳回的是 VirtualMemoryStat 結構體,它在 String() 方法中呼叫了 json.Marshal() 函數。 ######
type VirtualMemoryStat struct { Total uint64 `json:"total"` Available uint64 `json:"available"` Used uint64 `json:"used"` UsedPercent float64 `json:"usedPercent"` Free uint64 `json:"free"` Active uint64 `json:"active"` Inactive uint64 `json:"inactive"` Wired uint64 `json:"wired"` func (m VirtualMemoryStat) String() string { s, _ := json.Marshal(m) return string(s) }######gopsutil 透過不同採集單元劃分為不同的子包,在使用中引入不同的子包,即可呼叫相關的方法。 ######
import ( "github.com/shirou/gopsutil/v3/mem" "github.com/shirou/gopsutil/v3/cpu" "github.com/shirou/gopsutil/v3/disk" "github.com/shirou/gopsutil/v3/docker" "github.com/shirou/gopsutil/v3/host" "github.com/shirou/gopsutil/v3/internal" "github.com/shirou/gopsutil/v3/load" "github.com/shirou/gopsutil/v3/mem" "github.com/shirou/gopsutil/v3/net" "github.com/shirou/gopsutil/v3/process" "github.com/shirou/gopsutil/v3/winservices" )######例如我們想要獲取主機信息,就需要引入 github.com/shirou/gopsutil/v3/host 子包######
import ( "fmt" "github.com/shirou/gopsutil/v3/host" ) func main() { hostInfo, _ := host.Info() fmt.Println(hostInfo) }
输出
{"hostname":"MacBook-Pro.local","uptime":1619284,"bootTime":1644332729,"procs":301,"os":"darwin","platform":"darwin","platformFamily":"Standalone Workstation","platformVersion":"10.15.5","kernelVersion":"19.5.0","kernelArch":"x86_64","virtualizationSystem":"","virtualizationRole":"","hostId":"7a1a74f2-30fc-4cc1-b439-6b7aef22e45d"}
gopsutil 库有非常全面的覆盖单元,包括主机、磁盘、内存、CPU、网络、进程、docker等模块,它能很好地帮助我们获取系统信息。并且 gopsutil 处理了跨平台兼容性问题,对外接口基本保持一致,使用起来比较友好。
以上是還在自己寫 Go 系統監控函數嗎的詳細內容。更多資訊請關注PHP中文網其他相關文章!