|
1
|
|
|
# |
|
2
|
|
|
# This file is part of Glances. |
|
3
|
|
|
# |
|
4
|
|
|
# SPDX-FileCopyrightText: 2022 Nicolas Hennion <[email protected]> |
|
5
|
|
|
# |
|
6
|
|
|
# SPDX-License-Identifier: LGPL-3.0-only |
|
7
|
|
|
# |
|
8
|
|
|
|
|
9
|
|
|
"""Curses browser interface class .""" |
|
10
|
|
|
|
|
11
|
|
|
import curses |
|
12
|
|
|
import math |
|
13
|
|
|
|
|
14
|
|
|
from glances.logger import logger |
|
15
|
|
|
from glances.outputs.glances_curses import _GlancesCurses |
|
16
|
|
|
from glances.timer import Timer |
|
17
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
class GlancesCursesBrowser(_GlancesCurses): |
|
20
|
|
|
"""Class for the Glances curse client browser.""" |
|
21
|
|
|
|
|
22
|
|
|
def __init__(self, args=None): |
|
23
|
|
|
"""Init the father class.""" |
|
24
|
|
|
super().__init__(args=args) |
|
25
|
|
|
|
|
26
|
|
|
_colors_list = { |
|
27
|
|
|
'UNKNOWN': self.colors_list['DEFAULT'], |
|
28
|
|
|
'SNMP': self.colors_list['OK'], |
|
29
|
|
|
'ONLINE': self.colors_list['OK'], |
|
30
|
|
|
'OFFLINE': self.colors_list['CRITICAL'], |
|
31
|
|
|
'PROTECTED': self.colors_list['WARNING'], |
|
32
|
|
|
} |
|
33
|
|
|
self.colors_list.update(_colors_list) |
|
34
|
|
|
|
|
35
|
|
|
# First time scan tag |
|
36
|
|
|
# Used to display a specific message when the browser is started |
|
37
|
|
|
self.first_scan = True |
|
38
|
|
|
|
|
39
|
|
|
# Init refresh time |
|
40
|
|
|
self.__refresh_time = args.time |
|
41
|
|
|
|
|
42
|
|
|
# Init the cursor position for the client browser |
|
43
|
|
|
self.cursor_position = 0 |
|
44
|
|
|
|
|
45
|
|
|
# Active Glances server number |
|
46
|
|
|
self._active_server = None |
|
47
|
|
|
|
|
48
|
|
|
self._current_page = 0 |
|
49
|
|
|
self._page_max = 0 |
|
50
|
|
|
self._page_max_lines = 0 |
|
51
|
|
|
|
|
52
|
|
|
self.is_end = False |
|
53
|
|
|
self._revesed_sorting = False |
|
54
|
|
|
self._stats_list = None |
|
55
|
|
|
|
|
56
|
|
|
@property |
|
57
|
|
|
def active_server(self): |
|
58
|
|
|
"""Return the active server or None if it's the browser list.""" |
|
59
|
|
|
return self._active_server |
|
60
|
|
|
|
|
61
|
|
|
@active_server.setter |
|
62
|
|
|
def active_server(self, index): |
|
63
|
|
|
"""Set the active server or None if no server selected.""" |
|
64
|
|
|
self._active_server = index |
|
65
|
|
|
|
|
66
|
|
|
@property |
|
67
|
|
|
def cursor(self): |
|
68
|
|
|
"""Get the cursor position.""" |
|
69
|
|
|
return self.cursor_position |
|
70
|
|
|
|
|
71
|
|
|
@cursor.setter |
|
72
|
|
|
def cursor(self, position): |
|
73
|
|
|
"""Set the cursor position.""" |
|
74
|
|
|
self.cursor_position = position |
|
75
|
|
|
|
|
76
|
|
|
def get_pagelines(self, stats): |
|
77
|
|
|
if self._current_page == self._page_max - 1: |
|
78
|
|
|
page_lines = len(stats) % self._page_max_lines |
|
79
|
|
|
else: |
|
80
|
|
|
page_lines = self._page_max_lines |
|
81
|
|
|
return page_lines |
|
82
|
|
|
|
|
83
|
|
|
def _get_status_count(self, stats): |
|
84
|
|
|
counts = {} |
|
85
|
|
|
for item in stats: |
|
86
|
|
|
color = item['status'] |
|
87
|
|
|
counts[color] = counts.get(color, 0) + 1 |
|
88
|
|
|
|
|
89
|
|
|
result = '' |
|
90
|
|
|
for key in counts.keys(): |
|
91
|
|
|
result += key + ': ' + str(counts[key]) + ' ' |
|
92
|
|
|
|
|
93
|
|
|
return result |
|
94
|
|
|
|
|
95
|
|
|
def _get_stats(self, stats): |
|
96
|
|
|
stats_list = None |
|
97
|
|
|
if self._stats_list is not None: |
|
98
|
|
|
stats_list = self._stats_list |
|
99
|
|
|
stats_list.sort( |
|
100
|
|
|
reverse=self._revesed_sorting, |
|
101
|
|
|
key=lambda x: {'UNKNOWN': 0, 'OFFLINE': 1, 'PROTECTED': 2, 'SNMP': 3, 'ONLINE': 4}.get(x['status'], 99), |
|
102
|
|
|
) |
|
103
|
|
|
else: |
|
104
|
|
|
stats_list = stats |
|
105
|
|
|
|
|
106
|
|
|
return stats_list |
|
107
|
|
|
|
|
108
|
|
|
def cursor_up(self, stats): |
|
109
|
|
|
"""Set the cursor to position N-1 in the list.""" |
|
110
|
|
|
if 0 <= self.cursor_position - 1: |
|
111
|
|
|
self.cursor_position -= 1 |
|
112
|
|
|
else: |
|
113
|
|
|
if self._current_page - 1 < 0: |
|
114
|
|
|
self._current_page = self._page_max - 1 |
|
115
|
|
|
self.cursor_position = (len(stats) - 1) % self._page_max_lines |
|
116
|
|
|
else: |
|
117
|
|
|
self._current_page -= 1 |
|
118
|
|
|
self.cursor_position = self._page_max_lines - 1 |
|
119
|
|
|
|
|
120
|
|
|
def cursor_down(self, stats): |
|
121
|
|
|
"""Set the cursor to position N-1 in the list.""" |
|
122
|
|
|
|
|
123
|
|
|
if self.cursor_position + 1 < self.get_pagelines(stats): |
|
124
|
|
|
self.cursor_position += 1 |
|
125
|
|
|
else: |
|
126
|
|
|
if self._current_page + 1 < self._page_max: |
|
127
|
|
|
self._current_page += 1 |
|
128
|
|
|
else: |
|
129
|
|
|
self._current_page = 0 |
|
130
|
|
|
self.cursor_position = 0 |
|
131
|
|
|
|
|
132
|
|
|
def cursor_pageup(self, stats): |
|
133
|
|
|
"""Set prev page.""" |
|
134
|
|
|
if self._current_page - 1 < 0: |
|
135
|
|
|
self._current_page = self._page_max - 1 |
|
136
|
|
|
else: |
|
137
|
|
|
self._current_page -= 1 |
|
138
|
|
|
self.cursor_position = 0 |
|
139
|
|
|
|
|
140
|
|
|
def cursor_pagedown(self, stats): |
|
141
|
|
|
"""Set next page.""" |
|
142
|
|
|
if self._current_page + 1 < self._page_max: |
|
143
|
|
|
self._current_page += 1 |
|
144
|
|
|
else: |
|
145
|
|
|
self._current_page = 0 |
|
146
|
|
|
self.cursor_position = 0 |
|
147
|
|
|
|
|
148
|
|
|
def __catch_key(self, stats): |
|
149
|
|
|
# Catch the browser pressed key |
|
150
|
|
|
self.pressedkey = self.get_key(self.term_window) |
|
151
|
|
|
refresh = False |
|
152
|
|
|
if self.pressedkey != -1: |
|
153
|
|
|
logger.debug(f"Key pressed. Code={self.pressedkey}") |
|
154
|
|
|
|
|
155
|
|
|
# Actions... |
|
156
|
|
|
if self.pressedkey == ord('\x1b') or self.pressedkey == ord('q'): |
|
157
|
|
|
# 'ESC'|'q' > Quit |
|
158
|
|
|
self.end() |
|
159
|
|
|
logger.info("Stop Glances client browser") |
|
160
|
|
|
# sys.exit(0) |
|
161
|
|
|
self.is_end = True |
|
162
|
|
|
elif self.pressedkey == 10: |
|
163
|
|
|
# 'ENTER' > Run Glances on the selected server |
|
164
|
|
|
self.active_server = self._current_page * self._page_max_lines + self.cursor_position |
|
165
|
|
|
logger.debug(f"Server {self.active_server}/{len(stats)} selected") |
|
166
|
|
|
elif self.pressedkey == curses.KEY_UP or self.pressedkey == 65: |
|
167
|
|
|
# 'UP' > Up in the server list |
|
168
|
|
|
self.cursor_up(stats) |
|
169
|
|
|
logger.debug(f"Server {self.cursor + 1}/{len(stats)} selected") |
|
170
|
|
|
elif self.pressedkey == curses.KEY_DOWN or self.pressedkey == 66: |
|
171
|
|
|
# 'DOWN' > Down in the server list |
|
172
|
|
|
self.cursor_down(stats) |
|
173
|
|
|
logger.debug(f"Server {self.cursor + 1}/{len(stats)} selected") |
|
174
|
|
|
elif self.pressedkey == curses.KEY_PPAGE: |
|
175
|
|
|
# 'Page UP' > Prev page in the server list |
|
176
|
|
|
self.cursor_pageup(stats) |
|
177
|
|
|
logger.debug(f"PageUP: Server ({self._current_page + 1}/{self._page_max}) pages.") |
|
178
|
|
|
elif self.pressedkey == curses.KEY_NPAGE: |
|
179
|
|
|
# 'Page Down' > Next page in the server list |
|
180
|
|
|
self.cursor_pagedown(stats) |
|
181
|
|
|
logger.debug(f"PageDown: Server {self._current_page + 1}/{self._page_max} pages") |
|
182
|
|
|
elif self.pressedkey == ord('1'): |
|
183
|
|
|
self._stats_list = None |
|
184
|
|
|
refresh = True |
|
185
|
|
|
elif self.pressedkey == ord('2'): |
|
186
|
|
|
self._revesed_sorting = False |
|
187
|
|
|
self._stats_list = stats.copy() |
|
188
|
|
|
refresh = True |
|
189
|
|
|
elif self.pressedkey == ord('3'): |
|
190
|
|
|
self._revesed_sorting = True |
|
191
|
|
|
self._stats_list = stats.copy() |
|
192
|
|
|
refresh = True |
|
193
|
|
|
|
|
194
|
|
|
if refresh: |
|
195
|
|
|
self._current_page = 0 |
|
196
|
|
|
self.cursor_position = 0 |
|
197
|
|
|
self.flush(stats) |
|
198
|
|
|
|
|
199
|
|
|
# Return the key code |
|
200
|
|
|
return self.pressedkey |
|
201
|
|
|
|
|
202
|
|
|
def update(self, stats, duration=3, cs_status=None, return_to_browser=False): |
|
203
|
|
|
"""Update the servers' list screen. |
|
204
|
|
|
|
|
205
|
|
|
Wait for __refresh_time sec / catch key every 100 ms. |
|
206
|
|
|
|
|
207
|
|
|
:param stats: Dict of dict with servers stats |
|
208
|
|
|
:param cs_status: |
|
209
|
|
|
:param duration: |
|
210
|
|
|
:param return_to_browser: |
|
211
|
|
|
""" |
|
212
|
|
|
# Flush display |
|
213
|
|
|
logger.debug(f'Servers list: {stats}') |
|
214
|
|
|
self.flush(stats) |
|
215
|
|
|
|
|
216
|
|
|
# Wait |
|
217
|
|
|
exitkey = False |
|
218
|
|
|
countdown = Timer(self.__refresh_time) |
|
219
|
|
|
while not countdown.finished() and not exitkey: |
|
220
|
|
|
# Getkey |
|
221
|
|
|
pressedkey = self.__catch_key(stats) |
|
222
|
|
|
# Is it an exit or select server key ? |
|
223
|
|
|
exitkey = pressedkey == ord('\x1b') or pressedkey == ord('q') or pressedkey == 10 |
|
224
|
|
|
if not exitkey and pressedkey > -1: |
|
225
|
|
|
# Redraw display |
|
226
|
|
|
self.flush(stats) |
|
227
|
|
|
# Wait 100ms... |
|
228
|
|
|
self.wait() |
|
229
|
|
|
|
|
230
|
|
|
return self.active_server |
|
231
|
|
|
|
|
232
|
|
|
def flush(self, stats): |
|
233
|
|
|
"""Update the servers' list screen. |
|
234
|
|
|
|
|
235
|
|
|
:param stats: List of dict with servers stats |
|
236
|
|
|
""" |
|
237
|
|
|
self.erase() |
|
238
|
|
|
self.display(stats) |
|
239
|
|
|
|
|
240
|
|
|
def display(self, stats, cs_status=None): |
|
241
|
|
|
"""Display the servers list. |
|
242
|
|
|
|
|
243
|
|
|
:return: True if the stats have been displayed else False (no server available) |
|
244
|
|
|
""" |
|
245
|
|
|
# Init the internal line/column for Glances Curses |
|
246
|
|
|
self.init_line_column() |
|
247
|
|
|
|
|
248
|
|
|
# Get the current screen size |
|
249
|
|
|
screen_x = self.screen.getmaxyx()[1] |
|
250
|
|
|
screen_y = self.screen.getmaxyx()[0] |
|
251
|
|
|
stats_max = screen_y - 3 |
|
252
|
|
|
self._page_max_lines = stats_max |
|
253
|
|
|
self._page_max = int(math.ceil(len(stats) / stats_max)) |
|
254
|
|
|
|
|
255
|
|
|
# Display header |
|
256
|
|
|
x, y = self.__display_header(stats, 0, 0, screen_x, screen_y) |
|
257
|
|
|
|
|
258
|
|
|
# Display Glances server list |
|
259
|
|
|
# ================================ |
|
260
|
|
|
return self.__display_server_list(stats, x, y, screen_x, screen_y) |
|
261
|
|
|
|
|
262
|
|
|
def __display_header(self, stats, x, y, screen_x, screen_y): |
|
263
|
|
|
stats_len = len(stats) |
|
264
|
|
|
stats_max = screen_y - 3 |
|
265
|
|
|
if stats_len == 0: |
|
266
|
|
|
if self.first_scan and not self.args.disable_autodiscover: |
|
267
|
|
|
msg = 'Glances is scanning your network. Please wait...' |
|
268
|
|
|
self.first_scan = False |
|
269
|
|
|
else: |
|
270
|
|
|
msg = 'No Glances server available' |
|
271
|
|
|
elif len(stats) == 1: |
|
272
|
|
|
msg = 'One Glances server available' |
|
273
|
|
|
else: |
|
274
|
|
|
msg = f'{stats_len} Glances servers available' |
|
275
|
|
|
# if self.args.disable_autodiscover: |
|
276
|
|
|
# msg += ' (auto discover is disabled)' |
|
277
|
|
|
if screen_y > 1: |
|
278
|
|
|
self.term_window.addnstr(y, x, msg, screen_x - x, self.colors_list['TITLE']) |
|
279
|
|
|
|
|
280
|
|
|
msg = f'{self._get_status_count(stats)}' |
|
281
|
|
|
self.term_window.addnstr(y + 1, x, msg, screen_x - x) |
|
282
|
|
|
|
|
283
|
|
|
if stats_len > stats_max and screen_y > 2: |
|
284
|
|
|
page_lines = self.get_pagelines(stats) |
|
285
|
|
|
status_count = self._get_status_count(stats) |
|
286
|
|
|
msg = f'{page_lines} servers displayed.({self._current_page + 1}/{self._page_max}) {status_count}' |
|
287
|
|
|
self.term_window.addnstr(y + 1, x, msg, screen_x - x) |
|
288
|
|
|
|
|
289
|
|
|
return x, y |
|
290
|
|
|
|
|
291
|
|
|
def __build_column_def(self, current_page): |
|
292
|
|
|
"""Define the column and it size to display in the browser""" |
|
293
|
|
|
column_def = {'name': 16, 'ip': 15, 'status': 9, 'protocol': 8} |
|
294
|
|
|
|
|
295
|
|
|
# Add dynamic columns |
|
296
|
|
|
for server_stat in current_page: |
|
297
|
|
|
for k, v in server_stat.items(): |
|
298
|
|
|
if k.endswith('_decoration'): |
|
299
|
|
|
column_def[k.split('_decoration')[0]] = 6 |
|
300
|
|
|
return column_def |
|
301
|
|
|
|
|
302
|
|
|
def __display_server_list(self, stats, x, y, screen_x, screen_y): |
|
303
|
|
|
if not stats: |
|
304
|
|
|
# No server to display |
|
305
|
|
|
return False |
|
306
|
|
|
|
|
307
|
|
|
stats_list = self._get_stats(stats) |
|
308
|
|
|
start_line = self._page_max_lines * self._current_page |
|
309
|
|
|
end_line = start_line + self.get_pagelines(stats_list) |
|
310
|
|
|
current_page = stats_list[start_line:end_line] |
|
311
|
|
|
column_def = self.__build_column_def(current_page) |
|
312
|
|
|
|
|
313
|
|
|
# Display table header |
|
314
|
|
|
stats_max = screen_y - 3 |
|
315
|
|
|
y = 2 |
|
316
|
|
|
xc = x + 2 |
|
317
|
|
|
# First line (plugin name) |
|
318
|
|
|
for k, v in column_def.items(): |
|
319
|
|
|
k_split = k.split('_') |
|
320
|
|
|
if len(k_split) == 1: |
|
321
|
|
|
xc += v + self.space_between_column |
|
322
|
|
|
continue |
|
323
|
|
|
if xc < screen_x and y < screen_y and v is not None: |
|
324
|
|
|
self.term_window.addnstr(y, xc, k_split[0].upper(), screen_x - x, self.colors_list['BOLD']) |
|
325
|
|
|
xc += v + self.space_between_column |
|
326
|
|
|
xc = x + 2 |
|
327
|
|
|
y += 1 |
|
328
|
|
|
# Second line (for item/key) |
|
329
|
|
|
for k, v in column_def.items(): |
|
330
|
|
|
k_split = k.split('_') |
|
331
|
|
|
if xc < screen_x and y < screen_y and v is not None: |
|
332
|
|
|
self.term_window.addnstr(y, xc, ' '.join(k_split[1:]).upper(), screen_x - x, self.colors_list['BOLD']) |
|
333
|
|
|
xc += v + self.space_between_column |
|
334
|
|
|
y += 1 |
|
335
|
|
|
|
|
336
|
|
|
# If a servers has been deleted from the list... |
|
337
|
|
|
# ... and if the cursor is in the latest position |
|
338
|
|
|
if self.cursor > len(stats) - 1: |
|
339
|
|
|
# Set the cursor position to the latest item |
|
340
|
|
|
self.cursor = len(stats) - 1 |
|
341
|
|
|
|
|
342
|
|
|
# Display table |
|
343
|
|
|
line = 0 |
|
344
|
|
|
for server_stat in current_page: |
|
345
|
|
|
# Limit the number of displayed server (see issue #1256) |
|
346
|
|
|
if line >= stats_max: |
|
347
|
|
|
continue |
|
348
|
|
|
|
|
349
|
|
|
# Display line for server stats |
|
350
|
|
|
cpt = 0 |
|
351
|
|
|
xc = x |
|
352
|
|
|
|
|
353
|
|
|
# Is the line selected ? |
|
354
|
|
|
if line == self.cursor: |
|
355
|
|
|
# Display cursor |
|
356
|
|
|
self.term_window.addnstr(y, xc, ">", screen_x - xc, self.colors_list['BOLD']) |
|
357
|
|
|
|
|
358
|
|
|
# Display the line |
|
359
|
|
|
xc += 2 |
|
360
|
|
|
for k, v in column_def.items(): |
|
361
|
|
|
if xc < screen_x and y < screen_y: |
|
362
|
|
|
# Display server stats |
|
363
|
|
|
value = server_stat.get(k, '?') |
|
364
|
|
|
if isinstance(value, float): |
|
365
|
|
|
value = round(value, 1) |
|
366
|
|
|
if k == 'name' and 'alias' in server_stat and server_stat['alias'] is not None: |
|
367
|
|
|
value = server_stat['alias'] |
|
368
|
|
|
decoration = self.colors_list.get( |
|
369
|
|
|
server_stat[k + '_decoration'].replace('_LOG', '') |
|
370
|
|
|
if k + '_decoration' in server_stat |
|
371
|
|
|
else self.colors_list[server_stat['status']], |
|
372
|
|
|
self.colors_list['DEFAULT'], |
|
373
|
|
|
) |
|
374
|
|
|
if k == 'status': |
|
375
|
|
|
decoration = self.colors_list[server_stat['status']] |
|
376
|
|
|
self.term_window.addnstr(y, xc, format(value), v, decoration) |
|
377
|
|
|
xc += v + self.space_between_column |
|
378
|
|
|
cpt += 1 |
|
379
|
|
|
# Next line, next server... |
|
380
|
|
|
y += 1 |
|
381
|
|
|
line += 1 |
|
382
|
|
|
|
|
383
|
|
|
return True |
|
384
|
|
|
|