Python学习之psutil模块详解

来源:互联网 发布:抢小米神器软件 编辑:程序博客网 时间:2024/05/22 08:25

本文和大家分享的主要是python的psutil模块相关内容,一起来看看吧,希望对大家学习python有所帮助。

  python 安装psutil 来实现获取系统信息

  # yum -y install python*pip# yum -y groupinstall "Development Tools# yum -y install python34-devel.x86_64 # pip3 install --upgrade pip# pip3 install ipython# pip3 install psutil

  1ipython 中终端界面测试

  [root@localhost ~]# ipython

  Python 3.4.5 (default, May 29 2017, 15:17:55)

  Type 'copyright', 'credits' or 'license' for more information

  IPython 6.1.0 -- An enhanced Interactive Python. Type '?' for help.

  In [1]: import psutil

  In [2]: mem = psutil.virtual_memory()

  In [3]: mem.total,mem.used

  Out[3]: (1033498624, 332750848)

  (1)获取系统性能信息

  采集系统的基本性能包括CPU、内存、磁盘、网络等等,可以完整描述当前系统的运行状态及质量。而psutil已经封闭了这些方法

  运维工程师可以根据自己的需求来调用

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

  1User Time ,执行用户进程的时间百分比;

  2System Time, 执行内核进程和中断的时间百分比

  3Wait IO,由于IO等待而使CPU处于idle(空闲)状态的时间百分比

  4IdleCPU处于idle状态的时间百分比

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

  eg:

  In [5]: psutil.cpu_times()Out[5]: scputimes(user=137.75, nice=0.52, system=68.2, idle=5436.8, iowait=60.51, irq=0.0, softirq=2.1, steal=0.0, guest=0.0, guest_nice=0.0)

  In [6]: psutil.cpu_times().userOut[6]: 137.96

  In [7]: psutil.cpu_count() ##获取CPU的逻辑个数Out[7]: 1

  In [8]: psutil.cpu_count(logical=False) ##获取CPU的物理个数 Out[8]: 1

  (2)内存信息

  Linux系统的内存信息利用率信息涉及total(内存总数)used(已使用的内存数)free(空闲内存数)buffers(缓冲使用数)

  cache(缓存使用数)swap(交换分区使用数)等,分别使用psutil.virtual_memory()psutil.swap_memory()方法获取这些信息

  操作如下

  In [1]: import psutil

  In [2]: mem = psutil.virtual_memory() ##获取内存完整信息

  In [3]: mem

  Out[3]: svmem(total=1033498624, available=573415424, percent=44.5, used=288641024, free=77266944, active=339841024, inactive=378482688, buffers=0, cached=667590656, shared=7475200)

  In [4]: mem.total ##获取内存总数

  Out[4]: 1033498624

  In [5]: mem.free ##获取空闲内存数

  Out[5]: 77266944

  In [6]: psutil.swap_memory() ##获取SWAP分区信息

  Out[6]: sswap(total=2147479552, used=12288, free=2147467264, percent=0.0, sin=0, sout=12288)

  (3)磁盘信息

  在系统的所有磁盘信息中,我们更加关注硬盘的I/O信息,其中磁盘利用率使用psutil.disk_usage方法获取。磁盘IO信息包括read_count(IO)

  write_count(IO)read_bytes(IO读字节数)write_bytes(IO写字节数)read_time(磁盘读时间)write_time(磁盘写时间)等。这些IO信息可以

  使用psutil.disk_io_counter()获取。

  操作如下

  In [7]: psutil.disk_partitions() ##使用psutil.disk_partitions方法获取磁盘完整信息

  Out[7]:

  [sdiskpart(device='/dev/mapper/centos-root', mountpoint='/', fstype='xfs', opts='rw,seclabel,relatime,attr2,inode64,noquota'),

  sdiskpart(device='/dev/mapper/centos-home', mountpoint='/home', fstype='xfs', opts='rw,seclabel,relatime,attr2,inode64,noquota'),

  sdiskpart(device='/dev/sda1', mountpoint='/boot', fstype='xfs', opts='rw,seclabel,relatime,attr2,inode64,noquota')]

  In [8]: psutil.disk_usage('/') ##使用psutil.disk_usage方法获取分区参数的使用情况

  Out[8]: sdiskusage(total=53660876800, used=4668710912, free=48992165888, percent=8.7)

  In [9]: psutil.disk_io_counters()##使用psutil.disk_io_counters获取硬盘总数的IO个数

  Out[9]: sdiskio(read_count=35014, write_count=73927, read_bytes=923575296, write_bytes=2137012224, read_time=411040, write_time=9266640, read_merged_count=12, write_merged_count=17756, busy_time=271857)In [10]: psutil.disk_io_counters(perdisk=True) ##"perdisk=True"参数获取单个分区IO个数

  Out[10]:

  {'dm-0': sdiskio(read_count=15871, write_count=44823, read_bytes=456472576, write_bytes=1065395200, read_time=203757, write_time=7633124, read_merged_count=0, write_merged_count=0, busy_time=132981),'dm-1': sdiskio(read_count=141, write_count=3, read_bytes=1298432, write_bytes=12288, read_time=213, write_time=4195, read_merged_count=0, write_merged_count=0, busy_time=2406),'dm-2': sdiskio(read_count=464, write_count=6, read_bytes=1353728, write_bytes=2098176, read_time=566, write_time=22, read_merged_count=0, write_merged_count=0, busy_time=568),'fd0': sdiskio(read_count=0, write_count=0, read_bytes=0, write_bytes=0, read_time=0, write_time=0, read_merged_count=0, write_merged_count=0, busy_time=0),'sda1': sdiskio(read_count=1953, write_count=2057, read_bytes=4658176, write_bytes=2110464, read_time=1880, write_time=454, read_merged_count=0, write_merged_count=0, busy_time=2248),'sda2': sdiskio(read_count=16574, write_count=27068, read_bytes=459612160, write_bytes=1067505664, read_time=204457, write_time=1628869, read_merged_count=12, write_merged_count=17758, busy_time=133511),'sr0': sdiskio(read_count=11, write_count=0, read_bytes=180224, write_bytes=0, read_time=167, write_time=0, read_merged_count=0, write_merged_count=0, busy_time=167)}

  (4)网络信息

  系统的网络信息的与磁盘的IO类似,涉及几个关键点,包括bytes_sent(发送字节数)bytes_recv=28220119(接收字节数)packets_sent=200978(发送数据包数)

  packets_recv=212672(接收数据包数)等。这些网终信息使用psutil.net_io_counters()方法获取。

  操作如下

  In [11]: psutil.net_io_counters() ##使用psutil.net_io_counters获取网络总的IO信息,默认pernic=False

  Out[11]: snetio(bytes_sent=3123740, bytes_recv=157671880, packets_sent=33813, packets_recv=220535, errin=0, errout=0, dropin=0, dropout=0)

  (5)其他系统信息

  psutil模块还支持获取用户登录,开机时间等信息

  操作如下

  In [12]: psutil.users()  ##使用psutil.users 方法当当登录系统的用户信息。Out[12]:

  [suser(name='root', terminal='tty1', host='', started=1498524288.0),

  suser(name='root', terminal='pts/2', host='10.2.84.143', started=1498545280.0)]

  In [13]: psutil.boot_time()Out[13]: 1498524250.0

  2、系统进程管理方法

  获得当前系统的进程信息,可以让运维人员得知应用程序的状态,包括进程的启动时间、查看或设置CPU亲和度、内存使用率、IO信息

  socket连接、线程数等,这些信息可以呈现出指定进程是否存活、资源利用情况,为开发人员的代码优化、问题定位提供良好的数据参考

  (1)进程信息

  psutil模块在获取进程信息方面也提供了很好的支持,包括使用psutil.pids()方法获取所有进程的PID,使用psutil.Process()方法获取单

  个进程的名称、路径、状态、系统资源利用率等信息。

  操作如下

  In [19]: import psutil

  In [20]: psutil.pids() ##列出所有进程的PID

  Out[20]:

  [1,2,3,7,8,9,10,11,12,13,14,15,16,17,18,19,20,

  ......In [21]: p = psutil.Process(7509) ##实例化一个Process对象,参数为一个进程PID

  In [22]: p.name  ##进程名

  Out[22]:<bound method Process.name of <PSUTIL.PROCESS(PID=7509, 140135402646272="" at="" name="crond">>

  In [23]: p.exe() ##进程bin路径

  Out[23]: '/usr/sbin/crond'

  In [24]: p.cwd() ##进程工作目录绝对路径

  Out[24]: '/'

  In [25]: p.status() ##进程状态

  Out[25]: 'sleeping'

  In [26]: p.create_time() ##进程创建时间,时间戳格式

  Out[26]: 1498540040.44

  In [27]: p.uids() ##进程uid信息

  Out[27]: puids(real=0, effective=0, saved=0)

  In [28]: p.gids() ##进程gid信息

  Out[28]: pgids(real=0, effective=0, saved=0)

  In [29]: p.cpu_times() ##进程CPU时间信息,包括usersystem两个CPU时间

  Out[29]: pcputimes(user=0.0, system=0.49, children_user=0.21, children_system=0.68)

  In [30]: p.cpu_affinity() ##get进程CPU亲和度,如要设置进程CPU亲和度,将CPU号作为参数即可

  Out[30]: [0]

  In [31]: p.memory_percent() ##进程内存利用率

  Out[31]: 0.16843757307218243

  In [32]: p.memory_info() ##进程在内 rss\\vms信息

  Out[32]: pmem(rss=1740800, vms=129363968, shared=1093632, text=61440, lib=0, data=1347584, dirty=0)

  In [33]: p.io_counters() ##进程IO信息,包括读写IO数及字节数

  Out[33]: pio(read_count=9719, write_count=934, read_bytes=937984, write_bytes=258048, read_chars=5607895, write_chars=111727)

  In [34]: p.connections() ##返回打开进程socketnamedutples列表,包括fs\\family\\laddr等信息

  Out[34]: []

  In [35]: p.num_threads() ##进程开启的线程数

  Out[35]: 1

  (2)popen类的使用

  psutil提供的popen类的作用是获取用户启动的应用程序进程信息,以便跟踪程序进程的运行状态。

  操作如下

  In [49]: p = psutil.Popen(["/usr/bin/python3", "-c" ,"print('hello')"],stdout=PIPE)In [45]: p.name()

  Out[45]: 'python'

  In [46]: p.username()

  Out[46]: 'root'

  In [47]: p.communicate()

  Out[47]: (b'hello\\n', None)

  In [50]: p.cpu_times()

  Out[50]: pcputimes(user=0.01, system=0.01, children_user=0.0, children_system=0.0)

 

 

来源:运维部落

原创粉丝点击