1 | # -*- coding: utf-8 -*- |
||
2 | # |
||
3 | # This file is part of Glances. |
||
4 | # |
||
5 | # Copyright (C) 2016 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 NoOptionError, NoSectionError, 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 | # Load the Riemann configuration |
||
43 | self.riemann_host = None |
||
44 | self.riemann_port = None |
||
45 | self.hostname = socket.gethostname() |
||
46 | self.export_enable = self.load_conf() |
||
47 | if not self.export_enable: |
||
48 | sys.exit(2) |
||
49 | |||
50 | # Init the Riemann client |
||
51 | self.client = self.init() |
||
52 | |||
53 | View Code Duplication | def load_conf(self, section="riemann"): |
|
0 ignored issues
–
show
Duplication
introduced
by
Loading history...
|
|||
54 | """Load the Riemann configuration in the Glances configuration file.""" |
||
55 | if self.config is None: |
||
56 | return False |
||
57 | try: |
||
58 | self.riemann_host = self.config.get_value(section, 'host') |
||
59 | self.riemann_port = int(self.config.get_value(section, 'port')) |
||
60 | except NoSectionError: |
||
61 | logger.critical("No riemann configuration found") |
||
62 | return False |
||
63 | except NoOptionError as e: |
||
64 | logger.critical("Error in the Riemann configuration (%s)" % e) |
||
65 | return False |
||
66 | else: |
||
67 | logger.debug("Load Riemann from the Glances configuration file") |
||
68 | return True |
||
69 | |||
70 | def init(self): |
||
71 | """Init the connection to the Riemann server.""" |
||
72 | if not self.export_enable: |
||
73 | return None |
||
74 | try: |
||
75 | client = bernhard.Client(host=self.riemann_host, port=self.riemann_port) |
||
76 | return client |
||
77 | except Exception as e: |
||
78 | logger.critical("Connection to Riemann failed : %s " % e) |
||
79 | return None |
||
80 | |||
81 | View Code Duplication | def export(self, name, columns, points): |
|
82 | """Write the points in Riemann.""" |
||
83 | for i in range(len(columns)): |
||
84 | if not isinstance(points[i], Number): |
||
85 | continue |
||
86 | else: |
||
87 | data = {'host': self.hostname, 'service': name + " " + columns[i], 'metric': points[i]} |
||
88 | logger.debug(data) |
||
89 | try: |
||
90 | self.client.send(data) |
||
91 | except Exception as e: |
||
92 | logger.error("Cannot export stats to Riemann (%s)" % e) |
||
93 |