天天看點

擷取Linux系統性能資訊(Python)

1)CPU資訊

[root@localhost ~]# python

Python 2.7.5 (default, Jul 13 2018, 13:06:57)

[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>> import psutil

#使用cpu_times方法擷取CPU完整資訊

>>> psutil.cpu_times()

scputimes(user=14571.83, nice=10.13, system=10459.3, idle=9390763.99, iowait=716.03, irq=0.0, softirq=3.8, steal=0.0, guest=0.0, guest_nice=0.0)

#擷取單項資料資訊,如使用者user的時間比

>>> psutil.cpu_times().user

14571.87

#擷取CPU的邏輯個數,預設logical=True4

>>> psutil.cpu_count()

1

#擷取CPU的實體個數

>>> psutil.cpu_count(logical=False)

1

擷取Linux系統性能資訊(Python)

2)記憶體資訊

>>> import psutil

#使用psutil.virtual_memory方法擷取記憶體完整資訊

>>> mem = psutil.virtual_memory()

>>> mem

svmem(total=1928695808L, available=1699635200L, percent=11.9, used=1617731584L, free=310964224L, active=733216768, inactive=700944384, buffers=194965504L, cached=1193705472)

#擷取記憶體總數

>>> mem.total

1928695808L

#擷取空閑記憶體數

>>> mem.free

310964224L

#擷取swap分區資訊

>>> psutil.swap_memory()

sswap(total=0L, used=0L, free=0L, percent=0.0, sin=0, sout=0)

擷取Linux系統性能資訊(Python)

3)磁盤資訊

#使用psutil.disk_partitions方法擷取磁盤完整資訊

>>> psutil.disk_partitions()

[sdiskpart(device='/dev/vda1', mountpoint='/', fstype='ext4', opts='rw,relatime,data=ordered')]

#使用psutil.disk_usage方法擷取分區(參數)的使用情況

>>> psutil.disk_usage('/')

sdiskusage(total=42139451392, used=3013500928, free=36961796096, percent=7.2)

#使用psutil.disk_io_counters方法擷取磁盤總的IO個數、讀寫資訊

>>> psutil.disk_io_counters()

sdiskio(read_count=26141, write_count=1716089, read_bytes=474932224, write_bytes=13267562496, read_time=109538, write_time=15169214)

#"perdisk=True"參數擷取單個分區IO個數、讀寫資訊

>>> psutil.disk_io_counters(perdisk=True)

{'vda1': sdiskio(read_count=26141, write_count=1716089, read_bytes=474932224, write_bytes=13267562496, read_time=109538, write_time=15169214)}

擷取Linux系統性能資訊(Python)

4)網絡資訊

#使用psutil.net_io_counters擷取網絡總的IO資訊,預設pernic=False

>>> psutil.net_io_counters()

snetio(bytes_sent=287203066, bytes_recv=887595622, packets_sent=2422790, packets_recv=2704748, errin=0, errout=0, dropin=0, dropout=0)

#pernic=True輸出每個網絡接口的IO資訊

>>> psutil.net_io_counters(pernic=True)

{'lo': snetio(bytes_sent=502615, bytes_recv=502615, packets_sent=2200, packets_recv=2200, errin=0, errout=0, dropin=0, dropout=0), 'eth0': snetio(bytes_sent=286703163, bytes_recv=887096509, packets_sent=2420612, packets_recv=2702590, errin=0, errout=0, dropin=0, dropout=0)}

>>>

擷取Linux系統性能資訊(Python)

5)其他系統資訊

#使用psutil.users方法傳回目前登入系統的使用者資訊

>>> psutil.users()

[suser(name='root', terminal='tty1', host='', started=1536040960.0), suser(name='root', terminal='pts/1', host='14.155.220.101', started=1542177536.0), suser(name='root', terminal='pts/2', host='14.155.220.101', started=1542181760.0)]

#轉換成自然時間格式

>>> import psutil, datetime

>>> datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S")

'2018-07-28 12:50:26'

#使用psutil.boot_time方法擷取開機時間,以Linux時間戳格式傳回

>>> psutil.boot_time()

1532753426.0

擷取Linux系統性能資訊(Python)