Completed
Push — master ( 91d406...777314 )
by Nicolas
08:02 queued 03:13
created

glances.exports.glances_influxdb.Export.__init__()   A

Complexity

Conditions 2

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 18
nop 3
dl 0
loc 27
rs 9.5
c 0
b 0
f 0
1
# -*- coding: utf-8 -*-
0 ignored issues
show
Bug introduced by
There seems to be a cyclic import (glances -> glances.main).

Cyclic imports may cause partly loaded modules to be returned. This might lead to unexpected runtime behavior which is hard to debug.

Loading history...
Bug introduced by
There seems to be a cyclic import (glances -> glances.client).

Cyclic imports may cause partly loaded modules to be returned. This might lead to unexpected runtime behavior which is hard to debug.

Loading history...
Bug introduced by
There seems to be a cyclic import (glances -> glances.client_browser -> glances.client).

Cyclic imports may cause partly loaded modules to be returned. This might lead to unexpected runtime behavior which is hard to debug.

Loading history...
Bug introduced by
There seems to be a cyclic import (glances -> glances.standalone -> glances.outdated).

Cyclic imports may cause partly loaded modules to be returned. This might lead to unexpected runtime behavior which is hard to debug.

Loading history...
Bug introduced by
There seems to be a cyclic import (glances -> glances.server).

Cyclic imports may cause partly loaded modules to be returned. This might lead to unexpected runtime behavior which is hard to debug.

Loading history...
2
#
3
# This file is part of Glances.
4
#
5
# Copyright (C) 2019 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 interface class."""
21
22
import sys
0 ignored issues
show
introduced by
import missing from __future__ import absolute_import
Loading history...
23
24
from glances.logger import logger
0 ignored issues
show
introduced by
import missing from __future__ import absolute_import
Loading history...
25
from glances.exports.glances_export import GlancesExport
0 ignored issues
show
introduced by
import missing from __future__ import absolute_import
Loading history...
26
27
from influxdb import InfluxDBClient
0 ignored issues
show
introduced by
import missing from __future__ import absolute_import
Loading history...
introduced by
Unable to import 'influxdb'
Loading history...
28
from influxdb.client import InfluxDBClientError
0 ignored issues
show
introduced by
import missing from __future__ import absolute_import
Loading history...
introduced by
Unable to import 'influxdb.client'
Loading history...
29
30
31
class Export(GlancesExport):
0 ignored issues
show
best-practice introduced by
Too many instance attributes (8/7)
Loading history...
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
0 ignored issues
show
Coding Style Naming introduced by
The name db does not conform to the attribute naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
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'):
0 ignored issues
show
Unused Code introduced by
The if statement can be replaced with 'var = bool(test)'
Loading history...
69
            ssl = True
70
        else:
71
            ssl = False
72
73
        try:
74
            db = InfluxDBClient(host=self.host,
0 ignored issues
show
Coding Style Naming introduced by
The name db does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
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:
0 ignored issues
show
Coding Style Naming introduced by
The name e does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
83
            logger.critical("Cannot connect to InfluxDB database '%s' (%s)" % (self.db, e))
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (91/80).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
Coding Style Best Practice introduced by
Specify string format arguments as logging function parameters
Loading history...
84
            sys.exit(2)
85
86
        if self.db in get_all_db:
0 ignored issues
show
introduced by
The variable get_all_db does not seem to be defined for all execution paths.
Loading history...
87
            logger.info(
88
                "Stats will be exported to InfluxDB server: {}".format(db._baseurl))
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (84/80).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
introduced by
Use formatting in logging functions and pass the parameters as arguments
Loading history...
Coding Style Best Practice introduced by
It seems like _baseurl was declared protected and should not be accessed from this context.

Prefixing a member variable _ is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class:

class MyParent:
    def __init__(self):
        self._x = 1;
        self.y = 2;

class MyChild(MyParent):
    def some_method(self):
        return self._x    # Ok, since accessed from a child class

class AnotherClass:
    def some_method(self, instance_of_my_child):
        return instance_of_my_child._x   # Would be flagged as AnotherClass is not
                                         # a child class of MyParent
Loading history...
89
        else:
90
            logger.critical("InfluxDB database '%s' did not exist. Please create it" % self.db)
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (95/80).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
Coding Style Best Practice introduced by
Specify string format arguments as logging function parameters
Loading history...
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])
0 ignored issues
show
Unused Code Coding Style introduced by
Unnecessary parens after u'del' keyword
Loading history...
104
                del(columns[i])
0 ignored issues
show
Unused Code Coding Style introduced by
Unnecessary parens after u'del' keyword
Loading history...
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
        try:
130
            self.client.write_points(self._normalize(name, columns, points))
131
        except Exception as e:
0 ignored issues
show
Best Practice introduced by
Catching very general exceptions such as Exception is usually not recommended.

Generally, you would want to handle very specific errors in the exception handler. This ensure that you do not hide other types of errors which should be fixed.

So, unless you specifically plan to handle any error, consider adding a more specific exception.

Loading history...
Coding Style Naming introduced by
The name e does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
132
            logger.error("Cannot export {} stats to InfluxDB ({})".format(name,
0 ignored issues
show
introduced by
Use formatting in logging functions and pass the parameters as arguments
Loading history...
133
                                                                          e))
134
        else:
135
            logger.debug("Export {} stats to InfluxDB".format(name))
0 ignored issues
show
introduced by
Use formatting in logging functions and pass the parameters as arguments
Loading history...
136