Python学习笔记-系统性能信息模块psutil

系统性能信息模块 psutil:

    参考:https://github.com/giampaolo/psutil
    安装psutil模块:

 
 
1
[ root@ kurol  ~ ]python36  - m  easy_install  - i  http: // pypi. douban. com / simple /  psutil  


1、获取系统性能信息:
    1.1、获取CPU信息:


  
  
1
import  psutil  


        获取CPU完整信息:


  
  
1
2
>>>  psutil. cpu_times ( )
  scputimes ( user = 60984.989999999998nice = 27.280000000000001system = 37572.639999999999idle = 6605536.1100000003iowait = 88463.169999999998irq = 0.53000000000000003softirq = 151.34
    ,  steal = 0.0guest = 0.0 )   

        获取单项数据信息(用户user的CPU时间比):


  
  
1
2
>>>  psutil. cpu_times ( ). user
  60985.209999999999  


        获取CPU的逻辑个数,默认logical=True:


  
  
1
2
>>>  psutil. cpu_count ( ) 
  1  


        获取CPU的物理个数:


  
  
1
2
>>>  psutil. cpu_count ( logical = False )
  1   

    1.2、获取内存信息:


  
  
1
In  [ 1 ]import  psutil  


           获取内存完整信息:


  
  
1
2
3
In  [ 2 ]mem  =  psutil. virtual_memory ( )
  In  [ 3 ]mem
  Out [ 3 ]svmem ( total = 1044832256available = 357302272percent = 65.8used = 554692608free = 69898240active = 732987392inactive = 152940544buffers = 121593856cached = 298647552shared
     = 278528 )  


        获取内存总数:


  
  
1
2
In  [ 4 ]mem. total
  Out [ 4 ]1044832256  


        获取空闲内存数:


  
  
1
2
In  [ 5 ]mem. free
  Out [ 5 ]69898240  


        获取SWAP分区信息:


  
  
1
2
In  [ 6 ]psutil. swap_memory ( )
  Out [ 6 ]sswap ( total = 0used = 0free = 0percent = 0sin = 0sout = 0 )  

    1.3、获取磁盘信息:
        获取磁盘完整信息:


  
  
1
2
In  [ 7 ]psutil. disk_partitions ( )
  Out [ 7 ][ sdiskpart ( device = '/dev/vda1'mountpoint = '/'fstype = 'ext3'opts = 'rw,noatime,acl,user_xattr' )]  


        获取分区(参数)的使用方法


  
  
1
2
In  [ 8 ]psutil. disk_usage ( '/' )
  Out [ 8 ]sdiskusage ( total = 21136797696used = 3255058432free = 16808058880percent = 16.2 )  


        获取硬盘总的IO个数、读写信息


  
  
1
2
In  [ 9 ]psutil. disk_io_counters ( )
  Out [ 9 ]sdiskio ( read_count = 3147178write_count = 9242170read_bytes = 88165640192write_bytes = 293065478144read_time = 38673564write_time = 428113153read_merged_count = 41555
    ,  write_merged_count = 62306169busy_time = 66422434 )  


        获取单个分区IO的个数、读写信息


  
  
1
2
In  [ 10 ]psutil. disk_io_counters ( perdisk = True )
  Out [ 10 ]{ 'vda1'sdiskio ( read_count = 3147178write_count = 9242205read_bytes = 88165640192write_bytes = 293065760768read_time = 38673564write_time = 428113482read_merged_count
     = 41555write_merged_count = 62306203busy_time = 66422585 )}  


    1.4、获取网络信息:
        获取网络总的IO信息,默认pernic=False:


  
  
1
2
In  [ 11 ]psutil. net_io_counters ( )
  Out [ 11 ]snetio ( bytes_sent = 7783736637bytes_recv = 3332052537packets_sent = 17192882packets_recv = 26834127errin = 0errout = 0dropin = 0dropout = 0 )  


        获取pernic=True输出每个网络接口的IO信息:


  
  
1
2
In  [ 12 ]psutil. net_io_counters ( pernic = False )
  Out [ 12 ]snetio ( bytes_sent = 7783790463bytes_recv = 3332100618packets_sent = 17193280packets_recv = 26834627errin = 0errout = 0dropin = 0dropout = 0 )  

    1.5、获取其他系统信息:
        获取当前登录系统的用户:


  
  
1
2
3
4
5
6
7
In  [ 13 ]psutil. users ( )
  Out [ 13 ]
  [ suser ( name = 'root'terminal = 'tty1'host = ''started = 1492050176.0 ),
  suser ( name = 'root'terminal = 'pts/3'host = '127.0.0.1'started = 1496363136.0 ),
  suser ( name = 'root'terminal = 'pts/5'host = '127.0.0.1'started = 1496386560.0 ),
  suser ( name = 'root'terminal = 'pts/6'host = '183.19.153.161'started = 1496634240.0 ),
  suser ( name = 'root'terminal = 'pts/7'host = '183.19.153.161'started = 1496645632.0 )]  


        获取开机时间,以Linux时间戳格式返回:


  
  
