psutil模块介绍
psutil是一个开源切跨平台的库,其提供了便利的函数用来获取才做系统的信息,比如CPU,内存,磁盘,网络等。此外,psutil还可以用来进行进程管理,包括判断进程是否存在、获取进程列表、获取进程详细信息等。而且psutil还提供了许多命令行工具提供的功能,包括:ps,top,lsof,netstat,ifconfig, who,df,kill,free,nice,ionice,iostat,iotop,uptime,pidof,tty,taskset,pmap。
psutil是一个跨平台的库,在官方网站上查到其支持如下操作系统。
- Linux
- Windows
- OSX
- FreeBSD
- OpenBSD
- NetBSD
- Sun Solaris
- AIX
Works with Python versions from 2.6 to 3.X.
安装psutil模块
psutil是一个第三方的开源项目,因此需要安装才能使用。这里利用pip命令进行安装
`pip3 ``install` `psutil`
psutil包含了异常、类、功能函数和常量,其中功能函数用来获取系统的信息,如CPU、磁盘、内存、网络等。类用来实现进程的管理功能。
功能函数
根据函数的功能,这里主要分为以下几类,下面将会从几个维度来说明psutil提供的功能函数。
CPU相关
cpu_count(,[logical]):默认返回逻辑CPU的个数,当设置logical的参数为False时,返回物理CPU的个数。
`>>> ``import` `psutil``>>> psutil.cpu_count()``8``>>> psutil.cpu_count(logical``=``False``)``4`
cpu_percent(,[percpu],[interval]):返回CPU的利用率,percpu为True时显示所有物理核心的利用率,interval不为0时,则阻塞时显示interval执行的时间内的平均利用率
`>>> ``import` `psutil``>>> psutil.cpu_percent()``6.7``>>> psutil.cpu_percent(percpu``=``True``)``[``17.7``, ``0.9``, ``11.0``, ``1.0``, ``11.1``, ``0.9``, ``10.7``, ``0.9``]``>>> psutil.cpu_percent(percpu``=``True``,interval``=``2``)``[``16.0``, ``0.0``, ``8.5``, ``1.0``, ``14.4``, ``0.5``, ``9.5``, ``1.0``]`
cpu_time(,[percpu]):以命名元组(namedtuple)的形式返回cpu的时间花费,percpu表示获取每个CPU的时间花费
`>>> ``import` `psutil``>>> psutil.cpu_times()``scputimes(user``=``5471.2``, nice``=``0.0``, system``=``5633.92``, idle``=``1295903.87``, iowait``=``2651.2``, irq``=``16.44``, softirq``=``137.87``, steal``=``0.0``, guest``=``0.0``)``>>> psutil.cpu_times(percpu``=``True``)``[scputimes(user``=``2803.53``, nice``=``0.0``, system``=``2824.3``, idle``=``648996.02``, iowait``=``153.71``, irq``=``16.26``, softirq``=``64.71``, steal``=``0.0``, guest``=``0.0``),``scputimes(user``=``2667.74``, nice``=``0.0``, system``=``2809.74``, idle``=``646935.11``, iowait``=``2497.58``, irq``=``0.18``, softirq``=``73.15``, steal``=``0.0``, guest``=``0.0``)]`
命名元组获取信息的简便方法
cpu_times_percent(,[percpu]):功能和cpu_times大致相同,看字面意思就能知道,该函数返回的是耗时比例。
`>>> ``import` `psutil``>>> psutil.cpu_times_percent()``scputimes(user``=``0.3``, nice``=``0.0``, system``=``0.4``, idle``=``99.0``, iowait``=``0.3``, irq``=``0.0``, softirq``=``0.0``, steal``=``0.0``, guest``=``0.0``)``>>> psutil.cpu_times_percent(percpu``=``True``)``[scputimes(user``=``0.3``, nice``=``0.0``, system``=``0.4``, idle``=``99.3``, iowait``=``0.0``, irq``=``0.0``, softirq``=``0.0``, steal``=``0.0``, guest``=``0.0``),``scputimes(user``=``0.3``, nice``=``0.0``, system``=``0.4``, idle``=``98.7``, iowait``=``0.6``, irq``=``0.0``, softirq``=``0.0``, steal``=``0.0``, guest``=``0.0``)]`
cpu_stats():以命名元组的形式返回CPU的统计信息,包括上下文切换,中断,软中断和系统调用次数。
`>>> ``import` `psutil``>>> psutil.cpu_stats()``scpustats(ctx_switches``=``393538808``, interrupts``=``194683724``, soft_interrupts``=``151546977``, syscalls``=``0``)`
Memory相关
virtual_memory():以命名元组的形式返回内存使用情况,包括总内存,可用内存,内存利用率,buffer和cache等。单位为字节。
`>>> ``import` `psutil``>>> psutil.virtual_memory()``svmem(total``=``1964584960``, available``=``1375827968``, percent``=``30.0``, used``=``432644096``,``free``=``600588288``, active``=``710012928``, inactive``=``484106240``, buffers``=``238247936``,``cached``=``693104640``, shared``=``716800``)`
按 Ctrl+C 复制代码
按 Ctrl+C 复制代码
swap_memory():以命名元组的形式返回swap memory使用情况,包含swap 中页的换入和换出。
`>>> ``import` `psutil``>>> psutil.swap_memory()``sswap(total``=``2113925120``, used``=``12206080``, free``=``2101719040``, percent``=``0.6``,``sin``=``29061120``, sout``=``87228416``)`
Disk相关
disk_partitions([all=False]):以命名元组的形式返回所有已挂载的磁盘,包含磁盘名称,挂载点,文件系统类型等信息。当all等于True时,返回包含/proc等特殊文件系统的挂载信息
`>>> ``import` `psutil``>>> psutil.disk_partitions()``[sdiskpart(device``=``'/dev/mapper/VolGroup-lv_root'``, mountpoint``=``'/'``, fstype``=``'ext4'``, opts``=``'rw'``),``sdiskpart(device``=``'/dev/sda1'``, mountpoint``=``'/boot'``, fstype``=``'ext4'``, opts``=``'rw'``),``sdiskpart(device``=``'/dev/mapper/VolGroup-lv_home'``, mountpoint``=``'/home'``, fstype``=``'ext4'``, opts``=``'rw'``)]` `# 查询指定挂载点的信息``>>> [ device ``for` `device ``in` `psutil.disk_partitions() ``if` `device.mountpoint ``=``=` `'/'` `]``[sdiskpart(device``=``'/dev/mapper/VolGroup-lv_root'``, mountpoint``=``'/'``, fstype``=``'ext4'``, opts``=``'rw'``)]`
disk_usage(path):以命名元组的形式返回path所在磁盘的使用情况,包括磁盘的容量、已经使用的磁盘容量、磁盘的空间利用率等。
`>>> ``import` `psutil``>>> psutil.disk_usage(``'/'``)``sdiskusage(total``=``32105152512``, used``=``11395198976``, free``=``19072290816``, percent``=``37.4``)`
disk_io_counters([perdisk]):以命名元组的形式返回磁盘io统计信息(汇总的),包括读、写的次数,读、写的字节数等。当perdisk的值为True,则分别列出单个磁盘的统计信息(字典:key为磁盘名称,value为统计的namedtuple)。
`import` `psutil``>>> psutil.disk_io_counters()``sdiskio(read_count``=``356568``, write_count``=``12058367``, read_bytes``=``12599494656``,``write_bytes``=``92961665024``, read_time``=``260945``, write_time``=``40815092``,``read_merged_count``=``32656``, write_merged_count``=``6334256``, busy_time``=``4421438``)``>>> psutil.disk_io_counters(perdisk``=``True``)``{``'sda1'``: sdiskio(read_count``=``737``, write_count``=``129``, read_bytes``=``7100416``,``write_bytes``=``5615616``, read_time``=``61``, write_time``=``578``, read_merged_count``=``496``,``write_merged_count``=``5349``, busy_time``=``257``), ``'sda2'``: sdiskio(read_count``=``161575``,``write_count``=``2825934``, read_bytes``=``6296442880``, write_bytes``=``46478901248``,``read_time``=``113557``, write_time``=``1580954``, read_merged_count``=``32160``,``write_merged_count``=``6328960``, busy_time``=``1026283``),``'dm-0'``: sdiskio(read_count``=``111371``, write_count``=``4944247``, read_bytes``=``2401461248``,``write_bytes``=``28988342272``, read_time``=``117846``, write_time``=``25992150``, read_merged_count``=``0``,``write_merged_count``=``0``, busy_time``=``2924323``), ``'dm-1'``: sdiskio(read_count``=``7482``,``write_count``=``21296``, read_bytes``=``30646272``, write_bytes``=``87228416``, read_time``=``8656``,``write_time``=``55930``, read_merged_count``=``0``, write_merged_count``=``0``, busy_time``=``1326``),``'dm-2'``: sdiskio(read_count``=``75407``, write_count``=``4267078``, read_bytes``=``3863860224``,``write_bytes``=``17403330560``, read_time``=``20849``, write_time``=``13185771``, read_merged_count``=``0``,``write_merged_count``=``0``, busy_time``=``469527``)}`
Network相关
net_io_counter([pernic]):以命名元组的形式返回当前系统中每块网卡的网络io统计信息,包括收发字节数,收发包的数量、出错的情况和删包情况。当pernic为True时,则列出所有网卡的统计信息。
`>>> ``import` `psutil``>>> psutil.net_io_counters()``snetio(bytes_sent``=``428286398``, bytes_recv``=``542982044``, packets_sent``=``3768555``,``packets_recv``=``4169425``, errin``=``0``, errout``=``0``, dropin``=``0``, dropout``=``0``)``>>> psutil.net_io_counters(pernic``=``True``)``{``'lo'``: snetio(bytes_sent``=``339700487``, bytes_recv``=``339700487``, packets_sent``=``3363412``,``packets_recv``=``3363412``, errin``=``0``, errout``=``0``, dropin``=``0``, dropout``=``0``), ``'eth0'``:``snetio(bytes_sent``=``88616264``, bytes_recv``=``203314200``, packets_sent``=``405585``,``packets_recv``=``806516``, errin``=``0``, errout``=``0``, dropin``=``0``, dropout``=``0``), ``'virbr0'``: snetio(bytes_sent``=``0``,``bytes_recv``=``0``, packets_sent``=``0``, packets_recv``=``0``, errin``=``0``, errout``=``0``, dropin``=``0``, dropout``=``0``),``'virbr0-nic'``: snetio(bytes_sent``=``0``, bytes_recv``=``0``, packets_sent``=``0``, packets_recv``=``0``, errin``=``0``,``errout``=``0``, dropin``=``0``, dropout``=``0``)}`
net_connections([kind]):以列表的形式返回每个网络连接的详细信息(namedtuple)。命名元组包含fd, family, type, laddr, raddr, status, pid等信息。kind表示过滤的连接类型,支持的值如下:(默认为inet)
- inet:IPv4 and IPv6
- inet4:IPv4
- inet6:IPv6
- tcp:TCP
- tcp4:TCP over IPv4
- tcp6:TCP over IPv6
- udp:UDP
- udp4:UDP over IPv4
- udp6:UDP over IPv6
- unix:UNIX socket (both UDP and TCP protocols)
- all:the sum of all the possible families and protocols
- unix:UNIX socket (both UDP and TCP protocols)
- udp6:UDP over IPv6
- udp4:UDP over IPv4
- udp:UDP
- tcp6:TCP over IPv6
- tcp4:TCP over IPv4
- tcp:TCP
- inet6:IPv6
- inet4:IPv4
`>>> ``import` `psutil``>>> psutil.net_connections()``[sconn(fd``=``6``, family``=```2``>, ``type``=```1``>,``laddr``=``addr(ip``=``'0.0.0.0'``, port``=``8080``), raddr``=``(), status``=``'LISTEN'``, pid``=``21834``),``sconn(fd``=``3``, family``=```2``>, ``type``=```1``>,``laddr``=``addr(ip``=``'0.0.0.0'``, port``=``22``), raddr``=``(), status``=``'LISTEN'``, pid``=``8971``),``sconn(fd``=``-``1``, family``=```2``>, ``type``=```1``>,``laddr``=``addr(ip``=``'127.0.0.1'``, port``=``10050``), raddr``=``addr(ip``=``'127.0.0.1'``, port``=``46710``),``status``=``'TIME_WAIT'``, pid``=``None``),`
net_if_addrs():以字典的形式返回网卡的配置信息,包括IP地址和mac地址、子网掩码和广播地址。
`>>> psutil.net_if_addrs()``{``'lo'``: [snic(family``=```2``>, address``=``'127.0.0.1'``, netmask``=``'255.0.0.0'``, broadcast``=``None``, ptp``=``None``),``snic(family``=```10``>, address``=``'::1'``, netmask``=``'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff'``, broadcast``=``None``, ptp``=``None``),``snic(family``=```17``>, address``=``'00:00:00:00:00:00'``, netmask``=``None``, broadcast``=``None``, ptp``=``None``)],``'eth0'``: [snic(family``=```2``>, address``=``'10.0.0.3'``, netmask``=``'255.255.255.0'``, broadcast``=``'10.0.0.255'``, ptp``=``None``),``snic(family``=```10``>, address``=``'fe80::21c:42ff:fe52:ae47%eth0'``, netmask``=``'ffff:ffff:ffff:ffff::'``, broadcast``=``None``, ptp``=``None``),``snic(family``=```17``>, address``=``'00:1c:42:52:ae:47'``, netmask``=``None``, broadcast``=``'ff:ff:ff:ff:ff:ff'``, ptp``=``None``)],``'virbr0'``: [snic(family``=```2``>, address``=``'192.168.122.1'``, netmask``=``'255.255.255.0'``, broadcast``=``'192.168.122.255'``, ptp``=``None``),``snic(family``=```17``>, address``=``'52:54:00:04:87:ce'``, netmask``=``None``, broadcast``=``'ff:ff:ff:ff:ff:ff'``, ptp``=``None``)],``'virbr0-nic'``: [snic(family``=```17``>, address``=``'52:54:00:04:87:ce'``, netmask``=``None``, broadcast``=``'ff:ff:ff:ff:ff:ff'``, ptp``=``None``)]}`
net_if_stats():返回网卡的详细信息,包括是否启动、通信类型、传输速度与mtu。
`>>> ``import` `psutil``>>> psutil.net_if_stats()``{``'lo'``: snicstats(isup``=``True``, duplex``=```0``>, speed``=``0``, mtu``=``65536``),``'eth0'``: snicstats(isup``=``True``, duplex``=```0``>, speed``=``0``, mtu``=``1500``),``'virbr0'``: snicstats(isup``=``True``, duplex``=```0``>, speed``=``0``, mtu``=``1500``),``'virbr0-nic'``: snicstats(isup``=``False``, duplex``=```2``>, speed``=``10``, mtu``=``1500``)}``>>>`
Other
users():以命名元组的方式返回当前登陆用户的信息,包括用户名,登陆时间,终端,与主机信息
`>>> ``import` `psutil``>>> psutil.users()``[suser(name``=``'root'``, terminal``=``'tty1'``, host``=``'', started``=``1513498496.0``, pid``=``9327``),``suser(name``=``'root'``, terminal``=``'pts/0'``, host``=``'10.0.0.2'``, started``=``1515335808.0``, pid``=``30838``),``suser(name``=``'root'``, terminal``=``'pts/2'``, host``=``'10.0.0.2'``, started``=``1515324032.0``, pid``=``21939``)]` `# start 表示登陆的时间(时间戳格式)`
boot_time():以时间戳的形式返回系统的启动时间
`>>> ``import` `psutil``>>> psutil.boot_time()``1514678349.0`` `
1 >>> import datetime
2 >>> import psutil
3 >>> datetime.datetime.fromtimestamp(psutil.boot_time ()).strftime("%Y-%m-%d %H: %M: %S")
psutil进程管理
psutil还提供了作为进程管理的功能函数,包括获取进程列表,判断是否存在。
Process类:对进程进行封装,可以使用该类的方法获取进行的详细信息,或者给进程发送信号。
`>>> ``import` `psutil``>>> init_process ``=` `psutil.Process(``1``)``>>> init_process``psutil.Process(pid``=``1``, name``=``'launchd'``, started``=``'20:41:22'``) ``# 进程id、名称、启动时间`
Process类提供了很多其他用来获取进程相关信息的方法
- name:获取进程的名称
- cmdline:获取启动进程的命令行参数
- create_time:获取进程的创建时间(时间戳格式)
- num_fds:进程打开的文件个数
- num_threads:进程的子进程个数
- is_running:判断进程是否正在运行
- send_signal:给进程发送信号,类似与os.kill等
- kill:发送SIGKILL信号结束进程
- terminate:发送SIGTEAM信号结束进程
`>>> ``import` `psutil``>>> process ``=` `psutil.Process(``3297``)``>>> process.name()``'Google Chrome Helper'``>>> process.create_time()``1519832141.532822``>>> process.num_fds()``22``>>> process.num_threads()``19`
pids:以列表的形式返回当前正在运行的进程
*pid_exists:判断给点定的pid是否存在*
****process_iter:迭代当前正在运行的进程,返回的是每个进程的Process对象******
`>>> psutil.pids()``[``3373``, ``3372``, ``3365``, ``3364``, ``3363``, ``3361``, ``3360``, ``3358``, ``3355``, ``3351``, ``3335``, ``3301``, ``3299``, ``3297``, ``3291``, ``3290``, ``3289``, ``3279``, ``3278``, ``3274``, ``3251``, ``3248``, ``3247``, ``3246``, ``3244``, ``3242``, ``3241``, ``3225``, ``3209``, ``3198``, ``3194``, ``3193``, ``3101``, ``2828``, ``2816``, ``2805``, ``2802``, ``2799``, ``2797``, ``2767``, ``2497``, ``2441``, ``2437``, ``2343``, ``2333``, ``2330``, ``2287``, ``2283``, ``2258``, ``2257``, ``2251``, ``2248``, ``2247``, ``2246``, ``2245``, ``2241``, ``2240``, ``2238``, ``2231``, ``2225``]``>>> psutil.pid_exists(``12``)``False``>>> ``list``(psutil.process_iter())[:``5``]``[psutil.Process(pid``=``0``, name``=``'kernel_task'``, started``=``'20:41:22'``), psutil.Process(pid``=``1``, name``=``'launchd'``, started``=``'20:41:22'``), psutil.Process(pid``=``48``, name``=``'UserEventAgent'``, started``=``'20:41:27'``), psutil.Process(pid``=``50``, name``=``'uninstalld'``, started``=``'20:41:27'``), psutil.Process(pid``=``51``, name``=``'kextd'``, started``=``'20:41:27'``)] ``# 每个都是一个Process对象``>>> `
所有巧合的是要么是上天注定要么是一个人偷偷的在努力。
原文地址:https://www.cnblogs.com/ham-731/p/12121619.html