From 47547951cfa03b708340fed23710cb78a7685663 Mon Sep 17 00:00:00 2001 From: Ricardo Bartels Date: Thu, 9 May 2019 16:39:41 +0200 Subject: [PATCH] fixed minor bugs and added compatibility for RHEL/CentOS 7.x * change ssl.PROTOCOL_TLS to ssl.PROTOCOL_SSLv23 * fixed bug that response var not passed outside try/except block * fixed arrer in nagios.append_metrics() --- check_http_json.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/check_http_json.py b/check_http_json.py index bc7b14f..c5b7960 100755 --- a/check_http_json.py +++ b/check_http_json.py @@ -69,8 +69,8 @@ class NagiosHelper: def append_unknown(self, unknown_message): self.unknown_message += unknown_message - def append_metrics(self, performance_data, - warning_message, critical_message): + def append_metrics(self, (performance_data, + warning_message, critical_message)): self.performance_data += performance_data self.append_warning(warning_message) self.append_critical(critical_message) @@ -689,7 +689,7 @@ if __name__ == "__main__": if args.ssl: url = "https://%s" % args.host - context = ssl.SSLContext(ssl.PROTOCOL_TLS) + context = ssl.SSLContext(ssl.PROTOCOL_SSLv23) context.options |= ssl.OP_NO_SSLv2 context.options |= ssl.OP_NO_SSLv3 @@ -729,6 +729,7 @@ if __name__ == "__main__": if args.path: url += "/%s" % args.path debugPrint(args.debug, "url:%s" % url) + json_data = '' try: req = urllib2.Request(url) req.add_header("User-Agent", "check_http_json") @@ -750,13 +751,16 @@ if __name__ == "__main__": response = urllib2.urlopen(req, data=args.data, context=context) else: response = urllib2.urlopen(req, context=context) + + json_data = response.read() + except HTTPError as e: nagios.append_unknown(" HTTPError[%s], url:%s" % (str(e.code), url)) except URLError as e: nagios.append_critical(" URLError[%s], url:%s" % (str(e.reason), url)) try: - data = json.loads(response.read()) + data = json.loads(json_data) except ValueError as e: nagios.append_unknown(" Parser error: %s" % str(e))