1
2
3
In  [ 14 ]import  psutil, datetime
  In  [ 15 ]psutil. boot_time ( )
  Out [ 15 ]1489823731.0  


        获取开机时间,转换成自然时间格式返回:


  
  
1
2
In  [ 19 ]datetime. datetime. fromtimestamp ( psutil. boot_time ( )). strftime ( "%Y-%m-%d %H:%M:%S" )
  Out [ 19 ]'2017-03-18 15:55:31'  

2、系统进程管理方法:
    psutil模块在获取进程信息方面也停工了很好的支持。

    2.1、获取进程信息:
        列出所有进程PID


  
  
1
2
3
4
5
6
7
In  [ 10 ]psutil. pids ( )
  Out [ 10 ]
  [ 1,
  2,
 ......
  32381,
  32407 ]  


        实例化一个Process对象,参数为一进程PID


  
  
1
In  [ 11 ]p  =  psutil. Process ( 8843 )  


        获取进程名:


  
  
1
2
In  [ 12 ]p. name ( )
  Out [ 12 ]'httpd'  


        获取bin路径:


  
  
1
2
In  [ 13 ]p. exe ( )
  Out [ 13 ]'/usr/sbin/httpd'  


        获取进程工作路径绝对路径:


  
  
1
2
In  [ 14 ]p. cwd ( )
  Out [ 14 ]'/'  


        获取进程状态:


  
  
1
2
In  [ 15 ]p. status ( )
  Out [ 15 ]'sleeping'  


        获取进程创建时间,时间戳格式:


  
  
1
2
In  [ 16 ]p. create_time ( )
  Out [ 16 ]1492649546.17  


        获取uid信息:


  
  
1
2
In  [ 17 ]p. uids ( )
  Out [ 17 ]puids ( real = 0effective = 0saved = 0 )  


        获取gid信息:


  
  
1
2
In  [ 18 ]p. gids ( )
  Out [ 18 ]pgids ( real = 0effective = 0saved = 0 )  


        获取获取进程CPU亲和度,如要设置进程CPU亲和度,将CPU号作为参数即可


  
  
1
2
In  [ 19 ]p. cpu_affinity ( )
  Out [ 19 ][ 0 ]  


        获取CPU时间信息,包括user、system两个CPU时间


  
  
1
2
In  [ 20 ]p. cpu_times ( )
  Out [ 20 ]pcputimes ( user = 34.49system = 83.82children_user = 14932.68children_system = 1664.72 )  


        获取内存利用率


  
  
1
2
In  [ 21 ]p. memory_percent ( )
  Out [ 21 ]1.3066181601499103  


        获取进程内存rss、vms信息


  
  
1
2
In  [ 22 ]p. memory_info ( )
  Out [ 22 ]pmem ( rss = 13651968vms = 309997568shared = 7004160text = 344064lib = 0data = 5300224dirty = 0 )  


        获取IO信息,包括读写IO数及字节数


  
  
1
2
In  [ 23 ]p. io_counters ( )
  Out [ 23 ]pio ( read_count = 829019264write_count = 1492003read_bytes = 2957004800write_bytes = 4966547456read_chars = 6639699389927write_chars = 6973819808 )  


        获取打开进程socket的namedutples列表,包括fs、family、laddr等信息


  
  
1
2
In  [ 24 ]p. connections ( )
  Out [ 24 ][ pconn ( fd = 3family =< AddressFamily. AF_INET2 >type =< SocketKind. SOCK_STREAM1 >laddr = ( '0.0.0.0'80 )raddr = ( )status = 'LISTEN' )]  


    获取进程开启的线程数


  
  
1
2
In  [ 26 ]p. num_threads ( )
  Out [ 26 ]1  


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


  
  
1
2
In  [ 1 ]import  psutil
  In  [ 2 ]from  subprocess  import  PIPE  


        通过psutil的Popen方法启动的应用程序,可以跟踪该程序运行的所有相关信息。


  
  
1
2
3
4
5
6
7
8
In  [ 3 ]p  =  psutil. Popen ([ "/usr/bin/python3", "-c", "print('hello')" ], stdout = PIPE )
  In  [ 4 ]p. name ( )
  Out [ 4 ]'python3'
  In  [ 5 ]p. username ( )
  Out [ 5 ]'root'
  In  [ 6 ]p. communicate ( )
  Out [ 6 ]( b 'hello \n 'None )
  In  [ 7 ]p. cpu_time ( )  #CPU  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值