python獲取系統信息模塊psutil


  psutil,(process and system utilities),可以通過一兩行代碼實現系統監控,還可以跨平台使用,支持Linux/UNIX/OSX/Windows等,是系統管理員和運維小伙伴不可或缺的必備模塊。
 

1 安裝

1 pip install psutil 

2 獲取CPU信息 

 1 In [2]: psutil.cpu_count()                 # CPU邏輯數量                                               
 2 Out[2]: 8
 3 
 4 In [3]: psutil.cpu_count(logical=False)    # CPU物理核心                                   
 5 Out[3]: 4
 6 
 7 In [4]: psutil.cpu_times()                 # 統計CPU的用戶/系統/空閑時間                                             
 8 Out[4]: scputimes(user=830519.73, nice=0.0, system=723038.84, idle=42315894.17)
 9 
10 - 實現類似top命令的CPU使用率,每秒刷新一次,累計10次
11 In [6]: for x in range(10): 
12    ...:     r = psutil.cpu_percent(interval=1, percpu=True) 
13    ...:     print(r) 
14    ...:                                                                         
15 [19.0, 0.0, 12.7, 1.0, 8.0, 1.0, 4.0, 1.0]
16 [20.0, 0.0, 12.1, 0.0, 8.0, 0.0, 4.0, 1.0]
17 [32.0, 5.0, 23.0, 4.0, 19.0, 4.0, 16.0, 2.0]
18 [20.0, 0.0, 15.0, 0.0, 7.1, 0.0, 5.9, 2.0]
19 [24.8, 1.0, 14.9, 1.0, 9.0, 1.0, 8.1, 0.0]
20 [34.0, 4.0, 27.0, 4.0, 24.8, 4.0, 17.8, 5.0]
21 [28.7, 2.0, 20.0, 1.0, 13.0, 2.0, 11.0, 1.0]
22 [21.0, 1.0, 15.8, 1.0, 10.9, 0.0, 6.9, 1.0]
23 [22.0, 0.0, 14.9, 0.0, 9.0, 0.0, 7.0, 0.0]
24 [25.0, 2.0, 15.2, 1.0, 11.0, 1.0, 5.0, 0.0]

3 獲取內存信息

1 In [7]: psutil.virtual_memory()           # 獲取物理內存信息                                          
2 Out[7]: svmem(total=17179869184, available=5688819712, percent=66.9, used=9728593920, free=159133696, active=5531738112, inactive=5306654720, wired=4196855808)
3 
4 In [8]: psutil.swap_memory()              # 獲取交換內存信息                                        
5 Out[8]: sswap(total=5368709120, used=4316463104, free=1052246016, percent=80.4, sin=659438723072, sout=4277862400)
6 - 注意: 返回的是字節為單位的整數。 總內存17179869184/1024/1024/1024 = 16.0 G。

4 獲取磁盤信息

 1 In [9]: psutil.disk_partitions()          # 磁盤分區信息                             
 2 Out[9]: 
 3 [sdiskpart(device='/dev/disk1s1', mountpoint='/', fstype='apfs', opts='rw,local,rootfs,dovolfs,journaled,multilabel'),
 4  sdiskpart(device='/dev/disk1s4', mountpoint='/private/var/vm', fstype='apfs', opts='rw,noexec,local,dovolfs,dontbrowse,journaled,multilabel,noatime')]
 5 - 有兩個分區,磁盤格式apfs,opts中journaled表示支持日志。
 6 
 7 In [10]: psutil.disk_usage('/')           # 磁盤使用情況                             
 8 Out[10]: sdiskusage(total=250685575168, used=118098038784, free=125440405504, percent=48.5)
 9 - 磁盤總共250685575168/1024/1024/1024233.469G,已使用48.5%10 
11 In [11]: psutil.disk_io_counters()        # 磁盤IO                                 
12 Out[11]: sdiskio(read_count=55081907, write_count=65301593, read_bytes=996595785728, write_bytes=1063386177536, read_time=18258582, write_time=10621452)

5 獲取網絡信息

In [12]: psutil.net_io_counters()         # 獲取網絡讀寫字節/包的個數              
Out[12]: snetio(bytes_sent=80803235840, bytes_recv=124806110208, packets_sent=254195219, packets_recv=182450714, errin=0, errout=130782, dropin=0, dropout=0)

In [13]: psutil.net_if_addrs()            # 獲取網絡接口信息

