Test Failed
Push — develop ( d0629e...eebab0 )
by Nicolas
03:11
created

GlancesStaticServer.__init__()   A

Complexity

Conditions 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nop 3
dl 0
loc 7
rs 10
c 0
b 0
f 0
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, 'protocol': 'rpc', '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
                # Read the configuration
43
                new_server = {}
44
                postfix = f'server_{str(i)}_'
45
                for s in ['name', 'port', 'alias', 'protocol']:
46
                    new_server[s] = config.get_value(self._section, f'{postfix}{s}')
47
48
                if new_server['name'] is None:
49
                    continue
50
51
                # Type in order to support both RPC and REST servers (see #1121)
52
                if new_server['protocol'] is None:
53
                    new_server['protocol'] = 'rpc'
54
                new_server['protocol'] = new_server['protocol'].lower()
55
                if new_server['protocol'] not in ('rpc', 'rest'):
56
                    logger.error(f'Unknow protocol for {postfix}, skip it.')
57
                    continue
58
59
                # Default port
60
                if new_server['port'] is None:
61
                    new_server['port'] = '61209' if new_server['type'] == 'rpc' else '61208'
62
63
                # By default, try empty (aka no) password
64
                new_server['username'] = 'glances'
65
                new_server['password'] = ''
66
67
                try:
68
                    new_server['ip'] = gethostbyname(new_server['name'])
69
                except gaierror as e:
70
                    logger.error("Cannot get IP address for server {} ({})".format(new_server['name'], e))
71
                    continue
72
                new_server['key'] = new_server['name'] + ':' + new_server['port']
73
74
                # Default status is 'UNKNOWN'
75
                new_server['status'] = 'UNKNOWN'
76
77
                # Server type is 'STATIC'
78
                new_server['type'] = 'STATIC'
79
80
                # Add the server to the list
81
                logger.debug("Add server {} to the static list".format(new_server['name']))
82
                server_list.append(new_server)
83
84
            # Server list loaded
85
            logger.info(f"{len(server_list)} server(s) loaded from the configuration file")
86
            logger.debug(f"Static server list: {server_list}")
87
88
        return server_list
89
90
    def load_columns(self, config):
91
        """Load columns definition from the configuration file.
92
        Read:   'system:hr_name,load:min5,cpu:total,mem:percent,sensors:value:Ambient'
93
        Return: [{'plugin': 'system', 'field': 'hr_name'},
94
                 {'plugin': 'load', 'field': 'min5'},
95
                 {'plugin': 'cpu', 'field': 'total'},
96
                 {'plugin': 'mem', 'field': 'percent'},
97
                 {'plugin': 'sensors', 'field': 'value', 'key': 'Ambient'}]
98
        """
99
        if config is None:
100
            logger.debug("No configuration file available. Cannot load columns definition.")
101
        elif not config.has_section(self._section):
102
            logger.warning(f"No [{self._section}] section in the configuration file. Cannot load columns definition.")
103
104
        columns_def = (
105
            config.get_value(self._section, 'columns')
106
            if config.get_value(self._section, 'columns')
107
            else DEFAULT_COLUMNS
108
        )
109
110
        return [dict(zip(['plugin', 'field', 'key'], i.split(':'))) for i in columns_def.split(',')]
111
112
    def get_servers_list(self):
113
        """Return the current server list (list of dict)."""
114
        return self._server_list
115
116
    def set_server(self, server_pos, key, value):
117
        """Set the key to the value for the server_pos (position in the list)."""
118
        self._server_list[server_pos][key] = value
119
120
    def get_columns(self):
121
        """Return the columns definitions"""
122
        return self._columns
123