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