系统性能模块psutil

psutil是一个跨平台库,能够轻松实现获取系统运行的进程和系统利用率(包括cpu、内存、磁盘、网络等)信息。它主要用于系统监控,分析和限制系统资源及进程的管理。它实现了同等命令行工具提供的功能,如ps、top等。

一、获取系统性能信息

采集系统性能的信息包括cpu、内存、硬盘、网络等,可以完整描述当前系统的运行状态及质量。

1.cpu信息

linux操作系统的cpu利用率有以下几个部分:

User Time:执行用户进程的时间比百分比;

System Time:执行内核进程和中断的时间比百分比;

Wait IO:由IO等待而使cpu处于idle(空闲)状态的时间比;

Idle:cpu处于空闲状态的时间百分比。

In [1]: import psutil

In [2]: psutil.cpu_times()   #查看cpu的完整时间
Out[2]: scputimes(user=21.27, nice=0.08, system=25.34, idle=10201.62, iowait=35.87, irq=0.0, softirq=0.59, steal=0.0, guest=0.0, guest_nice=0.0)

#如果需要单项查找,直接接到后面就能查询
In [3]: psutil.cpu_times().user  #查看用户user的cpu时间比
Out[3]: 21.46

In [4]: psutil.cpu_count()   #获取cpu的逻辑个数
Out[4]: 1

In [5]: psutil.cpu_count(logical=False)   #获取cpu的物理个数
Out[5]: 1

2.内存信息

linux系统的内存利用率信息涉及total(内存总数)、used(已使用的内存数)、free(空闲内存数)、buffers(缓冲使用数)、cache(缓存使用数)、sawp(交换分区使用数)等,可以一起获取也可以单项获取。

In [6]: mem = psutil.virtual_memory()   #获取完整内存信息

In [7]: mem
Out[7]: svmem(total=1023934464, available=680968192, percent=33.5, used=187260928, free=533970944, active=225165312, inactive=129376256, buffers=1433600, cached=301268992, shared=7016448)

In [8]: mem.total   #获取内存总数
Out[8]: 1023934464

In [9]: mem.free    #获取空闲内存数
Out[9]: 533970944

In [10]: psutil.swap_memory()  #获取swap分区信息
Out[10]: sswap(total=2147479552, used=0, free=2147479552, percent=0.0, sin=0, sout=0)

3.磁盘信息

在系统所有的磁盘信息中,我们更加关注磁盘的利用率及IO信息。

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

In [11]: psutil.disk_partitions()  #获取磁盘完整信息
Out[11]: 
[sdiskpart(device='/dev/sda3', mountpoint='/', fstype='xfs', opts='rw,seclabel,relatime,attr2,inode64,noquota'),
 sdiskpart(device='/dev/sda1', mountpoint='/boot', fstype='xfs', opts='rw,seclabel,relatime,attr2,inode64,noquota'),
 sdiskpart(device='/dev/sr0', mountpoint='/mnt', fstype='iso9660', opts='ro,relatime')]

In [12]: psutil.disk_usage('/')获取分区使用情况
Out[12]: sdiskusage(total=18791530496, used=1923350528, free=16868179968, percent=10.2)

