采集系统的基本性能信息包括CPU、内存、磁盘、网络等,可以完整描述当前系统的运行状态及质量。psutil模块已经封装了这些方法,用户可以根据自身的应用场景,调用相应的方法来满足需求,非常简单实用。
(1)内存信息
Linux系统的内存利用率信息涉及total(内存总数)、used(已使用的内存数)、free(空闲内存数)、buffers(缓冲使用数)、cache(缓存使用数)、swap(交换分区使用数)等,分别使用psutil.virtual_memory()与psutil.swap_memory()方法获取这些信息,具体见下面的操作例子:
import psutil
phymem=psutil.virtual_memory()
print('内存信息:',phymem)
print(type(phymem.used))
print('已使用内存:',str(round(phymem.used/1024/1024/1024,1))+'G')
print('总内存:',str(round(phymem.total/1024/1024/1024,1))+'G')
print('空闲内存:',str(round(phymem.free/1024/1024/1024,5))+'G')
print('获取SWAP分区信息:',psutil.swap_memory())
运行结果:
内存信息: svmem(total=8589934592, available=1781874688, percent=79.3, used=6217367552, free=36175872, active=1827381248, inactive=1745698816, wired=2644287488)
'int'>
已使用内存: 5.8G
总内存: 8.0G
空闲内存: 0.03369G
获取SWAP分区信息: sswap(total=3221225472, used=1991770112, free=1229455360, percent=61.8, sin=317321252864, sout=29568856064)
(2)网络信息:
系统的网络信息与磁盘IO类似,涉及几个关键点,包括bytes_sent(发送字节数)、bytes_recv=28220119(接收字节数)、packets_sent=200978(发送数据包数)、packets_recv=212672(接收数据包数)等。这些网络信息使用psutil.net_io_counters()方法获取,具体见下面的操作例子:
net_io=psutil.net_io_counters()
print('获取网络总的IO信息:',net_io)
net_in=psutil.net_io_counters(pernic=True)
print('pernic=True:每一个网络接口的IO信息:',net_in)
print('发送的字节数:bytes_sent:',net_io.bytes_sent)
print('接受的字节数:bytes_recv:',net_io.bytes_recv)
print('发送的数据包数:packets_sent:',net_io.packets_sent)
print('接受的数据包数:packets_recv:',net_io.packets_recv)
运行结果:
获取网络总的IO信息: snetio(bytes_sent=430844928, bytes_recv=6605564928, packets_sent=2947768, packets_recv=9266213, errin=0, errout=0, dropin=0, dropout=0)
pernic=True:每一个网络接口的IO信息: {'lo0': snetio(bytes_sent=8908800, bytes_recv=8908800, packets_sent=53661, packets_recv=53661, errin=0, errout=0, dropin=0, dropout=0), 'gif0': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'stf0': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'XHC20': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'en0': snetio(bytes_sent=421856256, bytes_recv=6596570112, packets_sent=2893764, packets_recv=9212165, errin=0, errout=0, dropin=0, dropout=0), 'p2p0': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'awdl0': snetio(bytes_sent=69632, bytes_recv=68608, packets_sent=285, packets_recv=304, errin=0, errout=0, dropin=0, dropout=0), 'en1': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'bridge0': snetio(bytes_sent=0, bytes_recv=0, packets_sent=1, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'utun0': snetio(bytes_sent=0, bytes_recv=0, packets_sent=2, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'utun1': snetio(bytes_sent=10240, bytes_recv=11264, packets_sent=55, packets_recv=52, errin=0, errout=0, dropin=0, dropout=0), 'vmnet1': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'vmnet8': snetio(bytes_sent=0, bytes_recv=6144, packets_sent=0, packets_recv=31, errin=0, errout=0, dropin=0, dropout=0)}
发送的字节数:bytes_sent: 430844928
接受的字节数:bytes_recv: 6605564928
发送的数据包数:packets_sent: 2947768
接受的数据包数:packets_recv: 9266213
(3)CPU信息
cpu_time=psutil.cpu_times()
print('获取CPU完整信息:',cpu_time)
cpu_times=psutil.cpu_times(percpu=True)
print('获取所有逻辑CPU信息:',cpu_times)
# print('由于IO等待而使CPU处于idle(空闲状态)的时间百分比:',cpu_time.user)
print('获取CPU的逻辑个数:',psutil.cpu_count()) #morenlogical=True
print('获取CPU的物理个数:',psutil.cpu_count(logical=False))
print('列出所有进程:',psutil.pids())
print('判断某个进程是否存在:',psutil.pid_exists(400))
p=psutil.Process(411) #实例化411的进程
print('411进程:',p)
print('进程名字:',p.name())
print('进程路径:',p.exe())
print('进程工作目录绝对路径:',p.cwd())
print('进程状态:',p.status())
print('进程创建时间:',p.create_time())
print('进程内存利用率:',p.memory_percent())
print('进程内存信息:',p.memory_info())
#print('进程IO信息:',p.io_counters()) #要确保该进程对象有io操作
print('进程scoket的namedutples列表')
print('进程开启的线程数:',p.num_threads())
运行结果:
获取CPU完整信息: scputimes(user=215223.19, nice=0.0, system=77141.81, idle=764077.41)
获取所有逻辑CPU信息: [scputimes(user=66956.22, nice=0.0, system=23825.91, idle=173339.45), scputimes(user=39515.12, nice=0.0, system=16182.41, idle=208409.41), scputimes(user=68534.44, nice=0.0, system=20926.37, idle=174646.16), scputimes(user=40217.41, nice=0.0, system=16207.12, idle=207682.39)]
获取CPU的逻辑个数: 4
获取CPU的物理个数: 2
列出所有进程: [18786, 18765, 18755, 18745, 18727, 18726, 18725, 18724, 18723, 18722, 18716, 18621, 18618, 18616, 18613, 18612, 18611, 18602, 18599, 18598, 18596, 18560, 18464, 18463, 18460, 18459, 18455, 18454, 18453, 18416, 18412, 18411, 18410, 18409, 18408, 18341, 18196, 18179, 18091, 18068, 18067, 18046, 18003, 18002, 17886, 17864, 17861, 17859, 17858, 17855, 17854, 17852, 17851, 17850, 17848, 17845, 17844, 17843, 17834, 17833, 17831, 17829, 17828, 17827, 17826, 17821, 17819, 17818, 17817, 17816, 17815, 17808, 17798, 17691, 17689, 17688, 17686, 17681, 17680, 17678, 17677, 17676, 17675, 17674, 17673, 17672, 17671, 17670, 17669, 17661, 17653, 17652, 17646, 17642, 17639, 17556, 17555, 17515, 17514, 17446, 14929, 14626, 14611, 14265, 14092, 13719, 13646, 13644, 13306, 13250, 13236, 13203, 13074, 13067, 12711, 12710, 12663, 11395, 11139, 9956, 9955, 9953, 9951, 9950, 9948, 9935, 9933, 9832, 9827, 9825, 9823, 9821, 9818, 9816, 9813, 9770, 9767, 9728, 9332, 9330, 9329, 9327, 7997, 7787, 7675, 7491, 7489, 7355, 7353, 7242, 7230, 7126, 7113, 5858, 5851, 5193, 4230, 4229, 4228, 4227, 4226, 4225, 4221, 4219, 4218, 3899, 3797, 3661, 2902, 2897, 2679, 2481, 2455, 2454, 2448, 2444, 2443, 2442, 2441, 2440, 2439, 2438, 2437, 2436, 2435, 2434, 2430, 2424, 2421, 2015, 2012, 1927, 1923, 1170, 1019, 939, 895, 749, 654, 640, 625, 607, 545, 543, 541, 539, 537, 532, 524, 521, 520, 511, 497, 491, 490, 488, 483, 481, 480, 473, 467, 465, 464, 462, 447, 445, 443, 442, 439, 437, 435, 432, 427, 425, 424, 421, 419, 417, 414, 413, 411, 408, 407, 406, 405, 403, 401, 399, 398, 397, 394, 392, 386, 380, 379, 378, 370, 367, 365, 362, 360, 358, 356, 347, 338, 337, 334, 333, 332, 330, 323, 320, 319, 318, 317, 312, 311, 310, 308, 307, 306, 302, 298, 297, 292, 287, 286, 284, 283, 282, 281, 278, 277, 276, 275, 274, 273, 271, 269, 268, 267, 266, 245, 243, 239, 220, 213, 204, 202, 201, 171, 168, 164, 163, 162, 160, 157, 153, 152, 134, 131, 123, 117, 112, 110, 109, 108, 106, 105, 104, 103, 101, 100, 99, 98, 97, 94, 93, 92, 91, 89, 87, 86, 84, 83, 82, 81, 76, 74, 72, 69, 68, 62, 59, 58, 57, 55, 54, 50, 49, 48, 45, 44, 1, 0, 9936]
判断某个进程是否存在: False
411进程: psutil.Process(pid=411, name='nbagent', started='2018-08-13 10:50:16')
进程名字: nbagent
进程路径: /System/Library/PrivateFrameworks/Noticeboard.framework/Versions/A/Resources/nbagent.app/Contents/MacOS/nbagent
进程工作目录绝对路径: /
进程状态: running
进程创建时间: 1534128616.486395
进程内存利用率: 0.05955696105957031
进程内存信息: pmem(rss=5115904, vms=4487757824, pfaults=128870, pageins=164)
进程scoket的namedutples列表
进程开启的线程数: 5
(4)硬盘信息
在系统的所有磁盘信息中,我们更加关注磁盘的利用率及IO信息,其中磁盘利用率使用psutil.disk_usage方法获取。磁盘IO信息包括read_count(读IO数)、write_count(写IO数)、read_bytes(IO读字节数)、write_bytes(IO写字节数)、read_time(磁盘读时间)、write_time(磁盘写时间)等。这些IO信息可以使用psutil.disk_io_counters()获取,具体见下面的操作例子:
disk_msg=psutil.disk_partitions()
print('获取磁盘完整信息:',disk_msg)
print('获取分区(参数)的使用情况:',psutil.disk_usage('/'))
print('获取磁盘总的io个数:',psutil.disk_io_counters())
print('获取单个分区的读写信息:',psutil.disk_io_counters(perdisk=True))
运行结果:
获取磁盘完整信息: [sdiskpart(device='/dev/disk1s1', mountpoint='/', fstype='apfs', opts='rw,local,rootfs,dovolfs,journaled,multilabel'), sdiskpart(device='/dev/disk1s4', mountpoint='/private/var/vm', fstype='apfs', opts='rw,noexec,local,dovolfs,dontbrowse,journaled,multilabel,noatime')]
获取分区(参数)的使用情况: sdiskusage(total=121123069952, used=58478456832, free=57681903616, percent=50.3)
获取磁盘总的io个数: sdiskio(read_count=15718813, write_count=7610058, read_bytes=487984355840, write_bytes=252641662976, read_time=11053734, write_time=3306953)
获取单个分区的读写信息: {'disk0': sdiskio(read_count=15718576, write_count=7610058, read_bytes=487959991808, write_bytes=252641662976, read_time=11049616, write_time=3306953), 'disk2': sdiskio(read_count=237, write_count=0, read_bytes=24364032, write_bytes=0, read_time=4118, write_time=0)}
import psutil
pids = psutil.pids()
for pid in pids:
p = psutil.Process(pid)
# print('pid-%s,pname-%s' % (pid, p.name()))
if p.name() == 'dllhost.exe':
cmd = 'taskkill /F /IM dllhost.exe'
os.system(cmd)