Test Failed
Push — master ( aea994...69b639 )
by Nicolas
03:44 queued 10s
created

glances.exports.glances_mqtt.Export.export()   C

Complexity

Conditions 10

Size

Total Lines 46
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 34
nop 4
dl 0
loc 46
rs 5.9999
c 0
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like glances.exports.glances_mqtt.Export.export() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
# -*- coding: utf-8 -*-
2
#
3
# This file is part of Glances.
4
#
5
# Copyright (C) 2021 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
"""MQTT interface class."""
21
22
import socket
23
import string
24
import json
25
26
from glances.logger import logger
27
from glances.exports.glances_export import GlancesExport
28
29
# Import paho for MQTT
30
from requests import certs
31
import paho.mqtt.client as paho
32
33
34
class Export(GlancesExport):
35
36
    """This class manages the MQTT export module."""
37
38
    def __init__(self, config=None, args=None):
39
        """Init the MQTT export IF."""
40
        super(Export, self).__init__(config=config, args=args)
41
42
        # Mandatories configuration keys (additional to host and port)
43
        self.user = None
44
        self.password = None
45
        self.topic = None
46
        self.tls = 'true'
47
48
        # Load the MQTT configuration file
49
        self.export_enable = self.load_conf('mqtt',
50
                                            mandatories=['host', 'password'],
51
                                            options=['port', 'user', 'topic', 'tls', 'topic_structure'])
52
        if not self.export_enable:
53
            exit('Missing MQTT config')
54
55
        # Get the current hostname
56
        self.hostname = socket.gethostname()
57
58
        self.port = int(self.port) or 8883
59
        self.topic = self.topic or 'glances'
60
        self.user = self.user or 'glances'
61
        self.tls = (self.tls and self.tls.lower() == 'true')
62
63
        self.topic_structure = (self.topic_structure or 'per-metric').lower()
64
        if self.topic_structure not in ['per-metric', 'per-plugin']:
65
            logger.critical("topic_structure must be either 'per-metric' or 'per-plugin'.")
66
            return None
67
68
        # Init the MQTT client
69
        self.client = self.init()
70
71
    def init(self):
72
        """Init the connection to the MQTT server."""
73
        if not self.export_enable:
74
            return None
75
        try:
76
            client = paho.Client(client_id='glances_' + self.hostname,
77
                                 clean_session=False)
78
            client.username_pw_set(username=self.user,
79
                                   password=self.password)
80
            if self.tls:
81
                client.tls_set(certs.where())
82
            client.connect(host=self.host,
83
                           port=self.port)
84
            client.loop_start()
85
            return client
86
        except Exception as e:
87
            logger.critical("Connection to MQTT server failed : %s " % e)
88
            return None
89
90
    def export(self, name, columns, points):
91
        """Write the points in MQTT."""
92
93
        WHITELIST = '_-' + string.ascii_letters + string.digits
94
        SUBSTITUTE = '_'
95
96
        def whitelisted(s,
97
                        whitelist=WHITELIST,
98
                        substitute=SUBSTITUTE):
99
            return ''.join(c if c in whitelist else substitute for c in s)
100
101
        if self.topic_structure == 'per-metric':
102
            for sensor, value in zip(columns, points):
103
                try:
104
                    sensor = [whitelisted(name) for name in sensor.split('.')]
105
                    tobeexport = [self.topic, self.hostname, name]
106
                    tobeexport.extend(sensor)
107
                    topic = '/'.join(tobeexport)
108
109
                    self.client.publish(topic, value)
110
                except Exception as e:
111
                    logger.error("Can not export stats to MQTT server (%s)" % e)
112
        elif self.topic_structure == 'per-plugin':
113
            try:
114
                topic = '/'.join([self.topic, self.hostname, name])
115
                sensor_values = dict(zip(columns, points))
116
117
                # Build the value to output
118
                output_value = dict()
119
                for key in sensor_values:
120
                    split_key = key.split('.')
121
                    
122
                    # Add the parent keys if they don't exist
123
                    current_level = output_value
124
                    for depth in range(len(split_key) - 1):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable len does not seem to be defined.
Loading history...
125
                        if split_key[depth] not in current_level:
126
                            current_level[split_key[depth]] = dict()
127
                        current_level = current_level[split_key[depth]]
128
                        
129
                    # Add the value
130
                    current_level[split_key[len(split_key) - 1]] = sensor_values[key]
131
132
                json_value = json.dumps(output_value)
133
                self.client.publish(topic, json_value)
134
            except Exception as e:
135
                logger.error("Can not export stats to MQTT server (%s)" % e)
136
            
137