Python中的 Psutil 模块
Psutil是一个Python跨平台库,用于访问系统详细信息和处理实用程序。它用于跟踪系统中各种资源的使用情况。可以监控 CPU、内存、磁盘、网络、传感器等资源的使用情况。因此,该库用于系统监控、分析、限制进程资源和管理正在运行的进程。它在Python版本 2.6、2.7 和 3.4+ 中受支持。
Linux Ubuntu/Debian 中的安装步骤
sudo pip install psutil
系统功能
中央处理器
1) psutil.cpu_times() -此函数以命名元组的形式提供系统 CPU 时间。
参数:
- user – 正常进程在用户模式下执行所花费的时间
- system – 进程在内核模式下执行所花费的时间
- idle - 系统空闲的时间
- nice – 优先进程在用户模式下执行所花费的时间
- iowait – 等待 I/O 完成所花费的时间。这不计入空闲时间计数器。
- irq - 服务硬件中断所花费的时间
- softirq – 服务软件中断所花费的时间
- 偷窃——其他操作系统在虚拟化环境中运行所花费的时间
- 来宾——在 Linux 内核的控制下为来宾操作系统运行虚拟 CPU 所花费的时间
例子 :
Python
import psutil
print(psutil.cpu_times())
Python
import psutil
print(psutil.cpu_percent(1))
Python
import psutil
print("Number of cores in system", psutil.cpu_count())
print("\nNumber of physical cores in system",)
Python
import psutil
print("CPU Statistics", psutil.cpu_stats())
Python
import psutil
print(psutil.cpu_freq())
Python
import psutil
print(psutil.getloadavg())
Python
import psutil
print(psutil.virtual_memory())
Python
import psutil
print(psutil.swap_memory())
Python
import psutil
print(psutil.disk_partitions())
Python
import psutil
print(psutil.disk_usage('/'))
Python
import psutil
print(psutil.net_io_counters())
Python
import psutil
print(psutil.net_connections())
Python
import psutil
print(psutil.net_if_addrs())
Python
import psutil
print(psutil.sensors_temperatures())
Python
import psutil
print(psutil.sensors_fans())
Python
import psutil
print(psutil.sensors_battery())
Python
import psutil, datetime
print(psutil.boot_time())
Python
import psutil
print(psutil.users())
输出
scputimes(user=5461.14, nice=2.44, system=1326.65, idle=45502.33, iowait=506.24, irq=0.0, softirq=5.46, steal=0.0, guest=0.0, guest_nice=0.0)
2) psutil.cpu_percent(interval) -此函数以百分比形式计算当前系统范围内的 CPU 使用率。建议为计算平均 CPU 使用率的函数提供时间间隔(秒)作为参数,忽略间隔参数可能导致使用值的高度变化。
例子 :
Python
import psutil
print(psutil.cpu_percent(1))
输出
5.0
3) psutil.cpu_count(logical=True) –该函数显示系统中逻辑 CPU 的数量。逻辑核心的计算方法是物理核心的数量乘以每个核心上可以运行的线程数。在没有逻辑核心的情况下,它只计算物理核心的数量。
例子 :
Python
import psutil
print("Number of cores in system", psutil.cpu_count())
print("\nNumber of physical cores in system",)
输出:
Number of cores in system 4
Number of physical cores in system 2
4) psutil.cpu_stats() -该函数以命名元组的形式提供 CPU 统计信息。统计数据包括:
- ctx_switches – 自引导以来的上下文切换次数。
- 中断 – 自引导以来的中断数。
- soft_interrupts – 自启动以来的软件中断数。
- syscalls – 自启动以来的系统调用数。在 Ubuntu 中始终设置为 0。
例子 :
Python
import psutil
print("CPU Statistics", psutil.cpu_stats())
scpustats(ctx_switches=37382771, interrupts=13744347, soft_interrupts=6769413, syscalls=0)
5) psutil.cpu_freq() -此函数将 CPU 频率作为一个元组提供,其中包括以 Mhz 表示的当前、最小和最大频率。在 Ubuntu 上当前频率报告实时值。而在所有其他平台上,它代表名义上的“固定”值。
例子 :
Python
import psutil
print(psutil.cpu_freq())
输出:
scpufreq(current=931.42925, min=400.0, max=2000.0)
6) psutil.getloadavg() -此函数以元组形式给出过去 1、5 和 15 分钟的平均系统负载。负载表示处于可运行状态的进程,或者使用 CPU 或者等待使用 CPU(例如等待磁盘 I/O)。
例子 :
Python
import psutil
print(psutil.getloadavg())
输出:
(0.22, 0.33, 0.35)
记忆
1) psutil.virtual_memory() -此函数以字节为单位给出系统内存使用情况。 used 和 available 之和可能等于也可能不等于总数。为了获得可用物理内存的详细信息,使用了这个函数。
参数:
- total – 不包括交换的总物理内存。
- 可用的——可以在系统不进入交换的情况下立即分配给进程的内存。
- used – 使用的内存。
- free – 内存未使用且随时可用
- active – 当前正在使用或最近使用的内存。
- inactive – 标记为未使用的内存。
- 缓冲区——缓存数据,如文件系统元数据。
- cached – 缓存数据
- shared – 可以被多个进程访问的内存。
例子 :
Python
import psutil
print(psutil.virtual_memory())
输出:
svmem(total=4028772352, available=1061466112, percent=73.7, used=2401546240, free=412352512, active=2176798720, inactive=1196470272, buffers=70774784, cached=1144098816, shared=313872384, slab=125116416)
2) psutil.swap_memory() -此函数以元组的形式提供交换内存统计信息的详细信息。
参数:
- total – 总交换内存(以字节为单位)
- used – 已使用的交换内存(以字节为单位)
- free – 以字节为单位的空闲交换内存
- percent – 使用百分比计算为(总计 – 可用)/总计 * 100
- sin – 系统从磁盘换入的字节数
- sout – 系统从磁盘换出的字节数
例子 :
Python
import psutil
print(psutil.swap_memory())
输出:
sswap(total=2097147904L, used=886620160L, free=1210527744L, percent=42.3, sin=1050411008, sout=1906720768)
磁盘
1) psutil.disk_partitions() -此函数提供所有已挂载磁盘分区的详细信息,作为元组列表,包括设备、挂载点和文件系统类型。
例子 :
Python
import psutil
print(psutil.disk_partitions())
输出:
[sdiskpart(device=’/dev/sda1′, mountpoint=’/’, fstype=’ext4′, opts=’rw, relatime, errors=remount-ro, data=ordered’)]
2) psutil.disk_usage(path) -此函数将磁盘使用统计信息作为给定路径的元组提供。总空间、已用空间和可用空间以字节表示,以及使用百分比。
例子 :
Python
import psutil
print(psutil.disk_usage('/'))
输出:
sdiskusage(total=787310764032, used=26450710528, free=720843354112, percent=3.5)
网络
1) psutil.net_io_counters()-该函数以元组的形式给出网络输入输出统计的详细信息。
参数:
- bytes_sent – 发送的字节数
- bytes_recv – 接收的字节数
- packet_sent – 发送的数据包数
- packet_recv – 收到的数据包数
- errin – 接收时的错误总数
- errout – 发送时的错误总数
- dropin – 被丢弃的传入数据包总数
- dropout – 丢弃的传出数据包总数
例子 :
Python
import psutil
print(psutil.net_io_counters())
输出:
snetio(bytes_sent=14508483, bytes_recv=62749361, packets_sent=84311, packets_recv=94888, errin=0, errout=0, dropin=0, dropout=0)
2) psutil.net_connections() -此函数以命名元组的形式给出系统的套接字连接列表。
参数:
- fd - 套接字文件描述符。
- family – 套接字系列,AF_INET、AF_INET6 或 AF_UNIX。
- type – 套接字类型,SOCK_STREAM、SOCK_DGRAM 或 SOCK_SEQPACKET。
- laddr – 本地地址作为 (ip, port) 命名的元组
- raddr – 远程地址作为 (ip, port) 命名的元组
- status – 表示 TCP 连接的状态。
- pid – 打开套接字的进程的 PID,如果可检索,否则无。
例子 :
Python
import psutil
print(psutil.net_connections())
输出:
[sconn(fd=118, family=2, type=1, laddr=addr(ip=’192.168.12.184′, port=59666), raddr=addr(ip=’172.217.166.42′, port=443), status=’ESTABLISHED’, pid=2428),
sconn(fd=-1, family=2, type=2, laddr=addr(ip=’0.0.0.0′, port=631), raddr=(), status=’NONE’, pid=None),
sconn(fd=-1, family=2, type=1, laddr=addr(ip=’127.0.0.1′, port=3306), raddr=(), status=’LISTEN’, pid=None),
sconn(fd=145, family=2, type=1, laddr=addr(ip=’192.168.12.184′, port=56568), raddr=addr(ip=’172.217.166.35′, port=443), status=’ESTABLISHED’, pid=2428),
sconn(fd=-1, family=2, type=2, laddr=addr(ip=’0.0.0.0′, port=52253), raddr=(), status=’NONE’, pid=None)]
3) psutil.net_if_addrs() -该函数用于获取系统上安装的每个网络接口卡的地址。它是一个字典,其键是网络接口卡名称,值是分配给它的每个地址的命名元组列表。每个元组包括:
- family – 套接字系列,AF_INET 或 AF_INET6
- address – 主 NIC 地址
- netmask – 网络掩码地址
- 广播——广播地址。
- ptp——“点对点”,它是点对点接口上的目标地址。
例子 :
Python
import psutil
print(psutil.net_if_addrs())
输出:
{‘wlo1′: [snicaddr(family=2, address=’192.168.12.184′, netmask=’255.255.255.0′, broadcast=’192.168.12.255′, ptp=None), snicaddr(family=10, address=’fe80::664f:767c:91f0:71c0%wlo1′, netmask=’ffff:ffff:ffff:ffff::’, broadcast=None, ptp=None), snicaddr(family=17, address=’3c:f8:62:32:b7:70′, netmask=None, broadcast=’ff:ff:ff:ff:ff:ff’, ptp=None)], ‘lo’: [snicaddr(family=2, address=’127.0.0.1′, netmask=’255.0.0.0′, broadcast=None, ptp=None), snicaddr(family=10, address=’::1′, netmask=’ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff’, broadcast=None, ptp=None), snicaddr(family=17, address=’00:00:00:00:00:00′, netmask=None, broadcast=None, ptp=None)], ‘docker0′: [snicaddr(family=2, address=’172.17.0.1′, netmask=’255.255.0.0′, broadcast=’172.17.255.255′, ptp=None), snicaddr(family=17, address=’02:42:ef:4c:3b:d9′, netmask=None, broadcast=’ff:ff:ff:ff:ff:ff’, ptp=None)], ‘eno1′: [snicaddr(family=17, address=’3c:52:82:09:8e:c2′, netmask=None, broadcast=’ff:ff:ff:ff:ff:ff’, ptp=None)]}
传感器
1) psutil.sensors_temperatures() -此函数以摄氏度返回系统的硬件温度。每个条目都是一个命名元组,表示某个硬件温度传感器。
例子 :
Python
import psutil
print(psutil.sensors_temperatures())
输出:
{‘acpitz’: [shwtemp(label=”, current=27.8, high=119.0, critical=119.0), shwtemp(label=”, current=29.8, high=119.0, critical=119.0), shwtemp(label=”, current=10.0, high=None, critical=None)], ‘coretemp’: [shwtemp(label=’Physical id 0′, current=42.0, high=100.0, critical=100.0), shwtemp(label=’Core 0′, current=41.0, high=100.0, critical=100.0), shwtemp(label=’Core 1′, current=41.0, high=100.0, critical=100.0), shwtemp(label=’Physical id 0′, current=42.0, high=100.0, critical=100.0), shwtemp(label=’Core 0′, current=41.0, high=100.0, critical=100.0), shwtemp(label=’Core 1′, current=41.0, high=100.0, critical=100.0)]}
2) psutil.sensors_fans() -此函数提供以 RPM(每分钟转数)表示的硬件风扇速度的详细信息。如果操作系统不支持传感器,则返回空字典。
例子 :
Python
import psutil
print(psutil.sensors_fans())
输出:
{'asus': [sfan(label='cpu_fan', current=3000)]}
3) psutil.sensors_battery() -此函数以命名元组的形式提供电池状态信息。
参数:
- 百分比 – 剩余电量百分比。
- secsleft – 电池完全放电前的大致时间(以秒为单位)。
- power_plugged – 如果连接了交流电源线,则为 True,如果未连接,则为 False。
例子 :
Python
import psutil
print(psutil.sensors_battery())
输出:
sbattery(percent=98.98572501878287, secsleft=22913, power_plugged=False)
其他系统信息
1) psutil.boot_time() –该函数返回系统启动时间,以秒为单位表示。
例子 :
Python
import psutil, datetime
print(psutil.boot_time())
输出:
1582860765.0
2) psutil.users() -此函数将系统上连接的用户列表作为命名元组提供。
参数:
- user – 这是用户的系统名称。
- 终端 - 用户的 tty。
- host – 用户的主机名。
- 开始——创建时间,以浮点数表示,自纪元以来以秒为单位。
- pid – 登录进程的 PID。
例子 :
Python
import psutil
print(psutil.users())
输出:
[suser(name=’admin1′, terminal=’tty7′, host=’localhost’, started=1582860800.0, pid=1747)]