From 6e1e1df61c96f3acca5368905621f8d489f440e0 Mon Sep 17 00:00:00 2001 From: Napsty Date: Fri, 14 Feb 2025 13:20:42 +0100 Subject: [PATCH] Update to newer pywbem exception call --- check_esxi_hardware.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/check_esxi_hardware.py b/check_esxi_hardware.py index 6896c94..1b8446d 100755 --- a/check_esxi_hardware.py +++ b/check_esxi_hardware.py @@ -309,6 +309,7 @@ import re import json from optparse import OptionParser,OptionGroup +from packaging.version import Version version = '20250214' @@ -741,6 +742,16 @@ def handler(signum, frame): verboseoutput("Connection to "+hosturl) wbemclient = pywbem.WBEMConnection(hosturl, (user,password), NS, no_verification=True) +# Backward compatibility for pywbem <= 1.0.0 +if Version(pywbemversion) >= Version("1.0.0"): + verboseoutput("pywbem is 1.0.0 or newer") + import pywbem._cim_operations as PywbemCimOperations + import pywbem._cim_http as PywbemCimHttp +else: + verboseoutput("pywbem is older than 1.0.0") + import pywbem.cim_operations as PywbemCimOperations + import pywbem.cim_http as PywbemCimHttp + # Add a timeout for the script. When using with Nagios, the Nagios timeout cannot be < than plugin timeout. if on_windows == False and timeout > 0: signal.signal(signal.SIGALRM, handler) @@ -758,7 +769,7 @@ def handler(signum, frame): if vendor=='auto': try: c=wbemclient.EnumerateInstances('CIM_Chassis') - except pywbem._cim_operations.CIMError as args: + except PywbemCimOperations.CIMError as args: if ( args[1].find('Socket error') >= 0 ): print("UNKNOWN: {}".format(args)) sys.exit (ExitUnknown) @@ -771,7 +782,7 @@ def handler(signum, frame): GlobalStatus = ExitUnknown print("UNKNOWN: {}".format(args)) sys.exit (GlobalStatus) - except pywbem._cim_http.AuthError as arg: + except PywbemCimHttp.AuthError as arg: verboseoutput("Global exit set to UNKNOWN") GlobalStatus = ExitUnknown print("UNKNOWN: Authentication Error") @@ -793,7 +804,7 @@ def handler(signum, frame): verboseoutput("Check classe "+classe) try: instance_list = wbemclient.EnumerateInstances(classe) - except pywbem._cim_operations.CIMError as args: + except PywbemCimOperations.CIMError as args: if ( args[1].find('Socket error') >= 0 ): print("UNKNOWN: {}".format(args)) sys.exit (ExitUnknown) @@ -806,7 +817,7 @@ def handler(signum, frame): GlobalStatus = ExitUnknown print("UNKNOWN: {}".format(args)) sys.exit (GlobalStatus) - except pywbem._cim_http.AuthError as arg: + except PywbemCimHttp.AuthError as arg: verboseoutput("Global exit set to UNKNOWN") GlobalStatus = ExitUnknown print("UNKNOWN: Authentication Error")