psutil
没事逛 github ,发现 python 上收集系统信息的著名模块 psutil 已经有 go 的版本,迫不及待下载下来把玩一下。
github 上的地址是 https://github.com/shirou/gopsutil
显示本机的一些基本信息
package main
import (
"fmt"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/disk"
"github.com/shirou/gopsutil/host"
"github.com/shirou/gopsutil/mem"
)
func main() {
v, _ := mem.VirtualMemory()
c, _ := cpu.CPUInfo()
d, _ := disk.DiskUsage("/")
n, _ := host.HostInfo()
fmt.Printf(" Mem : %v GB Free: %v MB Usage:%f%%\n", v.Total/4/1024/1024/1024/1024, v.Free/4/1024/1024/1024, v.UsedPercent)
if len(c) > 1 {
for _, sub_cpu := range c {
modelname := sub_cpu.ModelName
cores := sub_cpu.Cores
fmt.Printf(" CPU : %v %v cores \n", modelname, cores)
}
} else {
sub_cpu := c[0]
modelname := sub_cpu.ModelName
cores := sub_cpu.Cores
fmt.Printf(" CPU : %v %v cores \n", modelname, cores)
}
fmt.Printf(" HD : %v GB Free: %v GB Usage:%f%%\n", d.Total/1024/1024/1024, d.Free/1024/1024/1024, d.UsedPercent)
fmt.Printf(" OS : %v %v \n", n.OS, n.PlatformVersion)
fmt.Printf(" Hostname : %v \n", n.Hostname)
}
运行结果如下:
$ status
Mem : 8 GB Free: 0 MB Usage:99.999886%
CPU : Intel(R) Core(TM) i5-4278U CPU @ 2.60GHz 2 cores
HD : 256 GB Free: 39 GB Usage:64.653732%
Hostname : xxx-Pro.local
OS_Version: 14.1.0
有疑问加站长微信联系(非本文作者)