Pārlūkot izejas kodu

Update 'localchecks/edir-version.py'

Michael Honkoop 3 dienas atpakaļ
vecāks
revīzija
1da7e7e738
1 mainītis faili ar 37 papildinājumiem un 5 dzēšanām
  1. 37 5
      localchecks/edir-version.py

+ 37 - 5
localchecks/edir-version.py

@@ -6,23 +6,55 @@
 
 # Version 0.1
 # - Initial release
+# Version 0.2
+# - refined routines to cover output more accurately.
+
 
 # A similar check is available in this repository written in bash (edir-version.sh)
 #
+# If you want more then just a version-check on eDirectory, consider switching to the plugin/special agent for eDirectory.
+
 # This check should be placed in /usr/lib/check_mk_agent/local and made executable with `chmod +x edir-version.py`
 # The script assumes that you have exported the environment variables when installing eDirectory, if not it will fail/not get detected on discovery.
 
 import subprocess
+import sys
 
 # The process to be executed
 myexec = "ndsd"
 
-# Attempt to execute the process, if successful output CheckMK formatted data, if not return nothing.
+# the displayed service in CheckMK
+service = "eDirectory"
+
+# Attempt to run the executable, if successful output CheckMK formatted data, if not found return unknown status.
 try:
-    output = subprocess.check_output([myexec, "--version"])
-    output_arr = output.split(' ')
-    print("0 \"" +  output_arr[1] + "\" - Binary Version: " + output_arr[2] + ", DS Version: " + output_arr[3])
+    output = subprocess.check_output(
+        [myexec, "--version"],
+        stderr=subprocess.STDOUT,
+        universal_newlines=True
+    )
+
+    output_arr = output.split()
+
+# output status when the executable returns expected result
+    if len(output_arr) >= 4:
+        print('0 "eDirectory" - Binary Version: {}, DS Version: {}'.format(
+            output_arr[2], output_arr[3]
+        ))
+    else:
+        print('3 "eDirectory" - Unable to parse version output')
 
+# Executable not found handling
+except FileNotFoundError:
+    print('3 "eDirectory" - ndsd executable not found')
+    exit()
+
+# Executable exists, but returned an error handling
 except subprocess.CalledProcessError as e:
-    print("3 \"" + e)
+    print('2 "eDirectory" - ndsd returned error: {}'.format(e.returncode))
+    exit()
+
+    # Catch unexpected errors
+except Exception as e:
+    print('3 "eDirectory" - Unexpected error: {}'.format(e))
     exit()