01-gopsutil包使用
系统监控库-gopsutils
简介
在说gopsutils
之前我们必须先说下psutils
是啥, 因为gopsutils
实际上就是一个golang版本的psutils
(从名字上也能看出来)
psutils
是一个比较出名的python库, psutils
是python process and system utilities
的一个缩写. 它有如下特点
- 跨平台: Linux, Windows, OSX, Sun Solaris, FreeBSD, OpenBSD and NetBSD的32位和64位系统
- 功能丰富: 实现了进程管理,系统诊断, 这个库基本实现了这些命令行工具的功能: ps, top, lsof, netstat, ifconfig, who, df, kill, free, nice, ionice, iostat, iotop, uptime, pidof, tty, taskset, pmap
如果想要了解关于gopsutils
更多的详情 请查看gopsutils github地址
安装
go get -v "github.com/shirou/gopsutil"
使用
具体的使用文档可以参考gopsutil的godoc文档
以下以测试收集cpu, disk, load, mem, net, process 为列, 注意这些对象都使用String方法, 因此可以直接调用fmt打印,String方法会将其转换成Json输出.
package main
import (
"fmt"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/disk"
"github.com/shirou/gopsutil/load"
"github.com/shirou/gopsutil/mem"
"github.com/shirou/gopsutil/net"
"github.com/shirou/gopsutil/process"
)
func main() {
fmt.Println("CPU统计:")
c, _ := cpu.Info()
fmt.Println(c)
fmt.Println("内存统计:")
m, _ := mem.VirtualMemory()
fmt.Println(m)
fmt.Println("磁盘用量和IO统计:")
dp, _ := disk.Partitions(true)
du, _ := disk.Usage("/")
di, _ := disk.IOCounters()
fmt.Println(du)
fmt.Println(dp)
fmt.Println(di)
fmt.Println("网络IO统计:")
ni, _ := net.IOCounters(true)
fmt.Println(ni)
fmt.Println("协议统计:")
nt, _ := net.ProtoCounters(nil)
fmt.Println(nt)
fmt.Println("链接状态统计:")
nc, _ := net.Connections("all")
fmt.Println(nc)
fmt.Println("进程统计:")
pi, _ := process.Pids()
fmt.Println(pi)
p, _ := process.NewProcess(614)
pm, _ := p.MemoryPercent()
pn, _ := p.Username()
fmt.Println(pm)
fmt.Println(pn)
fmt.Println("负载统计:")
pl, _ := load.Avg()
fmt.Println(pl)
}
最后执行过会的结果大概为这样:
CPU统计:
[{"cpu":0,"vendorId":"GenuineIntel","family":"6","model":"69","stepping":1,"physicalId":"","coreId":"","cores":2,"modelName":"Intel(R) Core(TM) i5-4278U CPU @ 2.60GHz","mhz":2600,"cacheSize":256,"flags":["fpu","vme","de","pse","tsc","msr","pae","mce","cx8","apic","sep","mtrr","pge","mca","cmov","pat","pse36","clfsh","ds","acpi","mmx","fxsr","sse","sse2","ss","htt","tm","pbe","sse3","pclmulqdq","dtes64","mon","dscpl","vmx","est","tm2","ssse3","fma","cx16","tpr","pdcm","sse4.1","sse4.2","x2apic","movbe","popcnt","aes","pcid","xsave","osxsave","seglim64","tsctmr","avx1.0","rdrand","f16c","smep","erms","rdwrfsgs","tsc_thread_offset","bmi1","avx2","bmi2","invpcid","fpu_csds","syscall","xd","1gbpage","em64t","lahf","lzcnt","rdtscp","tsci"]}]
内存统计:
{"total":8589934592,"available":4315959296,"used":4273975296,"usedPercent":49.7556209564209,"free":3390992384,"active":2908176384,"inactive":924966912,"wired":1364955136,"buffers":0,"cached":0,"writeback":0,"dirty":0,"writebacktmp":0,"shared":0,"slab":0,"pagetables":0,"swapcached":0}
磁盘用量和IO统计:
{"path":"/","fstype":"hfs","total":249769230336,"free":201003577344,"used":48503508992,"usedPercent":19.41932916506611,"inodesTotal":60978814,"inodesUsed":11905675,"inodesFree":49073139,"inodesUsedPercent":19.524281006842802}
[{"device":"/dev/disk1","mountpoint":"/","fstype":"hfs","opts":"rw,multilabel"} {"device":"devfs","mountpoint":"/dev","fstype":"devfs","opts":"rw,suiddir,multilabel"} {"device":"map -hosts","mountpoint":"/net","fstype":"autofs","opts":"rw,nosuid,suiddir,nosymfollow,multilabel"} {"device":"map auto_home","mountpoint":"/home","fstype":"autofs","opts":"rw,suiddir,nosymfollow,multilabel"}]
map[]
网络IO统计:
[{"name":"lo0","bytesSent":22687,"bytesRecv":22687,"packetsSent":215,"packetsRecv":215,"errin":0,"errout":0,"dropin":0,"dropout":0,"fifoin":0,"fifoout":0} {"name":"gif0","bytesSent":0,"bytesRecv":0,"packetsSent":0,"packetsRecv":0,"errin":0,"errout":0,"dropin":0,"dropout":0,"fifoin":0,"fifoout":0} {"name":"stf0","bytesSent":0,"bytesRecv":0,"packetsSent":0,"packetsRecv":0,"errin":0,"errout":0,"dropin":0,"dropout":0,"fifoin":0,"fifoout":0} {"name":"en0","bytesSent":6401764,"bytesRecv":120874758,"packetsSent":85192,"packetsRecv":87266,"errin":0,"errout":0,"dropin":0,"dropout":0,"fifoin":0,"fifoout":0} {"name":"en1","bytesSent":0,"bytesRecv":0,"packetsSent":0,"packetsRecv":0,"errin":0,"errout":0,"dropin":0,"dropout":0,"fifoin":0,"fifoout":0} {"name":"en2","bytesSent":0,"bytesRecv":0,"packetsSent":0,"packetsRecv":0,"errin":0,"errout":0,"dropin":0,"dropout":0,"fifoin":0,"fifoout":0} {"name":"p2p0","bytesSent":0,"bytesRecv":0,"packetsSent":0,"packetsRecv":0,"errin":0,"errout":0,"dropin":0,"dropout":0,"fifoin":0,"fifoout":0} {"name":"awdl0","bytesSent":2331,"bytesRecv":0,"packetsSent":2,"packetsRecv":0,"errin":0,"errout":0,"dropin":0,"dropout":0,"fifoin":0,"fifoout":0} {"name":"bridg","bytesSent":342,"bytesRecv":0,"packetsSent":1,"packetsRecv":0,"errin":0,"errout":0,"dropin":0,"dropout":0,"fifoin":0,"fifoout":0}]
协议统计:
[]
链接状态统计:
[{"fd":13,"family":2,"type":2,"localaddr":{"ip":"*","port":63824},"remoteaddr":{"ip":"","port":0},"status":"","uids":null,"pid":238} {"fd":64,"family":2,"type":1,"localaddr":{"ip":"192.168.3.7","port":49224},"remoteaddr":{"ip":"191.238.172.191","port":443},"status":"CLOSED","uids":null,"pid":394}]
进程统计:
[1 45 46 48 49 53 54 55 62 64 65 69 70 71 73 74 76 77 79 80 81 82 83 85 88 89 93 95 96 97 98 100 101 102 105 110 118 130 133 135 136 142 143 147 149 159 168 169 170 171 172 173 174 175 179 182 183 184 185 187 188 189 190 192 195 196 197 198 200 218 219 220 226 227 229 231 232 233 237 238 239 242 243 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 265 266 267 268 269 270 271 272 273 274 276 277 278 279 280 281 282 283 284 285 286 287 289 290 291 292 294 295 296 297 299 300 302 305 307 308 311 312 314 316 317 324 325 329 339 340 342 343 344 345 346 348 360 361 362 366 367 368 371 377 386 391 392 393 394 397 423 426 427 428 429 430 433 437 443 445 446 451 452 454 455 456 461 463 469 470 474 475 477 481 483 486 487 488 511 514 554 555 624 650 711 722 723 724 725 726 727 729 736 737 741 742 743 791 866 867 878 880 883 889 516 517 518]
0
root
负载统计:
{"load1":1.32,"load5":1.35,"load15":1.31}