package main
import (
"fmt"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/disk"
"github.com/shirou/gopsutil/host"
"github.com/shirou/gopsutil/load"
"github.com/shirou/gopsutil/mem"
"github.com/shirou/gopsutil/net"
"time"
)
func main() {
getCpuInfo()
getCpuLoad()
fmt.Println("----------")
getMemInfo()
fmt.Println("----------")
getHostInfo()
fmt.Println("----------")
getDiskInfo()
fmt.Println("----------")
getNetInfo()
}
// cpu信息
func getCpuInfo() {
cpuNumber, err := cpu.Counts(true)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(fmt.Sprintf("CPU核心数: %v", cpuNumber))
cpuPercent, err := cpu.Percent(time.Second, true)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(fmt.Sprintf("CPU使用率: %.1f%%", cpuPercent[0]))
}
// cpu负载
func getCpuLoad() {
loadInfo, err := load.Avg()
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(fmt.Sprintf("CPU平均负载: %v", loadInfo))
}
// 内存信息
func getMemInfo() {
memInfo, err := mem.VirtualMemory()
if err != nil {
fmt.Println(err.Error())
}
// 总内存(GB)
memTotal := memInfo.Total / 1024 / 1024 / 1024
fmt.Println(fmt.Sprintf("总内存: %v GB", memTotal))
// 已用内存(MB)
memUsed := memInfo.Used / 1024 / 1024
fmt.Println(fmt.Sprintf("已用内存: %v MB", memUsed))
// 可用内存(MB)
memAva := memInfo.Available / 1024 / 1024
fmt.Println(fmt.Sprintf("可用内存: %v MB", memAva))
// 内存使用率
memUsedPercent := memInfo.UsedPercent
fmt.Println(fmt.Sprintf("内存使用率: %.1f%%", memUsedPercent))
}
// 主机信息
func getHostInfo() {
hostInfo, err := host.Info()
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(fmt.Sprintf("主机名: %v", hostInfo.Hostname))
fmt.Println(fmt.Sprintf("操作系统: %v", hostInfo.Platform))
}
// 磁盘信息
func getDiskInfo() {
diskPart, err := disk.Partitions(false)
if err != nil {
fmt.Println(err)
}
for _, dp := range diskPart {
fmt.Println(dp)
diskUsed, _ := disk.Usage(dp.Mountpoint)
fmt.Println(fmt.Sprintf("分区总大小: %d MB", diskUsed.Total/1024/1024))
fmt.Println(fmt.Sprintf("分区使用率: %.1f%%", diskUsed.UsedPercent))
fmt.Println(fmt.Sprintf("分区inode使用率: %.1f%%", diskUsed.InodesUsedPercent))
}
}
// 网络信息
func getNetInfo() {
info, _ := net.IOCounters(true)
for index, v := range info {
fmt.Println(fmt.Sprintf("%v:%v send:%v recv:%v", index, v, v.BytesSent, v.BytesRecv))
}
}
输出
CPU核心数: 12
CPU使用率: 4.6%
CPU平均负载: {"load1":0,"load5":0,"load15":0}
----------
总内存: 31 GB
已用内存: 26276 MB
可用内存: 6324 MB
内存使用率: 80.0%
----------
主机名: 无情哈拉少
操作系统: Microsoft Windows 11 Pro
----------
The network path was not found.
{"device":"C:","mountpoint":"C:","fstype":"NTFS","opts":"rw.compress"}
分区总大小: 102412 MB
分区使用率: 90.8%
分区inode使用率: 0.0%
{"device":"D:","mountpoint":"D:","fstype":"NTFS","opts":"rw.compress"}
分区总大小: 186367 MB
分区使用率: 13.4%
分区inode使用率: 0.0%
{"device":"E:","mountpoint":"E:","fstype":"NTFS","opts":"rw.compress"}
分区总大小: 187371 MB
分区使用率: 68.3%
分区inode使用率: 0.0%
{"device":"F:","mountpoint":"F:","fstype":"NTFS","opts":"rw.compress"}
分区总大小: 5723148 MB
分区使用率: 1.9%
分区inode使用率: 0.0%
----------
0:{"name":"以太网","bytesSent":0,"bytesRecv":0,"packetsSent":0,"packetsRecv":0,"errin":0,"errout":0,"dropin":0,"dropout":0,"fifoin":0,"fifoout":0} send:0 recv:0
1:{"name":"本地连接* 9","bytesSent":0,"bytesRecv":0,"packetsSent":0,"packetsRecv":0,"errin":0,"errout":0,"dropin":0,"dropout":0,"fifoin":0,"fifoout":0} send:0 recv:0
2:{"name":"本地连接* 10","bytesSent":0,"bytesRecv":0,"packetsSent":0,"packetsRecv":0,"errin":0,"errout":0,"dropin":0,"dropout":0,"fifoin":0,"fifoout":0} send:0 recv:0
3:{"name":"VMware Network Adapter VMnet1","bytesSent":7284264534016,"bytesRecv":94489280512,"packetsSent":81604378624,"packetsRecv":94489280512,"errin":0,"errout":0,"dropin":0,"dropout":0,"fifoin":0,"fifoout":0} send:7284264534016 recv:94489280512
4:{"name":"VMware Network Adapter VMnet8","bytesSent":3704679875739648,"bytesRecv":6736767872925696,"packetsSent":3690893030719488,"packetsRecv":6736505879920640,"errin":0,"errout":0,"dropin":0,"dropout":0,"fifoin":0,"fifoout":0} send:3704679875739648 recv:6736767872925696
5:{"name":"WLAN 2","bytesSent":301329291965104128,"bytesRecv":1938900090278117376,"packetsSent":2245920003457024,"packetsRecv":6078259192135680,"errin":0,"errout":0,"dropin":0,"dropout":0,"fifoin":0,"fifoout":0} send:301329291965104128 recv:1938900090278117376
6:{"name":"Loopback Pseudo-Interface 1","bytesSent":0,"bytesRecv":0,"packetsSent":0,"packetsRecv":0,"errin":0,"errout":0,"dropin":0,"dropout":0,"fifoin":0,"fifoout":0} send:0 recv:0
标签:err,MB,fmt,golang,Sprintf,监控,Println,服务器,使用率
From: https://www.cnblogs.com/qcy-blog/p/17835858.html