Loading

系统性能信息模块之psutil模块

一、psutil模块介绍

官方网址:https://pypi.org/

psutil模块安装:https://github.com/giampaolo/psutil/blob/master/INSTALL.rst

官方介绍:https://pypi.org/project/psutil/#downloads 

概要

plutil(进程和系统实用程序)是一个跨平台的库,用于在Python中检索有关运行进程和系统资源利用率(CPU,内存,磁盘,网络)的信息。它主要用于系统监视,分析和限制系统资源及运行进程的管理。它实现了Linux命令工具提供的许多功能,例如:ps,top,lsof,netstat,ifconfig,who,df,kill,nice,ionice,iostat,iotop,uptime,pidos,tty,tasket,pmap等。psutil目前支持以下平台:linux、Windows、OSX、FreeBSD,OpenBSD、NETBSD、sun Solaris、AIX操作系统。

二、获取系统性能信息

获取CPU信息

Linux操作系统的CPU利用率有以下几个部分:

  • User Time,执行用户进程的时间百分比
  • System Time,执行内核进程和中断的时间百分比
  • Wait IO,由于IO等待而使CPU处于idle(空闲)状态的时间百分比;
  • Idle,CPU处于idle状态的时间百分比。

使用Python的psutil.cpu_times()方法可以非常简单地得到这些信息,同时也可以获取CPU的硬件相关信息,比如CPU的物理个数与逻辑个数,示例如下:

>>> import psutil
>>> psutil.cpu_times()
scputimes(user=5046.13, nice=0.0, system=7241.73, idle=106233.48)
>>> psutil.cpu_times(percpu=True)    #显示所有逻辑CPU信息,指定方法变量percpu=True
[scputimes(user=1915.16, nice=0.0, system=3071.14, idle=24700.29), scputimes(user=612.52, nice=0.0, system=552.06, idle=28520.87), scputimes(user=1907.31, nice=0.0, system=3077.95, idle=24700.2), scputimes(user=628.63, nice=0.0, system=545.3, idle=28511.51)]

>>> psutil.cpu_times().user      #获取单项数据信息,如用户user的CPU时间比
5116.06
>>> psutil.cpu_count()              #获取CPU的逻辑个数,默认logical=True
4
>>> psutil.cpu_count(logical=False)           #获取CPU的物理个数
2
>>> psutil.cpu_stats()
scpustats(ctx_switches=128761, interrupts=473080, soft_interrupts=19603638, syscalls=595993)
>>> psutil.cpu_freq()
scpufreq(current=3100, min=3100, max=3100)

获取内存信息

Linux系统的内存利用率涉及total(内存总数)、used(以使用的内存数)、free(空闲内存数)、buffers(缓冲使用数)、cache(缓存使用数)、swap(交换分区使用数)等,分别使用

psutil.virtual_memory()与psutil.swap_memory()方法获取这些信息,示例如下:

>>> import psutil
>>> psutil.virtual_memory()           #使用psutil.virtaul_memory方法获取内存完整信息
svmem(total=8589934592, available=2785501184, percent=67.6, used=7854592000, free=346370048, active=3651194880, inactive=2439131136, wired=1764265984)
>>> psutil.swap_memory()             #获取SWAP分区信息
sswap(total=0, used=0, free=0, percent=0, sin=11696345088, sout=231964672)
>>> psutil.virtual_memory().total         #获取内存总数
8589934592
>>> psutil.virtual_memory().free          #获取空闲内存数
318025728

获取磁盘信息

磁盘IO信息包括read_count(读IO数)、write_count(写IO数)、read_bytes(IO读字节书)、write_bytes(IO写字节数)、read_time(磁盘读时间)、write_time(磁盘写时间)等,

使用psutil.disk_io_counters()获取

>>> import psutil
>>> psutil.disk_partitions()        #psutil.disk_partitions方法获取磁盘完整信息
[sdiskpart(device='/dev/disk1', mountpoint='/', fstype='hfs', opts='rw,local,rootfs,dovolfs,journaled,multilabel')]
>>> psutil.disk_usage('/')          #psutil.disk_usage方法获取分区(参数)
sdiskusage(total=249664372736, used=90747437056, free=158654791680, percent=36.4)
>>> psutil.disk_io_counters()       #psutil.disk_io_counters获取磁盘总的IO个数、读写信息
sdiskio(read_count=375539, write_count=149398, read_bytes=9541869056, write_bytes=5333078016, read_time=135111, write_time=94994)
>>> psutil.disk_io_counters(perdisk=True)     #perdisk=True 参数获取单个分区IO个数、读写信息
{'disk0': sdiskio(read_count=375543, write_count=149432, read_bytes=9541934592, write_bytes=5333483008, read_time=135115, write_time=95009)}

