1# -*- coding: utf-8 -*-
2#
3# This file is part of Glances.
4#
5# Copyright (C) 2019 Nicolargo <nicolas@nicolargo.com>
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 interface class."""
21
22import sys
23
24from glances.logger import logger
25from glances.exports.glances_export import GlancesExport
26
27from influxdb import InfluxDBClient
28from influxdb.client import InfluxDBClientError
29
30
31class Export(GlancesExport):
32    """This class manages the InfluxDB export module."""
33
34    def __init__(self, config=None, args=None):
35        """Init the InfluxDB export IF."""
36        super(Export, self).__init__(config=config, args=args)
37
38        # Mandatories configuration keys (additional to host and port)
39        self.user = None
40        self.password = None
41        self.db = None
42
43        # Optionals configuration keys
44        self.protocol = 'http'
45        self.prefix = None
46        self.tags = None
47
48        # Load the InfluxDB configuration file
49        self.export_enable = self.load_conf('influxdb',
50                                            mandatories=['host', 'port',
51                                                         'user', 'password',
52                                                         'db'],
53                                            options=['protocol',
54                                                     'prefix',
55                                                     'tags'])
56        if not self.export_enable:
57            sys.exit(2)
58
59        # Init the InfluxDB client
60        self.client = self.init()
61
62    def init(self):
63        """Init the connection to the InfluxDB server."""
64        if not self.export_enable:
65            return None
66
67        # Correct issue #1530
68        if self.protocol is not None and (self.protocol.lower() == 'https'):
69            ssl = True
70        else:
71            ssl = False
72
73        try:
74            db = InfluxDBClient(host=self.host,
75                                port=self.port,
76                                ssl=ssl,
77                                verify_ssl=False,
78                                username=self.user,
79                                password=self.password,
80                                database=self.db)
81            get_all_db = [i['name'] for i in db.get_list_database()]
82        except InfluxDBClientError as e:
83            logger.critical("Cannot connect to InfluxDB database '%s' (%s)" % (self.db, e))
84            sys.exit(2)
85
86        if self.db in get_all_db:
87            logger.info(
88                "Stats will be exported to InfluxDB server: {}".format(db._baseurl))
89        else:
90            logger.critical("InfluxDB database '%s' did not exist. Please create it" % self.db)
91            sys.exit(2)
92
93        return db
94
95    def _normalize(self, name, columns, points):
96        """Normalize data for the InfluxDB's data model."""
97
98        for i, _ in enumerate(points):
99            # Supported type:
100            # https://docs.influxdata.com/influxdb/v1.5/write_protocols/line_protocol_reference/
101            if points[i] is None:
102                # Ignore points with None value
103                del(points[i])
104                del(columns[i])
105                continue
106            try:
107                points[i] = float(points[i])
108            except (TypeError, ValueError):
109                pass
110            else:
111                continue
112            try:
113                points[i] = str(points[i])
114            except (TypeError, ValueError):
115                pass
116            else:
117                continue
118
119        return [{'measurement': name,
120                 'tags': self.parse_tags(self.tags),
121                 'fields': dict(zip(columns, points))}]
122
123    def export(self, name, columns, points):
124        """Write the points to the InfluxDB server."""
125        # Manage prefix
126        if self.prefix is not None:
127            name = self.prefix + '.' + name
128        # Write input to the InfluxDB database
129        if len(points) == 0:
130            logger.debug("Cannot export empty {} stats to InfluxDB".format(name))
131        else:
132            try:
133                self.client.write_points(self._normalize(name, columns, points), time_precision="s")
134            except Exception as e:
135                # Log level set to debug instead of error (see: issue #1561)
136                logger.debug("Cannot export {} stats to InfluxDB ({})".format(name, e))
137            else:
138                logger.debug("Export {} stats to InfluxDB".format(name))
139