python psutil模块
程序员文章站
2022-07-14 10:04:49
...
安装
- 下载
https://pypi.python.org 网站下载
- 安装
$ tar xvf psutil-4.1.0.tar.gz
$ psutil-4.1.0
$ python setup.py install
功能
psutil 是一个跨平台库,能够轻松的实现获取系统运行的进程和系统利用率(包括cpu、内存、磁盘、网络等)信息。
获取系统性能信息
(1) cpu 信息
>>> import psutil
#使用 cpu_times 方法获取CPU的完整信息
>>> psutil.cpu_times()
scputimes(user=29835.75, nice=0.0, system=18475.44, idle=546295.5)
>>> psutil.cpu_times().user
29851.7
>>> psutil.cpu_times().nice
0.0
# 指定方法变量 percpu=true ,显示所有逻辑cpu信息
>>> psutil.cpu_times(percpu=True)
[scputimes(user=11854.24, nice=0.0, system=8223.53, idle=128562.62), scputimes(user=3587.42, nice=0.0, system=2143.94, idle=142905.42), scputimes(user=10573.42, nice=0.0, system=5857.35, idle=132206.04), scputimes(user=3817.91, nice=0.0, system=2248.85, idle=142570.0)]
# 获取cpu逻辑个数,默认方法 logical=True
>>> psutil.cpu_count()
4
# 获取cpu物理个数
>>> psutil.cpu_count(logical=False)
2
(2) 内存信息
Linux系统的内存利用率信息设计total(内存总数)、used(已使用的内存数)、free(空闲内存数)、buffers(缓冲使用数)、cache(缓存使用数)、swap(交换分区使用数)等,分别使用
psutil.virtual_memory()
与psutil.swap_memory()
方法获取。
>>> mem = psutil.virtual_memory()
>>> mem
svmem(total=1043046400, available=489234432, percent=53.1, used=973860864, free=69185536, active=649924608, inactive=220930048, buffers=105512960, cached=314535936)
>>> psutil.virtual_memory()
svmem(total=1043046400, available=489242624, percent=53.1, used=973860864, free=69185536, active=649924608, inactive=220938240, buffers=105521152, cached=314535936)
>>> psutil.virtual_memory().total
1043046400
>>> psutil.virtual_memory().free
68669440
>>> mem.free
69185536
>>> psutil.swap_memory()
sswap(total=0, used=0, free=0, percent=0.0, sin=0, sout=0)
(3) 磁盘信息
# psutil.disk_partitions 获取磁盘完整信息
>>> psutil.disk_partitions()
[sdiskpart(device='/dev/xvda1', mountpoint='/', fstype='ext4', opts='rw,barrier=0')]
# psutil.disk_usage 获取分区(参数)的使用情况
>>> psutil.disk_usage('/')
sdiskusage(total=42273669120, used=3997454336, free=36128837632, percent=9.5)
# psutil.disk_io_counters 获取磁盘总的IO个数
>>> psutil.disk_io_counters()
sdiskio(read_count=73895, write_count=163753, read_bytes=1255839744, write_bytes=1650024448, read_time=470216, write_time=3454793, read_merged_count=268, write_merged_count=238305, busy_time=362447)
# psutil.disk_io_counters(perdisk=Ture) 获取单个分区的IO个数
>>> psutil.disk_io_counters(perdisk=True)
{'xvda1': sdiskio(read_count=73895, write_count=163784, read_bytes=1255839744, write_bytes=1650151424, read_time=470216, write_time=3454878, read_merged_count=268, write_merged_count=238305, busy_time=362486)}
(4) 网络信息
# 获取网络总的IO信息,默认 pernic=False
>>> psutil.net_io_counters()
snetio(bytes_sent=60804444, bytes_recv=187669805, packets_sent=259503, packets_recv=344634, errin=0, errout=0, dropin=0, dropout=0)
# 获取每个网络接口的IO信息
>>> psutil.net_io_counters(pernic=True)
{'lo': snetio(bytes_sent=22812792, bytes_recv=22812792, packets_sent=91403, packets_recv=91403, errin=0, errout=0, dropin=0, dropout=0), 'eth1': snetio(bytes_sent=36941320, bytes_recv=164227892, packets_sent=154261, packets_recv=245344, errin=0, errout=0, dropin=0, dropout=0), 'eth0': snetio(bytes_sent=1052292, bytes_recv=631555, packets_sent=13857, packets_recv=7922, errin=0, errout=0, dropin=0, dropout=0)}
(5) 其他系统信息
# 获取当前登陆用户的信息
>>> psutil.users()
[suser(name='root', terminal='pts/0', host='180.168.112.222', started=1461911552.0)]
# 获取开机时长
>>> psutil.boot_time()
1461576707.0
# 将开机时长转换成自然格式
>>> import datetime,psutil
>>> datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S")
'2016-04-25 17:31:47'
系统进程管理方法
(1) 进程信息
>>> import psutil
# 列出所有进程PID
>>> psutil.pids()
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 40, 42, 43, 73, 147, 148, 225, 226, 308, 525, 601, 823, 842, 874, 937, 969, 1003, 1020, 1022, 1024, 1026, 1027, 1029, 1031, 1059, 1094, 1392, 5427, 12014, 12016, 12105, 13842, 14099, 14191]
# 实例化一个Process对象,参数为一进程PID
>>> p = psutil.Process(14191)
# 进程名
>>> p.name()
'java'
# 进程bin路径
>>> p.exe()
'/usr/local/jdk/bin/java'
# 进程工作目录绝对路径
>>> p.cwd()
'/usr/local/nginx/conf/vhosts'
# 进程状态
>>> p.status()
'sleeping'
# 进程创建时间
>>> p.create_time()
1461738772.66
# 进程 uid 信息
>>> p.uids()
puids(real=0, effective=0, saved=0)
# 进程 git 信息
>>> p.gids()
pgids(real=0, effective=0, saved=0)
# 进程CPU时间信息,报错user、system两个CPU时间
>>> p.cpu_times()
pcputimes(user=263.48, system=43.18, children_user=0.21, children_system=0.13)
#get进程亲和度
>>> p.cpu_affinity()
[0]
# 进程内存利用率
>>> p.memory_percent()
42.151973296681724
# 进程内存rss、vms信息
>>> p.memory_info()
pmem(rss=439664640, vms=2446684160, shared=8585216, text=4096, lib=0, data=2281676800, dirty=0)
# 进程IO信息
>>> p.io_counters()
pio(read_count=870377, write_count=40979, read_bytes=21909504, write_bytes=115916800)
# 返回打开进程socket 的namedutples 列表,包括fd、family、laddr等信息
>>> p.connections()
[pconn(fd=343, family=2, type=2, laddr=('0.0.0.0', 5353), raddr=(), status='NONE'), pconn(fd=330, family=2, type=1, laddr=('0.0.0.0', 54711), raddr=(), status='LISTEN'), pconn(fd=47, family=2, type=1, laddr=('0.0.0.0', 8009), raddr=(), status='LISTEN'), pconn(fd=341, family=2, type=2, laddr=('0.0.0.0', 33848), raddr=(), status='NONE'), pconn(fd=340, family=2, type=1, laddr=('0.0.0.0', 32996), raddr=(), status='LISTEN'), pconn(fd=46, family=2, type=1, laddr=('0.0.0.0', 8080), raddr=(), status='LISTEN'), pconn(fd=180, family=2, type=1, laddr=('127.0.0.1', 8005), raddr=(), status='LISTEN')]
# 进程开启等线程数
>>> p.num_threads()
54
(2) popen 类的使用
# 通过psutil 的Popen方法启动的应用程序,可以跟踪该程序运行的所有相关信息
>>> psutil.Popen(["/usr/bin/python", "-c", "print('hello')"], stdout=PIPE)
<psutil.Popen(pid=12139, name='python') at 32848976>
>>> p = psutil.Popen(["/usr/bin/python", "-c", "print('hello')"], stdout=PIPE)
>>> p.username()
'root'
>>> p.communicate()
('hello\n', None)
上一篇: Python-psutil模块
下一篇: 安装psutil模块报错