|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
# |
|
3
|
|
|
# This file is part of Glances. |
|
4
|
|
|
# |
|
5
|
|
|
# Copyright (C) 2021 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
|
|
|
"""Graphite 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
|
|
|
from graphitesend import GraphiteClient |
|
30
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
class Export(GlancesExport): |
|
33
|
|
|
|
|
34
|
|
|
"""This class manages the Graphite export module.""" |
|
35
|
|
|
|
|
36
|
|
|
def __init__(self, config=None, args=None): |
|
37
|
|
|
"""Init the Graphite 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.debug = False |
|
45
|
|
|
self.prefix = None |
|
46
|
|
|
self.system_name = None |
|
47
|
|
|
|
|
48
|
|
|
# Load the configuration file |
|
49
|
|
|
self.export_enable = self.load_conf('graphite', |
|
50
|
|
|
mandatories=['host', |
|
51
|
|
|
'port'], |
|
52
|
|
|
options=['prefix', |
|
53
|
|
|
'system_name']) |
|
54
|
|
|
if not self.export_enable: |
|
55
|
|
|
sys.exit(2) |
|
56
|
|
|
|
|
57
|
|
|
# Default prefix for stats is 'glances' |
|
58
|
|
|
if self.prefix is None: |
|
59
|
|
|
self.prefix = 'glances' |
|
60
|
|
|
|
|
61
|
|
|
# Convert config option type |
|
62
|
|
|
self.port = int(self.port) |
|
63
|
|
|
|
|
64
|
|
|
# Init the Graphite client |
|
65
|
|
|
self.client = self.init() |
|
66
|
|
|
|
|
67
|
|
|
def init(self): |
|
68
|
|
|
"""Init the connection to the Graphite server.""" |
|
69
|
|
|
client = None |
|
70
|
|
|
|
|
71
|
|
|
if not self.export_enable: |
|
72
|
|
|
return client |
|
73
|
|
|
|
|
74
|
|
|
try: |
|
75
|
|
|
if self.system_name is None: |
|
76
|
|
|
client = GraphiteClient(graphite_server=self.host, |
|
77
|
|
|
graphite_port=self.port, |
|
78
|
|
|
prefix=self.prefix, |
|
79
|
|
|
lowercase_metric_names=True, |
|
80
|
|
|
debug=self.debug) |
|
81
|
|
|
else: |
|
82
|
|
|
client = GraphiteClient(graphite_server=self.host, |
|
83
|
|
|
graphite_port=self.port, |
|
84
|
|
|
prefix=self.prefix, |
|
85
|
|
|
system_name=self.system_name, |
|
86
|
|
|
lowercase_metric_names=True, |
|
87
|
|
|
debug=self.debug) |
|
88
|
|
|
except Exception as e: |
|
89
|
|
|
logger.error("Can not write data to Graphite server: {}:{} ({})".format(self.host, |
|
90
|
|
|
self.port, |
|
91
|
|
|
e)) |
|
92
|
|
|
client = None |
|
93
|
|
|
else: |
|
94
|
|
|
logger.info( |
|
95
|
|
|
"Stats will be exported to Graphite server: {}:{}".format(self.host, |
|
96
|
|
|
self.port)) |
|
97
|
|
|
|
|
98
|
|
|
return client |
|
99
|
|
|
|
|
100
|
|
|
def export(self, name, columns, points): |
|
101
|
|
|
"""Export the stats to the Graphite server.""" |
|
102
|
|
|
if self.client is None: |
|
103
|
|
|
return False |
|
104
|
|
|
before_filtering_dict = dict(zip( |
|
105
|
|
|
[normalize('{}.{}'.format(name, i)) for i in columns], |
|
106
|
|
|
points)) |
|
107
|
|
|
after_filtering_dict = dict( |
|
108
|
|
|
filter(lambda i: isinstance(i[1], Number), |
|
109
|
|
|
before_filtering_dict.items())) |
|
110
|
|
|
try: |
|
111
|
|
|
self.client.send_dict(after_filtering_dict) |
|
112
|
|
|
except Exception as e: |
|
113
|
|
|
logger.error("Can not export stats to Graphite (%s)" % e) |
|
114
|
|
|
return False |
|
115
|
|
|
else: |
|
116
|
|
|
logger.debug("Export {} stats to Graphite".format(name)) |
|
117
|
|
|
return True |
|
118
|
|
|
|
|
119
|
|
|
|
|
120
|
|
|
def normalize(name): |
|
121
|
|
|
"""Normalize name for the Graphite convention""" |
|
122
|
|
|
|
|
123
|
|
|
# Name should not contain space |
|
124
|
|
|
ret = name.replace(' ', '_') |
|
125
|
|
|
|
|
126
|
|
|
return ret |
|
127
|
|
|
|