From 2289fb2af37b2f54da98a0c03173309fa72ed78c Mon Sep 17 00:00:00 2001 From: Martin Liska Date: Tue, 28 Jan 2020 12:10:42 +0100 Subject: [PATCH] Port to Python3 (#48). I used 2to3 script and then I clean up result of the conversion. --- README.md | 2 +- check_http_json.py | 22 +++++++++++----------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index b021554..d6f450e 100644 --- a/README.md +++ b/README.md @@ -191,7 +191,7 @@ More info about Nagios Range format and Units of Measure can be found at [https: ### Requirements -* Python 2.7 +* Python 3 ### Configuration diff --git a/check_http_json.py b/check_http_json.py index d743e6b..b89ed65 100755 --- a/check_http_json.py +++ b/check_http_json.py @@ -1,4 +1,4 @@ -#!/usr/bin/python2.7 +#!/usr/bin/env python3 plugin_description = \ """ @@ -9,15 +9,15 @@ argument specified rules and determines the status and performance data for that service. """ -import urllib2 +import urllib.request, urllib.error, urllib.parse import base64 import json import argparse import sys import ssl from pprint import pprint -from urllib2 import HTTPError -from urllib2 import URLError +from urllib.error import HTTPError +from urllib.error import URLError OK_CODE = 0 WARNING_CODE = 1 @@ -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, metrics): + (performance_data, warning_message, critical_message) = metrics self.performance_data += performance_data self.append_warning(warning_message) self.append_critical(critical_message) @@ -734,7 +734,7 @@ if __name__ == "__main__": debugPrint(args.debug, "url:%s" % url) json_data = '' try: - req = urllib2.Request(url) + req = urllib.request.Request(url) req.add_header("User-Agent", "check_http_json") if args.auth: base64str = base64.encodestring(args.auth).replace('\n', '') @@ -745,15 +745,15 @@ if __name__ == "__main__": for header in headers: req.add_header(header, headers[header]) if args.timeout and args.data: - response = urllib2.urlopen(req, timeout=args.timeout, + response = urllib.request.urlopen(req, timeout=args.timeout, data=args.data, context=context) elif args.timeout: - response = urllib2.urlopen(req, timeout=args.timeout, + response = urllib.request.urlopen(req, timeout=args.timeout, context=context) elif args.data: - response = urllib2.urlopen(req, data=args.data, context=context) + response = urllib.request.urlopen(req, data=args.data, context=context) else: - response = urllib2.urlopen(req, context=context) + response = urllib.request.urlopen(req, context=context) json_data = response.read()