获取网络信息

系统的网络信息与磁盘IO类似,涉及几个关键点,包括bytes_set(发送字节数)、bytes_recv(接收字节数)、packets_set(发送数据包数)、packets_recv(接收数据包数)

>>> psutil.net_io_counters()     #psutil.net_io_counters获取网络总的IO信息,默认pernice=False
snetio(bytes_sent=53240820, bytes_recv=2436823493, packets_sent=510560, packets_recv=2321776, errin=0, errout=49, dropin=0, dropout=0)
>>> psutil.net_io_counters(pernic=True)     #pernice=True 参数 输出每个网络接口的IO信息
{'lo0': snetio(bytes_sent=170312, bytes_recv=170312, packets_sent=1271, packets_recv=1271, errin=0, errout=0, dropin=0, dropout=0), 'gif0': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'stf0': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'en0': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'en2': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'en4': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'en1': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'en3': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'bridge0': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'p2p0': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'awdl0': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'en6': snetio(bytes_sent=434385, bytes_recv=810643, packets_sent=2798, packets_recv=2942, errin=0, errout=0, dropin=0, dropout=0), 'utun0': snetio(bytes_sent=268, bytes_recv=0, packets_sent=3, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'en10': snetio(bytes_sent=52635963, bytes_recv=2435926406, packets_sent=506490, packets_recv=2317673, errin=0, errout=49, dropin=0, dropout=0), 'vmnet1': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'vmnet8': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0)}


