Go 系統監控利器-gopsutil
Go 系統監控利器-gopsutil
一、簡介
什麼是 gopsutil?
要說gopsutil就不得不先了解 psutil,顧名思義,psutil = process and system utilities, 它是 Python 的跨平臺庫,能夠輕鬆實現獲取系統執行的程式和系統利用率,包括 CPU、記憶體、磁碟、網路等資訊。 而gopsutil就是psutil的 Golang 移植版。
為什麼用 gopsutil?
和直接使用 syscall 呼叫對應的系統方法相比,gopsutil 為我們遮蔽了各個系統間的差異,可移植性非常強。
二、快速上手
安裝:
go get github.com/shirou/gopsutil
使用:
package main
import (
"fmt"
"github.com/shirou/gopsutil/cpu"
"time"
)
func main() {
info, _ := cpu.Info()
per, _ := cpu.Percent(1 * time.Second, true)
fmt.Printf("CPU Percent: %f\n", per)
fmt.Println(info)
}
輸出:
CPU Percent: [4.040404 4.000000 5.050505 6.930693]
[{"cpu":0,"vendorId":"AuthenticAMD","family":"23","model":"49","stepping":0,"physicalId":"0","coreId":"0","cores":1,"modelName":"AMD EPYC 7K62 48-Core Processor","mhz":2595.124,"cacheSize":512,"flags":["fpu","vme","de","pse","tsc","msr","pae","mce","cx8","apic","sep","mtrr","pge","mca","cmov","pat","pse36","clflush","mmx","fxsr","sse","sse2","ht","syscall","nx","mmxext","fxsr_opt","pdpe1gb","rdtscp","lm","rep_good","nopl","cpuid","extd_apicid","tsc_known_freq","pni","pclmulqdq","ssse3","fma","cx16","sse4_1","sse4_2","x2apic","movbe","popcnt","aes","xsave","avx","f16c","rdrand","hypervisor","lahf_lm","cmp_legacy","cr8_legacy","abm","sse4a","misalignsse","3dnowprefetch","osvw","topoext","ibpb","vmmcall","fsgsbase","bmi1","avx2","smep","bmi2","rdseed","adx","smap","clflushopt","sha_ni","xsaveopt","xsavec","xgetbv1","arat"],"microcode":"0x1000065"} {"cpu":1,"vendorId":"AuthenticAMD","family":"23","model":"49","stepping":0,"physicalId":"0","coreId":"1","cores":1,"modelName":"AMD EPYC 7K62 48-Core Processor","mhz":2595.124,"cacheSize":512,"flags":["fpu","vme","de","pse","tsc","msr","pae","mce","cx8","apic","sep","mtrr","pge","mca","cmov","pat","pse36","clflush","mmx","fxsr","sse","sse2","ht","syscall","nx","mmxext","fxsr_opt","pdpe1gb","rdtscp","lm","rep_good","nopl","cpuid","extd_apicid","tsc_known_freq","pni","pclmulqdq","ssse3","fma","cx16","sse4_1","sse4_2","x2apic","movbe","popcnt","aes","xsave","avx","f16c","rdrand","hypervisor","lahf_lm","cmp_legacy","cr8_legacy","abm","sse4a","misalignsse","3dnowprefetch","osvw","topoext","ibpb","vmmcall","fsgsbase","bmi1","avx2","smep","bmi2","rdseed","adx","smap","clflushopt","sha_ni","xsaveopt","xsavec","xgetbv1","arat"],"microcode":"0x1000065"} {"cpu":2,"vendorId":"AuthenticAMD","family":"23","model":"49","stepping":0,"physicalId":"0","coreId":"2","cores":1,"modelName":"AMD EPYC 7K62 48-Core Processor","mhz":2595.124,"cacheSize":512,"flags":["fpu","vme","de","pse","tsc","msr","pae","mce","cx8","apic","sep","mtrr","pge","mca","cmov","pat","pse36","clflush","mmx","fxsr","sse","sse2","ht","syscall","nx","mmxext","fxsr_opt","pdpe1gb","rdtscp","lm","rep_good","nopl","cpuid","extd_apicid","tsc_known_freq","pni","pclmulqdq","ssse3","fma","cx16","sse4_1","sse4_2","x2apic","movbe","popcnt","aes","xsave","avx","f16c","rdrand","hypervisor","lahf_lm","cmp_legacy","cr8_legacy","abm","sse4a","misalignsse","3dnowprefetch","osvw","topoext","ibpb","vmmcall","fsgsbase","bmi1","avx2","smep","bmi2","rdseed","adx","smap","clflushopt","sha_ni","xsaveopt","xsavec","xgetbv1","arat"],"microcode":"0x1000065"} {"cpu":3,"vendorId":"AuthenticAMD","family":"23","model":"49","stepping":0,"physicalId":"0","coreId":"3","cores":1,"modelName":"AMD EPYC 7K62 48-Core Processor","mhz":2595.124,"cacheSize":512,"flags":["fpu","vme","de","pse","tsc","msr","pae","mce","cx8","apic","sep","mtrr","pge","mca","cmov","pat","pse36","clflush","mmx","fxsr","sse","sse2","ht","syscall","nx","mmxext","fxsr_opt","pdpe1gb","rdtscp","lm","rep_good","nopl","cpuid","extd_apicid","tsc_known_freq","pni","pclmulqdq","ssse3","fma","cx16","sse4_1","sse4_2","x2apic","movbe","popcnt","aes","xsave","avx","f16c","rdrand","hypervisor","lahf_lm","cmp_legacy","cr8_legacy","abm","sse4a","misalignsse","3dnowprefetch","osvw","topoext","ibpb","vmmcall","fsgsbase","bmi1","avx2","smep","bmi2","rdseed","adx","smap","clflushopt","sha_ni","xsaveopt","xsavec","xgetbv1","arat"],"microcode":"0x1000065"}]
三、分工明確
gopsutil 將不同的功能劃分到不同的子包中:
主要為 cpu,disk,docker,host,mem,net,process,winservices 這幾個。 想要使用對應的功能,要匯入對應的子包。例如,上面程式碼中,我們要獲取 CPU 資訊,匯入的是 cpu 子包。 上述樣例中,我們獲取到了每個 cpu 的佔用率和所有 cpu 的詳細資訊。
閒言
最近正在寫一個 Golang 實現效能監控的 demo,之後還會寫這方面的介紹或者對比。
參考文件:
歡迎加入我們 GOLANG 中國社群:https://gocn.vip/
更多原創文章乾貨分享,請關注公眾號
- 加微信實戰群請加微信(註明:實戰群):gocnio
相關文章
- Go之獲取系統效能指標 - goPsutilGo指標
- iStat Menus:Mac系統狀態監控的終極利器Mac
- Go 每日一庫之 gopsutilGo
- 實時監控系統,統一監控企業APIAPI
- 開源監控利器Prometheus初探Prometheus
- OpManager:網路監控的利器
- 手刃前端監控系統前端
- python搭建系統監控Python
- Mac系統監控工具Mac
- 智慧影片監控系統
- Linux 系統監控指南Linux
- 智慧工地監控系統
- 打造前端監控系統前端
- 一、自動化監控利器-Zabbix
- PostgreSQL——51風控系統背後的利器SQL
- 系統監控&JVM監控指標資料查詢JVM指標
- linux系統 物理硬碟監控Linux硬碟
- Docker 容器監控系統初探Docker
- sysstat——系統效能監控神器
- Prometheus監控報警系統Prometheus
- 前端監控系統Sentry搭建前端
- 影片監控ai分析系統AI
- 影片監控智慧分析系統
- 電力影片監控系統
- 直播間截留監控系統
- 【DB寶41】監控利器PMM的使用--監控MySQL、PG、MongoDB、ProxySQL等MySqlMongoDB
- 前端監控基礎篇 — Docker + Sentry 搭建前端監控系統前端Docker
- 一種對雲主機進行效能監控的監控系統及其監控方法
- 搭建前端錯誤監控系統前端
- Kafka監控系統Kafka Eagle剖析Kafka
- 系統監控工具:MenuBar Stats for macMac
- 煤礦ai智慧監控系統AI
- 明廚亮灶監控系統
- 工地ai智慧影片監控系統AI
- 監控影片行為分析系統
- 煤礦影片監控分析系統
- 秸稈焚燒監控系統
- 智慧工地裝置監控系統