標籤:
參考: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模組用法樣本