>>> psutil.net_if_addrs()
{'lo0': [snic(family=<AddressFamily.AF_INET: 2>, address='127.0.0.1', netmask='255.0.0.0', broadcast=None, ptp=None), snic(family=<AddressFamily.AF_INET6: 30>, address='::1', netmask='ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff', broadcast=None, ptp=None), snic(family=<AddressFamily.AF_INET6: 30>, address='fe80::1%lo0', netmask='ffff:ffff:ffff:ffff::', broadcast=None, ptp=None)], 'en10': [snic(family=<AddressFamily.AF_INET: 2>, address='192.168.12.119', netmask='255.255.255.0', broadcast='192.168.12.255', ptp=None), snic(family=<AddressFamily.AF_LINK: 18>, address='d4:3a:65:07:2a:e2', netmask=None, broadcast=None, ptp=None), snic(family=<AddressFamily.AF_INET6: 30>, address='fe80::18a7:9f11:40a7:c7f4%en10', netmask='ffff:ffff:ffff:ffff::', broadcast=None, ptp=None)], 'vmnet1': [snic(family=<AddressFamily.AF_INET: 2>, address='192.168.75.1', netmask='255.255.255.0', broadcast='192.168.75.255', ptp=None), snic(family=<AddressFamily.AF_LINK: 18>, address='00:50:56:c0:00:01', netmask=None, broadcast=None, ptp=None)], 'vmnet8': [snic(family=<AddressFamily.AF_INET: 2>, address='192.168.121.1', netmask='255.255.255.0', broadcast='192.168.121.255', ptp=None), snic(family=<AddressFamily.AF_LINK: 18>, address='00:50:56:c0:00:08', netmask=None, broadcast=None, ptp=None)], 'en0': [snic(family=<AddressFamily.AF_LINK: 18>, address='8c:85:90:3c:fa:20', netmask=None, broadcast=None, ptp=None)], 'en2': [snic(family=<AddressFamily.AF_LINK: 18>, address='5a:00:24:a0:8d:04', netmask=None, broadcast=None, ptp=None)], 'en4': [snic(family=<AddressFamily.AF_LINK: 18>, address='5a:00:24:a0:8d:05', netmask=None, broadcast=None, ptp=None)], 'en1': [snic(family=<AddressFamily.AF_LINK: 18>, address='5a:00:24:a0:8d:00', netmask=None, broadcast=None, ptp=None)], 'en3': [snic(family=<AddressFamily.AF_LINK: 18>, address='5a:00:24:a0:8d:01', netmask=None, broadcast=None, ptp=None)], 'bridge0': [snic(family=<AddressFamily.AF_LINK: 18>, address='5a:00:24:a0:8d:00', netmask=None, broadcast=None, ptp=None)], 'p2p0': [snic(family=<AddressFamily.AF_LINK: 18>, address='0e:85:90:3c:fa:20', netmask=None, broadcast=None, ptp=None)], 'awdl0': [snic(family=<AddressFamily.AF_LINK: 18>, address='62:46:b9:22:e2:44', netmask=None, broadcast=None, ptp=None)], 'en6': [snic(family=<AddressFamily.AF_LINK: 18>, address='ac:de:48:00:11:22', netmask=None, broadcast=None, ptp=None), snic(family=<AddressFamily.AF_INET6: 30>, address='fe80::aede:48ff:fe00:1122%en6', netmask='ffff:ffff:ffff:ffff::', broadcast=None, ptp=None)], 'utun0': [snic(family=<AddressFamily.AF_INET6: 30>, address='fe80::37a7:ebf5:f72a:6017%utun0', netmask='ffff:ffff:ffff:ffff::', broadcast=None, ptp=None)]}
>>> 
>>> psutil.net_if_stats()
{'lo0': snicstats(isup=True, duplex=<NicDuplex.NIC_DUPLEX_UNKNOWN: 0>, speed=0, mtu=16384), 'gif0': snicstats(isup=False, duplex=<NicDuplex.NIC_DUPLEX_UNKNOWN: 0>, speed=0, mtu=1280), 'stf0': snicstats(isup=False, duplex=<NicDuplex.NIC_DUPLEX_UNKNOWN: 0>, speed=0, mtu=1280), 'en0': snicstats(isup=True, duplex=<NicDuplex.NIC_DUPLEX_UNKNOWN: 0>, speed=0, mtu=1500), 'en2': snicstats(isup=True, duplex=<NicDuplex.NIC_DUPLEX_FULL: 2>, speed=0, mtu=1500), 'en4': snicstats(isup=True, duplex=<NicDuplex.NIC_DUPLEX_FULL: 2>, speed=0, mtu=1500), 'en1': snicstats(isup=True, duplex=<NicDuplex.NIC_DUPLEX_FULL: 2>, speed=0, mtu=1500), 'en3': snicstats(isup=True, duplex=<NicDuplex.NIC_DUPLEX_FULL: 2>, speed=0, mtu=1500), 'bridge0': snicstats(isup=False, duplex=<NicDuplex.NIC_DUPLEX_UNKNOWN: 0>, speed=0, mtu=1500), 'p2p0': snicstats(isup=False, duplex=<NicDuplex.NIC_DUPLEX_UNKNOWN: 0>, speed=0, mtu=2304), 'awdl0': snicstats(isup=False, duplex=<NicDuplex.NIC_DUPLEX_UNKNOWN: 0>, speed=0, mtu=1484), 'en6': snicstats(isup=True, duplex=<NicDuplex.NIC_DUPLEX_UNKNOWN: 0>, speed=0, mtu=1500), 'utun0': snicstats(isup=True, duplex=<NicDuplex.NIC_DUPLEX_UNKNOWN: 0>, speed=0, mtu=2000), 'en10': snicstats(isup=True, duplex=<NicDuplex.NIC_DUPLEX_FULL: 2>, speed=100, mtu=1500), 'vmnet1': snicstats(isup=True, duplex=<NicDuplex.NIC_DUPLEX_UNKNOWN: 0>, speed=0, mtu=1500), 'vmnet8': snicstats(isup=True, duplex=<NicDuplex.NIC_DUPLEX_UNKNOWN: 0>, speed=0, mtu=1500)}

other system info

>>> import psutil
>>> psutil.users()       #psutil.users方法返回当前登录系统的用户信息
[suser(name='root', terminal='pts/0', host='192.168.121.1', started=1527069184.0)]
>>> psutil.boot_time()     #psutil.boot_time方法获取开机时间,以Linux时间戳格式返回
1527069188.0
>>> datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S")     #转换成自然时间格式
'2018-05-23 17:53:08'

 

posted @ 2018-05-24 15:16  KubeSec  阅读(2140)  评论(0编辑  收藏  举报