Test Failed
Push — develop ( d7cf39...faa4bd )
by Nicolas
04:34 queued 10s
created

glances/static_list.py (7 issues)

1
# -*- coding: utf-8 -*-
2
#
3
# This file is part of Glances.
4
#
5
# Copyright (C) 2019 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
"""Manage the Glances server static list."""
21
22
from socket import gaierror, gethostbyname
23
24
from glances.compat import range
0 ignored issues
show
Bug Best Practice introduced by
This seems to re-define the built-in range.

It is generally discouraged to redefine built-ins as this makes code very hard to read.

Loading history...
25
from glances.logger import logger
26
27
28
class GlancesStaticServer(object):
29
30
    """Manage the static servers list for the client browser."""
31
32
    _section = "serverlist"
33
34
    def __init__(self, config=None, args=None):
35
        # server_list is a list of dict (JSON compliant)
36
        # [ {'key': 'zeroconf name', ip': '172.1.2.3', 'port': 61209, 'cpu': 3, 'mem': 34 ...} ... ]
37
        # Load the configuration file
38
        self._server_list = self.load(config)
39
40
    def load(self, config):
41
        """Load the server list from the configuration file."""
42
        server_list = []
43
44
        if config is None:
45
            logger.debug("No configuration file available. Cannot load server list.")
46
        elif not config.has_section(self._section):
47
            logger.warning("No [%s] section in the configuration file. Cannot load server list." % self._section)
0 ignored issues
show
Coding Style Best Practice introduced by
Specify string format arguments as logging function parameters
Loading history...
48
        else:
49
            logger.info("Start reading the [%s] section in the configuration file" % self._section)
0 ignored issues
show
Coding Style Best Practice introduced by
Specify string format arguments as logging function parameters
Loading history...
50
            for i in range(1, 256):
51
                new_server = {}
52
                postfix = 'server_%s_' % str(i)
53
                # Read the server name (mandatory)
54
                for s in ['name', 'port', 'alias']:
55
                    new_server[s] = config.get_value(self._section, '%s%s' % (postfix, s))
56
                if new_server['name'] is not None:
57
                    # Manage optionnal information
58
                    if new_server['port'] is None:
59
                        new_server['port'] = '61209'
60
                    new_server['username'] = 'glances'
61
                    # By default, try empty (aka no) password
62
                    new_server['password'] = ''
63
                    try:
64
                        new_server['ip'] = gethostbyname(new_server['name'])
65
                    except gaierror as e:
66
                        logger.error("Cannot get IP address for server %s (%s)" % (new_server['name'], e))
0 ignored issues
show
Coding Style Best Practice introduced by
Specify string format arguments as logging function parameters
Loading history...
67
                        continue
68
                    new_server['key'] = new_server['name'] + ':' + new_server['port']
69
70
                    # Default status is 'UNKNOWN'
71
                    new_server['status'] = 'UNKNOWN'
72
73
                    # Server type is 'STATIC'
74
                    new_server['type'] = 'STATIC'
75
76
                    # Add the server to the list
77
                    logger.debug("Add server %s to the static list" % new_server['name'])
0 ignored issues
show
Coding Style Best Practice introduced by
Specify string format arguments as logging function parameters
Loading history...
78
                    server_list.append(new_server)
79
80
            # Server list loaded
81
            logger.info("%s server(s) loaded from the configuration file" % len(server_list))
0 ignored issues
show
Coding Style Best Practice introduced by
Specify string format arguments as logging function parameters
Loading history...
82
            logger.debug("Static server list: %s" % server_list)
0 ignored issues
show
Coding Style Best Practice introduced by
Specify string format arguments as logging function parameters
Loading history...
83
84
        return server_list
85
86
    def get_servers_list(self):
87
        """Return the current server list (list of dict)."""
88
        return self._server_list
89
90
    def set_server(self, server_pos, key, value):
91
        """Set the key to the value for the server_pos (position in the list)."""
92
        self._server_list[server_pos][key] = value
93