In [14]: psutil.net_if_stats()            # 獲取網絡接口狀態

In [15]: psutil.net_connections()         # 獲取當前網絡連接信息

6 獲取進程信息

 1 In [16]: psutil.pids()                    # 所有進程ID                                         
 2 Out[16]: [0,1,39,40, ...]
 3 
 4 In [17]: import os                                                          
 5 
 6 In [18]: os.getpid()                                                        
 7 Out[18]: 70042
 8 
 9 In [19]: p = psutil.Process(os.getpid())  # 獲取指定進程                                  
10 
11 In [20]: p.name()         # 進程名稱                                                
12 Out[20]: 'python3.7'
13 
14 In [21]: p.exe()          # 進程exe路徑                                              
15 Out[21]: '/opt/anaconda3/bin/python3.7'
16 
17 In [22]: p.cwd()          # 進程工作目錄                                             
18 Out[22]: '/Users/liuyouyuan/workspace/pyproject/scripts'
19 
20 In [23]: p.cmdline()      # 進程啟動的命令行                                     
21 Out[23]: ['/opt/anaconda3/bin/python', '/opt/anaconda3/bin/ipython']
22 
23 In [24]: p.ppid()         # 父進程ID                                                
24 Out[24]: 46810
25 
26 In [25]: p.parent()       # 父進程                                                
27 Out[25]: psutil.Process(pid=46810, name='zsh', started='2020-03-30 15:24:07')
28 
29 In [26]: p.children()     # 子進程列表                                          
30 Out[26]: []
31 
32 In [27]: p.status()       # 進程狀態                                              
33 Out[27]: 'running'
34 
35 In [28]: p.username()     # 進程用戶名                                          
36 Out[28]: 'liuyouyuan'
37 
38 In [29]: p.create_time()  # 進程創建時間                                     
39 Out[29]: 1586241277.044256
40 
41 In [30]: p.terminal()     # 進程終端                                            
42 Out[30]: '/dev/ttys003'
43 
44 In [31]: p.cpu_times()    # 進程使用的CPU時間                                  
45 Out[31]: pcputimes(user=3.119619328, system=0.63911072, children_user=0.0, children_system=0.0)
46 
47 In [32]: p.memory_info()  # 進程使用的內存                                   
48 Out[32]: pmem(rss=73498624, vms=4479959040, pfaults=28482, pageins=185)
49 
50 In [33]: p.open_files()   # 進程打開的文件                                    
51 Out[33]: 
52 [popenfile(path='/Applications/Visual Studio Code.app/Contents/Resources/electron.asar', fd=26),
53  popenfile(path='/Applications/Visual Studio Code.app/Contents/Resources/app/node_modules.asar', fd=28),
54  popenfile(path='/System/Library/Frameworks/Carbon.framework/Versions/A/Frameworks/HIToolbox.framework/Versions/A/Resources/English.lproj/Localized.rsrc', fd=33)]
55 
56 In [34]: p.connections()  # 進程相關網絡連接                                 
57 Out[34]: []
58 
59 In [35]: p.num_threads()  # 進程的線程數量                                   
60 Out[35]: 6
61 
62 In [36]: p.threads()      # 所有線程信息                                         
63 Out[36]: 
64 [pthread(id=1, user_time=3.008698, system_time=0.4743),
65  pthread(id=2, user_time=0.008189, system_time=0.028232),
66  pthread(id=3, user_time=3.7e-05, system_time=2.4e-05),
67  pthread(id=4, user_time=3.8e-05, system_time=2.5e-05),
68  pthread(id=5, user_time=3.3e-05, system_time=1.6e-05),
69  pthread(id=6, user_time=2e-05, system_time=1.7e-05)]
70 
71 In [37]: p.environ()      # 進程環境變量                                         
72 Out[37]: 
73 {'TMPDIR': '/var/folders/ys/hf5c1mt91qgdq3r803d6qdq80000gn/T/',
74  '__CF_USER_TEXT_ENCODING': '0x1F5:0x19:0x34',
75  'SHELL': '/bin/zsh',
76  ...
77  '_': '/opt/anaconda3/bin/ipython'}
78 
79 In [38]: p.terminate()    # 結束進程                                           
80 [1]    70042 terminated  ipython
81 (base) 

6 參考

https://www.liaoxuefeng.com/wiki/1016959663602400/1183565811281984


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM