diff --git a/.travis.yml b/.travis.yml index f79e86b..45d9447 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,7 +5,7 @@ python: - 2.7 install: - - pip install -r requirements.txt --use-mirrors + - pip install -r requirements.txt before_script: - "pep8 ." script: diff --git a/check_es_cluster_status.py b/check_es_cluster_status.py index e6878d4..4faa063 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,21 @@ 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 to login into ES port') + self.add_option('p', 'password', 'password', 'password to login into ES port') 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=urllib2.Request(r'http://%s:%d/_cluster/health' % (host, port)) + if username and password: + base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '') + url.add_header("Authorization","Basic %s" % base64string) + response = urllib2.urlopen(url) 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..b913145 100644 --- a/check_es_jvm_usage.py +++ b/check_es_jvm_usage.py @@ -3,13 +3,13 @@ from nagioscheck import PerformanceMetric, Status import urllib2 import optparse +import base64 try: import json except ImportError: import simplejson as json - class ESJVMHealthCheck(NagiosCheck): def __init__(self): @@ -24,19 +24,27 @@ def __init__(self): self.add_option('W', 'warning_threshold', 'warning_threshold', 'The level at which we throw a WARNING alert' ' - defaults to 90% of the JVM setting') + self.add_option('u', 'username', 'username', 'username to login into ES port') + self.add_option('p', 'password', 'password', 'password to login into ES port') def check(self, opts, args): host = opts.host port = int(opts.port or '9200') critical = int(opts.critical_threshold or '97') warning = int(opts.warning_threshold or '90') + username = opts.username + password = opts.password try: - response = urllib2.urlopen(r'http://%s:%d/_nodes/stats/jvm' - % (host, port)) + url=urllib2.Request(r'http://%s:%d/_nodes/stats/jvm' % (host, port)) + if username and password: + base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '') + url.add_header("Authorization","Basic %s" % base64string) + response = urllib2.urlopen(url) + except urllib2.HTTPError, e: raise Status('unknown', ("API failure", None, - "API failure:\n\n%s" % str(e))) + "API failure: %s" % str(e))) except urllib2.URLError, e: raise Status('critical', (e.reason)) @@ -51,6 +59,7 @@ def check(self, opts, args): critical_details = [] warnings = 0 warning_details = [] + details=[] nodes = nodes_jvm_data['nodes'] for node in nodes: @@ -65,27 +74,35 @@ def check(self, opts, args): warnings = warnings + 1 warning_details.append("%s currently running at %s%% JVM mem " % (node_name, jvm_percentage)) + else: + details.append("%s have %s%% JVM mem " % (node_name,jvm_percentage)) if criticals > 0: raise Status("Critical", "There are '%s' node(s) in the cluster that have " "breached the %% JVM heap usage critical threshold " - "of %s%%. They are:\r\n%s" + "of %s%%. They are: %s. OK are: %s " % ( criticals, critical, - str("\r\n".join(critical_details)) + str(" ".join(critical_details)), + str(" " .join(details)) )) elif warnings > 0: raise Status("Warning", "There are '%s' node(s) in the cluster that have " "breached the %% JVM mem usage warning threshold of " - "%s%%. They are:\r\n%s" + "%s%%. They are: %s OK are: %s" % (warnings, warning, - str("\r\n".join(warning_details)))) + str(" ".join(warning_details)), + str(" ".join(details)) + )) else: raise Status("OK", "All nodes in the cluster are currently below " - "the % JVM mem warning threshold") + "the %% JVM mem warning threshold. OK are: %s" + % ( + str(" ".join(details)) + )) if __name__ == "__main__": ESJVMHealthCheck().run() diff --git a/check_es_nodes.py b/check_es_nodes.py index ba7c0a1..6c56fc2 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,23 @@ 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 to login into ES port') + self.add_option('p', 'password', 'password', 'password to login into ES port') def check(self, opts, args): host = opts.host port = int(opts.port or '9200') nodes_in_cluster = int(opts.nodes_in_cluster) + username = opts.username + password = opts.password try: - response = urllib2.urlopen(r'http://%s:%d/_cluster/health' - % (host, port)) + url=urllib2.Request(r'http://%s:%d/_cluster/health' % (host, port)) + if username and password: + base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '') + url.add_header("Authorization","Basic %s" % base64string) + response = urllib2.urlopen(url) + 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..8744040 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,27 @@ 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 to login into ES port') + self.add_option('p', 'password', 'password', 'password to login into ES port') 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=urllib2.Request(r'http://%s:%d/_cluster/state/nodes,master_node/' % (node, port)) + if username and password: + base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '') + url.add_header("Authorization","Basic %s" % base64string) + response = urllib2.urlopen(url) + 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..87c8ed3 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,22 @@ 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 to login into ES port') + self.add_option('p', 'password', 'password', 'password to login into ES port') 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=urllib2.Request(r'http://%s:%d/_cluster/health' % (host, port)) + if username and password: + base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '') + url.add_header("Authorization","Basic %s" % base64string) + response = urllib2.urlopen(url) + except urllib2.HTTPError, e: raise Status('unknown', ("API failure", None, "API failure:\n\n%s" % str(e))) @@ -41,7 +50,7 @@ def check(self, opts, args): unassigned_shards = es_cluster_health['unassigned_shards'] - if es_cluster_health['unassigned_shards'] != unassigned_shards: + if unassigned_shards != 0: raise Status('CRITICAL', "There are '%s' unassigned shards in the cluster" % (unassigned_shards))