From 426bd0fe4a6a396ab9549aaba20a423fb8052de4 Mon Sep 17 00:00:00 2001 From: Maciej Sawicki Date: Fri, 23 May 2014 01:39:56 +0200 Subject: [PATCH 1/4] Update check_http_json.py casting compared values to floats instead of strings. String comparison uses lexicographical ordering: https://docs.python.org/2/tutorial/datastructures.html#comparing-sequences-and-other-types. It could cause unexpected behaviours. --- check_http_json.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/check_http_json.py b/check_http_json.py index 2db2fd1..cb98427 100755 --- a/check_http_json.py +++ b/check_http_json.py @@ -44,8 +44,8 @@ class JsonHelper: self.data = json_data def equals(self, key, value): return self.exists(key) and str(self.get(key)) == value - def lte(self, key, value): return self.exists(key) and str(self.get(key)) <= value - def gte(self, key, value): return self.exists(key) and str(self.get(key)) >= value + 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')) def get(self, key, temp_data=''): """Can navigate nested json keys with a dot format (Element.Key.NestedKey). Returns (None, 'not_found') if not found""" @@ -205,4 +205,4 @@ if __name__ == "__main__": # Print Nagios specific string and exit appropriately print nagios.getMessage() - exit(nagios.code) \ No newline at end of file + exit(nagios.code) From 0241480494653c38878092869b4de61defffc6ec Mon Sep 17 00:00:00 2001 From: Maciej Sawicki Date: Fri, 23 May 2014 02:33:22 +0200 Subject: [PATCH 2/4] Update check_http_json.py --- check_http_json.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/check_http_json.py b/check_http_json.py index cb98427..7bc4daf 100755 --- a/check_http_json.py +++ b/check_http_json.py @@ -11,6 +11,8 @@ import httplib, urllib, urllib2 import json import argparse from pprint import pprint +from urllib2 import HTTPError +from urllib2 import URLError class NagiosHelper: From 1c57ed3360e7eb54199a62ab11c9d7da799343be Mon Sep 17 00:00:00 2001 From: Chris Proto Date: Thu, 12 Jun 2014 12:13:38 -0600 Subject: [PATCH 3/4] Add Basic Auth support --- check_http_json.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/check_http_json.py b/check_http_json.py index 2db2fd1..927f3ba 100755 --- a/check_http_json.py +++ b/check_http_json.py @@ -7,7 +7,7 @@ Generic Nagios plugin which checks json values from a given endpoint against arg and determines the status and performance data for that service. """ -import httplib, urllib, urllib2 +import httplib, urllib, urllib2, base64 import json import argparse from pprint import pprint @@ -142,6 +142,7 @@ def parseArgs(): and determines the status and performance data for that service') parser.add_argument('-H', '--host', dest='host', required=True, help='Host.') + 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('-e', '--key_exists', dest='key_list', nargs='*', help='Checks existence of these keys to determine status.') @@ -180,6 +181,9 @@ if __name__ == "__main__": # Attempt to reach the endpoint try: req = urllib2.Request(url) + if args.auth: + base64str = base64.encodestring(args.auth).replace('\n', '') + req.add_header('Authorization', 'Basic %s' % base64str) response = urllib2.urlopen(req) except HTTPError as e: nagios.unknown("HTTPError[%s], url:%s" % (str(e.code), url)) @@ -205,4 +209,4 @@ if __name__ == "__main__": # Print Nagios specific string and exit appropriately print nagios.getMessage() - exit(nagios.code) \ No newline at end of file + exit(nagios.code) From c50f0c2e7491ede127f868bafcc042dd13ec876c Mon Sep 17 00:00:00 2001 From: Rob Wilson Date: Mon, 9 Feb 2015 10:20:22 +0000 Subject: [PATCH 4/4] add https option --- README.md | 4 +++- check_http_json.py | 9 +++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 1151b02..ee60a55 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,8 @@ usage: check_http_json.py [-h] -H HOST [-p PATH] [-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 ...]]] [-d] + [-m [METRIC_LIST [METRIC_LIST ...]]] [-s] [-d] + Nagios plugin which checks json values from a given endpoint against argument specified rules and determines the status and performance data for that @@ -84,6 +85,7 @@ optional arguments: plugins.org/doc/guidelines.html Additional formats for this parameter are: (key), (key,UnitOfMeasure), (key,UnitOfMeasure,Min,Max). + -s, --ssl HTTPS mode. -d, --debug Debug mode. ``` diff --git a/check_http_json.py b/check_http_json.py index 2db2fd1..61096f7 100755 --- a/check_http_json.py +++ b/check_http_json.py @@ -157,6 +157,7 @@ def parseArgs(): 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).') + parser.add_argument('-s', '--ssl', action='store_true', help='HTTPS mode.') parser.add_argument('-d', '--debug', action='store_true', help='Debug mode.') return parser.parse_args() @@ -173,7 +174,11 @@ if __name__ == "__main__": args = parseArgs() nagios = NagiosHelper() - url = "http://%s" % args.host + if args.ssl: + url = "https://%s" % args.host + else: + url = "http://%s" % args.host + if args.path: url += "/%s" % args.path debugPrint(args.debug, "url:%s" % url) @@ -205,4 +210,4 @@ if __name__ == "__main__": # Print Nagios specific string and exit appropriately print nagios.getMessage() - exit(nagios.code) \ No newline at end of file + exit(nagios.code)