Test Failed
Push — develop ( 66c9ff...e21229 )
by Nicolas
05:06
created

glances/exports/glances_riemann.py (9 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
"""Riemann interface class."""
21
22
import socket
0 ignored issues
show
import missing from __future__ import absolute_import
Loading history...
23
import sys
0 ignored issues
show
import missing from __future__ import absolute_import
Loading history...
24
from numbers import Number
0 ignored issues
show
import missing from __future__ import absolute_import
Loading history...
25
26
from glances.compat import range
27
from glances.logger import logger
0 ignored issues
show
import missing from __future__ import absolute_import
Loading history...
28
from glances.exports.glances_export import GlancesExport
0 ignored issues
show
import missing from __future__ import absolute_import
Loading history...
29
30
# Import bernhard for Riemann
31
import bernhard
0 ignored issues
show
import missing from __future__ import absolute_import
Loading history...
32
33
34
class Export(GlancesExport):
35
36
    """This class manages the Riemann export module."""
37
38
    def __init__(self, config=None, args=None):
39
        """Init the Riemann export IF."""
40
        super(Export, self).__init__(config=config, args=args)
41
42
        # Mandatories configuration keys (additional to host and port)
43
        # N/A
44
45
        # Optionals configuration keys
46
        # N/A
47
48
        # Load the Riemann configuration
49
        self.export_enable = self.load_conf('riemann',
50
                                            mandatories=['host', 'port'],
51
                                            options=[])
52
        if not self.export_enable:
53
            sys.exit(2)
54
55
        # Get the current hostname
56
        self.hostname = socket.gethostname()
57
58
        # Init the Riemann client
59
        self.client = self.init()
60
61
    def init(self):
62
        """Init the connection to the Riemann server."""
63
        if not self.export_enable:
64
            return None
65
        try:
66
            client = bernhard.Client(host=self.host, port=self.port)
67
            return client
68
        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...
69
            logger.critical("Connection to Riemann failed : %s " % e)
0 ignored issues
show
Coding Style Best Practice introduced by
Specify string format arguments as logging function parameters
Loading history...
70
            return None
71
72
    def export(self, name, columns, points):
73
        """Write the points in Riemann."""
74
        for i in range(len(columns)):
75
            if not isinstance(points[i], Number):
76
                continue
77
            else:
78
                data = {'host': self.hostname, 'service': name + " " + columns[i], 'metric': points[i]}
79
                logger.debug(data)
80
                try:
81
                    self.client.send(data)
82
                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...
83
                    logger.error("Cannot export stats to Riemann (%s)" % e)
84