import psutil
#1、系統效能資訊模組psutil
mem = psutil.virtual_memory()
print(mem)
#svmem(total=8442675200, available=1900957696, percent=77.5, used=6541717504, free=1900957696)
#比如百分比顯示的資訊和我的360上使用記憶體百分比一致的,單位是位元組 B
print(mem.total,mem.percent)
#8442675200 77.6
#1.1獲取系統效能資訊
#顯示所有邏輯cpu資訊
print(psutil.cpu_times(percpu=True))
#[scputimes(user=11973.46875, system=53070.921875, idle=71753.046875, interrupt=614.84375, dpc=556.765625), scputimes(user=7833.890625, system=14614.062499999985, idle=114349.15625, interrupt=200.015625, dpc=20.765625), scputimes(user=13077.875, system=64398.703125, idle=59320.53125, interrupt=261.5, dpc=16.203125), scputimes(user=10840.437499999998, system=21394.171875, idle=104562.49999999999, interrupt=193.5625, dpc=13.859375)]
#獲取cpu資訊,好像是總體的
print(psutil.cpu_times())
#scputimes(user=43860.14062499999, system=153661.00000000006, idle=350629.49999999994, interrupt=1274.59375, dpc=611.21875)
#獲取單項資料資訊,如使用者user的cpu時間比
print(psutil.cpu_times().user)
#43989.67187499999
#獲取cpu邏輯個數,預設logical=True4
print(psutil.cpu_count())
#4
#獲取cpu物理個數
print(psutil.cpu_count(logical=True))
#===
#獲取記憶體完整資訊
mem=psutil.virtual_memory()
print(mem)
#svmem(total=8442675200, available=2253422592, percent=73.3, used=6189252608, free=2253422592)
#獲取記憶體總數
print(mem.total)
#8442675200
#獲取空閒記憶體數
print(mem.free)
#2252984320
#獲取swap分割槽資訊
print(psutil.swap_memory())
#sswap(total=18643222528, used=13967691776, free=4675530752, percent=74.9, sin=0, sout=0)
#======
#獲取磁碟完整資訊
print(psutil.disk_partitions())
#[sdiskpart(device='C:\\', mountpoint='C:\\', fstype='NTFS', opts='rw,fixed', maxfile=255, maxpath=260), sdiskpart(device='D:\\', mountpoint='D:\\', fstype='NTFS', opts='rw,fixed', maxfile=255, maxpath=260), sdiskpart(device='E:\\', mountpoint='E:\\', fstype='NTFS', opts='rw,fixed', maxfile=255, maxpath=260)]
print(type(psutil.disk_partitions()))#<class 'list'>
#獲取分割槽的使用情況(單個磁碟):
print(psutil.disk_usage('c:\\'))
#sdiskusage(total=129176965120, used=97699663872, free=31477301248, percent=75.6)
#獲取磁碟總的io個數,讀寫資訊
print(psutil.disk_io_counters())
#sdiskio(read_count=909190, write_count=1472997, read_bytes=63249928704, write_bytes=40258563072, read_time=21921, write_time=8639)
#獲取單個分割槽的io個數,讀寫資訊
print(psutil.disk_io_counters(perdisk=True))
#{'PhysicalDrive0': sdiskio(read_count=909444, write_count=1474143, read_bytes=63257588224, write_bytes=40283741184, read_time=21923, write_time=8640)}
#======
#獲取網路總的io資訊,預設perinic=False
print(psutil.net_io_counters())
#snetio(bytes_sent=1840042559, bytes_recv=1945703253, packets_sent=3872624, packets_recv=5073757, errin=70, errout=0, dropin=70, dropout=0)
#輸出每個網路介面的IO資訊
print(psutil.net_io_counters(pernic=True))
#字典{'乙太網 3': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), '乙太網 5': snetio(bytes_sent=640579926, bytes_recv=184115712, packets_sent=958984, packets_recv=600803, errin=0, errout=0, dropin=0, dropout=0), '本地連線* 11': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), '本地連線* 12': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), '乙太網 2': snetio(bytes_sent=1200771752, bytes_recv=1603325755, packets_sent=2831223, packets_recv=4373762, errin=70, errout=0, dropin=70, dropout=0), 'VMware Network Adapter VMnet1': snetio(bytes_sent=39751, bytes_recv=310, packets_sent=39751, packets_recv=310, errin=0, errout=0, dropin=0, dropout=0), 'VMware Network Adapter VMnet8': snetio(bytes_sent=39397, bytes_recv=0, packets_sent=39398, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), '藍芽網路連線': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'WLAN': snetio(bytes_sent=1289787, bytes_recv=159911181, packets_sent=15921, packets_recv=112562, errin=0, errout=0, dropin=0, dropout=0), 'Loopback Pseudo-Interface 1': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0)}
#=======
#返回當前登入系統的使用者資訊
print(type(psutil.users()),psutil.users())
#<class 'list'> [suser(name='Administrator', terminal=None, host=None, started=1636939012.6702487, pid=None)]
#獲取開機時間,時間戳格式返回
print(psutil.boot_time())
#1636938961.3155193
#轉換成自然格式的開機時間
import datetime
print(datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S"))
#2021-11-15 09:16:01
#=====
#列出所有程式PID
print(psutil.pids())
#[0, 4, 72, 100, 368, 488, 520, 688, 696, 804, 808, 820, 880, 888, 1012, 1028, 1108, 1148, 1160, 1236, 1292, 1300, 1364, 1372, 1400, 1468, 1480, 1508, 1548, 1568, 1592, 1604, 1632, 1644, 1660, 1668, 1676, 1684, 1764, 1924, 1932, 1956, 2008, 2052, 2076, 2136, 2148, 2164, 2208, 2212, 2252, 2268, 2308, 2324, 2376, 2392, 2412, 2440, 2488, 2496, 2520, 2528, 2540, 2560, 2580, 2628, 2684, 2696, 2756, 2816, 2928, 3024, 3096, 3140, 3196, 3252, 3268, 3308, 3328, 3372, 3380, 3452, 3508, 3616, 3636, 3716, 3852, 3880, 3904, 3920, 4116, 4160, 4168, 4208, 4220, 4276, 4344, 4524, 4548, 4584, 4596, 4624, 4700, 4824, 4860, 4908, 4928, 4936, 4944, 4952, 4960, 4968, 4976, 4984, 4992, 5000, 5008, 5036, 5092, 5104, 5108, 5156, 5164, 5172, 5196, 5216, 5220, 5244, 5288, 5296, 5316, 5356, 5432, 5440, 5452, 5476, 5544, 5652, 5688, 5728, 5848, 5876, 5884, 5920, 6336, 6472, 6520, 6556, 6684, 6820, 6920, 7016, 7216, 7264, 7268, 7312, 7496, 7528, 7572, 7604, 7608, 7700, 7780, 7816, 7876, 7936, 7952, 8060, 8168, 8180, 8280, 8428, 8464, 8664, 8752, 8784, 8812, 8816, 8872, 8972, 9032, 9048, 9076, 9088, 9224, 9300, 9340, 9364, 9448, 9600, 9688, 9740, 9844, 9904, 9960, 10176, 10244, 10328, 10384, 10388, 10432, 10480, 10556, 10564, 10720, 10728, 10876, 11208, 11328, 11332, 11740, 11780, 11832, 11968, 12080, 12144, 12180, 12224, 12728, 12872, 13056, 13304, 13320, 13448, 13500, 13540, 13872, 13992, 14180, 14272, 14320, 14420, 14484, 14616, 14856, 14968, 15024, 15220, 15328, 15584, 15640, 16284, 16500, 16672, 17232, 17568, 17756, 17828, 18268, 18328, 18340, 18376, 18696, 18844, 18860, 19012, 19016, 19060, 19256, 19300, 19352, 19432, 19524, 19688, 19728, 19760, 19860, 19956, 20076, 20144, 20196, 20240, 20256, 20316, 20716, 20728, 20784, 21064, 21200]
#例項化一個Process物件,引數為一程式PID;即獲取指定pid的程式名
p=psutil.Process(520)
print(p.name())
#dllhost.exe
#獲取指定pid的程式bin路徑
print(p.exe())
#C:\Windows\System32\dllhost.exe
#指定程式工作目錄絕對路徑
print(p.cwd())
#C:\Windows\system32
#指定程式狀態
print(p.status())
#running
#指定程式的建立時間,時間戳格式
print(p.create_time())
#1636939051.8588655
#指定程式uid資訊
# print(p.uids())
#win好像沒有uid AttributeError: 'Process' object has no attribute 'uids'
#指定程式的gid資訊
# print(p.gids())
#win應該沒有gid
#程式cpu時間資訊,包括user、system兩個cpu時間
print(p.cpu_times)
#<bound method Process.cpu_times of psutil.Process(pid=520, name='dllhost.exe', status='running', started='2021-11-15 09:17:31')>
#貌似沒有cpu使用者和系統時間,可能是作業系統是win的原因吧
#獲取cpu親和度,如要設定程式cpu親和度,將cpu號作為引數即可
print(p.cpu_affinity())
#[0, 1, 2, 3]
#程式記憶體利用率
print(p.memory_percent())
#0.03454298466912478
#程式記憶體rss,vms資訊
print(p.memory_info)
#win的估計不支援,又不是記憶體資訊的<bound method Process.memory_info of psutil.Process(pid=520, name='dllhost.exe', status='running', started='2021-11-15 09:17:31')>
#程式IO資訊,包括讀寫IO數及位元組數
print(p.io_counters())
#pio(read_count=37, write_count=137, read_bytes=3260416, write_bytes=2072576, other_count=2032, other_bytes=45620)
#返回開啟程式socket的namedutples列表,包括fs,family,laddr等資訊
print(p.connections())
#[]
#程式開啟的執行緒數
print(p.num_threads())
#4
#====popen類的使用
from subprocess import PIPE
# p=psutil.Popen(["/usr/bin/python","-c","print('mcw')"],stdout=PIPE)
p.name()
#'python'
print(p.username())
#'root'
print(p.communicate())
#('hello\n',None)
print(p.cpu_times)
#pcputimes(user=0.001,system=xxx)
#參考地址 https://gihub.com/giampaolo/psutil http://psutil.readthedocs.org/en/latest
參考書籍:python自動化運維技術與最佳實踐 劉天斯