1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
# |
3
|
|
|
# This file is part of Glances. |
4
|
|
|
# |
5
|
|
|
# SPDX-FileCopyrightText: 2022 Nicolas Hennion <[email protected]> |
6
|
|
|
# |
7
|
|
|
# SPDX-License-Identifier: LGPL-3.0-only |
8
|
|
|
# |
9
|
|
|
|
10
|
|
|
"""CPU plugin.""" |
11
|
|
|
|
12
|
|
|
from glances.timer import getTimeSinceLastUpdate |
13
|
|
|
from glances.globals import LINUX, WINDOWS, SUNOS, iterkeys |
14
|
|
|
from glances.cpu_percent import cpu_percent |
15
|
|
|
from glances.plugins.core import PluginModel as CorePluginModel |
16
|
|
|
from glances.plugins.plugin.model import GlancesPluginModel |
17
|
|
|
|
18
|
|
|
import psutil |
19
|
|
|
|
20
|
|
|
# Fields description |
21
|
|
|
# description: human readable description |
22
|
|
|
# short_name: shortname to use un UI |
23
|
|
|
# unit: unit type |
24
|
|
|
# rate: is it a rate ? If yes, // by time_since_update when displayed, |
25
|
|
|
# min_symbol: Auto unit should be used if value > than 1 'X' (K, M, G)... |
26
|
|
|
fields_description = { |
27
|
|
|
'total': {'description': 'Sum of all CPU percentages (except idle).', 'unit': 'percent'}, |
28
|
|
|
'system': { |
29
|
|
|
'description': 'percent time spent in kernel space. System CPU time is the \ |
30
|
|
|
time spent running code in the Operating System kernel.', |
31
|
|
|
'unit': 'percent', |
32
|
|
|
}, |
33
|
|
|
'user': { |
34
|
|
|
'description': 'CPU percent time spent in user space. \ |
35
|
|
|
User CPU time is the time spent on the processor running your program\'s code (or code in libraries).', |
36
|
|
|
'unit': 'percent', |
37
|
|
|
}, |
38
|
|
|
'iowait': { |
39
|
|
|
'description': '*(Linux)*: percent time spent by the CPU waiting for I/O \ |
40
|
|
|
operations to complete.', |
41
|
|
|
'unit': 'percent', |
42
|
|
|
}, |
43
|
|
|
'dpc': { |
44
|
|
|
'description': '*(Windows)*: time spent servicing deferred procedure calls (DPCs)', |
45
|
|
|
'unit': 'percent', |
46
|
|
|
}, |
47
|
|
|
'idle': { |
48
|
|
|
'description': 'percent of CPU used by any program. Every program or task \ |
49
|
|
|
that runs on a computer system occupies a certain amount of processing \ |
50
|
|
|
time on the CPU. If the CPU has completed all tasks it is idle.', |
51
|
|
|
'unit': 'percent', |
52
|
|
|
}, |
53
|
|
|
'irq': { |
54
|
|
|
'description': '*(Linux and BSD)*: percent time spent servicing/handling \ |
55
|
|
|
hardware/software interrupts. Time servicing interrupts (hardware + \ |
56
|
|
|
software).', |
57
|
|
|
'unit': 'percent', |
58
|
|
|
}, |
59
|
|
|
'nice': { |
60
|
|
|
'description': '*(Unix)*: percent time occupied by user level processes with \ |
61
|
|
|
a positive nice value. The time the CPU has spent running users\' \ |
62
|
|
|
processes that have been *niced*.', |
63
|
|
|
'unit': 'percent', |
64
|
|
|
}, |
65
|
|
|
'steal': { |
66
|
|
|
'description': '*(Linux)*: percentage of time a virtual CPU waits for a real \ |
67
|
|
|
CPU while the hypervisor is servicing another virtual processor.', |
68
|
|
|
'unit': 'percent', |
69
|
|
|
}, |
70
|
|
|
'ctx_switches': { |
71
|
|
|
'description': 'number of context switches (voluntary + involuntary) per \ |
72
|
|
|
second. A context switch is a procedure that a computer\'s CPU (central \ |
73
|
|
|
processing unit) follows to change from one task (or process) to \ |
74
|
|
|
another while ensuring that the tasks do not conflict.', |
75
|
|
|
'unit': 'number', |
76
|
|
|
'rate': True, |
77
|
|
|
'min_symbol': 'K', |
78
|
|
|
'short_name': 'ctx_sw', |
79
|
|
|
}, |
80
|
|
|
'interrupts': { |
81
|
|
|
'description': 'number of interrupts per second.', |
82
|
|
|
'unit': 'number', |
83
|
|
|
'rate': True, |
84
|
|
|
'min_symbol': 'K', |
85
|
|
|
'short_name': 'inter', |
86
|
|
|
}, |
87
|
|
|
'soft_interrupts': { |
88
|
|
|
'description': 'number of software interrupts per second. Always set to \ |
89
|
|
|
0 on Windows and SunOS.', |
90
|
|
|
'unit': 'number', |
91
|
|
|
'rate': True, |
92
|
|
|
'min_symbol': 'K', |
93
|
|
|
'short_name': 'sw_int', |
94
|
|
|
}, |
95
|
|
|
'syscalls': { |
96
|
|
|
'description': 'number of system calls per second. Always 0 on Linux OS.', |
97
|
|
|
'unit': 'number', |
98
|
|
|
'rate': True, |
99
|
|
|
'min_symbol': 'K', |
100
|
|
|
'short_name': 'sys_call', |
101
|
|
|
}, |
102
|
|
|
'cpucore': {'description': 'Total number of CPU core.', 'unit': 'number'}, |
103
|
|
|
'time_since_update': {'description': 'Number of seconds since last update.', 'unit': 'seconds'}, |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
# SNMP OID |
107
|
|
|
# percentage of user CPU time: .1.3.6.1.4.1.2021.11.9.0 |
108
|
|
|
# percentages of system CPU time: .1.3.6.1.4.1.2021.11.10.0 |
109
|
|
|
# percentages of idle CPU time: .1.3.6.1.4.1.2021.11.11.0 |
110
|
|
|
snmp_oid = { |
111
|
|
|
'default': { |
112
|
|
|
'user': '1.3.6.1.4.1.2021.11.9.0', |
113
|
|
|
'system': '1.3.6.1.4.1.2021.11.10.0', |
114
|
|
|
'idle': '1.3.6.1.4.1.2021.11.11.0', |
115
|
|
|
}, |
116
|
|
|
'windows': {'percent': '1.3.6.1.2.1.25.3.3.1.2'}, |
117
|
|
|
'esxi': {'percent': '1.3.6.1.2.1.25.3.3.1.2'}, |
118
|
|
|
'netapp': { |
119
|
|
|
'system': '1.3.6.1.4.1.789.1.2.1.3.0', |
120
|
|
|
'idle': '1.3.6.1.4.1.789.1.2.1.5.0', |
121
|
|
|
'cpucore': '1.3.6.1.4.1.789.1.2.1.6.0', |
122
|
|
|
}, |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
# Define the history items list |
126
|
|
|
# - 'name' define the stat identifier |
127
|
|
|
# - 'y_unit' define the Y label |
128
|
|
|
items_history_list = [ |
129
|
|
|
{'name': 'user', 'description': 'User CPU usage', 'y_unit': '%'}, |
130
|
|
|
{'name': 'system', 'description': 'System CPU usage', 'y_unit': '%'}, |
131
|
|
|
] |
132
|
|
|
|
133
|
|
|
|
134
|
|
|
class PluginModel(GlancesPluginModel): |
135
|
|
|
"""Glances CPU plugin. |
136
|
|
|
|
137
|
|
|
'stats' is a dictionary that contains the system-wide CPU utilization as a |
138
|
|
|
percentage. |
139
|
|
|
""" |
140
|
|
|
|
141
|
|
|
def __init__(self, args=None, config=None): |
142
|
|
|
"""Init the CPU plugin.""" |
143
|
|
|
super(PluginModel, self).__init__( |
144
|
|
|
args=args, config=config, items_history_list=items_history_list, fields_description=fields_description |
145
|
|
|
) |
146
|
|
|
|
147
|
|
|
# We want to display the stat in the curse interface |
148
|
|
|
self.display_curse = True |
149
|
|
|
|
150
|
|
|
# Call CorePluginModel in order to display the core number |
151
|
|
|
try: |
152
|
|
|
self.nb_log_core = CorePluginModel(args=self.args).update()["log"] |
153
|
|
|
except Exception: |
154
|
|
|
self.nb_log_core = 1 |
155
|
|
|
|
156
|
|
|
@GlancesPluginModel._check_decorator |
157
|
|
|
@GlancesPluginModel._log_result_decorator |
158
|
|
|
def update(self): |
159
|
|
|
"""Update CPU stats using the input method.""" |
160
|
|
|
# Grab stats into self.stats |
161
|
|
|
if self.input_method == 'local': |
162
|
|
|
stats = self.update_local() |
163
|
|
|
elif self.input_method == 'snmp': |
164
|
|
|
stats = self.update_snmp() |
165
|
|
|
else: |
166
|
|
|
stats = self.get_init_value() |
167
|
|
|
|
168
|
|
|
# Update the stats |
169
|
|
|
self.stats = stats |
170
|
|
|
|
171
|
|
|
return self.stats |
172
|
|
|
|
173
|
|
|
def update_local(self): |
174
|
|
|
"""Update CPU stats using psutil.""" |
175
|
|
|
# Grab CPU stats using psutil's cpu_percent and cpu_times_percent |
176
|
|
|
# Get all possible values for CPU stats: user, system, idle, |
177
|
|
|
# nice (UNIX), iowait (Linux), irq (Linux, FreeBSD), steal (Linux 2.6.11+) |
178
|
|
|
# The following stats are returned by the API but not displayed in the UI: |
179
|
|
|
# softirq (Linux), guest (Linux 2.6.24+), guest_nice (Linux 3.2.0+) |
180
|
|
|
|
181
|
|
|
# Init new stats |
182
|
|
|
stats = self.get_init_value() |
183
|
|
|
|
184
|
|
|
stats['total'] = cpu_percent.get() |
185
|
|
|
# Standards stats |
186
|
|
|
# - user: time spent by normal processes executing in user mode; on Linux this also includes guest time |
187
|
|
|
# - system: time spent by processes executing in kernel mode |
188
|
|
|
# - idle: time spent doing nothing |
189
|
|
|
# - nice (UNIX): time spent by niced (prioritized) processes executing in user mode |
190
|
|
|
# on Linux this also includes guest_nice time |
191
|
|
|
# - iowait (Linux): time spent waiting for I/O to complete. |
192
|
|
|
# This is not accounted in idle time counter. |
193
|
|
|
# - irq (Linux, BSD): time spent for servicing hardware interrupts |
194
|
|
|
# - softirq (Linux): time spent for servicing software interrupts |
195
|
|
|
# - steal (Linux 2.6.11+): time spent by other operating systems running in a virtualized environment |
196
|
|
|
# - guest (Linux 2.6.24+): time spent running a virtual CPU for guest operating systems under |
197
|
|
|
# the control of the Linux kernel |
198
|
|
|
# - guest_nice (Linux 3.2.0+): time spent running a niced guest (virtual CPU for guest operating systems |
199
|
|
|
# under the control of the Linux kernel) |
200
|
|
|
# - interrupt (Windows): time spent for servicing hardware interrupts ( similar to “irq” on UNIX) |
201
|
|
|
# - dpc (Windows): time spent servicing deferred procedure calls (DPCs) |
202
|
|
|
cpu_times_percent = psutil.cpu_times_percent(interval=0.0) |
203
|
|
|
for stat in cpu_times_percent._fields: |
204
|
|
|
stats[stat] = getattr(cpu_times_percent, stat) |
205
|
|
|
|
206
|
|
|
# Additional CPU stats (number of events not as a %; psutil>=4.1.0) |
207
|
|
|
# - ctx_switches: number of context switches (voluntary + involuntary) since boot. |
208
|
|
|
# - interrupts: number of interrupts since boot. |
209
|
|
|
# - soft_interrupts: number of software interrupts since boot. Always set to 0 on Windows and SunOS. |
210
|
|
|
# - syscalls: number of system calls since boot. Always set to 0 on Linux. |
211
|
|
|
cpu_stats = psutil.cpu_stats() |
212
|
|
|
|
213
|
|
|
# By storing time data we enable Rx/s and Tx/s calculations in the |
214
|
|
|
# XML/RPC API, which would otherwise be overly difficult work |
215
|
|
|
# for users of the API |
216
|
|
|
stats['time_since_update'] = getTimeSinceLastUpdate('cpu') |
217
|
|
|
|
218
|
|
|
# Core number is needed to compute the CTX switch limit |
219
|
|
|
stats['cpucore'] = self.nb_log_core |
220
|
|
|
|
221
|
|
|
# Previous CPU stats are stored in the cpu_stats_old variable |
222
|
|
|
if not hasattr(self, 'cpu_stats_old'): |
223
|
|
|
# Init the stats (needed to have the key name for export) |
224
|
|
|
for stat in cpu_stats._fields: |
225
|
|
|
# @TODO: better to set it to None but should refactor views and UI... |
226
|
|
|
stats[stat] = 0 |
227
|
|
|
else: |
228
|
|
|
# Others calls... |
229
|
|
|
for stat in cpu_stats._fields: |
230
|
|
|
if getattr(cpu_stats, stat) is not None: |
231
|
|
|
stats[stat] = getattr(cpu_stats, stat) - getattr(self.cpu_stats_old, stat) |
232
|
|
|
|
233
|
|
|
# Save stats to compute next step |
234
|
|
|
self.cpu_stats_old = cpu_stats |
235
|
|
|
|
236
|
|
|
return stats |
237
|
|
|
|
238
|
|
|
def update_snmp(self): |
239
|
|
|
"""Update CPU stats using SNMP.""" |
240
|
|
|
|
241
|
|
|
# Init new stats |
242
|
|
|
stats = self.get_init_value() |
243
|
|
|
|
244
|
|
|
# Update stats using SNMP |
245
|
|
|
if self.short_system_name in ('windows', 'esxi'): |
246
|
|
|
# Windows or VMWare ESXi |
247
|
|
|
# You can find the CPU utilization of windows system by querying the oid |
248
|
|
|
# Give also the number of core (number of element in the table) |
249
|
|
|
try: |
250
|
|
|
cpu_stats = self.get_stats_snmp(snmp_oid=snmp_oid[self.short_system_name], bulk=True) |
251
|
|
|
except KeyError: |
252
|
|
|
self.reset() |
253
|
|
|
|
254
|
|
|
# Iter through CPU and compute the idle CPU stats |
255
|
|
|
stats['nb_log_core'] = 0 |
256
|
|
|
stats['idle'] = 0 |
257
|
|
|
for c in cpu_stats: |
258
|
|
|
if c.startswith('percent'): |
259
|
|
|
stats['idle'] += float(cpu_stats['percent.3']) |
260
|
|
|
stats['nb_log_core'] += 1 |
261
|
|
|
if stats['nb_log_core'] > 0: |
262
|
|
|
stats['idle'] = stats['idle'] / stats['nb_log_core'] |
263
|
|
|
stats['idle'] = 100 - stats['idle'] |
264
|
|
|
stats['total'] = 100 - stats['idle'] |
265
|
|
|
|
266
|
|
|
else: |
267
|
|
|
# Default behavior |
268
|
|
|
try: |
269
|
|
|
stats = self.get_stats_snmp(snmp_oid=snmp_oid[self.short_system_name]) |
270
|
|
|
except KeyError: |
271
|
|
|
stats = self.get_stats_snmp(snmp_oid=snmp_oid['default']) |
272
|
|
|
|
273
|
|
|
if stats['idle'] == '': |
274
|
|
|
self.reset() |
275
|
|
|
return self.stats |
276
|
|
|
|
277
|
|
|
# Convert SNMP stats to float |
278
|
|
|
for key in iterkeys(stats): |
279
|
|
|
stats[key] = float(stats[key]) |
280
|
|
|
stats['total'] = 100 - stats['idle'] |
281
|
|
|
|
282
|
|
|
return stats |
283
|
|
|
|
284
|
|
|
def update_views(self): |
285
|
|
|
"""Update stats views.""" |
286
|
|
|
# Call the father's method |
287
|
|
|
super(PluginModel, self).update_views() |
288
|
|
|
|
289
|
|
|
# Add specifics information |
290
|
|
|
# Alert and log |
291
|
|
|
for key in ['user', 'system', 'iowait', 'dpc', 'total']: |
292
|
|
|
if key in self.stats: |
293
|
|
|
self.views[key]['decoration'] = self.get_alert_log(self.stats[key], header=key) |
294
|
|
|
# Alert only |
295
|
|
|
for key in ['steal']: |
296
|
|
|
if key in self.stats: |
297
|
|
|
self.views[key]['decoration'] = self.get_alert(self.stats[key], header=key) |
298
|
|
|
# Alert only but depend on Core number |
299
|
|
|
for key in ['ctx_switches']: |
300
|
|
|
if key in self.stats: |
301
|
|
|
self.views[key]['decoration'] = self.get_alert( |
302
|
|
|
self.stats[key], maximum=100 * self.stats['cpucore'], header=key |
303
|
|
|
) |
304
|
|
|
# Optional |
305
|
|
|
for key in ['nice', 'irq', 'idle', 'steal', 'ctx_switches', 'interrupts', 'soft_interrupts', 'syscalls']: |
306
|
|
|
if key in self.stats: |
307
|
|
|
self.views[key]['optional'] = True |
308
|
|
|
|
309
|
|
|
def msg_curse(self, args=None, max_width=None): |
310
|
|
|
"""Return the list to display in the UI.""" |
311
|
|
|
# Init the return message |
312
|
|
|
ret = [] |
313
|
|
|
|
314
|
|
|
# Only process if stats exist and plugin not disable |
315
|
|
|
if not self.stats or self.args.percpu or self.is_disabled(): |
316
|
|
|
return ret |
317
|
|
|
|
318
|
|
|
# Some tag to enable/disable stats (example: idle_tag triggered on Windows OS) |
319
|
|
|
idle_tag = 'user' not in self.stats |
320
|
|
|
|
321
|
|
|
# First line |
322
|
|
|
# Total + (idle) + ctx_sw |
323
|
|
|
msg = '{}'.format('CPU') |
324
|
|
|
ret.append(self.curse_add_line(msg, "TITLE")) |
325
|
|
|
trend_user = self.get_trend('user') |
326
|
|
|
trend_system = self.get_trend('system') |
327
|
|
|
if trend_user is None or trend_user is None: |
328
|
|
|
trend_cpu = None |
329
|
|
|
else: |
330
|
|
|
trend_cpu = trend_user + trend_system |
331
|
|
|
msg = ' {:4}'.format(self.trend_msg(trend_cpu)) |
332
|
|
|
ret.append(self.curse_add_line(msg)) |
333
|
|
|
# Total CPU usage |
334
|
|
|
msg = '{:5.1f}%'.format(self.stats['total']) |
335
|
|
|
ret.append(self.curse_add_line(msg, self.get_views(key='total', option='decoration'))) |
336
|
|
|
# Idle CPU |
337
|
|
|
if 'idle' in self.stats and not idle_tag: |
338
|
|
|
msg = ' {:8}'.format('idle') |
339
|
|
|
ret.append(self.curse_add_line(msg, optional=self.get_views(key='idle', option='optional'))) |
340
|
|
|
msg = '{:4.1f}%'.format(self.stats['idle']) |
341
|
|
|
ret.append(self.curse_add_line(msg, optional=self.get_views(key='idle', option='optional'))) |
342
|
|
|
# ctx_switches |
343
|
|
|
# On WINDOWS/SUNOS the ctx_switches is displayed in the third line |
344
|
|
|
if not WINDOWS and not SUNOS: |
345
|
|
|
ret.extend(self.curse_add_stat('ctx_switches', width=15, header=' ')) |
346
|
|
|
|
347
|
|
|
# Second line |
348
|
|
|
# user|idle + irq + interrupts |
349
|
|
|
ret.append(self.curse_new_line()) |
350
|
|
|
# User CPU |
351
|
|
|
if not idle_tag: |
352
|
|
|
ret.extend(self.curse_add_stat('user', width=15)) |
353
|
|
|
elif 'idle' in self.stats: |
354
|
|
|
ret.extend(self.curse_add_stat('idle', width=15)) |
355
|
|
|
# IRQ CPU |
356
|
|
|
ret.extend(self.curse_add_stat('irq', width=14, header=' ')) |
357
|
|
|
# interrupts |
358
|
|
|
ret.extend(self.curse_add_stat('interrupts', width=15, header=' ')) |
359
|
|
|
|
360
|
|
|
# Third line |
361
|
|
|
# system|core + nice + sw_int |
362
|
|
|
ret.append(self.curse_new_line()) |
363
|
|
|
# System CPU |
364
|
|
|
if not idle_tag: |
365
|
|
|
ret.extend(self.curse_add_stat('system', width=15)) |
366
|
|
|
else: |
367
|
|
|
ret.extend(self.curse_add_stat('core', width=15)) |
368
|
|
|
# Nice CPU |
369
|
|
|
ret.extend(self.curse_add_stat('nice', width=14, header=' ')) |
370
|
|
|
# soft_interrupts |
371
|
|
|
if not WINDOWS and not SUNOS: |
372
|
|
|
ret.extend(self.curse_add_stat('soft_interrupts', width=15, header=' ')) |
373
|
|
|
else: |
374
|
|
|
ret.extend(self.curse_add_stat('ctx_switches', width=15, header=' ')) |
375
|
|
|
|
376
|
|
|
# Fourth line |
377
|
|
|
# iowait + steal + syscalls |
378
|
|
|
ret.append(self.curse_new_line()) |
379
|
|
|
if 'iowait' in self.stats: |
380
|
|
|
# IOWait CPU |
381
|
|
|
ret.extend(self.curse_add_stat('iowait', width=15)) |
382
|
|
|
elif 'dpc' in self.stats: |
383
|
|
|
# DPC CPU |
384
|
|
|
ret.extend(self.curse_add_stat('dpc', width=15)) |
385
|
|
|
# Steal CPU usage |
386
|
|
|
ret.extend(self.curse_add_stat('steal', width=14, header=' ')) |
387
|
|
|
# syscalls: number of system calls since boot. Always set to 0 on Linux. (do not display) |
388
|
|
|
if not LINUX: |
389
|
|
|
ret.extend(self.curse_add_stat('syscalls', width=15, header=' ')) |
390
|
|
|
|
391
|
|
|
# Return the message with decoration |
392
|
|
|
return ret |
393
|
|
|
|