psutil,(process and system utilities),可以通过一两行代码实现系统监控,还可以跨平台使用,支持Linux/UNIX/OSX/Windows等,是系统管理员和运维小伙伴不可或缺的必备模块。
1 pip install psutil
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]
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。
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)
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() # 获取当前网络连接信息
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