1
|
|
|
# |
2
|
|
|
# This file is part of Glances. |
3
|
|
|
# |
4
|
|
|
# SPDX-FileCopyrightText: 2024 Nicolas Hennion <[email protected]> |
5
|
|
|
# |
6
|
|
|
# SPDX-License-Identifier: LGPL-3.0-only |
7
|
|
|
# |
8
|
|
|
|
9
|
|
|
"""Quicklook plugin.""" |
10
|
|
|
|
11
|
|
|
import psutil |
12
|
|
|
|
13
|
|
|
from glances.cpu_percent import cpu_percent |
14
|
|
|
from glances.logger import logger |
15
|
|
|
from glances.outputs.glances_bars import Bar |
16
|
|
|
from glances.outputs.glances_sparklines import Sparkline |
17
|
|
|
from glances.plugins.load import get_load_average, get_nb_log_core, get_nb_phys_core |
18
|
|
|
from glances.plugins.plugin.model import GlancesPluginModel |
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
|
|
|
'cpu': { |
28
|
|
|
'description': 'CPU percent usage', |
29
|
|
|
'unit': 'percent', |
30
|
|
|
}, |
31
|
|
|
'mem': { |
32
|
|
|
'description': 'MEM percent usage', |
33
|
|
|
'unit': 'percent', |
34
|
|
|
}, |
35
|
|
|
'swap': { |
36
|
|
|
'description': 'SWAP percent usage', |
37
|
|
|
'unit': 'percent', |
38
|
|
|
}, |
39
|
|
|
'load': { |
40
|
|
|
'description': 'LOAD percent usage', |
41
|
|
|
'unit': 'percent', |
42
|
|
|
'alert': True, |
43
|
|
|
}, |
44
|
|
|
'cpu_log_core': { |
45
|
|
|
'description': 'Number of logical CPU core', |
46
|
|
|
'unit': 'number', |
47
|
|
|
}, |
48
|
|
|
'cpu_phys_core': { |
49
|
|
|
'description': 'Number of physical CPU core', |
50
|
|
|
'unit': 'number', |
51
|
|
|
}, |
52
|
|
|
'cpu_name': { |
53
|
|
|
'description': 'CPU name', |
54
|
|
|
}, |
55
|
|
|
'cpu_hz_current': { |
56
|
|
|
'description': 'CPU current frequency', |
57
|
|
|
'unit': 'hertz', |
58
|
|
|
}, |
59
|
|
|
'cpu_hz': { |
60
|
|
|
'description': 'CPU max frequency', |
61
|
|
|
'unit': 'hertz', |
62
|
|
|
}, |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
# Define the history items list |
66
|
|
|
# All items in this list will be historised if the --enable-history tag is set |
67
|
|
|
items_history_list = [ |
68
|
|
|
{'name': 'cpu', 'description': 'CPU percent usage', 'y_unit': '%'}, |
69
|
|
|
{'name': 'percpu', 'description': 'PERCPU percent usage', 'y_unit': '%'}, |
70
|
|
|
{'name': 'mem', 'description': 'MEM percent usage', 'y_unit': '%'}, |
71
|
|
|
{'name': 'swap', 'description': 'SWAP percent usage', 'y_unit': '%'}, |
72
|
|
|
{'name': 'load', 'description': 'LOAD percent usage', 'y_unit': '%'}, |
73
|
|
|
] |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
class QuicklookPlugin(GlancesPluginModel): |
77
|
|
|
"""Glances quicklook plugin. |
78
|
|
|
|
79
|
|
|
'stats' is a dictionary. |
80
|
|
|
""" |
81
|
|
|
|
82
|
|
|
AVAILABLE_STATS_LIST = ['cpu', 'mem', 'swap', 'load'] |
83
|
|
|
DEFAULT_STATS_LIST = ['cpu', 'mem', 'load'] |
84
|
|
|
|
85
|
|
|
def __init__(self, args=None, config=None): |
86
|
|
|
"""Init the quicklook plugin.""" |
87
|
|
|
super().__init__( |
88
|
|
|
args=args, config=config, items_history_list=items_history_list, fields_description=fields_description |
89
|
|
|
) |
90
|
|
|
|
91
|
|
|
# We want to display the stat in the curse interface |
92
|
|
|
self.display_curse = True |
93
|
|
|
|
94
|
|
|
# Manage the maximum number of CPU to display (related to enhancement request #2734) |
95
|
|
|
self.max_cpu_display = config.get_int_value('percpu', 'max_cpu_display', 4) if config else 4 |
96
|
|
|
|
97
|
|
|
# Define the stats list |
98
|
|
|
self.stats_list = self.get_conf_value('list', default=self.DEFAULT_STATS_LIST) |
99
|
|
|
if not set(self.stats_list).issubset(self.AVAILABLE_STATS_LIST): |
100
|
|
|
logger.warning(f'Quicklook plugin: Invalid stats list: {self.stats_list}') |
101
|
|
|
self.stats_list = self.AVAILABLE_STATS_LIST |
102
|
|
|
|
103
|
|
|
@GlancesPluginModel._check_decorator |
104
|
|
|
@GlancesPluginModel._log_result_decorator |
105
|
|
|
def update(self): |
106
|
|
|
"""Update quicklook stats using the input method.""" |
107
|
|
|
# Init new stats |
108
|
|
|
stats = self.get_init_value() |
109
|
|
|
|
110
|
|
|
# Grab quicklook stats: CPU, MEM and SWAP |
111
|
|
|
if self.input_method == 'local': |
112
|
|
|
# Get system information |
113
|
|
|
cpu_info = cpu_percent.get_info() |
114
|
|
|
stats['cpu_name'] = cpu_info['cpu_name'] |
115
|
|
|
stats['cpu_hz_current'] = ( |
116
|
|
|
self._mhz_to_hz(cpu_info['cpu_hz_current']) if cpu_info['cpu_hz_current'] is not None else None |
117
|
|
|
) |
118
|
|
|
stats['cpu_hz'] = self._mhz_to_hz(cpu_info['cpu_hz']) if cpu_info['cpu_hz'] is not None else None |
119
|
|
|
|
120
|
|
|
# Get the CPU percent value (global and per core) |
121
|
|
|
# Stats is shared across all plugins |
122
|
|
|
stats['cpu'] = cpu_percent.get_cpu() |
123
|
|
|
stats['percpu'] = cpu_percent.get_percpu() |
124
|
|
|
|
125
|
|
|
# Get the virtual and swap memory |
126
|
|
|
stats['mem'] = psutil.virtual_memory().percent |
127
|
|
|
try: |
128
|
|
|
stats['swap'] = psutil.swap_memory().percent |
129
|
|
|
except RuntimeError: |
130
|
|
|
# Correct issue in Illumos OS (see #1767) |
131
|
|
|
stats['swap'] = None |
132
|
|
|
|
133
|
|
|
# Get load |
134
|
|
|
stats['cpu_log_core'] = get_nb_log_core() |
135
|
|
|
stats['cpu_phys_core'] = get_nb_phys_core() |
136
|
|
|
try: |
137
|
|
|
# Load average is a tuple (1 min, 5 min, 15 min) |
138
|
|
|
# Process only the 15 min value (index 2) |
139
|
|
|
stats['load'] = get_load_average(percent=True)[2] |
140
|
|
|
except (TypeError, IndexError): |
141
|
|
|
stats['load'] = None |
142
|
|
|
|
143
|
|
|
elif self.input_method == 'snmp': |
144
|
|
|
# Not available |
145
|
|
|
pass |
146
|
|
|
|
147
|
|
|
# Update the stats |
148
|
|
|
self.stats = stats |
149
|
|
|
|
150
|
|
|
return self.stats |
151
|
|
|
|
152
|
|
|
def update_views(self): |
153
|
|
|
"""Update stats views.""" |
154
|
|
|
# Call the father's method |
155
|
|
|
super().update_views() |
156
|
|
|
|
157
|
|
|
# Alert for CPU, MEM and SWAP |
158
|
|
|
for key in self.stats_list: |
159
|
|
|
if key in self.stats: |
160
|
|
|
self.views[key]['decoration'] = self.get_alert(self.stats[key], header=key) |
161
|
|
|
|
162
|
|
|
# Define the list of stats to display |
163
|
|
|
self.views['list'] = self.stats_list |
164
|
|
|
|
165
|
|
|
def msg_curse(self, args=None, max_width=10): |
166
|
|
|
"""Return the list to display in the UI.""" |
167
|
|
|
# Init the return message |
168
|
|
|
ret = [] |
169
|
|
|
|
170
|
|
|
# Only process if stats exist... |
171
|
|
|
if not self.stats or self.is_disabled(): |
172
|
|
|
return ret |
173
|
|
|
|
174
|
|
|
if not max_width: |
175
|
|
|
# No max_width defined, return an empty message |
176
|
|
|
logger.debug(f"No max_width defined for the {self.plugin_name} plugin, it will not be displayed.") |
177
|
|
|
return ret |
178
|
|
|
|
179
|
|
|
# Define the data: Bar (default behavior) or Sparkline |
180
|
|
|
data = {} |
181
|
|
|
for key in self.stats_list: |
182
|
|
|
if self.args.sparkline and self.history_enable() and not self.args.client: |
183
|
|
|
data[key] = Sparkline(max_width) |
184
|
|
|
else: |
185
|
|
|
# Fallback to bar if Sparkline module is not installed |
186
|
|
|
data[key] = Bar(max_width, bar_char=self.get_conf_value('bar_char', default=['|'])[0]) |
187
|
|
|
|
188
|
|
|
# Build the string message |
189
|
|
|
########################## |
190
|
|
|
|
191
|
|
|
# System information |
192
|
|
|
if ( |
193
|
|
|
'cpu_hz_current' in self.stats |
194
|
|
|
and self.stats['cpu_hz_current'] is not None |
195
|
|
|
and 'cpu_hz' in self.stats |
196
|
|
|
and self.stats['cpu_hz'] is not None |
197
|
|
|
): |
198
|
|
|
msg_freq = ' {:.2f}/{:.2f}GHz'.format( |
199
|
|
|
self._hz_to_ghz(self.stats['cpu_hz_current']), self._hz_to_ghz(self.stats['cpu_hz']) |
200
|
|
|
) |
201
|
|
|
else: |
202
|
|
|
msg_freq = '' |
203
|
|
|
|
204
|
|
|
if 'cpu_name' in self.stats and (max_width - len(msg_freq) + 7) > 0: |
205
|
|
|
msg_name = '{:{width}}'.format(self.stats['cpu_name'], width=max_width - len(msg_freq) + 7) |
206
|
|
|
else: |
207
|
|
|
msg_name = '' if msg_freq == '' else 'Frequency' |
208
|
|
|
|
209
|
|
|
ret.append(self.curse_add_line(msg_name)) |
210
|
|
|
ret.append(self.curse_add_line(msg_freq)) |
211
|
|
|
ret.append(self.curse_new_line()) |
212
|
|
|
|
213
|
|
|
# Loop over CPU, MEM and LOAD |
214
|
|
|
for key in self.stats_list: |
215
|
|
|
if key == 'cpu' and args.percpu: |
216
|
|
|
ret.extend(self._msg_per_cpu(data, key, max_width)) |
217
|
|
|
else: |
218
|
|
|
if type(data[key]).__name__ == 'Sparkline': |
219
|
|
|
# Sparkline display an history |
220
|
|
|
data[key].percents = [i[1] for i in self.get_raw_history(item=key, nb=data[key].size)] |
221
|
|
|
# A simple padding in order to align metrics to the right |
222
|
|
|
data[key].percents += [None] * (data[key].size - len(data[key].percents)) |
223
|
|
|
else: |
224
|
|
|
# Bar only the last value |
225
|
|
|
data[key].percent = self.stats[key] |
226
|
|
|
msg = f'{key.upper():4} ' |
227
|
|
|
ret.extend(self._msg_create_line(msg, data[key], key)) |
228
|
|
|
ret.append(self.curse_new_line()) |
229
|
|
|
|
230
|
|
|
# Remove the last new line |
231
|
|
|
ret.pop() |
232
|
|
|
|
233
|
|
|
# Return the message with decoration |
234
|
|
|
return ret |
235
|
|
|
|
236
|
|
|
def _msg_per_cpu(self, data, key, max_width): |
237
|
|
|
"""Create per-cpu view""" |
238
|
|
|
ret = [] |
239
|
|
|
|
240
|
|
|
# Get history (only used with the sparkline option) |
241
|
|
|
if type(data[key]).__name__ == 'Sparkline': |
242
|
|
|
raw_cpu = self.get_raw_history(item='percpu', nb=data[key].size) |
243
|
|
|
|
244
|
|
|
# Manage the maximum number of CPU to display (related to enhancement request #2734) |
245
|
|
|
if len(self.stats['percpu']) > self.max_cpu_display: |
246
|
|
|
# If the number of CPU is > max_cpu_display then sort and display top 'n' |
247
|
|
|
percpu_list = sorted(self.stats['percpu'], key=lambda x: x['total'], reverse=True) |
248
|
|
|
else: |
249
|
|
|
percpu_list = self.stats['percpu'] |
250
|
|
|
|
251
|
|
|
# Display the first max_cpu_display CPU |
252
|
|
|
for cpu in percpu_list[0 : self.max_cpu_display]: |
253
|
|
|
cpu_id = cpu[cpu['key']] |
254
|
|
|
if type(data[key]).__name__ == 'Sparkline': |
255
|
|
|
# Sparkline display an history |
256
|
|
|
data[key].percents = [i[1][cpu_id]['total'] for i in raw_cpu] |
|
|
|
|
257
|
|
|
# A simple padding in order to align metrics to the right |
258
|
|
|
data[key].percents += [None] * (data[key].size - len(data[key].percents)) |
259
|
|
|
else: |
260
|
|
|
# Bar will only display the last value |
261
|
|
|
data[key].percent = cpu['total'] |
262
|
|
|
if cpu_id < 10: |
263
|
|
|
msg = f'{key.upper():3}{cpu_id} ' |
264
|
|
|
else: |
265
|
|
|
msg = f'{cpu_id:4} ' |
266
|
|
|
ret.extend(self._msg_create_line(msg, data[key], key)) |
267
|
|
|
ret.append(self.curse_new_line()) |
268
|
|
|
|
269
|
|
|
# Add a new line with sum of all others CPU |
270
|
|
|
if len(self.stats['percpu']) > self.max_cpu_display: |
271
|
|
|
if type(data[key]).__name__ == 'Sparkline': |
272
|
|
|
sum_other = Sparkline(max_width) |
273
|
|
|
# Sparkline display an history |
274
|
|
|
sum_other.percents = [ |
275
|
|
|
sum([i['total'] for i in r[1] if i[i['key']] >= self.max_cpu_display]) |
276
|
|
|
/ len([i['total'] for i in r[1] if i[i['key']] >= self.max_cpu_display]) |
277
|
|
|
for r in raw_cpu |
278
|
|
|
] |
279
|
|
|
# A simple padding in order to align metrics to the right |
280
|
|
|
sum_other.percents += [None] * (sum_other.size - len(sum_other.percents)) |
281
|
|
|
else: |
282
|
|
|
# Bar will only display the last value |
283
|
|
|
sum_other = Bar(max_width, bar_char=self.get_conf_value('bar_char', default=['|'])[0]) |
284
|
|
|
sum_other.percent = sum([i['total'] for i in percpu_list[self.max_cpu_display :]]) / len( |
285
|
|
|
percpu_list[self.max_cpu_display :] |
286
|
|
|
) |
287
|
|
|
msg = msg = f'{key.upper():3}* ' |
288
|
|
|
ret.extend(self._msg_create_line(msg, sum_other, key)) |
289
|
|
|
ret.append(self.curse_new_line()) |
290
|
|
|
|
291
|
|
|
return ret |
292
|
|
|
|
293
|
|
|
def _msg_create_line(self, msg, data, key): |
294
|
|
|
"""Create a new line to the Quick view.""" |
295
|
|
|
return [ |
296
|
|
|
self.curse_add_line(msg), |
297
|
|
|
self.curse_add_line(data.pre_char, decoration='BOLD'), |
298
|
|
|
self.curse_add_line(data.get(), self.get_views(key=key, option='decoration')), |
299
|
|
|
self.curse_add_line(data.post_char, decoration='BOLD'), |
300
|
|
|
self.curse_add_line(' '), |
301
|
|
|
] |
302
|
|
|
|
303
|
|
|
def _hz_to_ghz(self, hz): |
304
|
|
|
"""Convert Hz to Ghz.""" |
305
|
|
|
return hz / 1000000000.0 |
306
|
|
|
|
307
|
|
|
def _mhz_to_hz(self, hz): |
308
|
|
|
"""Convert Mhz to Hz.""" |
309
|
|
|
return hz * 1000000.0 |
310
|
|
|
|