glances.exports.glances_opentsdb.Export.exit()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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