Python psutil 模塊python
psutil是一個跨平臺庫,能夠獲取系統的運行進程和系統利用的資源(CPU、內存、磁盤、網絡)等信息。他主要應用於系統監控,分析和限制系統資源及進程管理。他實現了同等工具提供的功能,如ps,top,lsof,netstat,ifconfig,who,df,kill,free,nice,ionice,iostat,iotop,uptime,pidof,tty,taskset,pmap等。支持32位與64位的linux,windos,os x,freeb sd,sun solaris 操做系統。linux
CPU 詳情ios
查看cpu全部信息api
>> psutil.cpu_times()
scputimes(user=11677.09, nice=57.93, system=148675.58, idle=2167147.79, iowait=260828.48, irq=7876.28, softirq=0.0, steal=3694.59, guest=0.0, guest_nice=0.0)
查看cpu全部邏輯信息緩存
>>> psutil.cpu_times(percpu=True)
[scputimes(user=11684.17, nice=57.93, system=148683.01, idle=2168982.08, iowait=260833.18, irq=7882.35, softirq=0.0, steal=3697.3, guest=0.0, guest_nice=0.0)]
查看用戶cpu時間比網絡
>>> psutil.cpu_times().user
11684.4
查看cpu邏輯個數socket
>>> psutil.cpu_count()
1
查看cpu物理個數ionic
>>> psutil.cpu_count()
1
示例:工具
# t1 cpu時間 cpu_user1 = psutil.cpu_times() # 間隔時間 time.sleep(1) # t2 cpu時間 cpu_user2 = psutil.cpu_times() # 用戶使用CPU時間 t1_user = cpu_user1.user t2_user = cpu_user2.user # CPUnice爲負值的進程佔用cpu時間 t1_nice = cpu_user1.nice t2_nice = cpu_user2.nice # 系統內核使用cpu時間 t1_system = cpu_user1.system t2_system = cpu_user2.system # 系統啓動到如今除了iowait外的等待CPU時間 t1_idle = cpu_user1.idle t2_idle = cpu_user2.idle # CPUio磁盤io等待時間 t1_iowait = cpu_user1.iowait t2_iowait = cpu_user2.iowait # 經過中斷硬件消耗的cpu時間 t1_irq = cpu_user1.softirq t2_irq = cpu_user2.softirq # 經過中斷軟件消耗的cpu時間 t1_steal = cpu_user1.steal t2_steal = cpu_user2.steal # 經過內核控制操做系統的虛擬環境的cpu使用時間 t1_guest = cpu_user1.guest t2_guest = cpu_user2.guest # 經過內核控制操做系統的虛擬環境CPUnice值爲負數的進程使用時間 t1_guest_nice = cpu_user1.guest_nice t2_guest_nice = cpu_user2.guest_nice # CPU總時間 t1_total = t1_user + t1_nice + t1_system + t1_idle + \ t1_iowait + t1_irq + t1_steal + t1_guest + t1_guest_nice t2_total = t2_user + t2_nice + t2_system + t2_idle + \ t2_iowait + t2_irq + t2_steal + t2_guest + t2_guest_nice # CPU非空閒時間 t1_busy = t1_total - t1_idle t2_busy = t2_total - t2_idle # t1-t2 總使用時間 cpu_total = t2_total - t1_total print(cpu_total) # t1-t2 idle 總非空閒時間 cpu_busy = t2_busy - t1_busy print(cpu_busy) # t1-t2 總使用率 cpu_usage_rate = (cpu_busy / cpu_total) * 100 print(cpu_usage_rate) # t1-t2 user 使用率 t1t2_user = t2_user - t1_user cpu_user = (t1t2_user / cpu_total) * 100 print(cpu_user) # t1-t2 nice 使用率 t1t2_nice = t2_nice - t1_nice cpu_nice = (t1t2_nice / cpu_total) * 100 print(cpu_nice) # t1-t2 system 使用率 t1t2_system = t2_system - t1_system cpu_system = (t1t2_system / cpu_total) * 100 print(cpu_system) # t1-t2 iowait 使用率 t1t2_iowait = t2_iowait - t1_iowait cpu_iowait = (t1t2_iowait / cpu_total) * 100 print(cpu_iowait) # t1-t2 irq 使用率 t1t2_irq = t2_irq - t1_irq cpu_irq = (t1t2_irq / cpu_total) * 100 print(cpu_irq) # t1-t2 steal 使用率 t1t2_steal = t2_steal - t1_steal cpu_steal = (t1t2_steal / cpu_total) * 100 print(cpu_steal) # t1-t2 guest 使用率 t1t2_guest = t2_guest - t1_guest cpu_guest = (t1t2_guest / cpu_total) * 100 print(cpu_guest) # t1-t2 guest_nice 使用率 t1t2_guest_nice = t2_guest_nice - t1_guest_nice cpu_guest_nice = (t1t2_guest_nice / cpu_total) * 100 print(cpu_guest_nice)
內存詳情ui
查看系統內存
>>> mem = psutil.virtual_memory() >>> mem #系統內存的全部信息 svmem(total=1040662528, available=175054848, percent=83.2, used=965718016, free=74944512, active=566755328, inactive=59457536, buffers=9342976, cached=90767360)
系統總計內存
>>> mem.total
1040662528
系統已經使用內存
>>> mem.used
965718016
示例:
# 全部內存參數 mem = psutil.virtual_memory() # 內存總大小 mem_total = str(mem.total/1024/1024) + " MB" print(mem_total) # 內存使用大小 mem_used = str(mem.used/1024/1024) + " MB" print(mem_used) # 內存真實剩餘大小 mem_free = str(mem.free/1024/1024) + " MB" print(mem_free) # 內存程序認爲可以使用大小包含緩存緩衝 mem_active = str(mem.active/1024/1024) + " MB" print(mem_active) # 未使用內存不包含緩存 mem_inactive = str(mem.inactive/1024/1024) + " MB" print(mem_inactive) # 緩衝區使用內存 mem_buffers = str(mem.buffers/1024/1024) + " MB" print(mem_buffers) # 未使用內存不包含緩存 mem_cached = str(mem.cached/1024/1024) + " MB" print(mem_cached)
SWAP詳情
獲取swap內存信息
>>> psutil.swap_memory()
sswap(total=0, used=0, free=0, percent=0, sin=0, sout=0)
示例:
# 交換分區信息 swap_mess = psutil.swap_memory() print(swap_mess) # 交換分區總大小 swap_total = swap_mess.total print("交換分區總大小:", swap_total) # 交換分區已使用大小 swap_used = swap_mess.used print("交換分區已使用大小:", swap_used) # 交換分區剩餘大小 swap_free = swap_mess.free print("交換分區剩餘大小:", swap_free) # 系統從磁盤累計換入的字節數 swap_sin = swap_mess.sin print("系統從磁盤累換入的字節數:", swap_sin) # 系統從磁盤累計換出的字節數 swap_sout = swap_mess.sout print("交換分區總大小:", swap_sout)
磁盤詳情
讀取盤參數
磁盤利用率使用psutil.disk_usage方法獲取,
磁盤IO信息包括read_count(讀IO數),write_count(寫IO數)
read_bytes(IO寫字節數),read_time(磁盤讀時間),write_time(磁盤寫時間),這些IO信息用
psutil.disk_io_counters()
獲取磁盤的完整信息
psutil.disk_partitions()
獲取分區表的參數
psutil.disk_usage('/') #獲取/分區的狀態
獲取硬盤IO總個數
psutil.disk_io_counters()
獲取單個分區IO個數
psutil.disk_io_counters(perdisk=True) #perdisk=True參數獲取單個分區IO個數
讀取網絡信息
網絡信息與磁盤IO信息相似,涉及到幾個關鍵點,包括byes_sent(發送字節數),byte_recv=xxx(接受字節數),
pack-ets_sent=xxx(發送字節數),pack-ets_recv=xxx(接收數據包數),這些網絡信息用
獲取網絡總IO信息
psutil.net_io_counters()
磁盤容量示例:
# 輸出當前分區掛載詳情 disk_partitions = psutil.disk_partitions() # 循環列表 for i in disk_partitions: # 分區設備名稱 print(i[0])
# 分區掛載點 print(i[1])
# 分區格式類型 print(i[2])
# 分區所屬權限 print(i[3]) # 指定掛載分區大小 disk_usage = psutil.disk_usage('/') # 掛載磁盤總大小 disk_total = str(disk_usage.total/1024/1024/1024) + " GB" print(disk_total) # 掛載磁盤使用空間 disk_used = str(disk_usage.used/1024/1024/1024) + " GB" print(disk_used) # 掛載磁盤剩餘空間 disk_free = str(disk_usage.free/1024/1024/1024) + " GB" print(disk_free)
磁盤IO示例:
# 磁盤io信息 diskio = psutil.disk_io_counters() print(diskio)
# 系統每塊物理磁盤io的返回信息 diskio2 = psutil.disk_io_counters(perdisk=True) print(diskio2) # 磁盤io讀取次數 ioread_count = diskio.read_count print(ioread_count) # 磁盤io寫入次數 iowrite_count = diskio.write_count print(iowrite_count) # 讀取全部io數量 ioread_bytes = diskio.read_bytes print(ioread_bytes) # 寫入全部io數量 iowrite_bytes = diskio.write_bytes print(iowrite_bytes) # 讀取io時間 ioread_time = diskio.read_time print(ioread_time) # 寫入io時間 iowrite_time = diskio.write_time print(iowrite_time) # 合併讀取次數 ioread_merged_count = diskio.read_count print(ioread_merged_count) # 合併寫入次數 iowrite_merged_count = diskio.write_count print(iowrite_merged_count) # 實際作I/O操做時間 iobusy_time = diskio.busy_time print(iobusy_time)# 兩個時間段的磁盤信息 t1_io = psutil.disk_io_counters() time.sleep(1) t2_io = psutil.disk_io_counters() # 求出每秒磁盤IO的讀寫輸出 bytes 爲單位 read_bytes = str(t2_io.read_bytes - t1_io.read_bytes) + " bytes" write_bytes = str(t2_io.write_bytes - t1_io.write_bytes) + " bytes" # 求出讀寫IO的 tps TransactionsPerSecond的縮寫,也就是事務數/秒 # tps:讀寫IO處理的一次響應事務 tps = str(t2_io.read_count + t2_io.write_count - t1_io.read_count - t1_io.write_count) + " tps"
網絡信息
輸出網絡每一個接口信息
psutil.net_io_counters(pernic=True) #pernic=True
查看網卡
psutil.net_if_addrs().keys()
for i in psutil.net_if_addrs().keys(): network = networkapi[i] print("網卡IP地址:", network[0][1]) print("子網地址:", network[0][2]) print("網卡MAC地址:", network[1][1])
示例:
# 獲取網絡信息 ionetwork = psutil.net_io_counters()# 發送總字節數 iobytes_sent = ionetwork.bytes_sent # 接收總字節數 iobytes_recv = ionetwork.bytes_recv # 發送總包個數 netpackets_sent = ionetwork.packets_sent # 接收總包個數 netpackets_recv = ionetwork.packets_recv # 接收錯誤數 errin = ionetwork.errin # 發送錯誤數 errout = ionetwork.errout # 求其傳入數據包數量 dropin = ionetwork.dropin # 丟棄傳輸數據包數量 dropout = ionetwork.dropout# 求出每秒的網絡信息 t1_net = psutil.net_io_counters() time.sleep(1) t2_net = psutil.net_io_counters()# 求出每秒發送網卡流量 bytes_sent = t2_net.bytes_sent - t1_net.bytes_sent bytes_sent = str(int(bytes_sent)/1024) + " KB" print(bytes_sent) # 求出每秒接收網卡流量 bytes_recv = t2_net.bytes_recv - t1_net.bytes_recv bytes_recv = str(int(bytes_recv)/1024) + " KB" print(bytes_recv) # 求出每秒發送包的數量 packets_sent = t2_net.packets_sent - t1_net.packets_sent print(packets_sent) # 求出每秒接收報的數量 packets_recv = t2_net.packets_recv - t1_net.packets_recv print(packets_recv)
系統信息
獲取當前系統用戶登陸信息
psutil.users()
# 獲取當前用戶信息 user = psutil.users() for i in user: if i.terminal == a: # 獲取用戶名 username = i.name print("用戶名:", username) # 虛擬終端 terminal = i.terminal print("虛擬終端:", terminal) # 鏈接地址 host = i.host print("鏈接地址:", host) # 以使用時間 started = i.started print("使用時間:", started) # 用戶pid userpid = i.pid
獲取開機時間
# 以linux時間格式返回 psutil.boot_time()
# 轉換成天然時間格式 datetime.datetime.fromtimestamp(psutil.boot_time ()).strftime("%Y-%m-%d %H: %M: %S")
進程信息
系統進程管理
獲取當前系統的進程信息,獲取當前程序的運行狀態,包括進程的啓動時間,查看設置CPU親和度,內存使用率,IO信息
socket鏈接,線程數等
獲取進程信息
查看系統所有進程
psutil.pids()
查看單個進程
p = psutil.Process(2423) p.name() #進程名 p.exe() #進程的bin路徑 p.cwd() #進程的工做目錄絕對路徑 p.status() #進程狀態 p.create_time() #進程建立時間 p.uids() #進程uid信息 p.gids() #進程的gid信息 p.cpu_times() #進程的cpu時間信息,包括user,system兩個cpu信息 p.cpu_affinity() #get進程cpu親和度,若是要設置cpu親和度,將cpu號做爲參考就好 p.memory_percent() #進程內存利用率 p.memory_info() #進程內存rss,vms信息 p.io_counters() #進程的IO信息,包括讀寫IO數字及參數 p.connectios() #返回進程列表 p.num_threads() #進程開啓的線程數
p.cpu_percent(interval=1) #查看多少秒的進程cpu佔用 interval後面跟秒
# 聽過psutil的Popen方法啓動應用程序,能夠跟蹤程序的相關信息 from subprocess import PIPE p = psutil.Popen(["/usr/bin/python", "-c", "print('hello')"],stdout=PIPE) p.name() p.username()