In [13]: psutil.disk_io_counters()  #获取磁盘总的IO数及读写信息
Out[13]: sdiskio(read_count=8209, write_count=6079, read_bytes=227018240, write_bytes=118113792, read_time=138391, write_time=184971, read_merged_count=9, write_merged_count=3272, busy_time=94446)
In [15]: psutil.disk_io_counters(perdisk=True)  #获取单个分区IO数以及读写信息
Out[15]: 
{'sda1': sdiskio(read_count=2061, write_count=2061, read_bytes=20986880, write_bytes=2138112, read_time=18287, write_time=1472, read_merged_count=0, write_merged_count=0, busy_time=18477),
 'sda2': sdiskio(read_count=104, write_count=0, read_bytes=995328, write_bytes=0, read_time=1893, write_time=0, read_merged_count=0, write_merged_count=0, busy_time=1774),
 'sda3': sdiskio(read_count=5762, write_count=4035, read_bytes=201087488, write_bytes=116073472, read_time=117847, write_time=183589, read_merged_count=9, write_merged_count=3274, busy_time=73910),
 'sr0': sdiskio(read_count=282, write_count=0, read_bytes=3948544, write_bytes=0, read_time=364, write_time=0, read_merged_count=0, write_merged_count=0, busy_time=363)

4.网络信息

系统的网络信息包括bytes_sent(发送字节数)、bytes_recv(接收字节数)、packets_sent(发送数据包数)、packets_recv(接受数据包数)等

In [16]: psutil.net_io_counters()   #网络总的IO信息
Out[16]: snetio(bytes_sent=1103875, bytes_recv=10817036, packets_sent=7171, packets_recv=15141, errin=0, errout=0, dropin=0, dropout=0)

In [17]: psutil.net_io_counters(pernic=True)  #每个网络接口的IO信息
Out[17]: 
{'ens33': snetio(bytes_sent=1102723, bytes_recv=10814440, packets_sent=7129, packets_recv=15113, errin=0, errout=0, dropin=0, dropout=0),
 'lo': snetio(bytes_sent=6596, bytes_recv=6596, packets_sent=77, packets_recv=77, errin=0, errout=0, dropin=0, dropout=0)}

5.其它系统信息

psutil还可以获取用户登陆、开机时间等

In [2]: psutil.users()   #当前登陆系统的用户信息
Out[2]: 
[suser(name='root', terminal='tty1', host='root', started=1511610624.0, pid=526),
 suser(name='root', terminal='pts/1', host='root', started=1511616000.0, pid=2608)]
In [7]: psutil.boot_time()  #开机时间
Out[7]: 1511608620.0
In [8]: import time
In [9]: time.strftime("%Y-%m-%d %M:%H:%S",time.localtime(psutil.boot_time()))   #转化成格式化时间字符串
Out[9]: '2017-11-25 17:19:00'

二、系统进程管理方法

获得当前系统的进程信息,可以获取应用程序的运行状态,包括进程的启动时间、查看和设置cpu亲和度、内存使用率、IO信息、socket连接、线程数等,这些信息可以呈现出指定进程是否存活、资源利用情况等。

1.进程信息

In [10]: psutil.pids()  #列出所有进程的pid
Out[10]: 
[1, 2,3,7.....]


In [11]: p = psutil.Process(1)   #实例化一个进程对象

In [12]: p.name()   #进程名
Out[12]: 'systemd'

In [13]: p.exe()   #进程的bin路径
Out[13]: '/usr/lib/systemd/systemd'

In [14]: p.cwd()  #进程的工作目录绝对路径
Out[14]: '/'

In [15]: p.status()  #进程状态
Out[15]: 'sleeping'

In [16]: p.create_time()  #进程的创建时间
Out[16]: 1511608620.01

In [17]: p.uids()  #进程的uid信息
Out[17]: puids(real=0, effective=0, saved=0)

In [18]: p.gids()   #进程的gid信息
Out[18]: pgids(real=0, effective=0, saved=0)

In [19]: p.cpu_times()   #进程的CPU时间信息
Out[19]: pcputimes(user=0.3, system=2.04, children_user=18.74, children_system=19.09)

In [20]: p.cpu_affinity()  #进程cpu亲和度,如要设置cpu亲和度,将cpu号作为参考即可
Out[20]: [0]

In [21]: p.memory_percent()   #进程内存利用率
Out[21]: 0.6712429595494112

In [22]: p.memory_info()  #进程内存rss、vms信息
Out[22]: pmem(rss=6873088, vms=131166208, shared=4050944, text=1327104, lib=0, data=86659072, dirty=0)

In [23]: p.io_counters()  #进程的IO信息,包括读写IO数字及参数
Out[23]: pio(read_count=538046, write_count=179479, read_bytes=179045888, write_bytes=123748352, read_chars=635264297, write_chars=352107017)

In [24]: p.connections()  #返回进程列表
Out[24]: []

In [25]: p.num_threads()  #进程开启的线程数
Out[25]: 1

2.popen类的使用

pustil提供的popen类的作用是获取用户启动的应用程序进程信息,以便跟踪程序进程的运行状态。

In [26]: from subprocess import PIPE
#通过psutil的popen方法启动的应用程序,可以跟踪该应用程序运行的所有相关信息

In [27]: p = psutil.Popen(["/usr/bin/python", "-c", "print('hello')"]
    ...: ,stdout=PIPE)

In [28]: p.name()
Out[28]: 'python'

In [29]: p.username()
Out[29]: 'root'

In [30]: p.communicate()
Out[30]: (b'hello
', None)

In [31]: p.cpu_times()
原文地址:https://www.cnblogs.com/yangmingxianshen/p/7898151.html