elasticsearch_plugin: "python-requests" requirement removed
Ilya committed
Mar 14, 2017 at 15:58 UTC
f2dc2242324be9e0627dda496a6784758b73947b
1 file changed
+28
-37
python.d/elasticsearch.chart.py
+28
-37
@@ -3,14 +3,14 @@
3
# Author: l2isbad
4
5
from base import UrlService
6
-from requests import get
7
-from socket import gethostbyname
6
+from socket import gethostbyname, gaierror
7
try:
8
from queue import Queue
9
except ImportError:
10
from Queue import Queue
11
from threading import Thread
12
from collections import namedtuple
13
+from json import loads
14
15
# default module values (can be overridden per job in `config`)
16
# update_every = 2
@@ -294,30 +294,33 @@ class Service(UrlService):
294
self.order = ORDER
295
self.definitions = CHARTS
296
self.host = self.configuration.get('host')
297
- self.port = self.configuration.get('port')
298
- self.user = self.configuration.get('user')
299
- self.password = self.configuration.get('pass')
297
+ self.port = self.configuration.get('port', 9200)
298
+ self.scheme = self.configuration.get('scheme', 'http')
299
self.latency = dict()
300
self.methods = list()
302
- self.auth = self.user and self.password
301
302
def check(self):
303
# We can't start if <host> AND <port> not specified
306
- if not all([self.host, self.port]):
304
+ if not all([self.host, self.port, isinstance(self.host, str), isinstance(self.port, (str, int))]):
305
+ self.error('Host is not defined in the module configuration file')
306
return False
307
308
# It as a bad idea to use hostname.
309
# Hostname -> ip address
310
try:
311
self.host = gethostbyname(self.host)
313
- except Exception as error:
312
+ except gaierror as error:
313
self.error(str(error))
314
return False
315
316
+ scheme = 'http' if self.scheme else 'https'
317
+ # Add handlers (auth, self signed cert accept)
318
+ self.url = '%s://%s:%s' % (scheme, self.host, self.port)
319
+ self._UrlService__add_openers()
320
# Create URL for every Elasticsearch API
318
- url_node_stats = 'http://%s:%s/_nodes/_local/stats' % (self.host, self.port)
319
- url_cluster_health = 'http://%s:%s/_cluster/health' % (self.host, self.port)
320
- url_cluster_stats = 'http://%s:%s/_cluster/stats' % (self.host, self.port)
321
+ url_node_stats = '%s://%s:%s/_nodes/_local/stats' % (scheme, self.host, self.port)
322
+ url_cluster_health = '%s://%s:%s/_cluster/health' % (scheme, self.host, self.port)
323
+ url_cluster_stats = '%s://%s:%s/_cluster/stats' % (scheme, self.host, self.port)
324
325
# Create list of enabled API calls
326
user_choice = [bool(self.configuration.get('node_stats', True)),
@@ -329,13 +332,15 @@ class Service(UrlService):
332
METHODS(get_data_function=self._get_cluster_stats_, url=url_cluster_stats)]
333
334
# Remove disabled API calls from 'avail methods'
332
- self.methods = [avail_methods[_] for _ in range(len(avail_methods)) if user_choice[_]]
335
+ self.methods = [avail_methods[e[0]] for e in enumerate(avail_methods) if user_choice[e[0]]]
336
337
# Run _get_data for ALL active API calls.
338
api_check_result = dict()
339
+ data_from_check = dict()
340
for method in self.methods:
341
try:
338
- api_check_result[method.url] = (bool(method.get_data_function(None, method.url)))
342
+ api_check_result[method.url] = method.get_data_function(None, method.url)
343
+ data_from_check.update(api_check_result[method.url] or dict())
344
except KeyError as error:
345
self.error('Failed to parse %s. Error: %s' % (method.url, str(error)))
346
return False
@@ -343,25 +348,11 @@ class Service(UrlService):
348
# We can start ONLY if all active API calls returned NOT None
349
if not all(api_check_result.values()):
350
self.error('Plugin could not get data from all APIs')
346
- self.error('%s' % api_check_result)
351
return False
352
else:
349
- self.info('%s' % api_check_result)
350
- self.info('Plugin was started successfully')
351
-
353
+ self._data_from_check = data_from_check
354
return True
355
354
- def _get_raw_data(self, url):
355
- try:
356
- if not self.auth:
357
- raw_data = get(url)
358
- else:
359
- raw_data = get(url, auth=(self.user, self.password))
360
- except Exception:
361
- return None
362
-
363
- return raw_data
364
-
356
def _get_data(self):
357
threads = list()
358
queue = Queue()
@@ -384,12 +375,12 @@ class Service(UrlService):
375
:return: dict
376
"""
377
387
- data = self._get_raw_data(url)
378
+ raw_data = self._get_raw_data(url)
379
389
- if not data:
380
+ if not raw_data:
381
return queue.put(dict()) if queue else None
382
else:
392
- data = data.json() if hasattr(data.json, '__call__') else data.json
383
+ data = loads(raw_data)
384
385
to_netdata = fetch_data_(raw_data=data, metrics_list=HEALTH_STATS)
386
@@ -406,12 +397,12 @@ class Service(UrlService):
397
:return: dict
398
"""
399
409
- data = self._get_raw_data(url)
400
+ raw_data = self._get_raw_data(url)
401
411
- if not data:
402
+ if not raw_data:
403
return queue.put(dict()) if queue else None
404
else:
414
- data = data.json() if hasattr(data.json, '__call__') else data.json
405
+ data = loads(raw_data)
406
407
to_netdata = fetch_data_(raw_data=data, metrics_list=CLUSTER_STATS)
408
@@ -423,12 +414,12 @@ class Service(UrlService):
414
:return: dict
415
"""
416
426
- data = self._get_raw_data(url)
417
+ raw_data = self._get_raw_data(url)
418
428
- if not data:
419
+ if not raw_data:
420
return queue.put(dict()) if queue else None
421
else:
431
- data = data.json() if hasattr(data.json, '__call__') else data.json
422
+ data = loads(raw_data)
423
424
node = list(data['nodes'].keys())[0]
425
to_netdata = fetch_data_(raw_data=data['nodes'][node], metrics_list=NODE_STATS)