Home >Backend Development >Golang >Are you still writing your own Go system monitoring functions?
#The above method can meet the requirements, but we don’t have to reinvent the wheel because there are already quite complete third-party libraries that realize these collection requirements for us. It's gopsutil.
psutil (process and system utilities,) is a cross-platform library for obtaining process and system utilities in Python rate (CPU, memory, disk, network, sensor) information, and gopsutil is its Go language version.
gopsutil shields us from differences in various systems and has good portability.
Supported list
Partial support list
Using
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}
For example, in the above example, mem.VirtualMemory returns the VirtualMemoryStat structure, which calls the json.Marshal() function in the String() method.
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)
}
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"
)
输出 gopsutil 库有非常全面的覆盖单元,包括主机、磁盘、内存、CPU、网络、进程、docker等模块,它能很好地帮助我们获取系统信息。并且 gopsutil 处理了跨平台兼容性问题,对外接口基本保持一致,使用起来比较友好。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"}
总结
The above is the detailed content of Are you still writing your own Go system monitoring functions?. For more information, please follow other related articles on the PHP Chinese website!