一、CPU信息
>>> import psutil #導入psutil模塊
>>> psutil.cpu_times() #查看CPU的完整信息
scputimes(user=262.23, nice=0.27, system=170.81, idle=153135.17, iowait=50.04, irq=0.0, softirq=0.08, steal=0.0, guest=0.0, guest_nice=0.0)
>>> psutil.cpu_times(percpu=False) # 查看CPU完整信息的另一種方式
scputimes(user=262.24, nice=0.27, system=170.83, idle=153151.74, iowait=50.04, irq=0.0, softirq=0.08, steal=0.0, guest=0.0, guest_nice=0.0)
>>> psutil.cpu_times().user # 查看單類信息,例如用戶佔用的CPU時間比
262.3
>>> psutil.cpu_count() # 查看CPU的邏輯個數
1
>>> psutil.cpu_count(logical=False) # 查看CPU的物理個數
1
>>>
二、內存信息
total(內存總數)
used(已使用的內存數)
free(空閒內存數)
buffers(緩衝使用數)
cache(緩存使用數)
swap(交換分區使用數)
>>> import psutil # 導入psutil模塊
>>> psutil.virtual_memory() # 查看內存整體信息
svmem(total=1040551936, available=701288448, percent=32.6, used=180355072, free=452206592, active=275603456, inactive=224522240, buffers=60026880, cached=347963392, shared=352256, slab=65908736)
>>> jier = psutil.virtual_memory() # 將內存信息賦值給一個變量
>>> jier.total # 查看內存總數是多少
1040551936
>>> jier.free # 查看內存空餘數
452206592
>>>
三、磁盤信息
read_count(讀IO數)
write_count(寫IO數)
read_bytes(IO讀字節數)
write_bytes(IO寫字節數)
read_time(磁盤讀時間)
write_time(磁盤寫時間)
>>> import psutil # 導入模塊
>>> psutil.disk_partitions() # 查看完整磁盤信息
[sdiskpart(device='/dev/vda1', mountpoint='/', fstype='ext4', opts='rw,relatime,data=ordered')]
>>> psutil.disk_usage('/') # 查看根分區的磁盤使用信息
sdiskusage(total=42139451392, used=3381141504, free=36594155520, percent=8.5)
>>> psutil.disk_usage('/run/') # 查看run分區的磁盤使用信息
sdiskusage(total=520273920, used=352256, free=519921664, percent=0.1)
>>> psutil.disk_io_counters() # 查看磁盤的IO讀寫情況
sdiskio(read_count=16014, write_count=37632, read_bytes=329921536, write_bytes=278237184, read_time=146130, write_time=148078, read_merged_count=13, write_merged_count=16619, busy_time=55554)
>>> psutil.disk_io_counters(perdisk=True) # 獲取單個分區的IO個數和讀寫信息
{'vda': sdiskio(read_count=16014, write_count=37632, read_bytes=329921536, write_bytes=278237184, read_time=146130, write_time=148078, read_merged_count=13, write_merged_count=16619, busy_time=55554), 'vda1': sdiskio(read_count=15930, write_count=36897, read_bytes=327808000, write_bytes=278237184, read_time=146088, write_time=147968, read_merged_count=13, write_merged_count=16619, busy_time=55428)}
>>>
四、網絡信息
bytes_sent(發送字節數)
bytes_recv=28220119(接收字節數)
packets_sent=200978(發送數據包數)
packets_recv=212672(接收數據包數)
>>> import psutil # 導入模塊
>>> psutil.net_io_counters() # 查看整體網絡IO信息流量
snetio(bytes_sent=9978681, bytes_recv=5290561, packets_sent=37611, packets_recv=44983, errin=0, errout=0, dropin=0, dropout=0)
>>> psutil.net_io_counters(pernic=True) # 查看單獨接口IO信息流量
{'lo': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'eth0': snetio(bytes_sent=9982647, bytes_recv=5294811, packets_sent=37641, packets_recv=45036, errin=0, errout=0, dropin=0, dropout=0)}
>>>
五、其它系統信息
>>> import psutil # 導入模塊
>>> import datetime # 導入時間模塊
>>> psutil.users() # 獲取登陸用戶信息
[suser(name='root', terminal='pts/0', host='124.205.208.206', started=1534906496.0, pid=22583), suser(name='root', terminal='pts/1', host='124.205.208.206', started=1534907136.0, pid=22739)]
>>> psutil.boot_time() # 獲取系統啓動時間信息
1534755312.0
>>> datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %G:%M:%S") # 系統啓動時間信息柵格化
'2018-08-20 2018:55:12'
>>>