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
|
|
|
"""Manage the Glances server static list.""" |
10
|
|
|
|
11
|
|
|
from socket import gaierror, gethostbyname |
12
|
|
|
|
13
|
|
|
from glances.logger import logger |
14
|
|
|
|
15
|
|
|
DEFAULT_COLUMNS = "system:hr_name,load:min5,cpu:total,mem:percent" |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
class GlancesStaticServer: |
19
|
|
|
"""Manage the static servers list for the client browser.""" |
20
|
|
|
|
21
|
|
|
_section = "serverlist" |
22
|
|
|
|
23
|
|
|
def __init__(self, config=None, args=None): |
24
|
|
|
# server_list is a list of dict (JSON compliant) |
25
|
|
|
# [ {'key': 'zeroconf name', ip': '172.1.2.3', 'port': 61209, 'cpu': 3, 'mem': 34 ...} ... ] |
26
|
|
|
# Load server list from the Glances configuration file |
27
|
|
|
self._server_list = self.load_server_list(config) |
28
|
|
|
# Load columns to grab/display in the browser mode |
29
|
|
|
self._columns = self.load_columns(config) |
30
|
|
|
|
31
|
|
|
def load_server_list(self, config): |
32
|
|
|
"""Load the server list from the configuration file.""" |
33
|
|
|
server_list = [] |
34
|
|
|
|
35
|
|
|
if config is None: |
36
|
|
|
logger.debug("No configuration file available. Cannot load server list.") |
37
|
|
|
elif not config.has_section(self._section): |
38
|
|
|
logger.warning(f"No [{self._section}] section in the configuration file. Cannot load server list.") |
39
|
|
|
else: |
40
|
|
|
logger.info(f"Start reading the [{self._section}] section in the configuration file") |
41
|
|
|
for i in range(1, 256): |
42
|
|
|
new_server = {} |
43
|
|
|
postfix = f'server_{str(i)}_' |
44
|
|
|
# Read the server name (mandatory) |
45
|
|
|
for s in ['name', 'port', 'alias']: |
46
|
|
|
new_server[s] = config.get_value(self._section, f'{postfix}{s}') |
47
|
|
|
if new_server['name'] is not None: |
48
|
|
|
# Manage optional information |
49
|
|
|
if new_server['port'] is None: |
50
|
|
|
new_server['port'] = '61209' |
51
|
|
|
new_server['username'] = 'glances' |
52
|
|
|
# By default, try empty (aka no) password |
53
|
|
|
new_server['password'] = '' |
54
|
|
|
try: |
55
|
|
|
new_server['ip'] = gethostbyname(new_server['name']) |
56
|
|
|
except gaierror as e: |
57
|
|
|
logger.error("Cannot get IP address for server {} ({})".format(new_server['name'], e)) |
58
|
|
|
continue |
59
|
|
|
new_server['key'] = new_server['name'] + ':' + new_server['port'] |
60
|
|
|
|
61
|
|
|
# Default status is 'UNKNOWN' |
62
|
|
|
new_server['status'] = 'UNKNOWN' |
63
|
|
|
|
64
|
|
|
# Server type is 'STATIC' |
65
|
|
|
new_server['type'] = 'STATIC' |
66
|
|
|
|
67
|
|
|
# Add the server to the list |
68
|
|
|
logger.debug("Add server {} to the static list".format(new_server['name'])) |
69
|
|
|
server_list.append(new_server) |
70
|
|
|
|
71
|
|
|
# Server list loaded |
72
|
|
|
logger.info(f"{len(server_list)} server(s) loaded from the configuration file") |
73
|
|
|
logger.debug(f"Static server list: {server_list}") |
74
|
|
|
|
75
|
|
|
return server_list |
76
|
|
|
|
77
|
|
|
def load_columns(self, config): |
78
|
|
|
"""Load columns definition from the configuration file. |
79
|
|
|
Read: 'system:hr_name,load:min5,cpu:total,mem:percent,sensors:value:Ambient' |
80
|
|
|
Return: [{'plugin': 'system', 'field': 'hr_name'}, |
81
|
|
|
{'plugin': 'load', 'field': 'min5'}, |
82
|
|
|
{'plugin': 'cpu', 'field': 'total'}, |
83
|
|
|
{'plugin': 'mem', 'field': 'percent'}, |
84
|
|
|
{'plugin': 'sensors', 'field': 'value', 'key': 'Ambient'}] |
85
|
|
|
""" |
86
|
|
|
if config is None: |
87
|
|
|
logger.debug("No configuration file available. Cannot load columns definition.") |
88
|
|
|
elif not config.has_section(self._section): |
89
|
|
|
logger.warning(f"No [{self._section}] section in the configuration file. Cannot load columns definition.") |
90
|
|
|
|
91
|
|
|
columns_def = ( |
92
|
|
|
config.get_value(self._section, 'columns') |
93
|
|
|
if config.get_value(self._section, 'columns') |
94
|
|
|
else DEFAULT_COLUMNS |
95
|
|
|
) |
96
|
|
|
|
97
|
|
|
return [dict(zip(['plugin', 'field', 'key'], i.split(':'))) for i in columns_def.split(',')] |
98
|
|
|
|
99
|
|
|
def get_servers_list(self): |
100
|
|
|
"""Return the current server list (list of dict).""" |
101
|
|
|
return self._server_list |
102
|
|
|
|
103
|
|
|
def set_server(self, server_pos, key, value): |
104
|
|
|
"""Set the key to the value for the server_pos (position in the list).""" |
105
|
|
|
self._server_list[server_pos][key] = value |
106
|
|
|
|
107
|
|
|
def get_columns(self): |
108
|
|
|
"""Return the columns definitions""" |
109
|
|
|
return self._columns |
110
|
|
|
|