Home >Backend Development >Golang >Are you still writing your own Go system monitoring functions?

Are you still writing your own Go system monitoring functions?

Go语言进阶学习
Go语言进阶学习forward
2023-07-24 15:03:36688browse
If you have a Go development requirement: obtain the host’s hard disk, CPU, memory, process, etc. usage, what would you do? A simpler idea is to use os/exec to execute certain commands such as ps, cd, and top, and then analyze their execution results. Of course, based on Linux's idea that everything is a file, a more direct approach is to read the contents of related files, such as files in the /proc directory.

#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.

gopsutil Introduction

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

  • FreeBSD i386/amd64/arm
  • ##Linux i386/amd64/arm(raspberry pi)
  • Windows i386/amd64/arm/arm64
  • Darwin i386/amd64
  • OpenBSD amd64
  • Solaris amd64

Partial support list

  • CPU on DragonFly BSD
  • ##host on Linux RISC- V
#In addition, this project transplants the C structure to the Go structure. There is no cgo code in its implementation, which is more conducive to cross-compilation.

Using

gopsutil currently has v3 and v2 versions, and there is no backward compatibility guarantee, so there are two ways to use it

import (
    // "github.com/shirou/gopsutil/v3/mem" // to use v3
    "github.com/shirou/gopsutil/mem"  
)

For example, if we want to check the system memory usage information, we can obtain it in the following way

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)
}

The result is

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}

The gopsutil package is friendly in that most of the collection functions return a structure object, and they all implement the fmt.Stringer interface, so they will be output in json format when printing.

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)
}

gopsutil is divided into different sub-packages through different collection units. By introducing different sub-packages during use, related methods can be called.

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"
)

For example, if we want to obtain host information, we need to introduce the github.com/shirou/gopsutil/v3/host sub-package

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 处理了跨平台兼容性问题,对外接口基本保持一致,使用起来比较友好。

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!

Statement:
This article is reproduced at:Go语言进阶学习. If there is any infringement, please contact admin@php.cn delete