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

glances/exports/glances_opentsdb.py (21 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
"""OpenTSDB interface class."""
21
22
import sys
0 ignored issues
show
import missing from __future__ import absolute_import
Loading history...
23
from numbers import Number
0 ignored issues
show
import missing from __future__ import absolute_import
Loading history...
24
25
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...
import missing from __future__ import absolute_import
Loading history...
26
from glances.logger import logger
0 ignored issues
show
import missing from __future__ import absolute_import
Loading history...
27
from glances.exports.glances_export import GlancesExport
0 ignored issues
show
import missing from __future__ import absolute_import
Loading history...
28
29
import potsdb
0 ignored issues
show
import missing from __future__ import absolute_import
Loading history...
Unable to import 'potsdb'
Loading history...
30
31
32
class Export(GlancesExport):
33
34
    """This class manages the OpenTSDB export module."""
35
36 View Code Duplication
    def __init__(self, config=None, args=None):
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
37
        """Init the OpenTSDB export IF."""
38
        super(Export, self).__init__(config=config, args=args)
39
40
        # Mandatories configuration keys (additional to host and port)
41
        # N/A
42
43
        # Optionals configuration keys
44
        self.prefix = None
45
        self.tags = None
46
47
        # Load the InfluxDB configuration file
48
        self.export_enable = self.load_conf('opentsdb',
49
                                            mandatories=['host', 'port'],
50
                                            options=['prefix', 'tags'])
51
        if not self.export_enable:
52
            sys.exit(2)
53
54
        # Default prefix for stats is 'glances'
55
        if self.prefix is None:
56
            self.prefix = 'glances'
57
58
        # Init the OpenTSDB client
59
        self.client = self.init()
60
61
    def init(self):
62
        """Init the connection to the OpenTSDB server."""
63
        if not self.export_enable:
64
            return None
65
66
        try:
67
            db = potsdb.Client(self.host,
0 ignored issues
show
Coding Style Naming introduced by
The name db does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
68
                               port=int(self.port),
69
                               check_host=True)
70
        except Exception as e:
0 ignored issues
show
Catching very general exceptions such as Exception is usually not recommended.

Generally, you would want to handle very specific errors in the exception handler. This ensure that you do not hide other types of errors which should be fixed.

So, unless you specifically plan to handle any error, consider adding a more specific exception.

Loading history...
Coding Style Naming introduced by
The name e does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
71
            logger.critical("Cannot connect to OpenTSDB server %s:%s (%s)" % (self.host, self.port, e))
0 ignored issues
show
This line is too long as per the coding-style (103/80).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
Coding Style Best Practice introduced by
Specify string format arguments as logging function parameters
Loading history...
72
            sys.exit(2)
73
74
        return db
75
76 View Code Duplication
    def export(self, name, columns, points):
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
77
        """Export the stats to the Statsd server."""
78
        for i in range(len(columns)):
79
            if not isinstance(points[i], Number):
80
                continue
81
            stat_name = '{}.{}.{}'.format(self.prefix, name, columns[i])
82
            stat_value = points[i]
83
            tags = self.parse_tags(self.tags)
84
            try:
85
                self.client.send(stat_name, stat_value, **tags)
86
            except Exception as e:
0 ignored issues
show
Catching very general exceptions such as Exception is usually not recommended.

Generally, you would want to handle very specific errors in the exception handler. This ensure that you do not hide other types of errors which should be fixed.

So, unless you specifically plan to handle any error, consider adding a more specific exception.

Loading history...
Coding Style Naming introduced by
The name e does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
87
                logger.error("Can not export stats %s to OpenTSDB (%s)" % (name, e))
0 ignored issues
show
This line is too long as per the coding-style (84/80).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
Coding Style Best Practice introduced by
Specify string format arguments as logging function parameters
Loading history...
88
        logger.debug("Export {} stats to OpenTSDB".format(name))
0 ignored issues
show
Use formatting in logging functions and pass the parameters as arguments
Loading history...
89
90
    def exit(self):
91
        """Close the OpenTSDB export module."""
92
        # Waits for all outstanding metrics to be sent and background thread closes
0 ignored issues
show
This line is too long as per the coding-style (83/80).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
93
        self.client.wait()
94
        # Call the father method
95
        super(Export, self).exit()
96