欢迎访问我的博客,你的支持,是我最大的动力!

Psutil-系统性能信息模块

Psutil 小马奔腾 11641℃ 评论
目录:
[显示]

psutil能够获取系统运行的进程和利用率信息(CPU、内存、磁盘、网络等),主要用于系统监控,分析和限制系统资源的进程管理。它实现了等同于命令行工具的功能,如ps、top、lsof、netstat、ifconfig、who、df、kill、free、nice、ionice、iostat、iotop、uptime、pidof、tty、taskset、pmap等。

psutil大小单位一般都是字节(1byte=8bit),单位换算方法:

1byte=8bit

1KB=1024byte

1MB=1024KB=1024*1024byte

1GB=1024MB=1024*1024KB=1024*1024*1024byte

安装

当前版本:5.4.3

源码地址:https://github.com/giampaolo/psutil

pip install psutil  或者 python setup.py install

python -m pip install psutil

使用对比:

传统方法获取物理内存used值:free -m|grep Mem|awk '{print $3}'   #单位MB

psutil获取物理内存used值:

import psutil
mem=psutil.virtual_memory()
mem.used     #单位byte
可同时获取total,available,percent,used,free,active,inactive,buffers,cached,shared
#percent=(total-available)/total*100

CPU信息

user time:执行用户进程的时间百分比
system time:执行内核心进程和中断的时间百分比
wait io:由于IO等待而使CPU处于idle状态的时间百分比
idle:CPU处于idle状态的时间百分比
同时也可以获取硬件相关信息,如物理个数、逻辑个数等

#获取cpu使用情况
cpu = psutil.cpu_times()  #psutil.cpu_times(percpu=True)  分核心显示cpu[0].user
cpu
scputimes(user=327.98, nice=65.12, system=188.42, idle=17444.75, iowait=21.07, irq=0.0, softirq=9.18, steal=0.0, guest=0.0, guest_nice=0.0)
cpu.user  #取单个值
327.98

#获取cpu使用情况百度比形式
cpu = psutil.cpu_times_percent()
cpu
scputimes(user=2.4, nice=0.0, system=0.9, idle=96.7, iowait=0.1, irq=0.0, softirq=0.0, steal=0.0, guest=0.0, guest_nice=0.0)

#获取cpu状态
cpu = psutil.cpu_stats()
cpu
scpustats(ctx_switches=3656198, interrupts=1431176, soft_interrupts=1197236, syscalls=0)

#获取cpu频率
cpu = psutil.cpu_freq()
cpu#获取cpu核心数
cpu = psutil.cpu_count()  #psutil.cpu_cpunt(logical=False)  获取物理个数
cpu
1

内存信息

#获取交换分区使用情况
mem = psutil.swap_memory()
mem
sswap(total=1071640576, used=4993024, free=1066647552, percent=0.5, sin=32768, sout=4866048)

#获取内存使用情况
mem = psutil.virtual_memory()
mem
svmem(total=2065960960, available=768950272, percent=62.8, used=1106608128, free=116191232, active=1009811456, inactive=531845120, buffers=46563328, cached=796598272, shared=13750272)

磁盘信息

磁盘IO信息包括read_count(读IO数)、write_count(写IO数)、read_bytes(读字节数)、write_bytes(写字节数)、read_time(读时间)、write_time(写时间)

#获取磁盘分区信息
disk = psutil.disk_partitions()
disk
[sdiskpart(device='/dev/sda1', mountpoint='/', fstype='ext4', opts='rw,relatime,errors=remount-ro,data=ordered')]
disk[0].device
'/dev/sda1'

#获取磁盘使用情况,单位字节
disk = psutil.disk_usage('/')
disk
sdiskusage(total=9376751616, used=6017826816, free=2859016192, percent=67.8)
disk.total
9376751616

#获取磁盘总IO
disk = psutil.disk_io_counters()
disk
sdiskio(read_count=41533, write_count=18655, read_bytes=1222777856, write_bytes=1739624448, read_time=102556, write_time=537876, read_merged_count=6601, write_merged_count=45845, busy_time=109716)
disk.read_bytes
1222777856

#获取磁盘IO,区分分区
disk = psutil.disk_io_counters(perdisk=True)
disk
{'sr0': sdiskio(read_count=0, write_count=0, read_bytes=0, write_bytes=0, read_time=0, write_time=0, read_merged_count=0, write_merged_count=0, busy_time=0), 'sda5': sdiskio(read_count=45, write_count=28, read_bytes=2289664, write_bytes=4866048, read_time=36, write_time=528, read_merged_count=7, write_merged_count=1160, busy_time=460), 'sda2': sdiskio(read_count=2, write_count=0, read_bytes=2048, write_bytes=0, read_time=4, write_time=0, read_merged_count=0, write_merged_count=0, busy_time=4), 'sda1': sdiskio(read_count=41486, write_count=18664, read_bytes=1220486144, write_bytes=1735114752, read_time=102516, write_time=537616, read_merged_count=6594, write_merged_count=44735, busy_time=109324)}
disk['sr0'].read_bytes
0

网络信息

