9. psutil 检测系统各项指标信息
psutil是一个跨平台库,能够轻松实现获取系统的运行进程和系统利用率(如:cpu,内存,磁盘,网络等)信息,它主要用于系统监控,分析和限制系统资源及进程的管理。
psutil默认以字节显示(整型int),直接除以3个1024可转换为GB
9.1 获取系统性能信息
cpu
psutil.cpu_times()完整信息解释说明:
字段名称
含义
说明
user
用户态 CPU 时间
CPU 在用户态下运行普通用户程序所占用的时间。
nice
低优先级用户态 CPU 时间
CPU 在用户态下运行低优先级进程(通过 nice
命令调整优先级的进程)所占用的时间。
system
内核态 CPU 时间
CPU 在内核态下运行内核代码(如设备驱动程序、文件系统操作等)所占用的时间。
idle
空闲 CPU 时间
CPU 处于空闲状态的时间,即 CPU 没有执行任何任务的时间。
iowait
I/O 等待时间
CPU 等待 I/O 操作完成的时间(如磁盘读写操作)。
irq
硬件中断时间
CPU 处理硬件中断(如键盘、鼠标、网卡等外部设备触发的中断)所占用的时间。
softirq
软件中断时间
CPU 处理软件中断(如网络协议栈处理、定时器中断等)所占用的时间。
steal
被虚拟机偷走的时间
在虚拟化环境中,CPU 时间被宿主机或其他虚拟机占用的时间。在物理机上通常为 0.0
。
guest
运行虚拟机的时间
CPU 在运行虚拟机(如 KVM、Xen 等)时所占用的时间。在物理机上通常为 0.0
。
guest_nice
运行低优先级虚拟机的时间
CPU 在运行低优先级虚拟机时所占用的时间。在物理机上通常为 0.0
。
In [3]: import psutil
In [4]: cpu = psutil.cpu_times()
print (cpu)
In [5]: print (cpu)
scputimes(user=46999.48, nice =569.85, system=21047.34, idle=12173417.49, iowait=729.86, irq=0.0, softirq=89.96, steal=0.0, guest=0.0, guest_nice=0.0)
In [6]: print (f"user:{cpu.user},system:{cpu.system},idle:{cpu.idle}" )
user:46999.48,system:21047.34,idle:12173417.49
In [7]:
psutil.cpu_percent(interval=None)返回当前系统范围的cpu使用率(百分比)
In [4]: psutil.cpu_percent(interval=1)
Out[4]: 0.0
psutil.cpu_times_percent() 返回系统cpu时间的百分比
In [5]: psutil.cpu_times_percent()
Out[5]: scputimes(user=0.5, nice =0.0, system=0.2, idle=99.4, iowait=0.0, irq=0.0, softirq=0.0, steal=0.0, guest=0.0, guest_nice=0.0)
psutil.cpu_count()返回cpu的总数
In [6]: psutil.cpu_count()
Out[6]: 2
内存
In [1]: import psutil
In [2]: mem = psutil.virtual_memory()
In [3]: print (mem)
In [4]: print (f"total:{mem.total},used:{mem.used}" )
total:1754292224,used:333479936
In [5]: mem_swap = psutil.swap_memory()
In [6]: print (f"swap:{mem_swap}" )
swap:sswap(total=0, used=0, free=0, percent=0.0, sin=0, sout=0)
磁盘
disk = psutil.disk_partitions()
print (disk)
disk_usage = psutil.disk_usage('/' )
print (disk_usage)
disk_io = psutil.disk_io_counters()
print (disk_io)
disk_io_one = psutil.disk_io_counters(perdisk=True)
print (disk_io_one)
网络
网络信息重点关注: bytes_sent=579758412(发送字节数),bytes_recv=1744587596(接收字节数), packets_sent=3704487(发送数据包数),packets_recv=5320867(接收数据包数)
net = psutil.net_io_counters(pernic=True)
print (net)
其他信息
In [39]: psutil.users()
Out[39]: [suser(name='root' , terminal='pts/2' , host='111.26.86.20' , started=1738993613.0, pid=191667)]
In [40]: import psutil,datetime
In [41]: psutil.boot_time()
Out[41]: 1733188166.0
In [42]: datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S" )
Out[42]: '2024-12-03 09:09:26'
案例: 给出进程名输出该进程信息
#!/usr/bin/python3
import psutil
import sys
def print_proc_name(proc_names):
pids_list = psutil.pids()
try:
for pid in pids_list:
for proc_name in proc_names:
p = psutil.Process(pid)
if p.name() == proc_name:
print (p)
except Exception as e:
print (str(e))
if __name__ == '__main__' :
print_proc_name(sys.argv[1:])
9.2 系统进程管理
psutil.process_iter()返回一个包含进程对象的迭代器
In [9]: for proc in psutil.process_iter(['name' ,'pid' ,'username' ]):
...: print (proc.info)
...:
{'username' : 'root' , 'pid' : 1, 'name' : 'systemd' }
{'username' : 'root' , 'pid' : 2, 'name' : 'kthreadd' }
{'username' : 'root' , 'pid' : 3, 'name' : 'rcu_gp' }
{'username' : 'root' , 'pid' : 4, 'name' : 'rcu_par_gp' }
......
psutil.pids()返回当前运行的pid的有序列表
In [12]: psutil.pids()
Out[12]:
[1,
2,
3,
4,
5,
......]
psutil.pid_exists(PID)检查指定PID是否存在于当前进程列表中
In [13]: psutil.pid_exists(1)
Out[13]: True
9.3 psutil.Process()类
psutil.Process(pid=Node) Process类是psutil对进程的封装,可以基于指定pid创建Process对象
In [1]: import psutil
In [3]: psutil.Process(178951)
Out[3]: psutil.Process(pid=178951, name='nginx' , status='sleeping' , started='2025-02-05 16:02:15' )
In [4]: p = psutil.Process(178951)
In [5]: p.name()
Out[5]: 'nginx'
In [6]: p.exe()
Out[6]: '/usr/sbin/nginx'
In [7]: p.cwd()
Out[7]: '/'
In [8]: p.status()
Out[8]: 'sleeping'
In [9]: p.create_time()
Out[9]: 1738742535.53
In [10]: p.uids()
Out[10]: puids(real=0, effective=0, saved=0)
In [11]: p.gids()
Out[11]: pgids(real=0, effective=0, saved=0)
In [12]: p.cpu_times()
Out[12]: pcputimes(user=0.0, system=0.0, children_user=0.0, children_system=0.0, iowait=0.0)
In [13]: p.cpu_affinity()
Out[13]: [0, 1]
In [14]: p.memory_percent()
Out[14]: 0.09876393318608247
In [15]: p.memory_info()
Out[15]: pmem(rss=1732608, vms=56537088, shared=20480, text=786432, lib=0, data=1482752, dirty=0)
In [16]: p.io_counters()
Out[16]: pio(read_count=0, write_count=0, read_bytes=0, write_bytes=0, read_chars=0, write_chars=0)
In [17]: p.connections()
<ipython-input-17-d50301053d22>:1: DeprecationWarning: connections() is deprecated and will be removed; use net_connections() instead
p.connections()
Out[17]:
[pconn(fd=6, family=<AddressFamily.AF_INET: 2>, type =<SocketKind.SOCK_STREAM: 1>, laddr=addr(ip='0.0.0.0' , port=80), raddr=(), status='LISTEN' ),
pconn(fd=7, family=<AddressFamily.AF_INET6: 10>, type =<SocketKind.SOCK_STREAM: 1>, laddr=addr(ip='::' , port=80), raddr=(), status='LISTEN' )]
In [18]: p.num_threads()
Out[18]: 1
9.4 psutil.Popen()类
psutil提供的popen类的作用是获取用户启动的应用程序进程信息,以便跟踪程序进程的运行状态。
#!/usr/bin/python3
import psutil
from subprocess import PIPE
p = psutil.Popen(['/usr/bin/python3' ,'-c' ,'print("hello python")' ],stdout=PIPE)
print (p.name())
print (p.username())
print (p.cpu_times())
print (p.communicate())
9.5 psutil实例(监控系统信息)
#!/usr/bin/python3
import psutil
import socket
def byte2human(n):
symbols = ('K' ,'M' ,'G' ,'T' ,'P' ,'E' ,'Z' ,'y' )
prefix = {}
for i,s in enumerate(symbols):
prefix[s] = 1 << (i + 1) * 10
for s in reversed(symbols):
if n >= prefix[s]:
value = float (n) / prefix[s]
return f'{value:.1f}{s}'
return f'{n}B'
def get_cpu_usage():
cpu_count = psutil.cpu_count()
cpu_precent = psutil.cpu_times_percent(interval=2)
return dict(cpu_count=cpu_count,cpu_precent=cpu_precent)
def get_mem_usage():
mem = psutil.virtual_memory()
mem_total = byte2human(mem.total)
mem_used = byte2human(mem.used)
mem_free = byte2human(mem.free)
return dict(mem_total=mem_total,mem_used=mem_used,mem_free=mem_free)
def get_disk_usage():
disk = psutil.disk_usage('/' )
disk_total = byte2human(disk.total)
disk_used = byte2human(disk.used)
disk_free = byte2human(disk.free)
disk_precent = disk.percent
disk_io = psutil.disk_io_counters()
disk_io_read = byte2human(disk_io.read_bytes)
disk_io_write = byte2human(disk_io.write_bytes)
return dict(disk_total=disk_total,disk_used=disk_used,disk_free=disk_free,disk_precent=disk_precent,
disk_io_read=disk_io_read,disk_io_write=disk_io_write)
def get_net_data():
net_io = psutil.net_io_counters()
net_io_sent = byte2human(net_io.bytes_sent)
net_io_recv = byte2human(net_io.bytes_recv)
return dict(net_io_sent=net_io_sent,net_io_recv=net_io_recv)
def gather_monitor_data():
data = {}
data.update(get_cpu_usage())
data.update(get_mem_usage())
data.update(get_disk_usage())
data.update(get_net_data())
return data
def report():
hostname = socket.gethostname()
data = gather_monitor_data()
data.update(dict(hostname=hostname))
print (f'{hostname}主机系统信息' )
print ("------------------------" )
print (f'cpu总数: {data["cpu_count"]}' )
print (f'cpu使用率: {data["cpu_precent"]}%' )
print ("------------------------" )
print (f'内存总量: {data["mem_total"]}' )
print (f'内存使用量: {data["mem_used"]}' )
print (f'内存剩余量: {data["mem_free"]}' )
print ("------------------------" )
print (f'磁盘总量: {data["disk_total"]}' )
print (f'磁盘使用量: {data["disk_used"]}' )
print (f'磁盘剩余量: {data["disk_free"]}' )
print (f'磁盘使用率: {data["disk_precent"]}%' )
print (f'磁盘读取数据量: {data["disk_io_read"]}' )
print (f'磁盘写入数据量: {data["disk_io_write"]}' )
print ("------------------------" )
print (f'网卡发送数据量: {data["net_io_sent"]}' )
print (f'网卡接收数据量: {data["net_io_recv"]}' )
if __name__ == '__main__' :
report()
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