Go之获取系统性能指标 - goPsutil
简介
psutil
是一个跨平台进程和系统监控的Python库,而gopsutil
是其Go语言版本的实现。
Go语言部署简单、性能好的特点非常适合做一些诸如采集系统信息和监控的服务,本文介绍的gopsutil库是知名Python库:psutil的一个Go语言版本的实现。
特点
gopsutils为我们屏蔽了各个系统之间的差异, 具有非常强悍的可移植性, 有了gopsutil, 我们不再需要针对不同的系统使用syscall调用对应的系统方法, 更棒的是gopsutil的实现中没有任何cgo的代码, 使得交叉编译成为可能.
安装
go get github.com/shirou/gopsutil
功能
gopsutil将不同的功能划分到不同的子包中
/*
cpu: CPU相关
disk: 磁盘相关
docker: docker相关
host: 主机相关
mem: 内存相关
net: 网络相关
process: 进程相关
*/
CPU
采集CPU相关信息
package main
import (
"fmt"
"github.com/shirou/gopsutil/cpu"
"time"
)
// cpu info
func getCpuInfo() {
cpuInfos, err := cpu.Info()
if err != nil {
fmt.Printf("get cpu info failed, err:%v", err)
}
for _, ci := range cpuInfos {
fmt.Println(ci)
}
// CPU使用率
for {
percent, _ := cpu.Percent(time.Second, false)
fmt.Printf("cpu percent:%v\n", percent)
}
}
func main() {
getCpuInfo()
}
go run ps.go
{"cpu":0,"vendorId":"GenuineIntel","family":"6","model":"142","stepping":10,"physicalId":"","coreId":"","cores":4,"modelName":"Intel(R) Core(TM) i5-8257U CPU @ 1.40GHz","mhz":1400,"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","rdwrfsgs","tsc_thread_offset","sgx","bmi1","avx2","smep","bmi2","erms","invpcid","fpu_csds","mpx","rdseed","adx","smap","clfsopt","ipt","mdclear","tsxfa","ibrs","stibp","l1df","ssbd","syscall","xd","1gbpage","em64t","lahf","lzcnt","prefetchw","rdtscp","tsci"],"microcode":""}
cpu percent:[10.099750623570355]
cpu percent:[7.615480649005255]
获取CPU负载消息
package main
import (
"fmt"
"github.com/shirou/gopsutil/load"
)
func getCpuLoad() {
info, _ := load.Avg()
fmt.Printf("%v\n",info)
}
func main() {
getCpuLoad()
}
go run ps.go
{"load1":2.591796875,"load5":2.86572265625,"load15":2.7578125}
**
Memory
package main
import (
"fmt"
"github.com/shirou/gopsutil/mem"
)
func getMemInfo() {
memInfo, _ := mem.VirtualMemory()
fmt.Printf("mem info:%v\n",memInfo)
}
func main() {
getMemInfo()
}
go run ps.go
mem info:{"total":8589934592,"available":1984147456,"used":6605787136,"usedPercent":76.9014835357666,"free":122396672,"active":1884549120,"inactive":1861750784,"wired":2657144832,"laundry":0,"buffers":0,"cached":0,"writeback":0,"dirty":0,"writebacktmp":0,"shared":0,"slab":0,"sreclaimable":0,"sunreclaim":0,"pagetables":0,"swapcached":0,"commitlimit":0,"committedas":0,"hightotal":0,"highfree":0,"lowtotal":0,"lowfree":0,"swaptotal":0,"swapfree":0,"mapped":0,"vmalloctotal":0,"vmallocused":0,"vmallocchunk":0,"hugepagestotal":0,"hugepagesfree":0,"hugepagesize":0}
Host
package main
import (
"fmt"
"github.com/shirou/gopsutil/host"
)
func getHostInfo() {
hInfo, _ := host.Info()
fmt.Printf("host info:%v uptime:%v boottime:%v\n", hInfo, hInfo.Uptime, hInfo.BootTime)
}
func main() {
getHostInfo()
}
go run ps.go
host info:{"hostname":"youmendeMacBook-Pro.local","uptime":314216,"bootTime":1609206164,"procs":525,"os":"darwin","platform":"darwin","platformFamily":"Standalone Workstation","platformVersion":"11.1","kernelVersion":"20.2.0","kernelArch":"x86_64","virtualizationSystem":"","virtualizationRole":"","hostid":"82e2050c-5936-3d24-ad3b-ec4ec5c09e11"} uptime:314216 boottime:1609206164
Disk
package main
import (
"fmt"
"github.com/shirou/gopsutil/disk"
)
// disk info
func getDiskInfo() {
parts, err := disk.Partitions(true)
if err != nil {
fmt.Printf("get Partitions failed, err:%v\n", err)
return
}
for _, part := range parts {
fmt.Printf("part:%v\n", part.String())
diskInfo, _ := disk.Usage(part.Mountpoint)
fmt.Printf("disk info:used:%v free:%v\n", diskInfo.UsedPercent, diskInfo.Free)
}
ioStat, _ := disk.IOCounters()
for k, v := range ioStat {
fmt.Printf("%v:%v\n", k, v)
}
}
func main() {
getDiskInfo()
}
go run ps.go
part:{"device":"/dev/disk1s2s1","mountpoint":"/","fstype":"apfs","opts":"ro,journaled,multilabel"}
disk info:used:21.590916437596942 free:54644285440
part:{"device":"devfs","mountpoint":"/dev","fstype":"devfs","opts":"rw,nobrowse,multilabel"}
disk info:used:100 free:0
part:{"device":"/dev/disk1s5","mountpoint":"/System/Volumes/VM","fstype":"apfs","opts":"rw,noexec,nobrowse,journaled,multilabel,noatime"}
disk info:used:3.781419239013729 free:54644285440
part:{"device":"/dev/disk1s3","mountpoint":"/System/Volumes/Preboot","fstype":"apfs","opts":"rw,nobrowse,journaled,multilabel"}
disk info:used:0.5894192171529189 free:54644285440
part:{"device":"/dev/disk1s6","mountpoint":"/System/Volumes/Update","fstype":"apfs","opts":"rw,nobrowse,journaled,multilabel"}
disk info:used:0.0014916323547500506 free:54644285440
part:{"device":"/dev/disk1s1","mountpoint":"/System/Volumes/Data","fstype":"apfs","opts":"rw,nobrowse,journaled,multilabel"}
disk info:used:76.48839834497826 free:54644285440
part:{"device":"map auto_home","mountpoint":"/System/Volumes/Data/home","fstype":"autofs","opts":"rw,nobrowse,automounted,multilabel"}
disk info:used:0 free:0
disk0:{"readCount":7171236,"mergedReadCount":0,"writeCount":3480390,"mergedWriteCount":0,"readBytes":157156835328,"writeBytes":130453229568,"readTime":6791329,"writeTime":2234950,"iopsInProgress":0,"ioTime":9026279,"weightedIO":0,"name":"disk0","serialNumber":"","label":""}
IO
package main
import (
"fmt"
"github.com/shirou/gopsutil/net"
)
func getNetInfo() {
info, _ := net.IOCounters(true)
for index, v := range info {
fmt.Printf("%v:%v send:%v recv:%v\n", index, v, v.BytesSent, v.BytesRecv)
}
}
func main() {
getNetInfo()
}
go run ps.go
0:{"name":"lo0","bytesSent":121344185,"bytesRecv":121344185,"packetsSent":433070,"packetsRecv":433070,"errin":0,"errout":0,"dropin":0,"dropout":0,"fifoin":0,"fifoout":0} send:121344185 recv:121344185
1:{"name":"gif0","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":"stf0","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":"en3","bytesSent":14748522,"bytesRecv":8111958,"packetsSent":61122,"packetsRecv":61250,"errin":0,"errout":1394,"dropin":0,"dropout":369,"fifoin":0,"fifoout":0} send:14748522 recv:8111958
4:{"name":"ap1","bytesSent":0,"bytesRecv":0,"packetsSent":0,"packetsRecv":0,"errin":0,"errout":0,"dropin":0,"dropout":0,"fifoin":0,"fifoout":0} send:0 recv:0
5:{"name":"en0","bytesSent":2744132038,"bytesRecv":12672282198,"packetsSent":7843906,"packetsRecv":9697152,"errin":0,"errout":1194,"dropin":0,"dropout":666,"fifoin":0,"fifoout":0} send:2744132038 recv:12672282198
6:{"name":"en1","bytesSent":0,"bytesRecv":0,"packetsSent":0,"packetsRecv":0,"errin":0,"errout":0,"dropin":0,"dropout":0,"fifoin":0,"fifoout":0} send:0 recv:0
7:{"name":"en2","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":"awdl0","bytesSent":241402,"bytesRecv":0,"packetsSent":1961,"packetsRecv":0,"errin":0,"errout":0,"dropin":0,"dropout":0,"fifoin":0,"fifoout":0} send:241402 recv:0
9:{"name":"llw0","bytesSent":0,"bytesRecv":0,"packetsSent":0,"packetsRecv":0,"errin":0,"errout":0,"dropin":0,"dropout":0,"fifoin":0,"fifoout":0} send:0 recv:0
10:{"name":"utun0","bytesSent":187730,"bytesRecv":0,"packetsSent":1629,"packetsRecv":0,"errin":0,"errout":0,"dropin":0,"dropout":0,"fifoin":0,"fifoout":0} send:187730 recv:0
11:{"name":"utun1","bytesSent":187730,"bytesRecv":0,"packetsSent":1629,"packetsRecv":0,"errin":0,"errout":0,"dropin":0,"dropout":0,"fifoin":0,"fifoout":0} send:187730 recv:0
12:{"name":"en6","bytesSent":20906,"bytesRecv":6600,"packetsSent":149,"packetsRecv":60,"errin":0,"errout":0,"dropin":0,"dropout":0,"fifoin":0,"fifoout":0} send:20906 recv:6600
13:{"name":"en7","bytesSent":20906,"bytesRecv":6600,"packetsSent":149,"packetsRecv":60,"errin":0,"errout":0,"dropin":0,"dropout":0,"fifoin":0,"fifoout":0} send:20906 recv:6600
14:{"name":"bridg","bytesSent":0,"bytesRecv":0,"packetsSent":0,"packetsRecv":0,"errin":0,"errout":0,"dropin":0,"dropout":0,"fifoin":0,"fifoout":0} send:0 recv:0
15:{"name":"bridg","bytesSent":41812,"bytesRecv":12360,"packetsSent":298,"packetsRecv":120,"errin":0,"errout":0,"dropin":0,"dropout":0,"fifoin":0,"fifoout":0} send:41812 recv:12360
16:{"name":"bridg","bytesSent":41812,"bytesRecv":12360,"packetsSent":298,"packetsRecv":120,"errin":0,"errout":0,"dropin":0,"dropout":0,"fifoin":0,"fifoout":0} send:41812 recv:12360
Go之获取系统性能指标 - goPsutil的更多相关文章
- ThinPHP命名空间,连接数据库是要修改的配置文件,Model数据模型层,跨控制器调用,如何获取系统常量信息,
一.命名空间(主要是为了实现自动加载类) *命名空间(相当于虚拟的目录),为了让类有一个统一的文件夹来管理(可以自动加载'类'),每个文件都要有命名空间*tp如何做命名空间:*TP框架下有一个初始命名 ...
- Delphi编程获取系统当前进程、窗口句柄、文件属性以(转)
Delphi编程获取系统当前进程.窗口句柄.文件属性以及程序运行状态. uses TLHelp32,PsAPI; (1)显示进程列表:procedure TForm1.Button2Click(Sen ...
- android 获取系统联系人 完全解析
一.代码 1.ContactsEngine.java import java.util.ArrayList; import java.util.HashMap; import java.util.Li ...
- Android获取系统时间方法的总结
Android获取系统时间方法的方法有很多种,常用的有Calendar.Date.currentTimeMills等方法. (1)Calendar Calendar获取系统时间首先要用Calendar ...
- Android 获取系统的联系人
本文主要介绍android中怎样获取系统的联系人数据 首先打开模拟器 点击联系人图标按钮 说明系统联系人数据库是空的,打开File explorer,找到data/data下面的文件夹: 将conta ...
- Linux sysinfo获取系统相关信息
Linux中,可以用sysinfo来获取系统相关信息. #include <stdio.h> #include <stdlib.h> #include <errno.h& ...
- android获取系统通讯录
package com.example.administrator.yunphone.View; import android.app.Fragment; import android.databas ...
- Android如何通过shareduserid获取系统权限
[原文] android会为每个apk进程分配一个单独的空间(比如只能访问/data/data/自己包名下面的文件),一般情况下apk之间是禁止相互访问数据的.通过Shared User id,拥有同 ...
- 用PHP获取系统时间时,时间比当前时间少8个小时
自PHP5.0开始,用PHP获取系统时间时,时间比当前时间少8个小时.原因是PHP.ini中没有设置timezone时,PHP是使用的UTC时间,所以在中国时间要少8小时. 解决办法: 1.在PHP. ...
随机推荐
- PyQt(Python+Qt)学习随笔:containers容器类部件QStackedWidget重要方法介绍
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 StackedWidget堆叠窗口部件为一系列窗口部件的堆叠,对应类为QStackedWidget. ...
- PyQt(Python+Qt)学习随笔:QTreeWidgetItem项列图标的访问方法
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 树型部件QTreeWidget中的QTreeWidgetItem项中可以有多列数据,每列数据都可以设 ...
- 【Docker】 .Net Core 3.1 webapi 集成EF Code First,使用MySql进行业务操作 、配置swagger (三)
系列目录: [Docker] CentOS7 安装 Docker 及其使用方法 ( 一 ) [Docker] 使用Docker 在阿里云 Centos7 部署 MySQL 和 Redis (二) [D ...
- Django框架简单认识(一)
1.Django是什么 Django是一个由python写成的开放源代码的Web应用框架. 2.Django安装 2.1 Django与python版本的安装版本匹配 Django 版本 Python ...
- 深入剖析HashMap
前言 很高兴遇见你~ HashMap是一个非常重要的集合,日常使用也非常的频繁,同时也是面试重点.本文并不打算讲解基础的使用api,而是深入HashMap的底层,讲解关于HashMap的重点知识.需要 ...
- 笔记-Recursive Queries
Recursive Queries \[m_{l,r}=\textrm{id}(\max_{i=l}^r a_i)\\ f(l,r)= \begin{cases} (r-l+1)+f(l,m_{l,r ...
- OpenWrt下基于OLSR的Ad-Hoc组网实现网络摄像头多节点访问
文章目录 Ad-Hoc组网配置 摄像头端口映射 PC连接设置 结果 Ad-Hoc组网配置 参照博客 链接: link. 摄像头端口映射 这里使用到了海康网络摄像头,先将网络摄像头的网口连接到任意一个节 ...
- 调用windows系统下的cmd命令窗口处理文件
从后缀名为grib2的文件中查询相关的信息,并将查出来的信息保存起来. 主要是学习java中调用windows下的cmd平台,并进行执行相关的命令. package com.wis.wgrib2; i ...
- 跨站点脚本编制 - SpringBoot配置XSS过滤器(基于Jsoup)
1. 跨站点脚本编制 风险:可能会窃取或操纵客户会话和 cookie,它们可能用于模仿合法用户,从而使黑客能够以该用户身份查看或变更用户记录以及执行事务. 原因:未对用户输入正确执行危险字符清 ...
- xwiki升级8.8.4
安装包下载: http://download.forge.ow2.org/xwiki/xwiki-enterprise-jetty-hsqldb-8.4.4.zip 推荐使用jetty包,方便快捷,不 ...