diff --git a/check_es_cluster_status.py b/check_es_cluster_status.py index e6878d4..329fe99 100644 --- a/check_es_cluster_status.py +++ b/check_es_cluster_status.py @@ -3,6 +3,7 @@ from nagioscheck import PerformanceMetric, Status import urllib2 import optparse +import base64 try: import json @@ -18,14 +19,23 @@ def __init__(self): self.add_option('H', 'host', 'host', 'The cluster to check') self.add_option('P', 'port', 'port', 'The ES port - defaults to 9200') + self.add_option('u', 'username', 'username', 'Username for authentication') + self.add_option('p', 'password', 'password', 'password for authentication') def check(self, opts, args): host = opts.host port = int(opts.port or '9200') + username = opts.username + password = opts.password try: - response = urllib2.urlopen(r'http://%s:%d/_cluster/health' - % (host, port)) + url = r'http://%s:%d/_cluster/health' % (host, port) + request = urllib2.Request(url) + if username is not None and password is not None: + base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '') + request.add_header("Authorization", "Basic %s" % base64string) + response = urllib2.urlopen(request) + except urllib2.HTTPError, e: raise Status('unknown', ("API failure", None, "API failure:\n\n%s" % str(e))) diff --git a/check_es_jvm_usage.py b/check_es_jvm_usage.py index 444bd93..7eb595b 100644 --- a/check_es_jvm_usage.py +++ b/check_es_jvm_usage.py @@ -3,6 +3,7 @@ from nagioscheck import PerformanceMetric, Status import urllib2 import optparse +import base64 try: import json @@ -18,6 +19,8 @@ def __init__(self): self.add_option('H', 'host', 'host', 'The cluster to check') self.add_option('P', 'port', 'port', 'The ES port - defaults to 9200') + self.add_option('u', 'username', 'username', 'Username for authentication') + self.add_option('p', 'password', 'password', 'password for authentication') self.add_option('C', 'critical_threshold', 'critical_threshold', 'The level at which we throw a CRITICAL alert' ' - defaults to 97% of the JVM setting') @@ -28,12 +31,19 @@ def __init__(self): def check(self, opts, args): host = opts.host port = int(opts.port or '9200') + username = opts.username + password = opts.password critical = int(opts.critical_threshold or '97') warning = int(opts.warning_threshold or '90') try: - response = urllib2.urlopen(r'http://%s:%d/_nodes/stats/jvm' - % (host, port)) + url = r'http://%s:%d/_nodes/stats/jvm' % (host, port) + request = urllib2.Request(url) + if username is not None and password is not None: + base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '') + request.add_header("Authorization", "Basic %s" % base64string) + response = urllib2.urlopen(request) + except urllib2.HTTPError, e: raise Status('unknown', ("API failure", None, "API failure:\n\n%s" % str(e))) diff --git a/check_es_nodes.py b/check_es_nodes.py index ba7c0a1..7c7e3d8 100644 --- a/check_es_nodes.py +++ b/check_es_nodes.py @@ -3,6 +3,7 @@ from nagioscheck import PerformanceMetric, Status import urllib2 import optparse +import base64 try: import json @@ -20,15 +21,24 @@ def __init__(self): 'This is the expected number of nodes in the cluster') self.add_option('H', 'host', 'host', 'The cluster to check') self.add_option('P', 'port', 'port', 'The ES port - defaults to 9200') + self.add_option('u', 'username', 'username', 'Username for authentication') + self.add_option('p', 'password', 'password', 'password for authentication') def check(self, opts, args): host = opts.host port = int(opts.port or '9200') + username = opts.username + password = opts.password nodes_in_cluster = int(opts.nodes_in_cluster) try: - response = urllib2.urlopen(r'http://%s:%d/_cluster/health' - % (host, port)) + url = r'http://%s:%d/_cluster/health' % (host, port) + request = urllib2.Request(url) + if username is not None and password is not None: + base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '') + request.add_header("Authorization", "Basic %s" % base64string) + response = urllib2.urlopen(request) + except urllib2.HTTPError, e: raise Status('unknown', ("API failure", None, "API failure:\n\n%s" % str(e))) diff --git a/check_es_split_brain.py b/check_es_split_brain.py index e3643d6..6e3f51c 100644 --- a/check_es_split_brain.py +++ b/check_es_split_brain.py @@ -3,6 +3,7 @@ from nagioscheck import PerformanceMetric, Status import urllib2 import optparse +import base64 try: import json @@ -18,19 +19,26 @@ def __init__(self): self.add_option('N', 'nodes', 'nodes', 'Cluster nodes') self.add_option('P', 'port', 'port', 'The ES port - defaults to 9200') + self.add_option('u', 'username', 'username', 'Username for authentication') + self.add_option('p', 'password', 'password', 'password for authentication') def check(self, opts, args): nodes = opts.nodes.split(",") port = int(opts.port or '9200') + username = opts.username + password = opts.password masters = [] responding_nodes = [] failed_nodes = [] for node in nodes: try: - response = urllib2.urlopen( - r'http://%s:%d/_cluster/state/nodes,master_node/' - % (node, port)) + url = r'http://%s:%d/_cluster/state/nodes,master_node/' % (node, port) + request = urllib2.Request(url) + if username is not None and password is not None: + base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '') + request.add_header("Authorization", "Basic %s" % base64string) + response = urllib2.urlopen(request) response_body = response.read() response = json.loads(response_body) except (urllib2.HTTPError, urllib2.URLError), e: diff --git a/check_es_unassigned_shards.py b/check_es_unassigned_shards.py index 46e3ef2..7f13361 100644 --- a/check_es_unassigned_shards.py +++ b/check_es_unassigned_shards.py @@ -3,6 +3,7 @@ from nagioscheck import PerformanceMetric, Status import urllib2 import optparse +import base64 try: import json @@ -18,14 +19,23 @@ def __init__(self): self.add_option('H', 'host', 'host', 'The cluster to check') self.add_option('P', 'port', 'port', 'The ES port - defaults to 9200') + self.add_option('u', 'username', 'username', 'Username for authentication') + self.add_option('p', 'password', 'password', 'password for authentication') def check(self, opts, args): host = opts.host port = int(opts.port or '9200') + username = opts.username + password = opts.password try: - response = urllib2.urlopen(r'http://%s:%d/_cluster/health' - % (host, port)) + url = r'http://%s:%d/_cluster/health' % (host, port) + request = urllib2.Request(url) + if username is not None and password is not None: + base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '') + request.add_header("Authorization", "Basic %s" % base64string) + response = urllib2.urlopen(request) + except urllib2.HTTPError, e: raise Status('unknown', ("API failure", None, "API failure:\n\n%s" % str(e)))