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