python psutil模組用法樣本

來源:互聯網
上載者:User

標籤:

參考:http://www.jbxue.com/python/29871.htm

 

1,擷取系統效能資訊

1)cpu資訊

返回內容中關鍵字的列表中項的意義(自己的理解,詳細解釋參考此文章):

  • user  使用者態使用的cpu時間
  • system 系統態使用的cpu時間
  • idle 閒置cpu時間
  • nice (UNIX) 用做nice(進程的優先順序修正值)加權的進程指派的使用者態cpu時間
  • iowait (Linux) cpu等待磁碟寫入完成時間
  • irq (Linux, FreeBSD) 硬中斷消耗時間
  • softirq (Linux) 非強制中斷消耗時間
  • steal (Linux 2.6.11+) 虛擬機器偷取時間
  • guest (Linux 2.6.24+) 訪客狀態下使用的cpu時間
  • guest_nice (Linux 3.2.0+) 訪客狀態下做nice加權的進程指派的使用者態cpu時間

 

>>> print u"CPU 個數 %s"%psutil.cpu_count() 
CPU 個數 1
>>> print u"物理CPU個數 %s"%psutil.cpu_count(logical=False) 
物理CPU個數 1

>>> psutil.cpu_count(logical=True)1>>> psutil.cpu_times()scputimes(user=541.82, nice=1.32, system=781.5, idle=11678.47, iowait=42.81, irq=3.14, softirq=3.56, steal=0.0, guest=0.0)>>> psutil.cpu_percent()1.1>>> psutil.cpu_times_percent()scputimes(user=0.5, nice=0.0, system=0.5, idle=98.6, iowait=0.2, irq=0.0, softirq=0.0, steal=0.0, guest=0.0)

2)記憶體資訊

total, 記憶體總量

used, 已使用的記憶體數

free, 空閑記憶體數

buffers, 緩衝區使用數

swap, 交換分區使用數

>>> psutil.swap_memory()sswap(total=3221221376L, used=0L, free=3221221376L, percent=0.0, sin=0, sout=0)>>> psutil.virtual_memory()svmem(total=1036587008L, available=697393152L, percent=32.7, used=877322240L, free=159264768L, active=243052544, inactive=459927552, buffers=44429312L, cached=493699072)

3)磁碟資訊

磁碟利用率及IO資訊
read_count,讀IO數
write_count,寫IO數
read_bytes,讀IO位元組數
write_count,寫IO位元組數
read_time,磁碟讀時間
write_time,磁碟寫時間

>>> psutil.disk_partitions()[sdiskpart(device=‘/dev/sda3‘, mountpoint=‘/‘, fstype=‘ext4‘, opts=‘rw‘), sdiskpart(device=‘/dev/sda1‘, mountpoint=‘/boot‘, fstype=‘ext4‘, opts=‘rw‘)]>>> psutil.disk_usage(‘/‘)sdiskusage(total=28090511360, used=3418284032, free=23238459392, percent=12.2)>>> psutil.disk_usage(‘/boot‘)sdiskusage(total=296236032, used=33723392, free=246784000, percent=11.4)>>> psutil.disk_io_counters(‘perdisk=False/True‘){‘sda2‘: sdiskio(read_count=328, write_count=0, read_bytes=1470464, write_bytes=0, read_time=281, write_time=0), ‘sda3‘: sdiskio(read_count=12427, write_count=8975, read_bytes=392832000, write_bytes=192872448, read_time=95939, write_time=102753), ‘sda1‘: sdiskio(read_count=500, write_count=3, read_bytes=2026496, write_bytes=9216, read_time=237, write_time=2)}

 

4)網路資訊

bytes_sent,發送位元組數
packets_sent,接收位元組數
packets_sent,發送資料包數
packets_sent,接收資料包數

>>> psutil.net_io_counters(pernic=False/True)snetio(bytes_sent=64530, bytes_recv=817240, packets_sent=887, packets_recv=2600, errin=0, errout=0, dropin=0, dropout=0)

5)其他系統資訊

使用者登入、開機時間

>>> psutil.users()[suser(name=‘wst‘, terminal=‘tty1‘, host=‘localhost‘, started=1436421376.0), suser(name=‘wst‘, terminal=‘pts/0‘, host=‘localhost‘, started=1436427392.0), suser(name=‘wst‘, terminal=‘pts/1‘, host=‘localhost‘, started=1436429696.0)]>>> psutil.boot_time()1436421348.0

2,系統進程管理方法

1)進程資訊

2)popen類的使用

import psutilfrom subprocess import PIPEp = pstuil.Popen([‘/etc/init.d/mysqld’,’-c’,’start’],stdout=PIPE)

通過psutil的Popen方法啟動的應用程式,可以跟蹤該程式啟動並執行所有資訊。

python psutil模組用法樣本

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.