Python學習筆記-系統性能信息模塊psutil

系統性能信息模塊 psutil:

    參考:https://github.com/giampaolo/psutil
    安裝psutil模塊:

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

1、獲取系統性能信息:
    1.1、獲取CPU信息:

import psutil

        獲取CPU完整信息:

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

        獲取單項數據信息(用戶user的CPU時間比):

>>> psutil.cpu_times().user 60985.209999999999

       獲取CPU的邏輯個數,默認logical=True:

>>> psutil.cpu_count()  1

       獲取CPU的物理個數:

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

    1.2、獲取內存信息:

In [1]: import psutil

          獲取內存完整信息

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


        獲取內存總數:

In [4]: mem.total Out[4]: 1044832256


        獲取空閒內存數:

In [5]: mem.free Out[5]: 69898240


        獲取SWAP分區信息:

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

    1.3、獲取磁盤信息:
        獲取磁盤完整信息:

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


        獲取分區(參數)的使用方法

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


        獲取硬盤總的IO個數、讀寫信息

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


        獲取單個分區IO的個數、讀寫信息

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


    1.4、獲取網絡信息:
        獲取網絡總的IO信息,默認pernic=False:

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


        獲取pernic=True輸出每個網絡接口的IO信息:

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

    1.5、獲取其他系統信息:
        獲取當前登錄系統的用戶:

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時間戳格式返回:

In [14]: import psutil,datetime In [15]: psutil.boot_time() Out[15]: 1489823731.0


        獲取開機時間,轉換成自然時間格式返回:

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

In [10]: psutil.pids() Out[10]:  [1, 2, ...... 32381, 32407]


        實例化一個Process對象,參數爲一進程PID

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


        獲取進程名:

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


        獲取bin路徑:

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


        獲取進程工作路徑絕對路徑:

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


        獲取進程狀態:

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


        獲取進程創建時間,時間戳格式:

In [16]: p.create_time() Out[16]: 1492649546.17


        獲取uid信息:

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


        獲取gid信息:

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


        獲取獲取進程CPU親和度,如要設置進程CPU親和度,將CPU號作爲參數即可

In [19]: p.cpu_affinity() Out[19]: [0]


        獲取CPU時間信息,包括user、system兩個CPU時間

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


        獲取內存利用率

In [21]: p.memory_percent() Out[21]: 1.3066181601499103


        獲取進程內存rss、vms信息

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


        獲取IO信息,包括讀寫IO數及字節數

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


        獲取打開進程socket的namedutples列表,包括fs、family、laddr等信息

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


    獲取進程開啓的線程數

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


    2.2、popen類的使用
        psutil提供的popen類的作用:獲取用戶啓動的應用程序進程信息,以便跟蹤程序進程的運行狀態。

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


        通過psutil的Popen方法啓動的應用程序,可以跟蹤該程序運行的所有相關信息。

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時間


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章