]> git.michaelhowe.org Git - packages/n/nagios-plugins-local.git/commitdiff
Update remaining plugins to use python3 0.25
authorMichael Howe <michael@michaelhowe.org>
Tue, 21 Dec 2021 11:09:20 +0000 (11:09 +0000)
committerMichael Howe <michael@michaelhowe.org>
Tue, 21 Dec 2021 11:09:20 +0000 (11:09 +0000)
debian/changelog
debian/control
plugins/check_md_raid
plugins/check_monit

index 6db357258c96bcb4c4c5f77a116b3bd3f41547e4..2138a33ef926a622d80702cb211d6ff03d780507 100644 (file)
@@ -1,3 +1,9 @@
+nagios-plugins-local (0.25) unstable; urgency=medium
+
+  * Update remaining nagios plugins to python3, and also modernise them
+
+ -- Michael Howe <michael@michaelhowe.org>  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 
index 5ebadaa483f00ece1148bd43beb9a973c60760fb..5a463e0a538bc14294fd71d8bdf5e9739698f3b8 100644 (file)
@@ -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
index ee3109e14fedab7cf989d695cf28ff82158d39cf..fe64243b669722cdd85486dd858d2c4884fc4abf 100755 (executable)
@@ -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)
index 3abe13b7cb632f2a262606c6fcd85f6ce13b3bcc..b3416fa4045439d18ee7ff04967412472aa480cd 100755 (executable)
@@ -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)