Skip to content

Commit 8b6917f

Browse files
authored
Improve pywbem exception handling (#75)
This PR improves exception handling of the `pywbem` module, which uses different exception calls before and starting with pywbem 1.0.0. This allows correct exception handling with older `pywbem` versions (< 1.0.0) and newer versions. The PR also adds exception handling for HTTP exception, when a HTTP server responds on the requested host and port, but it's not an ESXi CIM server.
1 parent 7b02dda commit 8b6917f

1 file changed

Lines changed: 34 additions & 8 deletions

File tree

check_esxi_hardware.py

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
# Copyright (c) 2008 David Ligeret
2323
# Copyright (c) 2009 Joshua Daniel Franklin
2424
# Copyright (c) 2010 Branden Schneider
25-
# Copyright (c) 2010-2024 Claudio Kuenzler
25+
# Copyright (c) 2010-2025 Claudio Kuenzler
2626
# Copyright (c) 2010 Samir Ibradzic
2727
# Copyright (c) 2010 Aaron Rogers
2828
# Copyright (c) 2011 Ludovic Hutin
@@ -298,15 +298,21 @@
298298
# Remove python2 compatibility
299299
# Remove pywbem 0.7.0 compatibility
300300
#@---------------------------------------------------
301+
#@ Date : 20250221
302+
#@ Author : Claudio Kuenzler
303+
#@ Reason : Update to newer pywbem exception call, catch HTTPError
304+
#@ Attn : Requires 'packaging' Python module from now on!
305+
#@---------------------------------------------------
301306

302307
import sys
303308
import time
304309
import pywbem
305310
import re
306311
import json
307312
from optparse import OptionParser,OptionGroup
313+
from packaging.version import Version
308314

309-
version = '20241129'
315+
version = '20250221'
310316

311317
NS = 'root/cimv2'
312318
hosturl = ''
@@ -737,6 +743,18 @@ def handler(signum, frame):
737743
verboseoutput("Connection to "+hosturl)
738744
wbemclient = pywbem.WBEMConnection(hosturl, (user,password), NS, no_verification=True)
739745

746+
# Backward compatibility for older pywbem exceptions, big thanks to Claire M.!
747+
if Version(pywbemversion) >= Version("1.0.0"):
748+
verboseoutput("pywbem is 1.0.0 or newer")
749+
import pywbem._cim_operations as PywbemCimOperations
750+
import pywbem._cim_http as PywbemCimHttp
751+
import pywbem._exceptions as PywbemExceptions
752+
else:
753+
verboseoutput("pywbem is older than 1.0.0")
754+
import pywbem.cim_operations as PywbemCimOperations
755+
import pywbem.cim_http as PywbemCimHttp
756+
import pywbem.exceptions as PywbemExceptions
757+
740758
# Add a timeout for the script. When using with Nagios, the Nagios timeout cannot be < than plugin timeout.
741759
if on_windows == False and timeout > 0:
742760
signal.signal(signal.SIGALRM, handler)
@@ -754,7 +772,7 @@ def handler(signum, frame):
754772
if vendor=='auto':
755773
try:
756774
c=wbemclient.EnumerateInstances('CIM_Chassis')
757-
except pywbem.cim_operations.CIMError as args:
775+
except PywbemCimOperations.CIMError as args:
758776
if ( args[1].find('Socket error') >= 0 ):
759777
print("UNKNOWN: {}".format(args))
760778
sys.exit (ExitUnknown)
@@ -763,11 +781,15 @@ def handler(signum, frame):
763781
sys.exit (ExitUnknown)
764782
else:
765783
verboseoutput("Unknown CIM Error: %s" % args)
766-
except pywbem._exceptions.ConnectionError as args:
784+
except PywbemExceptions.ConnectionError as args:
785+
GlobalStatus = ExitUnknown
786+
print("UNKNOWN: {}".format(args))
787+
sys.exit (GlobalStatus)
788+
except PywbemExceptions.HTTPError as args:
767789
GlobalStatus = ExitUnknown
768790
print("UNKNOWN: {}".format(args))
769791
sys.exit (GlobalStatus)
770-
except pywbem.cim_http.AuthError as arg:
792+
except PywbemCimHttp.AuthError as arg:
771793
verboseoutput("Global exit set to UNKNOWN")
772794
GlobalStatus = ExitUnknown
773795
print("UNKNOWN: Authentication Error")
@@ -789,7 +811,7 @@ def handler(signum, frame):
789811
verboseoutput("Check classe "+classe)
790812
try:
791813
instance_list = wbemclient.EnumerateInstances(classe)
792-
except pywbem._cim_operations.CIMError as args:
814+
except PywbemCimOperations.CIMError as args:
793815
if ( args[1].find('Socket error') >= 0 ):
794816
print("UNKNOWN: {}".format(args))
795817
sys.exit (ExitUnknown)
@@ -798,11 +820,15 @@ def handler(signum, frame):
798820
sys.exit (ExitUnknown)
799821
else:
800822
verboseoutput("Unknown CIM Error: %s" % args)
801-
except pywbem._exceptions.ConnectionError as args:
823+
except PywbemExceptions.ConnectionError as args:
824+
GlobalStatus = ExitUnknown
825+
print("UNKNOWN: {}".format(args))
826+
sys.exit (GlobalStatus)
827+
except PywbemExceptions.HTTPError as args:
802828
GlobalStatus = ExitUnknown
803829
print("UNKNOWN: {}".format(args))
804830
sys.exit (GlobalStatus)
805-
except pywbem._cim_http.AuthError as arg:
831+
except PywbemCimHttp.AuthError as arg:
806832
verboseoutput("Global exit set to UNKNOWN")
807833
GlobalStatus = ExitUnknown
808834
print("UNKNOWN: Authentication Error")

0 commit comments

Comments
 (0)