From 1be1b2e5a2ddabec83ee7783e97c5eb0a659311f Mon Sep 17 00:00:00 2001 From: "frederic.oppermann" Date: Mon, 13 Jul 2015 17:05:27 +0200 Subject: [PATCH 1/5] synchronized readme.me and help of plugin regarding usage description --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 42508ee..e67ec35 100644 --- a/README.md +++ b/README.md @@ -47,13 +47,13 @@ More info about options in Usage. Executing `./check_http_json.py -h` will yield the following details: ``` -usage: check_http_json.py [-h] -H HOST [-B AUTH] [-p PATH] +usage: check_http_json.py [-h] -H HOST [-P PORT] [-B AUTH] [-p PATH] [-e [KEY_LIST [KEY_LIST ...]]] [-q [KEY_VALUE_LIST [KEY_VALUE_LIST ...]]] [-l [KEY_LTE_LIST [KEY_LTE_LIST ...]]] [-g [KEY_GTE_LIST [KEY_GTE_LIST ...]]] [-m [METRIC_LIST [METRIC_LIST ...]]] [-s] - [-f SEPARATOR] [-d] + [-t TIMEOUT] [-f SEPARATOR] [-d] Nagios plugin which checks json values from a given endpoint against argument specified rules and determines the status and performance data for that @@ -62,7 +62,7 @@ service optional arguments: -h, --help show this help message and exit -H HOST, --host HOST Host. - -H PORT, --port PORT TCP port. + -P PORT, --port PORT TCP port -B AUTH, --basic-auth AUTH Basic auth string "username:password" -p PATH, --path PATH Path. From b9d03c899f1f66784400b1a7016cff730fe83c91 Mon Sep 17 00:00:00 2001 From: Jernej Porenta Date: Tue, 28 Jul 2015 09:08:55 +0200 Subject: [PATCH 2/5] Allow multiple key value specification Multiple key values can be specified by using colon delimiter. --- README.md | 3 ++- check_http_json.py | 5 +++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index e67ec35..ba66438 100644 --- a/README.md +++ b/README.md @@ -67,7 +67,8 @@ optional arguments: Basic auth string "username:password" -p PATH, --path PATH Path. -e [KEY_LIST [KEY_LIST ...]], --key_exists [KEY_LIST [KEY_LIST ...]] - Checks existence of these keys to determine status. + Checks existence of these keys to determine status. Multiple key values can + be delimited with colon (key,value1:value2). -q [KEY_VALUE_LIST [KEY_VALUE_LIST ...]], --key_equals [KEY_VALUE_LIST [KEY_VALUE_LIST ...]] Checks equality of these keys and values (key,value key2,value2) to determine status. diff --git a/check_http_json.py b/check_http_json.py index bd064b3..eb1227e 100755 --- a/check_http_json.py +++ b/check_http_json.py @@ -75,7 +75,7 @@ class JsonHelper: else: return (None, 'not_found') - def equals(self, key, value): return self.exists(key) and str(self.get(key)) == value + def equals(self, key, value): return self.exists(key) and str(self.get(key)) in value.split(':') def lte(self, key, value): return self.exists(key) and float(self.get(key)) <= float(value) def gte(self, key, value): return self.exists(key) and float(self.get(key)) >= float(value) def exists(self, key): return (self.get(key) != (None, 'not_found')) @@ -193,7 +193,8 @@ def parseArgs(): parser.add_argument('-e', '--key_exists', dest='key_list', nargs='*', help='Checks existence of these keys to determine status.') parser.add_argument('-q', '--key_equals', dest='key_value_list', nargs='*', - help='Checks equality of these keys and values (key,value key2,value2) to determine status.') + help='Checks equality of these keys and values (key,value key2,value2) to determine status.\ + Multiple key values can be delimited with colon (key,value1:value2)') parser.add_argument('-l', '--key_lte', dest='key_lte_list', nargs='*', help='Checks that these keys and values (key,value key2,value2) are less than or equal to\ the returned json value to determine status.') From 3058176ba1951c1570dc73be6d05f47f2a810bd6 Mon Sep 17 00:00:00 2001 From: Bill Moritz Date: Sat, 22 Aug 2015 09:42:06 -0400 Subject: [PATCH 3/5] Add data argument Add an option to HTTP POST data to the host. --- check_http_json.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/check_http_json.py b/check_http_json.py index eb1227e..090fafe 100755 --- a/check_http_json.py +++ b/check_http_json.py @@ -190,6 +190,7 @@ def parseArgs(): parser.add_argument('-P', '--port', dest='port', required=False, help='TCP port') parser.add_argument('-B', '--basic-auth', dest='auth', required=False, help='Basic auth string "username:password"') parser.add_argument('-p', '--path', dest='path', help='Path.') + parser.add_argument('-D', '--data', dest='data', help='The http payload to send as a POST') parser.add_argument('-e', '--key_exists', dest='key_list', nargs='*', help='Checks existence of these keys to determine status.') parser.add_argument('-q', '--key_equals', dest='key_value_list', nargs='*', @@ -239,8 +240,12 @@ if __name__ == "__main__": if args.auth: base64str = base64.encodestring(args.auth).replace('\n', '') req.add_header('Authorization', 'Basic %s' % base64str) - if args.timeout: + if args.timeout and args.data: + response = urllib2.urlopen(req, timeout=args.timeout, data=args.data) + elif args.timeout: response = urllib2.urlopen(req, timeout=args.timeout) + elif args.data: + response = urllib2.urlopen(req, data=args.data) else: response = urllib2.urlopen(req) except HTTPError as e: From 42e75abcad8b8451689f99d158dd19b31a11ca49 Mon Sep 17 00:00:00 2001 From: Bill Moritz Date: Sat, 22 Aug 2015 09:47:25 -0400 Subject: [PATCH 4/5] Update README.md --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ba66438..3152c7e 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ More info about options in Usage. Executing `./check_http_json.py -h` will yield the following details: ``` -usage: check_http_json.py [-h] -H HOST [-P PORT] [-B AUTH] [-p PATH] +usage: check_http_json.py [-h] -H HOST [-P PORT] [-B AUTH] [-p PATH] [-D DATA] [-e [KEY_LIST [KEY_LIST ...]]] [-q [KEY_VALUE_LIST [KEY_VALUE_LIST ...]]] [-l [KEY_LTE_LIST [KEY_LTE_LIST ...]]] @@ -66,6 +66,7 @@ optional arguments: -B AUTH, --basic-auth AUTH Basic auth string "username:password" -p PATH, --path PATH Path. + -D DATA, --data DATA The http payload to send as an POST. -e [KEY_LIST [KEY_LIST ...]], --key_exists [KEY_LIST [KEY_LIST ...]] Checks existence of these keys to determine status. Multiple key values can be delimited with colon (key,value1:value2). From a4be4d42c6557516a4c354670022571a91f136fd Mon Sep 17 00:00:00 2001 From: Bill Moritz Date: Mon, 31 Aug 2015 11:36:27 -0400 Subject: [PATCH 5/5] Return metrics no matter the result --- check_http_json.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/check_http_json.py b/check_http_json.py index 090fafe..daeb935 100755 --- a/check_http_json.py +++ b/check_http_json.py @@ -31,7 +31,7 @@ class NagiosHelper: text += "|%s" % self.performance_data return text - def setCodeAndMessage(self, code, text): + def setCodeAndMessage(self, code, text): self.code = code self.message_text = text @@ -74,7 +74,7 @@ class JsonHelper: return self.get(remainingKey, data[index]) else: return (None, 'not_found') - + def equals(self, key, value): return self.exists(key) and str(self.get(key)) in value.split(':') def lte(self, key, value): return self.exists(key) and float(self.get(key)) <= float(value) def gte(self, key, value): return self.exists(key) and float(self.get(key)) >= float(value) @@ -95,7 +95,7 @@ class JsonHelper: else: return self.getSubArrayElement(key, data) else: - if key.find(self.separator) != -1 : + if key.find(self.separator) != -1 : return self.getSubElement(key, data) else: if key.find(self.arrayOpener) != -1 : @@ -178,7 +178,7 @@ class JsonRuleProcessor: metrics += ' ' - + return "%s" % metrics def parseArgs(): @@ -191,18 +191,18 @@ def parseArgs(): parser.add_argument('-B', '--basic-auth', dest='auth', required=False, help='Basic auth string "username:password"') parser.add_argument('-p', '--path', dest='path', help='Path.') parser.add_argument('-D', '--data', dest='data', help='The http payload to send as a POST') - parser.add_argument('-e', '--key_exists', dest='key_list', nargs='*', + parser.add_argument('-e', '--key_exists', dest='key_list', nargs='*', help='Checks existence of these keys to determine status.') - parser.add_argument('-q', '--key_equals', dest='key_value_list', nargs='*', + parser.add_argument('-q', '--key_equals', dest='key_value_list', nargs='*', help='Checks equality of these keys and values (key,value key2,value2) to determine status.\ Multiple key values can be delimited with colon (key,value1:value2)') - parser.add_argument('-l', '--key_lte', dest='key_lte_list', nargs='*', + parser.add_argument('-l', '--key_lte', dest='key_lte_list', nargs='*', help='Checks that these keys and values (key,value key2,value2) are less than or equal to\ the returned json value to determine status.') - parser.add_argument('-g', '--key_gte', dest='key_gte_list', nargs='*', + parser.add_argument('-g', '--key_gte', dest='key_gte_list', nargs='*', help='Checks that these keys and values (key,value key2,value2) are greater than or equal to\ the returned json value to determine status.') - parser.add_argument('-m', '--key_metric', dest='metric_list', nargs='*', + parser.add_argument('-m', '--key_metric', dest='metric_list', nargs='*', help='Gathers the values of these keys (key,UnitOfMeasure,Min,Max,WarnRange,CriticalRange) for Nagios performance data.\ More information about Range format and units of measure for nagios can be found at https://nagios-plugins.org/doc/guidelines.html\ Additional formats for this parameter are: (key), (key,UnitOfMeasure), (key,UnitOfMeasure,Min,Max).') @@ -263,9 +263,10 @@ if __name__ == "__main__": processor = JsonRuleProcessor(data, args) is_alive, reason = processor.isAlive() + # Gather metrics for display + nagios.performance_data = processor.getMetrics() + if is_alive: - # Rules all passed, attempt to get performance data - nagios.performance_data = processor.getMetrics() nagios.ok("Status OK.") else: nagios.warning("Status check failed, reason:%s" % reason)