Skip to content

Commit

Permalink
Improve pywbem exception handling (#75)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Napsty authored Feb 21, 2025
1 parent 7b02dda commit 8b6917f
Showing 1 changed file with 34 additions and 8 deletions.
42 changes: 34 additions & 8 deletions check_esxi_hardware.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
# Copyright (c) 2008 David Ligeret
# Copyright (c) 2009 Joshua Daniel Franklin
# Copyright (c) 2010 Branden Schneider
# Copyright (c) 2010-2024 Claudio Kuenzler
# Copyright (c) 2010-2025 Claudio Kuenzler
# Copyright (c) 2010 Samir Ibradzic
# Copyright (c) 2010 Aaron Rogers
# Copyright (c) 2011 Ludovic Hutin
Expand Down Expand Up @@ -298,15 +298,21 @@
# Remove python2 compatibility
# Remove pywbem 0.7.0 compatibility
#@---------------------------------------------------
#@ Date : 20250221
#@ Author : Claudio Kuenzler
#@ Reason : Update to newer pywbem exception call, catch HTTPError
#@ Attn : Requires 'packaging' Python module from now on!
#@---------------------------------------------------

import sys
import time
import pywbem
import re
import json
from optparse import OptionParser,OptionGroup
from packaging.version import Version

version = '20241129'
version = '20250221'

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

# Backward compatibility for older pywbem exceptions, big thanks to Claire M.!
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
import pywbem._exceptions as PywbemExceptions
else:
verboseoutput("pywbem is older than 1.0.0")
import pywbem.cim_operations as PywbemCimOperations
import pywbem.cim_http as PywbemCimHttp
import pywbem.exceptions as PywbemExceptions

# 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)
Expand All @@ -754,7 +772,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)
Expand All @@ -763,11 +781,15 @@ def handler(signum, frame):
sys.exit (ExitUnknown)
else:
verboseoutput("Unknown CIM Error: %s" % args)
except pywbem._exceptions.ConnectionError as args:
except PywbemExceptions.ConnectionError as args:
GlobalStatus = ExitUnknown
print("UNKNOWN: {}".format(args))
sys.exit (GlobalStatus)
except PywbemExceptions.HTTPError as args:
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")
Expand All @@ -789,7 +811,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)
Expand All @@ -798,11 +820,15 @@ def handler(signum, frame):
sys.exit (ExitUnknown)
else:
verboseoutput("Unknown CIM Error: %s" % args)
except pywbem._exceptions.ConnectionError as args:
except PywbemExceptions.ConnectionError as args:
GlobalStatus = ExitUnknown
print("UNKNOWN: {}".format(args))
sys.exit (GlobalStatus)
except PywbemExceptions.HTTPError as args:
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")
Expand Down

0 comments on commit 8b6917f

Please sign in to comment.