1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
# |
3
|
|
|
# This file is part of Glances. |
4
|
|
|
# |
5
|
|
|
# Copyright (C) 2020 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
|
|
|
"""InfluxDB (from to InfluxDB 1.8+) interface class.""" |
21
|
|
|
|
22
|
|
|
import sys |
|
|
|
|
23
|
|
|
|
24
|
|
|
from glances.logger import logger |
|
|
|
|
25
|
|
|
from glances.exports.glances_export import GlancesExport |
|
|
|
|
26
|
|
|
|
27
|
|
|
from influxdb_client import InfluxDBClient, WriteOptions |
|
|
|
|
28
|
|
|
|
29
|
|
|
|
30
|
|
|
class Export(GlancesExport): |
|
|
|
|
31
|
|
|
"""This class manages the InfluxDB export module.""" |
32
|
|
|
|
33
|
|
|
def __init__(self, config=None, args=None): |
34
|
|
|
"""Init the InfluxDB export IF.""" |
35
|
|
|
super(Export, self).__init__(config=config, args=args) |
36
|
|
|
|
37
|
|
|
# Mandatories configuration keys (additional to host and port) |
38
|
|
|
self.org = None |
39
|
|
|
self.bucket = None |
40
|
|
|
self.token = None |
41
|
|
|
|
42
|
|
|
# Optionals configuration keys |
43
|
|
|
self.protocol = 'http' |
44
|
|
|
self.prefix = None |
45
|
|
|
self.tags = None |
46
|
|
|
|
47
|
|
|
# Load the InfluxDB configuration file |
48
|
|
|
self.export_enable = self.load_conf('influxdb2', |
49
|
|
|
mandatories=['host', 'port', |
50
|
|
|
'user', 'password', |
51
|
|
|
'org', 'bucket', 'token'], |
|
|
|
|
52
|
|
|
options=['protocol', |
53
|
|
|
'prefix', |
54
|
|
|
'tags']) |
55
|
|
|
if not self.export_enable: |
56
|
|
|
sys.exit(2) |
57
|
|
|
|
58
|
|
|
# Init the InfluxDB client |
59
|
|
|
self.client = self.init() |
60
|
|
|
|
61
|
|
|
def init(self): |
62
|
|
|
"""Init the connection to the InfluxDB server.""" |
63
|
|
|
if not self.export_enable: |
64
|
|
|
return None |
65
|
|
|
|
66
|
|
|
url = '{}://{}:{}'.format(self.protocol, self.host, self.port) |
67
|
|
|
try: |
68
|
|
|
client = InfluxDBClient(url=url, |
69
|
|
|
enable_gzip=False, |
70
|
|
|
org=self.org, |
|
|
|
|
71
|
|
|
token=self.token) |
72
|
|
|
except Exception as e: |
|
|
|
|
73
|
|
|
logger.critical("Cannot connect to InfluxDB server '%s' (%s)" % (url, e)) |
|
|
|
|
74
|
|
|
sys.exit(2) |
75
|
|
|
else: |
76
|
|
|
logger.info("Connected to InfluxDB server version {} ({})".format(client.health().version, |
|
|
|
|
77
|
|
|
client.health().message)) |
|
|
|
|
78
|
|
|
|
79
|
|
|
|
80
|
|
|
# Create the write client |
81
|
|
|
write_client = client.write_api(write_options=WriteOptions(batch_size=500, |
|
|
|
|
82
|
|
|
flush_interval=10000, |
|
|
|
|
83
|
|
|
jitter_interval=2000, |
|
|
|
|
84
|
|
|
retry_interval=5000, |
|
|
|
|
85
|
|
|
max_retries=5, |
|
|
|
|
86
|
|
|
max_retry_delay=30000, |
|
|
|
|
87
|
|
|
exponential_base=2)) |
|
|
|
|
88
|
|
|
return write_client |
89
|
|
|
|
90
|
|
View Code Duplication |
def _normalize(self, name, columns, points): |
|
|
|
|
91
|
|
|
"""Normalize data for the InfluxDB's data model.""" |
92
|
|
|
|
93
|
|
|
for i, _ in enumerate(points): |
94
|
|
|
# Supported type: |
95
|
|
|
# https://docs.influxdata.com/influxdb/v2.0/reference/syntax/line-protocol/ |
96
|
|
|
if points[i] is None: |
97
|
|
|
# Ignore points with None value |
98
|
|
|
del(points[i]) |
|
|
|
|
99
|
|
|
del(columns[i]) |
|
|
|
|
100
|
|
|
continue |
101
|
|
|
try: |
102
|
|
|
points[i] = float(points[i]) |
103
|
|
|
except (TypeError, ValueError): |
104
|
|
|
pass |
105
|
|
|
else: |
106
|
|
|
continue |
107
|
|
|
try: |
108
|
|
|
points[i] = str(points[i]) |
109
|
|
|
except (TypeError, ValueError): |
110
|
|
|
pass |
111
|
|
|
else: |
112
|
|
|
continue |
113
|
|
|
|
114
|
|
|
return [{'measurement': name, |
115
|
|
|
'tags': self.parse_tags(self.tags), |
116
|
|
|
'fields': dict(zip(columns, points))}] |
117
|
|
|
|
118
|
|
View Code Duplication |
def export(self, name, columns, points): |
|
|
|
|
119
|
|
|
"""Write the points to the InfluxDB server.""" |
120
|
|
|
# Manage prefix |
121
|
|
|
if self.prefix is not None: |
122
|
|
|
name = self.prefix + '.' + name |
123
|
|
|
# Write input to the InfluxDB database |
124
|
|
|
if len(points) == 0: |
|
|
|
|
125
|
|
|
logger.debug("Cannot export empty {} stats to InfluxDB".format(name)) |
|
|
|
|
126
|
|
|
else: |
127
|
|
|
try: |
128
|
|
|
self.client.write(self.bucket, |
129
|
|
|
self.org, |
130
|
|
|
self._normalize(name, columns, points), |
|
|
|
|
131
|
|
|
time_precision="s") |
132
|
|
|
except Exception as e: |
|
|
|
|
133
|
|
|
# Log level set to debug instead of error (see: issue #1561) |
134
|
|
|
logger.debug("Cannot export {} stats to InfluxDB ({})".format(name, e)) |
|
|
|
|
135
|
|
|
else: |
136
|
|
|
logger.debug("Export {} stats to InfluxDB".format(name)) |
|
|
|
|
137
|
|
|
|