#获取系统当前连接情况
net = psutil.net_connections()
net  #列表
[sconn(fd=-1, family=2, type=2, laddr=addr(ip='0.0.0.0', port=41026), raddr=(), status='NONE', pid=None), sconn(fd=-1, family=2, type=2, laddr=addr(ip='0.0.0.0', port=631), raddr=(), status='NONE', pid=None), sconn(fd=23, family=2, type=1, laddr=addr(ip='127.0.0.1', port=42401), raddr=(), status='LISTEN', pid=10578), sconn(fd=3, family=2, type=1, laddr=addr(ip='127.0.0.1', port=43944), raddr=addr(ip='127.0.0.1', port=45735), status='ESTABLISHED', pid=10600), sconn(fd=-1, family=10, type=2, laddr=addr(ip='::', port=55349), raddr=(), status='NONE', pid=None), sconn(fd=31, family=2, type=1, laddr=addr(ip='127.0.0.1', port=45735), raddr=addr(ip='127.0.0.1', port=43944), status='ESTABLISHED', pid=10578), sconn(fd=-1, family=2, type=1, laddr=addr(ip='127.0.0.1', port=631), raddr=(), status='LISTEN', pid=None), sconn(fd=-1, family=10, type=2, laddr=addr(ip='::', port=5353), raddr=(), status='NONE', pid=None), sconn(fd=-1, family=2, type=2, laddr=addr(ip='0.0.0.0', port=5353), raddr=(), status='NONE', pid=None), sconn(fd=-1, family=2, type=1, laddr=addr(ip='127.0.1.1', port=53), raddr=(), status='LISTEN', pid=None), sconn(fd=-1, family=10, type=1, laddr=addr(ip='::1', port=631), raddr=(), status='LISTEN', pid=None), sconn(fd=-1, family=2, type=2, laddr=addr(ip='127.0.1.1', port=53), raddr=(), status='NONE', pid=None), sconn(fd=-1, family=2, type=1, laddr=addr(ip='0.0.0.0', port=22), raddr=(), status='LISTEN', pid=None), sconn(fd=-1, family=10, type=1, laddr=addr(ip='::', port=22), raddr=(), status='LISTEN', pid=None), sconn(fd=-1, family=2, type=2, laddr=addr(ip='0.0.0.0', port=56450), raddr=(), status='NONE', pid=None)]
net[0].type  #类似于字典
2
net[0].laddr
addr(ip='0.0.0.0', port=41026)
net[0].laddr.ip
'0.0.0.0'#获取接品地址信息
net = psutil.net_if_addrs()
net  #字典
{'lo': [snic(family=2, address='127.0.0.1', netmask='255.0.0.0', broadcast=None, ptp=None), snic(family=10, address='::1', netmask='ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff', broadcast=None, ptp=None), snic(family=17, address='00:00:00:00:00:00', netmask=None, broadcast=None, ptp=None)], 'ens33': [snic(family=2, address='192.168.158.110', netmask='255.255.255.0', broadcast='192.168.158.255', ptp=None), snic(family=10, address='fe80::e447:8c1:f0d6:1d86%ens33', netmask='ffff:ffff:ffff:ffff::', broadcast=None, ptp=None), snic(family=17, address='00:0c:29:e3:23:1f', netmask=None, broadcast='ff:ff:ff:ff:ff:ff', ptp=None)]}
net['lo']
[snic(family=2, address='127.0.0.1', netmask='255.0.0.0', broadcast=None, ptp=None), snic(family=10, address='::1', netmask='ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff', broadcast=None, ptp=None), snic(family=17, address='00:00:00:00:00:00', netmask=None, broadcast=None, ptp=None)]
net['lo'][0].address
'127.0.0.1'

#获取网络接口状态
net = psutil.net_if_stats()
net
{'lo': snicstats(isup=True, duplex=0, speed=0, mtu=65536), 'ens33': snicstats(isup=True, duplex=2, speed=1000, mtu=1500)}
net['ens33'].mtu
1500

#获取网络接口IO信息
net = psutil.net_io_counters()
net
snetio(bytes_sent=2546995, bytes_recv=108366996, packets_sent=33670, packets_recv=88368, errin=0, errout=0, dropin=0, dropout=0)

其它系统信息

#获取系统启动时刻
a = psutil.boot_time()
a
1516589844.0
datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S")
'2018-01-22 10:57:24'
#获取进程pid
a = psutil.pids()
a
[1, 2, 4, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 32, 33, 34, 76, 77, 78, 79, 80, 81, 87, 113, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 255, 263, 303, 304, 339, 360, 396, 526, 805, 809, 812, 828, 830, 835, 836, 837, 856, 897, 898, 903, 938, 944, 950, 965, 974, 1048, 1104, 1125, 1126, 1151, 1370, 1376, 1392, 1414, 1415, 1418, 1421, 1431, 1438, 1449, 1461, 1463, 1464, 1467, 1480, 1506, 1513, 1520, 1527, 1544, 1549, 1571, 1608, 1631, 1640, 1689, 1692, 1699, 1703, 1723, 1727, 1729, 1731, 1735, 1743, 1747, 1748, 1749, 1750, 1754, 1810, 1811, 1818, 1882, 1885, 1917, 1948, 1953, 1966, 1982, 1983, 1987, 2083, 2102, 2118, 2122, 2124, 2129, 2138, 2144, 2155, 2167, 2184, 2207, 2229, 2266, 2273, 2280, 2282, 2372, 2382, 2383, 2467, 2516, 3042, 3043, 3045, 4265, 4277, 4291, 4324, 4338, 10578, 10600, 10613, 10619, 10639, 10640, 10859, 11214, 11306]#获取电池情况、风扇、温度    sensors_fans/sensors_temperaturs
a = psutil.sensors_battery()
a

#获取用户信息
a = psutil.users()
a
[suser(name='dong', terminal='tty7', host='localhost', started=1516589824.0, pid=1151)]

进程管理

 

 

 

 

 

 

 

 

 

转载请注明:轻风博客 » Psutil-系统性能信息模块

喜欢 (1)or分享 (0)