Completed
Push — master ( 2b80fa...6ea077 )
by Nicolas
01:22
created

glances/exports/glances_riemann.py (1 issue)

1
# -*- coding: utf-8 -*-
2
#
3
# This file is part of Glances.
4
#
5
# Copyright (C) 2017 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
23
import sys
24
from numbers import Number
25
26
from glances.compat import range
27
from glances.logger import logger
28
from glances.exports.glances_export import GlancesExport
29
30
# Import bernhard for Riemann
31
import bernhard
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:
69
            logger.critical("Connection to Riemann failed : %s " % e)
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 View Code Duplication
                    self.client.send(data)
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
82
                except Exception as e:
83
                    logger.error("Cannot export stats to Riemann (%s)" % e)
84