1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
# |
3
|
|
|
# This file is part of Glances. |
4
|
|
|
# |
5
|
|
|
# Copyright (C) 2017 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
|
|
|
|
30
|
|
|
|
31
|
|
|
class Export(GlancesExport): |
32
|
|
|
|
33
|
|
|
"""This class manages the ElasticSearch (ES) export module.""" |
34
|
|
|
|
35
|
|
|
def __init__(self, config=None, args=None): |
36
|
|
|
"""Init the ES export IF.""" |
37
|
|
|
super(Export, self).__init__(config=config, args=args) |
38
|
|
|
|
39
|
|
|
# Mandatories configuration keys (additional to host and port) |
40
|
|
|
self.index = None |
41
|
|
|
|
42
|
|
|
# Optionals configuration keys |
43
|
|
|
# N/A |
44
|
|
|
|
45
|
|
|
# Load the ES configuration file |
46
|
|
|
self.export_enable = self.load_conf('elasticsearch', |
47
|
|
|
mandatories=['host', 'port', 'index'], |
48
|
|
|
options=[]) |
49
|
|
|
if not self.export_enable: |
50
|
|
|
sys.exit(2) |
51
|
|
|
|
52
|
|
|
# Init the ES client |
53
|
|
|
self.client = self.init() |
54
|
|
|
|
55
|
|
|
def init(self): |
56
|
|
|
"""Init the connection to the ES server.""" |
57
|
|
|
if not self.export_enable: |
58
|
|
|
return None |
59
|
|
|
|
60
|
|
|
try: |
61
|
|
|
es = Elasticsearch(hosts=['{}:{}'.format(self.host, self.port)]) |
62
|
|
|
except Exception as e: |
63
|
|
|
logger.critical("Cannot connect to ElasticSearch server %s:%s (%s)" % (self.host, self.port, e)) |
64
|
|
|
sys.exit(2) |
65
|
|
|
else: |
66
|
|
|
logger.info("Connected to the ElasticSearch server %s:%s" % (self.host, self.port)) |
67
|
|
|
|
68
|
|
|
try: |
69
|
|
|
index_count = es.count(index=self.index)['count'] |
70
|
|
|
except Exception as e: |
71
|
|
|
# Index did not exist, it will be created at the first write |
72
|
|
|
# Create it... |
73
|
|
|
es.indices.create(self.index) |
74
|
|
|
else: |
75
|
|
|
logger.info("There is already %s entries in the ElasticSearch %s index" % (index_count, self.index)) |
76
|
|
|
|
77
|
|
|
return es |
78
|
|
|
|
79
|
|
|
def export(self, name, columns, points): |
80
|
|
|
"""Write the points to the ES server.""" |
81
|
|
|
logger.debug("Export {} stats to ElasticSearch".format(name)) |
82
|
|
|
|
83
|
|
|
# Create DB input |
84
|
|
|
# https://elasticsearch-py.readthedocs.io/en/master/helpers.html |
85
|
|
|
actions = [] |
86
|
|
|
for c, p in zip(columns, points): |
87
|
|
|
action = { |
88
|
|
|
"_index": self.index, |
89
|
|
|
"_type": name, |
90
|
|
|
"_id": c, |
91
|
|
|
"_source": { |
92
|
|
|
"value": str(p), |
93
|
|
|
"timestamp": datetime.now() |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
actions.append(action) |
97
|
|
|
|
98
|
|
|
# Write input to the ES index |
99
|
|
|
try: |
100
|
|
|
helpers.bulk(self.client, actions) |
101
|
|
|
except Exception as e: |
102
|
|
|
logger.error("Cannot export {} stats to ElasticSearch ({})".format(name, e)) |
103
|
|
|
|