From: Michael Howe Date: Tue, 21 Dec 2021 11:09:20 +0000 (+0000) Subject: Update remaining plugins to use python3 X-Git-Tag: 0.25^0 X-Git-Url: https://git.michaelhowe.org/gitweb/?a=commitdiff_plain;h=c19d416a4545af45ded5914bc056183880d2a878;p=packages%2Fn%2Fnagios-plugins-local.git Update remaining plugins to use python3 --- diff --git a/debian/changelog b/debian/changelog index 6db3572..2138a33 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,9 @@ +nagios-plugins-local (0.25) unstable; urgency=medium + + * Update remaining nagios plugins to python3, and also modernise them + + -- Michael Howe Tue, 21 Dec 2021 11:09:05 +0000 + nagios-plugins-local (0.24) unstable; urgency=medium * Update nagios-plugins-local-server checks to support python3 diff --git a/debian/control b/debian/control index 5ebadaa..5a463e0 100644 --- a/debian/control +++ b/debian/control @@ -33,13 +33,14 @@ Package: nagios-plugins-local-client Architecture: any Depends: ${misc:Depends}, perl, - python, + python3, gnutls-bin, libmonitoring-plugin-perl, libtimedate-perl, liburi-perl, sysstat, bc + , python3-requests Breaks: nagios-plugins-local (<0.11) Description: Local nagios plugins (NRPE hosts) Nagios plugins customized for use on michaelhowe.org hosts, likely to need to diff --git a/plugins/check_md_raid b/plugins/check_md_raid index ee3109e..fe64243 100755 --- a/plugins/check_md_raid +++ b/plugins/check_md_raid @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # # Copyright Hari Sekhon 2007 # @@ -25,7 +25,7 @@ __version__ = "0.7.2" import os import re import sys -from optparse import OptionParser +import argparse # Standard Nagios return codes OK = 0 @@ -41,16 +41,16 @@ def end(status, message): arg as the message to output""" if status == OK: - print "RAID OK: %s" % message + print("RAID OK: %s" % message) sys.exit(OK) elif status == WARNING: - print "RAID WARNING: %s" % message + print("RAID WARNING: %s" % message) sys.exit(WARNING) elif status == CRITICAL: - print "RAID CRITICAL: %s" % message + print("RAID CRITICAL: %s" % message) sys.exit(CRITICAL) else: - print "UNKNOWN: %s" % message + print("UNKNOWN: %s" % message) sys.exit(UNKNOWN) @@ -69,14 +69,14 @@ def find_arrays(verbosity): them, or exits UNKNOWN if no MD arrays are found""" if verbosity >= 3: - print "finding all MD arrays via: %s --detail --scan" % BIN + print("finding all MD arrays via: %s --detail --scan" % BIN) devices_output = os.popen("%s --detail --scan" % BIN).readlines() raid_devices = [] for line in devices_output: if "ARRAY" in line: raid_device = line.split()[1] if verbosity >= 2: - print "found array %s" % raid_device + print("found array %s" % raid_device) raid_devices.append(raid_device) if len(raid_devices) == 0: @@ -97,13 +97,13 @@ def test_raid(verbosity): number_arrays = len(raid_devices) for array in raid_devices: if verbosity >= 2: - print 'Now testing raid device "%s"' % array + print('Now testing raid device "%s"' % array) detailed_output = os.popen("%s --detail %s" % (BIN, array) ).readlines() if verbosity >= 3: for line in detailed_output: - print line, + print(line, end=' ') state = "unknown" for line in detailed_output: @@ -162,32 +162,29 @@ def test_raid(verbosity): def main(): """parses args and calls func to test MD arrays""" - parser = OptionParser() + parser = argparse.ArgumentParser() - parser.add_option( "-v", + parser.add_argument("-v", "--verbose", action="count", dest="verbosity", + default=0, help="Verbose mode. Good for testing plugin. By default\ only one result line is printed as per Nagios standards") - parser.add_option( "-V", + parser.add_argument("-V", "--version", action="store_true", dest="version", help="Print version number and exit") - (options, args) = parser.parse_args() + args = parser.parse_args() - if args: - parser.print_help() - sys.exit(UNKNOWN) - - verbosity = options.verbosity - version = options.version + verbosity = args.verbosity + version = args.version if version: - print __version__ + print(__version__) sys.exit(OK) result, message = test_raid(verbosity) diff --git a/plugins/check_monit b/plugins/check_monit index 3abe13b..b3416fa 100755 --- a/plugins/check_monit +++ b/plugins/check_monit @@ -1,9 +1,8 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # from https://gist.github.com/mzupan/2911557 -import urllib2 -import base64 +import requests import sys import optparse @@ -19,16 +18,15 @@ options, arguments = p.parse_args() check_url = "http://%s:%s/_status?format=xml" % (options.host, options.port) -request = urllib2.Request(check_url) -base64string = base64.encodestring('%s:%s' % (options.user, options.passwd)).replace('\n', '') -request.add_header("Authorization", "Basic %s" % base64string) + try: - result = urllib2.urlopen(request) -except urllib2.HTTPError: - print "Cannot connect to %s as user '%s'" % (check_url, options.user) + r = requests.get(check_url, auth=(options.user, options.passwd)) + r.raise_for_status() +except requests.exceptions.HTTPError: + print("Cannot connect to %s as user '%s'" % (check_url, options.user)) sys.exit(3) -dom = parseString("".join(result.readlines())) +dom = parseString(r.content) monitored = [] unmonitored = [] exitcode = 0 @@ -43,10 +41,10 @@ for service in dom.getElementsByTagName('service'): monitored.append(name) if len(unmonitored): - print "monit: unmonitored services: %s" % (', '.join(unmonitored)) + print("monit: unmonitored services: %s" % (', '.join(unmonitored))) exitcode = 2 else: - print "monit: no unmonitored services" + print("monit: no unmonitored services") exitcode = 0 sys.exit(exitcode)