,转Python运维自动化psutil 模块详解

原文:https://cxybb.com/article/sj349781478/117815765

psutil 模块

参考官方文档:https://pypi.org/project/psutil/

一、psutil简介

psutil是一个开源且跨平台(http://code.google.com/p/psutil/)的库,能够轻松实现**获取系统运行的进程和系统利用率(包括CPU、内存、磁盘、网络等)信息。它主要应用于系统监控,分析和限制系统资源及进程的管理。它实现了同等命令行工具提供的功能,**如ps、top、lsof、netstat、ifconfig、who、df、kill、free、nice、ionice、iostat、iotop、uptime、pidof、tty、taskset、pmap等。

在Python中获取系统信息的另一个好办法是使用psutil这个第三方模块。还可以跨平台使用,支持Linux/UNIX/OSX/Windows等,是系统管理员和运维小伙伴不可或缺的必备模块。

Works with Python versions from 2.4 to 3.X.

二、安装psutil模块

CentOS安装psutil模块:

psutil版本:5.8

wget https://pypi.python.org/packages/source/p/psutil/psutil-5.8.0.tar.gz

tar zxvf psutil-5.8.0.tar.gz

cd psutil-5.8.0

yum -y install python-devel (如果提示缺少python.h头文件,执行此命令。)

python setup.py install

Windos安装psutil模块:

root@shawn:~# pip3 install psutil

Collecting psutil

Downloading psutil-5.8.0-cp38-cp38-manylinux2010_x86_64.whl (296 kB)

|████████████████████████████████| 296 kB 20 kB/s

Installing collected packages: psutil

Successfully installed psutil-5.8.0

三、使用psutil模块

1.获取CPU信息:

1.1使用psutil.cpu_times()方法

使用psutil.cpu_times()获取CPU的完整信息

>>> import psutil

>>> psutil.cpu_times()

scputimes(user=733.23, nice=2.62, system=122.87, idle=19414.35, iowait=29.46, irq=0.0, softirq=34.18, steal=0.0, guest=0.0, guest_nice=0.0)

获取单个数据,如cpu分配在用户程序上的执行时间(时间片)或io等待时间。

>>> psutil.cpu_times().user

793.19

>>> psutil.cpu_times().iowait

31.79

时间片的概念详解百度百科:https://baike.baidu.com/item/%E6%97%B6%E9%97%B4%E7%89%87/6525414?fr=aladdin

psutil.cpu_times(percpu=False)解释说明:

以元组的形式返回系统CPU时间。每个属性代表CPU在给定模式下花费的秒数。属性可用性取决于平台:

user: 从系统启动开始累积到当前时刻,处于用户态的运行时间,不包含 nice 值为负的进程。

system: 从系统启动开始累积到当前时刻,处于核心态的运行时间。

idle: 从系统启动开始累积到当前时刻,除 IO 等待时间以外的其他等待时间(空闲时间)。

特定于平台的字段:

nice (UNIX): 系统启动开始累积到当前时刻,nice 值为负的进程所占用的 CPU 时间。Nice值是类UNIX操作系统中表示静态优先级的数值。 每个进程都有自己的静态优先级,优先级高的进程得以优先运行。

iowait (Linux): 从系统启动开始累积到当前时刻,IO 等待时间。

irq (Linux, BSD): 从系统启动开始累积到当前时刻,服务中断时间

softirq (Linux): 从系统启动开始累积到当前时刻,软中断时间

steal (Linux 2.6.11+): 从系统启动开始累积到当前时刻,在虚拟环境运行时花费在其他操作系统的时间

guest (Linux 2.6.24+):从系统启动开始累积到当前时刻,在Linux内核控制下的操作系统虚拟cpu花费的时间。

guest_nice (Linux 3.2.0+): 从系统启动开始累积到当前时刻,在Linux内核控制下的操作系统虚拟cpu花费在nice进程上的时间。

interrupt (Windows):从系统启动开始累积到当前时刻,服务硬件中断所花费的时间(类似于UNIX上的“ irq”)

dpc (Windows): 为延迟过程调用(DPC)提供服务所花费的时间;DPC是以低于标准中断的优先级运行的中断。

参考文档:

Return system CPU times as a named tuple. Every attribute represents the seconds the CPU has spent in the given mode. The attributes availability varies depending on the platform:

user: time spent by normal processes executing in user mode; on Linux this also includes guest time

system: time spent by processes executing in kernel mode

idle: time spent doing nothing

Platform-specific fields:

nice (UNIX): time spent by niced (prioritized) processes executing in user mode; on Linux this also includes guest_nice time

iowait (Linux): time spent waiting for I/O to complete

irq (Linux, BSD): time spent for servicing hardware interrupts

softirq (Linux): time spent for servicing software interrupts

steal (Linux 2.6.11+): time spent by other operating systems running in a virtualized environment

guest (Linux 2.6.24+): time spent running a virtual CPU for guest operating systems under the control of the Linux kernel

guest_nice (Linux 3.2.0+): time spent running a niced guest (virtual CPU for guest operating systems under the control of the Linux kernel)

interrupt (Windows): time spent for servicing hardware interrupts ( similar to “irq” on UNIX)

dpc (Windows): time spent servicing deferred procedure calls (DPCs); DPCs are interrupts that run at a lower priority than standard interrupts.

1.2psutil.cpu_count()获取CPU个数

使用psutil.cpu_count()获取CPU逻辑个数

#cpu_count(,[logical]):默认返回逻辑CPU的个数,当设置logical的参数为False时,返回物理CPU的个数。

>>> psutil.cpu_count()

使用psutil.cpu_count(logical=False)获取CPU的物理个数,默认logical值为True

>>> psutil.cpu_count(logical=False)

8

1.3psutil.getloadavg()获取平均系统负载

使用psutil.getloadavg()可以获取平均系统负载,会以元组的形式返回最近1、5和15分钟内的平均系统负载。

在Windows上,这是通过使用Windows API模拟的,该API产生一个线程,该线程保持在后台运行,并每5秒更新一次结果,从而模仿UNIX行为。 因此,在Windows上,第一次调用此方法,在接下来的5秒钟内,它将返回无意义的(0.0,0.0,0.0)元组。

>>> psutil.getloadavg()

(1.22, 1.41, 1.38)

1.4、psutil.cpu_percent()获取CPU使用率

cpu_percent(,[percpu],[interval]):返回CPU的利用率

interval:指定的是计算cpu使用率的时间间隔,interval不为0时,则阻塞时显示interval执行的时间内的平均利用率

percpu:指定是选择总的使用率或者每个cpu的使用率,percpu为True时显示所有物理核心的利用率

1.指定的是计算cpu使用率的时间间隔

>>> for x in range(10):

... psutil.cpu_percent(interval=1)

...

2.4

2.5

2.7

2.3

2.5

2.2

2.0

2.2

2.4

2.2

2.实现类似top命令的CPU使用率,每秒刷新一次,累计10次:

>>> for x in range(10):

... psutil.cpu_percent(interval=1,percpu=True)

...

[1.0, 3.1, 5.0, 4.0, 0.0, 4.0, 3.0, 2.0]

...

[1.0, 1.0, 6.1, 3.1, 2.0, 2.1, 0.0, 0.0]

[2.0, 1.0, 6.0, 4.9, 1.0, 5.1, 1.0, 1.0]

1.5psutil.cpu_stats()获取CPU的统计信息

cpu_stats()以命名元组的形式返回CPU的统计信息,包括上下文切换,中断,软中断和系统调用次数。

>>> psutil.cpu_stats()

scpustats(ctx_switches=3928927, interrupts=2319133, soft_interrupts=1974116, syscalls=0)

1.6、psutil.cpu_freq()获取CPU频率

cpu_freq([percpu]):返回cpu频率

>>> psutil.cpu_freq()

scpufreq(current=1799.999, min=0.0, max=0.0)

1.7、psutil.cpu_times_percent()获取耗时比例

cpu_times_percent(,[percpu]):功能和cpu_times大致相同,看字面意思就能知道,该函数返回的是耗时比例。

>>> psutil.cpu_times_percent()

scputimes(user=0.1, nice=0.0, system=0.0, idle=99.9, iowait=0.0, irq=0.0, softirq=0.0, steal=0.0, guest=0.0, guest_nice=0.0)

2.获取内存信息

2.1psutil.virtual_memory()内存使用情况

psutil.virtual_memory():获取系统内存的使用情况,以命名元组的形式返回内存使用情况,包括总内存,可用内存,内存利用率,buffer和cache等。单位为字节。

获取内存的完整信息

>>> psutil.virtual_memory()

svmem(total=2028425216, available=982532096, percent=51.6, used=861827072, free=810414080, active=401735680, inactive=431902720, buffers=4096, cached=356179968, shared=9203712, slab=236351488)

'''

返回的是字节Byte为单位的整数

重点关注的参数是:

1.total表示内存总的大小

2.percent表示实际已经使用的内存占比。

3.available表示还可以使用的内存。

4.uused表示已经使用的内存

'''

使用total获取内存总大小

>>> psutil.virtual_memory().total

2028425216

使用获取已经使用的内存

>>> psutil.virtual_memory().used

865882112

使用free获取剩余的内存

>>> psutil.virtual_memory().free

805871616

2.2 psutil.swap_memory()获取系统交换内存(swap)的统计信息

psutil.swap_memory():获取系统交换内存的统计信息,以命名元组的形式返回swap/memory使用情况,包含swap中页的换入和换出。

获取交换分区相关

>>> psutil.swap_memory()

sswap(total=4091539456, used=173793280, free=3917746176, percent=4.2, sin=23683072, sout=188874752)

3.获取磁盘相关

磁盘信息主要两部分,一个是磁盘的利用率,一个是io。

3.1、psutil.disk_partitions()获取磁盘分区信息

disk_partitions([all=False]):以命名元组的形式返回所有已挂载的磁盘,包含磁盘名称,挂载点,文件系统类型等信息。

当all等于True时,返回包含/proc等特殊文件系统的挂载信息

获取磁盘分区的信息

>>> psutil.disk_partitions()

[sdiskpart(device='/dev/sda3', mountpoint='/', fstype='xfs', opts='rw,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota', maxfile=255, maxpath=4096), sdiskpart(device='/dev/loop1', mountpoint='/snap/core18/1944', fstype='squashfs', opts='ro,nodev,relatime', maxfile=256, maxpath=4096),。...sdiskpart(device='/dev/loop6', mountpoint='/snap/snap-store/467', fstype='squashfs', opts='ro,nodev,relatime', maxfile=256, maxpath=4096), sdiskpart(device='/dev/sda1', mountpoint='/boot', fstype='xfs', opts='rw,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota', maxfile=255, maxpath=4096)]

>>> io = psutil.disk_partitions()

>>> print(io[-1])

sdiskpart(device='/dev/sr0', mountpoint='/media/shawn/Ubuntu 20.04.1 LTS amd64', fstype='iso9660', opts='ro,nosuid,nodev,relatime,nojoliet,check=s,map=n,blocksize=2048,u, maxfile=255, maxpath=4096)

>>>

3.2、psutil.disk_usage()获取路径所在磁盘的使用情况

disk_usage(path):以命名元组的形式返回path所在磁盘的使用情况,包括磁盘的容量、已经使用的磁盘容量、磁盘的空间利用率等。

获取根分区的使用情况

>>> psutil.disk_usage('/')

sdiskusage(total=101184290816, used=8805330944, free=92378959872, percent=8.7)

>>>

3.3、disk_io_counters获取io统计信息

disk_io_counters([perdisk]):以命名元组的形式返回磁盘io统计信息(汇总的),包括读、写的次数,读、写的字节数等。

当perdisk的值为True,则分别列出单个磁盘的统计信息(字典:key为磁盘名称,value为统计的namedtuple)。

获取磁盘总的io个数,读写信息

>>> psutil.disk_io_counters()

sdiskio(read_count=60919, write_count=448417, read_bytes=1582292480, write_bytes=31438750208, read_time=50157, write_time=259374, read_merged_count=2527, write_merged_count=44226, busy_time=1096900)

'''补充说明

read_count(读IO数)

write_count(写IO数)

read_bytes(读IO字节数)

write_bytes(写IO字节数)

read_time(磁盘读时间)

write_time(磁盘写时间)

'''

获取单个分区的IO和读写信息

>>> psutil.disk_io_counters(perdisk=True)

{'loop0': sdiskio(read_count=43, write_count=0, read_bytes=358400, write_bytes=0, read_time=28, write_time=0, read_merged_count=0, write_merged_count=0, busy_time=44), 'loop1': sdiskio(read_count=424, write_count=0, read_bytes=6236160, write_bytes=0, read_time=277, write_time=0, read_merged_count=0, write_merged_count=0, busy_time=956),... write_merged_count=985, busy_time=1132488)}

4.获取网络信息

4.1、psutil.net_io_counter([pernic])获取网卡io统计信息

psutil.net_io_counter([pernic]):以命名元组的形式返回当前系统中每块网卡的网络io统计信息,包括收发字节数,收发包的数量、出错的情况和删包情况。当pernic为True时,则列出所有网卡的统计信息。

获取网络读写字节/包的个数

>>> psutil.net_io_counters()

snetio(bytes_sent=242309, bytes_recv=6775236, packets_sent=2563, packets_recv=44703, errin=0, errout=0, dropin=9301, dropout=0)

列出所有网卡的统计信息

>>> psutil.net_io_counters(pernic=True)

{'lo': snetio(bytes_sent=38379, bytes_recv=38379, packets_sent=413, packets_recv=413, errin=0, errout=0, dropin=0, dropout=0), 'ens32': snetio(bytes_sent=203930, bytes_recv=6756079, packets_sent=2150, packets_recv=44430, errin=0, errout=0, dropin=9334, dropout=0)}

4.2、psutil.net_if_addrs()获取网络接口信息

psutil.net_if_addrs():以字典的形式返回网卡的配置信息,包括IP地址和mac地址、子网掩码和广播地址。

>>> psutil.net_if_addrs()

{'lo': [snicaddr(family=<AddressFamily.AF_INET: 2>, address='127.0.0.1', netmask='255.0.0.0', broadcast=None, ptp=None), snicaddr(family=<AddressFamily.AF_INET6: 10>, address='::1', netmask='ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff', broadcast=None, ptp=None), snicaddr(family=<AddressFamily.AF_PACKET: 17>, address='00:00:00:00:00:00', netmask=None, broadcast=None, ptp=None)], 'ens32': [snicaddr(family=<AddressFamily.AF_INET: 2>, address='192.168.12.154', netmask='255.255.255.0', broadcast='192.168.12.255', ptp=None), snicaddr(family=<AddressFamily.AF_INET6: 10>, address='fe80::1c00:63d1:f5bf:1cec%ens32', netmask='ffff:ffff:ffff:ffff::', broadcast=None, ptp=None), snicaddr(family=<AddressFamily.AF_PACKET: 17>, address='00:0c:29:7a:81:66', netmask=None, broadcast='ff:ff:ff:ff:ff:ff', ptp=None)]}

4.3、psutil.net_if_stats()获取网络接口状态信息

psutil.net_if_stats():返回网卡的详细信息,包括是否启动、通信类型、传输速度与mtu。

>>> psutil.net_if_stats()

{'lo': snicstats(isup=True, duplex=<NicDuplex.NIC_DUPLEX_UNKNOWN: 0>, speed=0, mtu=65536), 'ens32': snicstats(isup=True, duplex=<NicDuplex.NIC_DUPLEX_FULL: 2>, speed=1000, mtu=1500)}

4、4、psutil.net_connections():获取当前网络连接信息

psutil.net_connections():以列表的形式返回,获取当前网络连接信息

>>> psutil.net_connections()

Traceback (most recent call last):

...

PermissionError: [Errno 1] Operation not permitted

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

...

psutil.AccessDenied: psutil.AccessDenied (pid=3847)

你可能会得到一个AccessDenied错误,原因是psutil获取信息也是要走系统接口,而获取网络连接信息需要root权限,这种情况下,可以退出Python交互环境,用sudo重新启动:

$ sudo python3

Password: ******

Python 3.6.3 ... on darwin

Type "help", ... for more information.

>>> import psutil

>>> psutil.net_connections()

[

sconn(fd=83, family=<AddressFamily.AF_INET6: 30>, type=1, laddr=addr(ip='::127.0.0.1', port=62911), raddr=addr(ip='::127.0.0.1', port=3306), status='ESTABLISHED', pid=3725),

sconn(fd=84, family=<AddressFamily.AF_INET6: 30>, type=1, laddr=addr(ip='::127.0.0.1', port=62905), raddr=addr(ip='::127.0.0.1', port=3306), status='ESTABLISHED', pid=3725),

sconn(fd=93, family=<AddressFamily.AF_INET6: 30>, type=1, laddr=addr(ip='::', port=8080), raddr=(), status='LISTEN', pid=3725),

sconn(fd=103, family=<AddressFamily.AF_INET6: 30>, type=1, laddr=addr(ip='::127.0.0.1', port=62918), raddr=addr(ip='::127.0.0.1', port=3306), status='ESTABLISHED', pid=3725),

sconn(fd=105, family=<AddressFamily.AF_INET6: 30>, type=1, ..., pid=3725),

sconn(fd=106, family=<AddressFamily.AF_INET6: 30>, type=1, ..., pid=3725),

sconn(fd=107, family=<AddressFamily.AF_INET6: 30>, type=1, ..., pid=3725),

...

sconn(fd=27, family=<AddressFamily.AF_INET: 2>, type=2, ..., pid=1)

]

4.5psutil.net_connections()网络连接的详细信息

psutil.net_connections([kind]):以列表的形式返回每个网络连接的详细信息(namedtuple)。命名元组包含fd, family, type, laddr, raddr, status, pid等信息。kind表示过滤的连接类型,支持的值如下:(默认为inet)

inet 代表 IPv4 and IPv6

>>> psutil.net_connections(kind='inet')

[sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=<SocketKind.SOCK_STREAM: 1>, laddr=addr(ip='192.168.12.154', port=58478)...sconn(fd=-1, family=<AddressFamily.AF_INET6: 10>, type=<SocketKind.SOCK_STREAM: 1>, laddr=addr(ip='::1', port=631), raddr=(), status='LISTEN', pid=None)]

>>>

'/dev/pts/1'

>>> p.open_files() # 进程打开的文件

[]

>>> p.environ() # 进程环境变量

{'SHELL': '/bin/bash', 'PATH': '/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:...', 'PWD': '/Users/michael', 'LANG': 'zh_CN.UTF-8', ...}

>>> p.terminate() # 发送SIGTEAM信号结束进程

Terminated: 15

>>> p.kill() #发送SIGKILL信号结束进程

>>> 已杀死

>>> p.is_running() #进程是否在运行

True

>>> p.num_fds() #进程打开的文件个数

4

>>> p.is_running() #判断进程是否正在运行

True

popen方法的使用:获取(跟踪)用户启动的应用程序进程信息

>>> import psutil

>>> from subprocess import PIPE

>>> p = psutil.Popen(["/usr/bin/python3", "-c", "print('hello')"],stdout=PIPE)

>>> p.name()

'python3'

>>> p.username()

'shawn'

>>> p.communicate()

(b'hello\n', None)

>>> p.wait(timeout=2)

0

7.其他进程相关的操作

1.psutil.pid_exists判断给点定的pid是否存在

>>> psutil.pid_exists(1)

True

2.psutil.process_iter迭代当前正在运行的进程,返回的是每个进程的Process对象

>>> psutil.process_iter(8216)

<generator object process_iter at 0x7f48a7275040>

8.psutil模块的其他补充:

psutil还提供了一个test()方法,可以模拟出ps命令的效果:

>>> import psutil

>>> psutil.test()

USER PID %MEM VSZ RSS NICE STATUS START TIME CMDLINE

root 1 0.6 164.9M 11.4M sleep 18:15 01:01 /sbin/init s

root 2 0.0 0.0B 0.0B sleep 18:15 00:00 kthreadd

...

root 7896 0.0 0.0B 0.0B idle 21:55 00:00 kworker/u256

shawn 7904 1.5 49.2M 29.1M runni 21:58 00:00 python3

>>>

Windows services:获取windows的服务

>>> list(psutil.win_service_iter())

[<WindowsService(name='AeLookupSvc', display_name='Application Experience') at 38850096>,

<WindowsService(name='ALG', display_name='Application Layer Gateway Service') at 38850128>,

<WindowsService(name='APNMCP', display_name='Ask Update Service') at 38850160>,

<WindowsService(name='AppIDSvc', display_name='Application Identity') at 38850192>,

...]

>>> psutil.win_service_get('alg')

<WindowsService(name='alg', display_name='Application Layer Gateway Service') at 2325310212128>

>>> s = psutil.win_service_get('alg')

>>> s.as_dict()

{'binpath': 'C:\\Windows\\System32\\alg.exe',

'description': 'Provides support for 3rd party protocol plug-ins for Internet Connection Sharing',

'display_name': 'Application Layer Gateway Service',

'name': 'alg',

'pid': None,

'start_type': 'manual',

'status': 'stopped',

'username': 'NT AUTHORITY\\LocalService'}

四、编写获取系统硬件脚本

'''

script_info:检测系统硬件脚本

Edition:v0.0.1

'''

# !/usr/bin/env python

# coding:utf-8

import psutil

import datetime

import time

# 当前时间

now_time = time.strftime('%Y-%m-%d-%H:%M:%S', time.localtime(time.time()))

print(now_time)

# 查看cpu物理个数的信息

print(u"物理CPU个数: %s" % psutil.cpu_count(logical=False))

# cpu的使用率

cpu = (str(psutil.cpu_percent(1))) + '%'

print("cup使用率: %s" % cpu)

# 查看内存信息,剩余内存.free 总共.total

# round()函数方法为返回浮点数x的四舍五入值。

free = str(round(psutil.virtual_memory().free / (1024.0 * 1024.0 * 1024.0), 2))

total = str(round(psutil.virtual_memory().total / (1024.0 * 1024.0 * 1024.0), 2))

memory = int(psutil.virtual_memory().total - psutil.virtual_memory().free) / float(psutil.virtual_memory().total)

print("物理内存: %s G" % total)

print("剩余物理内存: %s G" % free)

print("物理内存使用率: %s %%" % int(memory * 100))

# 获取系统启动时间

print("系统启动时间: %s" % datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S"))

# 获取系统用户

users_count = len(psutil.users())

users_list = ",".join([u.name for u in psutil.users()])

print("当前有%s个用户,分别是 %s" % (users_count, users_list))

# 获取网卡信息,可以得到得到网卡属性,连接数,当前数据等信息

net = psutil.net_io_counters()

bytes_sent = '{0:.2f} Mb'.format(net.bytes_recv / 1024 / 1024)

bytes_rcvd = '{0:.2f} Mb'.format(net.bytes_sent / 1024 / 1024)

print("网卡接收数据 %s 网卡发送数据 %s" % (bytes_rcvd, bytes_sent))

# 获取磁盘数据信息

io = psutil.disk_partitions()

print('-----------------------------磁盘信息---------------------------------------')

for i in io:

try:

o = psutil.disk_usage(i.device)

print("总容量:" + str(int(o.total / (1024.0 * 1024.0 * 1024.0))) + "G")

print("已用容量:" + str(int(o.used / (1024.0 * 1024.0 * 1024.0))) + "G")

print("可用容量:" + str(int(o.free / (1024.0 * 1024.0 * 1024.0))) + "G")

except PermissionError:

continue

print('-----------------------------进程信息-------------------------------------')

# 查看系统全部进程

for pnum in psutil.pids():

p = psutil.Process(pnum)

print(

"进程名 %-20s 内存利用率 %-18s 进程状态 %-10s 创建时间 %-10s " % (p.name(), p.memory_percent(), p.status(), p.create_time()))

五、参考资料

https://www.liaoxuefeng.com/wiki/1016959663602400/1183565811281984

https://pypi.org/project/psutil/

https://www.cnblogs.com/billie52707/p/12468740.html

https://www.zhihu.com/question/278027418/answer/397277169

https://psutil.readthedocs.io/en/latest/

————————————————