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 |
||
23 | from numbers import Number |
||
24 | |||
25 | from glances.compat import range |
||
26 | from glances.logger import logger |
||
27 | from glances.exports.glances_export import GlancesExport |
||
28 | |||
29 | import potsdb |
||
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
Duplication
introduced
by
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, |
||
68 | port=int(self.port), |
||
69 | check_host=True) |
||
70 | except Exception as e: |
||
71 | logger.critical("Cannot connect to OpenTSDB server %s:%s (%s)" % (self.host, self.port, e)) |
||
72 | sys.exit(2) |
||
73 | |||
74 | return db |
||
75 | |||
76 | View Code Duplication | def export(self, name, columns, points): |
|
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: |
||
87 | logger.error("Can not export stats %s to OpenTSDB (%s)" % (name, e)) |
||
88 | logger.debug("Export {} stats to OpenTSDB".format(name)) |
||
89 | |||
90 | def exit(self): |
||
91 | """Close the OpenTSDB export module.""" |
||
92 | # Waits for all outstanding metrics to be sent and background thread closes |
||
93 | self.client.wait() |
||
94 | # Call the father method |
||
95 | super(Export, self).exit() |
||
96 |