它实现了同等命令行工具提供的功能,如ps、top、lsof、netstat、ifconfig、who、df、kill、free、nice、ionice、iostat、iotop、uptime、pidof、tty、taskset、pmap等。目前支持的操作系统包括:Linux,Windows,macOS,,AIX等等。同时支持python2与python3版本,当前最新版本为5.4.8。
1. psutil的安装
|
|
2. psutil获取系统性能信息之CPU
2.1. cpu_times CPU运行时间
psutil.cpu_times(percpu = False )将每个特定状态下CPU运行的时间以元组的形式返回。
CPU运行状态 | 解释 |
user | 进程执行用户态代码耗费的CPU时间。 |
nice | 在优先级高的用户级别执行时CPU占用率的百分比。 |
system | 内核执行系统调用所使用的CPU时间。 |
idle | CPU空闲并且系统没有未完成的磁盘I / O请求的时间百分比。 |
iowait | CPU等待I/O输入输出的时间 |
irq | CPU用于维护硬件中断所花费的时间百分比。 |
softirq | CPU用于服务软件中断所花费的时间百分比。 |
steal | 在虚拟化环境中运行的其他操作系统所花费的时间 |
guest | CPU用于运行虚拟处理器的时间百分比。 |
dpc | 服务延迟过程调用所花费的时间 |
interrupt | 服务硬件中断所花费的时间 |
|
|
2.2. cpu_percent cpu利用率
psutil.cpu_percent(interval = None,percpu = False )返回一个浮点数,表示当前系统范围的CPU利用率百分比。如果interval=0.1,表示0.1sCPU的平均使用率。如果interval=0或者None时,比较自上次调用或模块导入后经过的系统CPU时间,立即返回。所有第一次的返回的数据是个无意义的数据。当percpu是True返回表示利用率的浮点数列表,以每个逻辑CPU的百分比表示。
|
|
2.3. cpu_times_percent 特定CPU状态的利用率
psutil.cpu_times(percpu=True)提供每个特定CPU时间的利用率百分比 。 interval和 percpu参数的含义与icpu_percent()中的含义相同。
|
|
2.4. cpu_count CPU的数量
psutil.cpu_count()返回系统中逻辑CPU的物理个数或者逻辑个数。
|
|
2.5. cpu_stats CPU 统计信息
cpu_stats() 将各种CPU统计信息作以元组形式返回。
CPU信息 | 解释 |
ctx_switches | 启动后的上下文切换次数 |
interrupts | 自引导以来的中断数 |
soft_interrupts | 自引导以来的软件中断次数 |
syscalls | 自引导以来的系统调用次数 |
|
|
2.6. cpu_freq CPU频率
psutil.cpu_freq(percpu = False )将CPU频率作为名称包返回,包括 以Mhz表示的当前,最小和最大频率。如果percpu是True并且系统支持每CPU频率检索,则为每个CPU返回一个频率列表,否则返回包含单个元素的列表(仅支持linux系统)。如果无法确定最小值和最大值,则将它们设置为0。在Linux 当前频率上报告实时值,在所有其他平台上它代表最大值。
|
|
3 psutil获取系统性能信息之内存
3.1. virtual_memory() 物理内存使用情况
内存信息 | 解释 |
total | 总物理内存 |
available | 在没有系统进入SWAP下立即提供的内存。 |
percent | 使用内存占比 |
used | 使用的物理内存 |
free | 没有使用的物理内存 |
active | 当前正在使用或最近使用的物理内存 |
inactive | 标记未使用的内存 |
buffers | buffers使用的缓存 |
cached | ccached使用的缓存 |
shared | 显示被共享使用的物理内存大小 |
slab | 内核数据结构缓存 |
|
|
3.2. swap_memory() 交换内存使用情况
|
|
4 psutil获取系统性能信息之此磁盘
4.1. disk_partitions 获取所有磁盘信息
|
|
4.2. disk_usage 获取单个磁盘信息
|
|
4.3. psutil.disk_io_counters 获取IO信息
|
|
5 psutil获取系统性能信息之网络
5.1. psutil.net_connections 获取网络套接字
|
|
5.2. psutil.net_if_addrs 获取网卡的信息
|
|
5.3. psutil.net_if_stats 获取网络接口的状态
|
|
5.4. psutil.net_io_counters 获取网口的流量
|
|
6 psutil获取系统性能信息之其他
6.1. 获取开机时间
|
|
6.2. 获取当前用户
|
|
6.3. 获取PID
|
|
6.4. 获取进程信息
|
|