欢迎访问我的网站,希望内容对您有用,感兴趣的可以加入我们的社群。

Python实用模块(七)psutil

实用模块 迷途小书童 5年前 (2019-12-03) 3497次浏览 0个评论

软硬件环境

  • ubuntu 18.04 64bit
  • anaconda with python 3.6
  • psutil

psutils

psutil(process and system utilities)是一个跨平台的获取系统信息(CPU、内存、磁盘、网络等)的库。在linux系统中可以通过一些命令工具来查看系统信息,如topdstatvmstat等,如果使用subprocess调用也能实现目标,但是自己解析起来比较麻烦。

安装psutil

pip install psutil

CPU相关实例

xugaoxiang@ubuntu:~$ ipython3 
Python 3.6.6 |Anaconda custom (64-bit)| (default, Oct  9 2018, 12:34:16) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.1.1 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import psutil                                                                                                                    

In [2]: psutil.cpu_times()                                                                                                               
Out[2]: scputimes(user=12359.56, nice=30.75, system=2791.42, idle=105554.11, iowait=945.91, irq=0.0, softirq=94.67, steal=0.0, guest=0.0, guest_nice=0.0)                                                                                                               

In [7]: psutil.cpu_count()                                                                                                               
Out[7]: 4

In [8]: psutil.cpu_count(logical=False)                                                                                                  
Out[8]: 2

In [9]: psutil.cpu_stats()                                                                                                               
Out[9]: scpustats(ctx_switches=161457633, interrupts=43726924, soft_interrupts=34136310, syscalls=0)

In [10]: psutil.cpu_freq()                                                                                                               
Out[10]: scpufreq(current=1829.73775, min=500.0, max=2700.0)

In [11]:  

内存相关实例

xugaoxiang@ubuntu:~$ ipython3 
Python 3.6.6 |Anaconda custom (64-bit)| (default, Oct  9 2018, 12:34:16) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.1.1 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import psutil                                                                                                                    

In [2]: psutil.virtual_memory()                                                                                                          
Out[2]: svmem(total=12505137152, available=5109202944, percent=59.1, used=6563024896, free=887455744, active=7999864832, inactive=2919788544, buffers=939851776, cached=4114804736, shared=508735488, slab=418844672)

In [3]: psutil.swap_memory()                                                                                                             
Out[3]: sswap(total=4154454016, used=0, free=4154454016, percent=0.0, sin=0, sout=0)

In [4]: 

磁盘相关实例

xugaoxiang@ubuntu:~$ ipython3 
Python 3.6.6 |Anaconda custom (64-bit)| (default, Oct  9 2018, 12:34:16) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.1.1 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import psutil                                                                                                                    

