系統性能信息模塊之psutil模塊

1、psutil模塊介紹

官方網址:https://pypi.org/linux

psutil模塊安裝:https://github.com/giampaolo/psutil/blob/master/INSTALL.rstios

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

概要

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操做系統。

2、獲取系統性能信息

獲取CPU信息

Linux操做系統的CPU利用率有如下幾個部分:github

  • 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()方法獲取這些信息,示例以下:ionic

>>> 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(接收數據包數)spa

>>> 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'
相關文章
相關標籤/搜索