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 | """Graphite interface class.""" |
||
11 | |||
12 | import sys |
||
13 | from numbers import Number |
||
14 | |||
15 | from glances.logger import logger |
||
16 | from glances.exports.glances_export import GlancesExport |
||
17 | |||
18 | from graphitesend import GraphiteClient |
||
19 | |||
20 | |||
21 | class Export(GlancesExport): |
||
22 | |||
23 | """This class manages the Graphite export module.""" |
||
24 | |||
25 | View Code Duplication | def __init__(self, config=None, args=None): |
|
0 ignored issues
–
show
Duplication
introduced
by
Loading history...
|
|||
26 | """Init the Graphite 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 | # Optional configuration keys |
||
33 | self.debug = False |
||
34 | self.prefix = None |
||
35 | self.system_name = None |
||
36 | |||
37 | # Load the configuration file |
||
38 | self.export_enable = self.load_conf('graphite', mandatories=['host', 'port'], options=['prefix', 'system_name']) |
||
39 | if not self.export_enable: |
||
40 | sys.exit(2) |
||
41 | |||
42 | # Default prefix for stats is 'glances' |
||
43 | if self.prefix is None: |
||
44 | self.prefix = 'glances' |
||
45 | |||
46 | # Convert config option type |
||
47 | self.port = int(self.port) |
||
48 | |||
49 | # Init the Graphite client |
||
50 | self.client = self.init() |
||
51 | |||
52 | def init(self): |
||
53 | """Init the connection to the Graphite server.""" |
||
54 | client = None |
||
55 | |||
56 | if not self.export_enable: |
||
57 | return client |
||
58 | |||
59 | try: |
||
60 | if self.system_name is None: |
||
61 | client = GraphiteClient( |
||
62 | graphite_server=self.host, |
||
63 | graphite_port=self.port, |
||
64 | prefix=self.prefix, |
||
65 | lowercase_metric_names=True, |
||
66 | debug=self.debug, |
||
67 | ) |
||
68 | else: |
||
69 | client = GraphiteClient( |
||
70 | graphite_server=self.host, |
||
71 | graphite_port=self.port, |
||
72 | prefix=self.prefix, |
||
73 | system_name=self.system_name, |
||
74 | lowercase_metric_names=True, |
||
75 | debug=self.debug, |
||
76 | ) |
||
77 | except Exception as e: |
||
78 | logger.error("Can not write data to Graphite server: {}:{} ({})".format(self.host, self.port, e)) |
||
79 | client = None |
||
80 | else: |
||
81 | logger.info("Stats will be exported to Graphite server: {}:{}".format(self.host, self.port)) |
||
82 | return client |
||
83 | |||
84 | def export(self, name, columns, points): |
||
85 | """Export the stats to the Graphite server.""" |
||
86 | if self.client is None: |
||
87 | return False |
||
88 | before_filtering_dict = dict(zip([normalize('{}.{}'.format(name, i)) for i in columns], points)) |
||
89 | after_filtering_dict = dict(filter(lambda i: isinstance(i[1], Number), before_filtering_dict.items())) |
||
90 | try: |
||
91 | self.client.send_dict(after_filtering_dict) |
||
92 | except Exception as e: |
||
93 | logger.error("Can not export stats to Graphite (%s)" % e) |
||
94 | return False |
||
95 | else: |
||
96 | logger.debug("Export {} stats to Graphite".format(name)) |
||
97 | return True |
||
98 | |||
99 | |||
100 | def normalize(name): |
||
101 | """Normalize name for the Graphite convention""" |
||
102 | |||
103 | # Name should not contain space |
||
104 | ret = name.replace(' ', '_') |
||
105 | |||
106 | return ret |
||
107 |