In [2]: psutil.disk_partitions()                                                                                                         
Out[2]: 
[sdiskpart(device='/dev/sda7', mountpoint='/', fstype='ext4', opts='rw,relatime,errors=remount-ro,data=ordered'),
 sdiskpart(device='/dev/loop0', mountpoint='/snap/sublime-text/38', fstype='squashfs', opts='ro,nodev,relatime'),
 sdiskpart(device='/dev/loop2', mountpoint='/snap/gnome-3-26-1604/70', fstype='squashfs', opts='ro,nodev,relatime'),
 sdiskpart(device='/dev/loop3', mountpoint='/snap/gnome-calculator/260', fstype='squashfs', opts='ro,nodev,relatime'),
 sdiskpart(device='/dev/loop4', mountpoint='/snap/core/5145', fstype='squashfs', opts='ro,nodev,relatime'),
 sdiskpart(device='/dev/loop5', mountpoint='/snap/core/5328', fstype='squashfs', opts='ro,nodev,relatime'),
 sdiskpart(device='/dev/loop6', mountpoint='/snap/gnome-calculator/222', fstype='squashfs', opts='ro,nodev,relatime'),
 sdiskpart(device='/dev/loop7', mountpoint='/snap/gnome-3-26-1604/64', fstype='squashfs', opts='ro,nodev,relatime'),
 sdiskpart(device='/dev/loop1', mountpoint='/snap/sublime-text/26', fstype='squashfs', opts='ro,nodev,relatime'),
 sdiskpart(device='/dev/loop10', mountpoint='/snap/gnome-system-monitor/51', fstype='squashfs', opts='ro,nodev,relatime'),
 sdiskpart(device='/dev/loop8', mountpoint='/snap/gnome-system-monitor/54', fstype='squashfs', opts='ro,nodev,relatime'),
 sdiskpart(device='/dev/loop15', mountpoint='/snap/gnome-system-monitor/57', fstype='squashfs', opts='ro,nodev,relatime'),
 sdiskpart(device='/dev/loop11', mountpoint='/snap/gnome-characters/124', fstype='squashfs', opts='ro,nodev,relatime'),
 sdiskpart(device='/dev/loop12', mountpoint='/snap/gnome-characters/139', fstype='squashfs', opts='ro,nodev,relatime'),
 sdiskpart(device='/dev/loop13', mountpoint='/snap/gnome-calculator/238', fstype='squashfs', opts='ro,nodev,relatime'),
 sdiskpart(device='/dev/loop14', mountpoint='/snap/sublime-text/29', fstype='squashfs', opts='ro,nodev,relatime'),
 sdiskpart(device='/dev/loop9', mountpoint='/snap/gnome-3-26-1604/74', fstype='squashfs', opts='ro,nodev,relatime'),
 sdiskpart(device='/dev/loop17', mountpoint='/snap/gnome-characters/117', fstype='squashfs', opts='ro,nodev,relatime'),
 sdiskpart(device='/dev/loop18', mountpoint='/snap/gnome-logs/43', fstype='squashfs', opts='ro,nodev,relatime'),
 sdiskpart(device='/dev/loop19', mountpoint='/snap/gtk-common-themes/808', fstype='squashfs', opts='ro,nodev,relatime'),
 sdiskpart(device='/dev/loop16', mountpoint='/snap/gnome-logs/40', fstype='squashfs', opts='ro,nodev,relatime'),
 sdiskpart(device='/dev/loop20', mountpoint='/snap/gtk-common-themes/319', fstype='squashfs', opts='ro,nodev,relatime'),
 sdiskpart(device='/dev/loop22', mountpoint='/snap/core/5742', fstype='squashfs', opts='ro,nodev,relatime'),
 sdiskpart(device='/dev/loop23', mountpoint='/snap/gnome-logs/45', fstype='squashfs', opts='ro,nodev,relatime'),
 sdiskpart(device='/dev/loop21', mountpoint='/snap/gtk-common-themes/701', fstype='squashfs', opts='ro,nodev,relatime'),
 sdiskpart(device='/dev/sda1', mountpoint='/boot/efi', fstype='vfat', opts='rw,relatime,fmask=0077,dmask=0077,codepage=437,iocharset=iso8859-1,shortname=mixed,errors=remount-ro'),
 sdiskpart(device='/dev/sda7', mountpoint='/var/lib/docker/plugins', fstype='ext4', opts='rw,relatime,errors=remount-ro,data=ordered')]

In [4]: psutil.disk_usage('/home/xugaoxiang')                                                                                            
Out[4]: sdiskusage(total=194802298880, used=105345220608, free=79490314240, percent=57.0)

In [5]: psutil.disk_io_counters(perdisk=False)                                                                                           
Out[5]: sdiskio(read_count=200046, write_count=382208, read_bytes=4329474560, write_bytes=13712557056, read_time=4256476, write_time=16670324, read_merged_count=63054, write_merged_count=525960, busy_time=2458360)

In [6]: 

网络相关实例

xugaoxiang@ubuntu:~$ ipython3 
Python 3.6.6 |Anaconda custom (64-bit)| (default, Oct  9 2018, 12:34:16) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.1.1 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import psutil                                                                                                                    

In [2]: psutil.net_io_counters(pernic=True)                                                                                              
Out[2]: 
{'lo': snetio(bytes_sent=537908952, bytes_recv=537908952, packets_sent=344287, packets_recv=344287, errin=0, errout=0, dropin=0, dropout=0),
 'docker0': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0),
 'br-24934d573699': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0),
 'enp0s25': snetio(bytes_sent=380208852, bytes_recv=701685327, packets_sent=522525, packets_recv=704237, errin=0, errout=0, dropin=0, dropout=0),
 'enxf01e3411d08f': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0),
 'wlp4s0': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0)}

