From 777e120e7a195f6d2b706156227a782215284c5e Mon Sep 17 00:00:00 2001 From: Ionut Date: Thu, 10 Aug 2017 17:00:21 +0100 Subject: [PATCH] Use Basic Auth if 401 error --- agent/elastizabbix | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/agent/elastizabbix b/agent/elastizabbix index 6057c0f..62d4d06 100755 --- a/agent/elastizabbix +++ b/agent/elastizabbix @@ -5,8 +5,11 @@ import json import urllib2 import time import errno +import base64 ttl = 60 +username = 'your_auth_user' +password = 'your_auth_pass' stats = { 'cluster': 'http://localhost:9200/_cluster/stats', @@ -38,6 +41,12 @@ def get_cache(api): d = urllib2.urlopen(stats[api]).read() with open(cache, 'w') as f: f.write(d) except Exception as e: + if e.code == 401: + auth = urllib2.Request(stats[api]) + base64string = base64.b64encode('%s:%s' % (username, password)) + auth.add_header("Authorization", "Basic %s" % base64string) + d = urllib2.urlopen(auth).read() + with open(cache, 'w') as f: f.write(d) pass if os.path.exists(lock): os.remove(lock)