Port to Python3 (#48).

I used 2to3 script and then I clean up result of the conversion.
This commit is contained in:
Martin Liska 2020-01-28 12:10:42 +01:00
parent 2541223cde
commit 2289fb2af3
No known key found for this signature in database
GPG Key ID: 4DC182DC0FA73785
2 changed files with 12 additions and 12 deletions

View File

@ -191,7 +191,7 @@ More info about Nagios Range format and Units of Measure can be found at [https:
### Requirements ### Requirements
* Python 2.7 * Python 3
### Configuration ### Configuration

View File

@ -1,4 +1,4 @@
#!/usr/bin/python2.7 #!/usr/bin/env python3
plugin_description = \ plugin_description = \
""" """
@ -9,15 +9,15 @@ argument specified rules and determines the status and performance data for
that service. that service.
""" """
import urllib2 import urllib.request, urllib.error, urllib.parse
import base64 import base64
import json import json
import argparse import argparse
import sys import sys
import ssl import ssl
from pprint import pprint from pprint import pprint
from urllib2 import HTTPError from urllib.error import HTTPError
from urllib2 import URLError from urllib.error import URLError
OK_CODE = 0 OK_CODE = 0
WARNING_CODE = 1 WARNING_CODE = 1
@ -69,8 +69,8 @@ class NagiosHelper:
def append_unknown(self, unknown_message): def append_unknown(self, unknown_message):
self.unknown_message += unknown_message self.unknown_message += unknown_message
def append_metrics(self, (performance_data, def append_metrics(self, metrics):
warning_message, critical_message)): (performance_data, warning_message, critical_message) = metrics
self.performance_data += performance_data self.performance_data += performance_data
self.append_warning(warning_message) self.append_warning(warning_message)
self.append_critical(critical_message) self.append_critical(critical_message)
@ -734,7 +734,7 @@ if __name__ == "__main__":
debugPrint(args.debug, "url:%s" % url) debugPrint(args.debug, "url:%s" % url)
json_data = '' json_data = ''
try: try:
req = urllib2.Request(url) req = urllib.request.Request(url)
req.add_header("User-Agent", "check_http_json") req.add_header("User-Agent", "check_http_json")
if args.auth: if args.auth:
base64str = base64.encodestring(args.auth).replace('\n', '') base64str = base64.encodestring(args.auth).replace('\n', '')
@ -745,15 +745,15 @@ if __name__ == "__main__":
for header in headers: for header in headers:
req.add_header(header, headers[header]) req.add_header(header, headers[header])
if args.timeout and args.data: 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) data=args.data, context=context)
elif args.timeout: elif args.timeout:
response = urllib2.urlopen(req, timeout=args.timeout, response = urllib.request.urlopen(req, timeout=args.timeout,
context=context) context=context)
elif args.data: elif args.data:
response = urllib2.urlopen(req, data=args.data, context=context) response = urllib.request.urlopen(req, data=args.data, context=context)
else: else:
response = urllib2.urlopen(req, context=context) response = urllib.request.urlopen(req, context=context)
json_data = response.read() json_data = response.read()