In [3]: psutil.net_if_addrs()                                                                                                            
Out[3]: 
{'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)],
 'enp0s25': [snicaddr(family=<AddressFamily.AF_INET: 2>, address='192.168.1.141', netmask='255.255.255.0', broadcast='192.168.1.255', ptp=None),
  snicaddr(family=<AddressFamily.AF_INET6: 10>, address='fe80::dae2:388d:d6dc:8cd5%enp0s25', netmask='ffff:ffff:ffff:ffff::', broadcast=None, ptp=None),
  snicaddr(family=<AddressFamily.AF_PACKET: 17>, address='50:7b:9d:d5:13:e6', netmask=None, broadcast='ff:ff:ff:ff:ff:ff', ptp=None)],
 'br-24934d573699': [snicaddr(family=<AddressFamily.AF_INET: 2>, address='172.18.0.1', netmask='255.255.0.0', broadcast='172.18.255.255', ptp=None),
  snicaddr(family=<AddressFamily.AF_PACKET: 17>, address='02:42:24:25:61:bf', netmask=None, broadcast='ff:ff:ff:ff:ff:ff', ptp=None)],
 'docker0': [snicaddr(family=<AddressFamily.AF_INET: 2>, address='172.17.0.1', netmask='255.255.0.0', broadcast='172.17.255.255', ptp=None),
  snicaddr(family=<AddressFamily.AF_PACKET: 17>, address='02:42:aa:65:ac:4c', netmask=None, broadcast='ff:ff:ff:ff:ff:ff', ptp=None)],
 'wlp4s0': [snicaddr(family=<AddressFamily.AF_PACKET: 17>, address='2c:6e:85:47:d0:d8', netmask=None, broadcast='ff:ff:ff:ff:ff:ff', ptp=None)],
 'enxf01e3411d08f': [snicaddr(family=<AddressFamily.AF_PACKET: 17>, address='f0:1e:34:11:d0:8f', netmask=None, broadcast='ff:ff:ff:ff:ff:ff', ptp=None)]}

In [4]: psutil.net_if_stats()                                                                                                            
Out[4]: 
{'lo': snicstats(isup=True, duplex=<NicDuplex.NIC_DUPLEX_UNKNOWN: 0>, speed=0, mtu=65536),
 'docker0': snicstats(isup=True, duplex=<NicDuplex.NIC_DUPLEX_UNKNOWN: 0>, speed=0, mtu=1500),
 'br-24934d573699': snicstats(isup=True, duplex=<NicDuplex.NIC_DUPLEX_UNKNOWN: 0>, speed=0, mtu=1500),
 'enp0s25': snicstats(isup=True, duplex=<NicDuplex.NIC_DUPLEX_FULL: 2>, speed=100, mtu=1500),
 'enxf01e3411d08f': snicstats(isup=True, duplex=<NicDuplex.NIC_DUPLEX_HALF: 1>, speed=10, mtu=1500),
 'wlp4s0': snicstats(isup=False, duplex=<NicDuplex.NIC_DUPLEX_UNKNOWN: 0>, speed=0, mtu=1500)}

In [5]: 

Popen封装

xugaoxiang@ubuntu:~$ ipython3 
Python 3.6.6 |Anaconda custom (64-bit)| (default, Oct  9 2018, 12:34:16) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.1.1 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import psutil                                                                                                                    

In [2]: from subprocess import PIPE                                                                                                      

In [3]: p = psutil.Popen(["python", "-c", "print('hello')"], stdout=PIPE)                                                                

In [4]: p.name()                                                                                                                         
Out[4]: 'python'

In [5]: p.username()                                                                                                                     
Out[5]: 'xugaoxiang'

In [6]: p.communicate()                                                                                                                  
Out[6]: (b'hello\n', None)

In [7]: p.wait(timeout=2)                                                                                                                
Out[7]: 0

In [8]:  
喜欢 (0)

您必须 登录 才能发表评论!