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"] = diskMapArrutil.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}}
}
最后图表显示