1 | # -*- coding: utf-8 -*- |
||
2 | # |
||
3 | # This file is part of Glances. |
||
4 | # |
||
5 | # SPDX-FileCopyrightText: 2022 Nicolas Hennion <[email protected]> |
||
6 | # |
||
7 | # SPDX-License-Identifier: LGPL-3.0-only |
||
8 | # |
||
9 | |||
10 | """OpenTSDB interface class.""" |
||
11 | |||
12 | import sys |
||
13 | from numbers import Number |
||
14 | |||
15 | from glances.compat import range |
||
16 | from glances.logger import logger |
||
17 | from glances.exports.glances_export import GlancesExport |
||
18 | |||
19 | import potsdb |
||
20 | |||
21 | |||
22 | class Export(GlancesExport): |
||
23 | |||
24 | """This class manages the OpenTSDB export module.""" |
||
25 | |||
26 | View Code Duplication | def __init__(self, config=None, args=None): |
|
0 ignored issues
–
show
Duplication
introduced
by
Loading history...
|
|||
27 | """Init the OpenTSDB export IF.""" |
||
28 | super(Export, self).__init__(config=config, args=args) |
||
29 | |||
30 | # Mandatory configuration keys (additional to host and port) |
||
31 | # N/A |
||
32 | |||
33 | # Optionals configuration keys |
||
34 | self.prefix = None |
||
35 | self.tags = None |
||
36 | |||
37 | # Load the configuration file |
||
38 | self.export_enable = self.load_conf('opentsdb', mandatories=['host', 'port'], options=['prefix', 'tags']) |
||
39 | if not self.export_enable: |
||
40 | exit('Missing OPENTSDB config') |
||
41 | |||
42 | # Default prefix for stats is 'glances' |
||
43 | if self.prefix is None: |
||
44 | self.prefix = 'glances' |
||
45 | |||
46 | # Init the OpenTSDB client |
||
47 | self.client = self.init() |
||
48 | |||
49 | def init(self): |
||
50 | """Init the connection to the OpenTSDB server.""" |
||
51 | if not self.export_enable: |
||
52 | return None |
||
53 | |||
54 | try: |
||
55 | db = potsdb.Client(self.host, port=int(self.port), check_host=True) |
||
56 | except Exception as e: |
||
57 | logger.critical("Cannot connect to OpenTSDB server %s:%s (%s)" % (self.host, self.port, e)) |
||
58 | sys.exit(2) |
||
59 | |||
60 | return db |
||
61 | |||
62 | View Code Duplication | def export(self, name, columns, points): |
|
0 ignored issues
–
show
|
|||
63 | """Export the stats to the Statsd server.""" |
||
64 | for i in range(len(columns)): |
||
65 | if not isinstance(points[i], Number): |
||
66 | continue |
||
67 | stat_name = '{}.{}.{}'.format(self.prefix, name, columns[i]) |
||
68 | stat_value = points[i] |
||
69 | tags = self.parse_tags(self.tags) |
||
70 | try: |
||
71 | self.client.send(stat_name, stat_value, **tags) |
||
72 | except Exception as e: |
||
73 | logger.error("Can not export stats %s to OpenTSDB (%s)" % (name, e)) |
||
74 | logger.debug("Export {} stats to OpenTSDB".format(name)) |
||
75 | |||
76 | def exit(self): |
||
77 | """Close the OpenTSDB export module.""" |
||
78 | # Waits for all outstanding metrics to be sent and background thread closes |
||
79 | self.client.wait() |
||
80 | # Call the father method |
||
81 | super(Export, self).exit() |
||
82 |