From b80801f36a781312d35f3bebc0d9b005dcc24f25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrzej=20Dopiera=C5=82a?= Date: Thu, 2 Feb 2017 16:07:45 +0100 Subject: [PATCH 1/4] add support to authentication --- check_es_cluster_status.py | 12 ++++++++++-- check_es_jvm_usage.py | 13 +++++++++++-- check_es_nodes.py | 13 +++++++++++-- check_es_split_brain.py | 15 ++++++++++++--- check_es_unassigned_shards.py | 13 +++++++++++-- 5 files changed, 55 insertions(+), 11 deletions(-) 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..b0c1833 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 @@ -24,16 +25,24 @@ 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))) 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..4bb977b 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))) From f01bc8cdcdaf3e4b9ff696af5abd7ddd1da032ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrzej=20Dopiera=C5=82a?= Date: Thu, 2 Feb 2017 16:10:42 +0100 Subject: [PATCH 2/4] remove non existed --use-mirrors option --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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: From c798f9a49568449bf7796813662aa01b238b036a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrzej=20Dopiera=C5=82a?= Date: Thu, 2 Feb 2017 16:50:22 +0100 Subject: [PATCH 3/4] typo --- check_es_unassigned_shards.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/check_es_unassigned_shards.py b/check_es_unassigned_shards.py index 4bb977b..c5af433 100644 --- a/check_es_unassigned_shards.py +++ b/check_es_unassigned_shards.py @@ -33,7 +33,7 @@ def check(self, opts, args): 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 = urllib2.urlopen(url) except urllib2.HTTPError, e: raise Status('unknown', ("API failure", None, From f6e4625dded84f769da324ea37f3d33463645d99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrzej=20Dopiera=C5=82a?= Date: Thu, 30 May 2019 10:18:25 +0200 Subject: [PATCH 4/4] some fixes --- check_es_jvm_usage.py | 22 +++++++++++++++------- check_es_unassigned_shards.py | 2 +- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/check_es_jvm_usage.py b/check_es_jvm_usage.py index b0c1833..b913145 100644 --- a/check_es_jvm_usage.py +++ b/check_es_jvm_usage.py @@ -10,7 +10,6 @@ except ImportError: import simplejson as json - class ESJVMHealthCheck(NagiosCheck): def __init__(self): @@ -45,7 +44,7 @@ def check(self, opts, args): 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)) @@ -60,6 +59,7 @@ def check(self, opts, args): critical_details = [] warnings = 0 warning_details = [] + details=[] nodes = nodes_jvm_data['nodes'] for node in nodes: @@ -74,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_unassigned_shards.py b/check_es_unassigned_shards.py index c5af433..87c8ed3 100644 --- a/check_es_unassigned_shards.py +++ b/check_es_unassigned_shards.py @@ -50,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))