Test Failed
Push — master ( aea994...69b639 )
by Nicolas
03:44 queued 10s
created

Export.init()   A

Complexity

Conditions 4

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
nop 1
dl 0
loc 14
rs 9.9
c 0
b 0
f 0
1
# -*- coding: utf-8 -*-
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
"""ElasticSearch interface class."""
21
22
import sys
23
from datetime import datetime
24
25
from glances.logger import logger
26
from glances.exports.glances_export import GlancesExport
27
28
from elasticsearch import Elasticsearch, helpers
29
from elasticsearch import __version__ as elk_version
30
31
32
class Export(GlancesExport):
33
34
    """This class manages the ElasticSearch (ES) export module."""
35
36
    def __init__(self, config=None, args=None):
37
        """Init the ES export IF."""
38
        super(Export, self).__init__(config=config, args=args)
39
40
        # Mandatories configuration keys (additional to host and port)
41
        self.index = None
42
43
        # Load the ES configuration file
44
        self.export_enable = self.load_conf('elasticsearch',
45
                                            mandatories=['host', 'port', 'index'],
46
                                            options=[])
47
        if not self.export_enable:
48
            sys.exit(2)
49
50
        # Init the ES client
51
        self.client = self.init()
52
53
    def init(self):
54
        """Init the connection to the ES server."""
55
        if not self.export_enable:
56
            return None
57
58
        try:
59
            es = Elasticsearch(hosts=['{}:{}'.format(self.host, self.port)])
60
        except Exception as e:
61
            logger.critical("Cannot connect to ElasticSearch server %s:%s (%s)" % (self.host, self.port, e))
62
            sys.exit(2)
63
        else:
64
            logger.info("Connected to the ElasticSearch server %s:%s" % (self.host, self.port))
65
66
        return es
67
68
    def export(self, name, columns, points):
69
        """Write the points to the ES server."""
70
        logger.debug("Export {} stats to ElasticSearch".format(name))
71
72
        # Generate index name with the index field + current day
73
        index = '{}-{}'.format(self.index,
74
                               datetime.utcnow().strftime("%Y.%m.%d"))
75
76
        # Create DB input
77
        # https://elasticsearch-py.readthedocs.io/en/master/helpers.html
78
        actions = []
79
        dtnow = datetime.utcnow().isoformat('T')
80
        action = {
81
            "_index": index,
82
            "_id": '{}.{}'.format(name, dtnow),
83
            "_type": 'glances-{}'.format(name),
84
            "_source": {
85
                "plugin": name,
86
                "timestamp": dtnow
87
            }
88
        }
89
        action['_source'].update(zip(columns, [str(p) for p in points]))
90
        actions.append(action)
91
92
        logger.debug(
93
            "Exporting the following object to elasticsearch: {}".format(action))
94
95
        # Write input to the ES index
96
        try:
97
            helpers.bulk(self.client, actions)
98
        except Exception as e:
99
            logger.error("Cannot export {} stats to ElasticSearch ({})".format(name, e))
100