golang 获取系统的主机 CPU 内存 磁盘等信息
要求
需要go1.18或更高版本
官方地址:https://github.com/shirou/gopsutil
使用
#下载包
go get github.com/shirou/gopsutil/v3/cpu
go get github.com/shirou/gopsutil/v3/disk
go get github.com/shirou/gopsutil/v3/host
go get github.com/shirou/gopsutil/v3/mem
go get github.com/shirou/gopsutil/v3/net
使用gin定义接口
hostRouters := r.Group("/host", func(context *gin.Context) {})
{
hostRouters.GET("stats", func(c *gin.Context) {
statMap := make(map[string]interface{})
// 获取主机相关信息
hostInfo, _ := host.Info()
hostMap := make(map[string]interface{})
hostMap["uptime"] = hostInfo.Uptime //运行时间
hostMap["bootTime"] = hostInfo.BootTime //启动时间
hostMap["procs"] = hostInfo.Procs //进程数
hostMap["os"] = hostInfo.OS //操作系统
hostMap["platform"] = hostInfo.Platform //平台
hostMap["platformVersion"] = hostInfo.PlatformVersion //平台版本
hostMap["kernelArch"] = hostInfo.KernelArch //内核
hostMap["kernelVersion"] = hostInfo.KernelVersion //内核版本
statMap["hosts"] = hostMap
// 获取内存信息
memInfo, _ := mem.VirtualMemory()
memMap := make(map[string]interface{})
memMap["total"] = memInfo.Total //总内存
memMap["available"] = memInfo.Available //可用内存
memMap["used"] = memInfo.Used //已使用内存
memMap["free"] = memInfo.Free //剩余内存
memMap["usedPercent"] = memInfo.UsedPercent //百分比
memMap["buffers"] = memInfo.Buffers //缓存
memMap["shared"] = memInfo.Shared //共享内存
memMap["cached"] = memInfo.Cached //缓冲区
statMap["mems"] = memMap
// 获取CPU信息
cpuInfo, _ := cpu.Info()
var cpuMapArr []map[string]interface{}
for _, c := range cpuInfo {
cpuMap := make(map[string]interface{})
cpuMap["cpu"] = c.CPU + 1 //第几个CPU 从0开始的
cpuMap["cores"] = c.Cores //CPU的核数
cpuMap["modelName"] = c.ModelName //CPU类型
cpuMapArr = append(cpuMapArr, cpuMap)
}
statMap["cpus"] = cpuMapArr
// 获取IO信息
ioInfo, _ := net.IOCounters(false)
var ioMapArr []map[string]interface{}
for _, i := range ioInfo {
ioMap := make(map[string]interface{})
ioMap["ioName"] = i.Name //网口名
ioMap["bytesSent"] = i.BytesSent //发送字节数
ioMap["bytesRecv"] = i.BytesRecv //接收字节数
ioMap["packetsSent"] = i.PacketsSent //发送的数据包数
ioMap["packetsRecv"] = i.PacketsRecv //接收的数据包数
ioMapArr = append(ioMapArr, ioMap)
}
statMap["ios"] = ioMapArr
// 获取磁盘信息
partitions, _ := disk.Partitions(false)
var diskMapArr []map[string]interface{}
for _, partition := range partitions {
diskMap := make(map[string]interface{})
usage, _ := disk.Usage(partition.Mountpoint)
diskMap["disk"] = partition.Mountpoint //第几块磁盘
diskMap["total"] = usage.Total //总大小
diskMap["free"] = usage.Free //剩余空间
diskMap["used"] = usage.Used //已使用空间
diskMap["usedPercent"] = usage.UsedPercent //百分比
diskMapArr = append(diskMapArr, diskMap)
}
statMap["disks"] = diskMapArr
util.R(c, nil, statMap)
})
}
调用接口,查看接口响应数据
{
"code": "0",
"msg": "成功",
"data": {
"cpus": [
{
"cores": 1,
"cpu": 1,
"modelName": ""
},
{
"cores": 1,
"cpu": 2,
"modelName": ""
},
{
"cores": 1,
"cpu": 3,
"modelName": ""
},
{
"cores": 1,
"cpu": 4,
"modelName": ""
},
{
"cores": 1,
"cpu": 5,
"modelName": ""
},
{
"cores": 1,
"cpu": 6,
"modelName": ""
},
{
"cores": 1,
"cpu": 7,
"modelName": ""
},
{
"cores": 1,
"cpu": 8,
"modelName": ""
},
{
"cores": 1,
"cpu": 9,
"modelName": ""
},
{
"cores": 1,
"cpu": 10,
"modelName": ""
},
{
"cores": 1,
"cpu": 11,
"modelName": ""
},
{
"cores": 1,
"cpu": 12,
"modelName": ""
},
{
"cores": 1,
"cpu": 13,
"modelName": ""
},
{
"cores": 1,
"cpu": 14,
"modelName": ""
},
{
"cores": 1,
"cpu": 15,
"modelName": ""
},
{
"cores": 1,
"cpu": 16,
"modelName": ""
}
],
"disks": [
{
"disk": "/etc/hosts",
"free": 134272794624,
"total": 469202288640,
"used": 311020077056,
"usedPercent": 69.84618367740407
},
{
"disk": "/dev/termination-log",
"free": 134272794624,
"total": 469202288640,
"used": 311020077056,
"usedPercent": 69.84618367740407
},
{
"disk": "/etc/hostname",
"free": 134272794624,
"total": 469202288640,
"used": 311020077056,
"usedPercent": 69.84618367740407
},
{
"disk": "/etc/resolv.conf",
"free": 134272794624,
"total": 469202288640,
"used": 311020077056,
"usedPercent": 69.84618367740407
}
],
"hosts": {
"bootTime": 1704638867,
"kernelArch": "aarch64",
"kernelVersion": "5.10.0-136.40.0.117.oe2203sp1.aarch64",
"os": "linux",
"platform": "alpine",
"platformVersion": "3.19.0",
"procs": 1,
"uptime": 14905256
},
"ios": [
{
"bytesRecv": 111431059,
"bytesSent": 209346524,
"ioName": "all",
"packetsRecv": 1505753,
"packetsSent": 287887
}
],
"mems": {
"available": 48697397248,
"buffers": 16645812224,
"cached": 33186672640,
"free": 2395566080,
"shared": 2713157632,
"total": 133410463744,
"used": 81182412800,
"usedPercent": 60.85160827847815
}
}
}
最后图表显示