==目的==
增加对服务器使用情况的监控。
==引入==
go get github.com/shirou/gopsutil go get github.com/yusufpapurcu/wmi
==代码==
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() } 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])) } 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核心数: 8 CPU使用率: 13.8% CPU平均负载: {"load1":0,"load5":0,"load15":0} ---------- 总内存: 31 GB 已用内存: 16078 MB 可用内存: 16462 MB 内存使用率: 49.0% ---------- 主机名: RCNL5CG0519B67 操作系统: Microsoft Windows 10 Pro ---------- {"device":"C:","mountpoint":"C:","fstype":"NTFS","opts":"rw.compress"} 分区总大小: 511158 MB 分区使用率: 60.9% 分区inode使用率: 0.0% {"device":"D:","mountpoint":"D:","fstype":"NTFS","opts":"rw.compress"} 分区总大小: 464131 MB 分区使用率: 20.3% 分区inode使用率: 0.0% ---------- 0:{"name":"本地连接 2","bytesSent":5509958,"bytesRecv":21418867,"packetsSent":15311,"packetsRecv":19217,"errin":0,"errout":0,"dropin":0,"dropout":0,"fifoin":0,"fifoout":0} send:5509958 recv:21418867 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":"以太网 5","bytesSent":0,"bytesRecv":0,"packetsSent":0,"packetsRecv":0,"errin":0,"errout":0,"dropin":0,"dropout":0,"fifoin":0,"fifoout":0} send:0 recv:0 4:{"name":"VMware Network Adapter VMnet1","bytesSent":45086,"bytesRecv":229,"packetsSent":220,"packetsRecv":229,"errin":0,"errout":0,"dropin":0,"dropout":0,"fifoin":0,"fifoout":0} send:45086 recv:229 5:{"name":"VMware Network Adapter VMnet8","bytesSent":83522,"bytesRecv":228,"packetsSent":220,"packetsRecv":228,"errin":0,"errout":0,"dropin":0,"dropout":0,"fifoin":0,"fifoout":0} send:83522 recv:228 6:{"name":"WLAN","bytesSent":487797694,"bytesRecv":1544453142,"packetsSent":2882494,"packetsRecv":4818176,"errin":0,"errout":0,"dropin":0,"dropout":0,"fifoin":0,"fifoout":0} send:487797694 recv:1544453142 7:{"name":"以太网 4","bytesSent":0,"bytesRecv":0,"packetsSent":0,"packetsRecv":0,"errin":0,"errout":0,"dropin":0,"dropout":0,"fifoin":0,"fifoout":0} send:0 recv:0 8:{"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
--end--
标签:err,fmt,样例,send,Sprintf,使用,Println,recv,gopsutil From: https://www.cnblogs.com/quchunhui/p/17028432.html