1
0
mirror of https://github.com/Napsty/check_esxi_hardware.git synced 2026-02-06 15:15:20 +01:00

3 Commits

Author SHA1 Message Date
Napsty
4f3e202c5f Bump version 2025-07-16 07:54:18 +02:00
Peter Newman
6a41af4a60 Change exit code from -1/255 to 3 to match Nagios plugin specs (#76) 2025-07-16 07:50:52 +02:00
Claudio Kuenzler
8b6917f0ca 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.
2025-02-21 11:08:04 +01:00

View File

@@ -22,7 +22,7 @@
# Copyright (c) 2008 David Ligeret # Copyright (c) 2008 David Ligeret
# Copyright (c) 2009 Joshua Daniel Franklin # Copyright (c) 2009 Joshua Daniel Franklin
# Copyright (c) 2010 Branden Schneider # 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 Samir Ibradzic
# Copyright (c) 2010 Aaron Rogers # Copyright (c) 2010 Aaron Rogers
# Copyright (c) 2011 Ludovic Hutin # Copyright (c) 2011 Ludovic Hutin
@@ -36,7 +36,7 @@
# Copyright (c) 2015 Andreas Gottwald # Copyright (c) 2015 Andreas Gottwald
# Copyright (c) 2015 Stanislav German-Evtushenko # Copyright (c) 2015 Stanislav German-Evtushenko
# Copyright (c) 2015 Stefan Roos # Copyright (c) 2015 Stefan Roos
# Copyright (c) 2018 Peter Newman # Copyright (c) 2018,2025 Peter Newman
# Copyright (c) 2020 Luca Berra # Copyright (c) 2020 Luca Berra
# Copyright (c) 2022 Marco Markgraf # Copyright (c) 2022 Marco Markgraf
# #
@@ -50,7 +50,7 @@
# https://www.claudiokuenzler.com/monitoring-plugins/check_esxi_hardware.php # https://www.claudiokuenzler.com/monitoring-plugins/check_esxi_hardware.php
# #
#@--------------------------------------------------- #@---------------------------------------------------
#@ History #@ History / ChangeLog
#@--------------------------------------------------- #@---------------------------------------------------
#@ Date : 20080820 #@ Date : 20080820
#@ Author : David Ligeret #@ Author : David Ligeret
@@ -298,6 +298,15 @@
# Remove python2 compatibility # Remove python2 compatibility
# Remove pywbem 0.7.0 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!
#@---------------------------------------------------
#@ Date : 20250716
#@ Author : Peter Newman
#@ Reason : Adjust exit code -1 to 3 (Nagios UNKNOWN)
#@---------------------------------------------------
import sys import sys
import time import time
@@ -305,8 +314,9 @@ import pywbem
import re import re
import json import json
from optparse import OptionParser,OptionGroup from optparse import OptionParser,OptionGroup
from packaging.version import Version
version = '20241129' version = '20250716'
NS = 'root/cimv2' NS = 'root/cimv2'
hosturl = '' hosturl = ''
@@ -601,14 +611,14 @@ def getopts() :
if len(sys.argv) < 2: if len(sys.argv) < 2:
print("no parameters specified\n") print("no parameters specified\n")
parser.print_help() parser.print_help()
sys.exit(-1) sys.exit(3)
# if first argument starts with 'https://' we have old-style parameters, so handle in old way # if first argument starts with 'https://' we have old-style parameters, so handle in old way
if re.match("https://",sys.argv[1]): if re.match("https://",sys.argv[1]):
# check input arguments # check input arguments
if len(sys.argv) < 5: if len(sys.argv) < 5:
print("too few parameters\n") print("too few parameters\n")
parser.print_help() parser.print_help()
sys.exit(-1) sys.exit(3)
if len(sys.argv) > 5 : if len(sys.argv) > 5 :
if sys.argv[5] == "verbose" : if sys.argv[5] == "verbose" :
verbose = True verbose = True
@@ -626,7 +636,7 @@ def getopts() :
if not options.__dict__[m]: if not options.__dict__[m]:
print("mandatory option '" + m + "' not defined. read usage in help.\n") print("mandatory option '" + m + "' not defined. read usage in help.\n")
parser.print_help() parser.print_help()
sys.exit(-1) sys.exit(3)
hostname=options.host.lower() hostname=options.host.lower()
# if user has put "https://" in front of hostname out of habit, do the right thing # if user has put "https://" in front of hostname out of habit, do the right thing
@@ -737,6 +747,18 @@ verboseoutput("Found pywbem version "+pywbemversion)
verboseoutput("Connection to "+hosturl) verboseoutput("Connection to "+hosturl)
wbemclient = pywbem.WBEMConnection(hosturl, (user,password), NS, no_verification=True) 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. # 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: if on_windows == False and timeout > 0:
signal.signal(signal.SIGALRM, handler) signal.signal(signal.SIGALRM, handler)
@@ -754,7 +776,7 @@ ExitMsg = ""
if vendor=='auto': if vendor=='auto':
try: try:
c=wbemclient.EnumerateInstances('CIM_Chassis') c=wbemclient.EnumerateInstances('CIM_Chassis')
except pywbem.cim_operations.CIMError as args: except PywbemCimOperations.CIMError as args:
if ( args[1].find('Socket error') >= 0 ): if ( args[1].find('Socket error') >= 0 ):
print("UNKNOWN: {}".format(args)) print("UNKNOWN: {}".format(args))
sys.exit (ExitUnknown) sys.exit (ExitUnknown)
@@ -763,11 +785,15 @@ if vendor=='auto':
sys.exit (ExitUnknown) sys.exit (ExitUnknown)
else: else:
verboseoutput("Unknown CIM Error: %s" % args) verboseoutput("Unknown CIM Error: %s" % args)
except pywbem._exceptions.ConnectionError as args: except PywbemExceptions.ConnectionError as args:
GlobalStatus = ExitUnknown GlobalStatus = ExitUnknown
print("UNKNOWN: {}".format(args)) print("UNKNOWN: {}".format(args))
sys.exit (GlobalStatus) sys.exit (GlobalStatus)
except pywbem.cim_http.AuthError as arg: except PywbemExceptions.HTTPError as args:
GlobalStatus = ExitUnknown
print("UNKNOWN: {}".format(args))
sys.exit (GlobalStatus)
except PywbemCimHttp.AuthError as arg:
verboseoutput("Global exit set to UNKNOWN") verboseoutput("Global exit set to UNKNOWN")
GlobalStatus = ExitUnknown GlobalStatus = ExitUnknown
print("UNKNOWN: Authentication Error") print("UNKNOWN: Authentication Error")
@@ -789,7 +815,7 @@ for classe in ClassesToCheck :
verboseoutput("Check classe "+classe) verboseoutput("Check classe "+classe)
try: try:
instance_list = wbemclient.EnumerateInstances(classe) instance_list = wbemclient.EnumerateInstances(classe)
except pywbem._cim_operations.CIMError as args: except PywbemCimOperations.CIMError as args:
if ( args[1].find('Socket error') >= 0 ): if ( args[1].find('Socket error') >= 0 ):
print("UNKNOWN: {}".format(args)) print("UNKNOWN: {}".format(args))
sys.exit (ExitUnknown) sys.exit (ExitUnknown)
@@ -798,11 +824,15 @@ for classe in ClassesToCheck :
sys.exit (ExitUnknown) sys.exit (ExitUnknown)
else: else:
verboseoutput("Unknown CIM Error: %s" % args) verboseoutput("Unknown CIM Error: %s" % args)
except pywbem._exceptions.ConnectionError as args: except PywbemExceptions.ConnectionError as args:
GlobalStatus = ExitUnknown GlobalStatus = ExitUnknown
print("UNKNOWN: {}".format(args)) print("UNKNOWN: {}".format(args))
sys.exit (GlobalStatus) sys.exit (GlobalStatus)
except pywbem._cim_http.AuthError as arg: except PywbemExceptions.HTTPError as args:
GlobalStatus = ExitUnknown
print("UNKNOWN: {}".format(args))
sys.exit (GlobalStatus)
except PywbemCimHttp.AuthError as arg:
verboseoutput("Global exit set to UNKNOWN") verboseoutput("Global exit set to UNKNOWN")
GlobalStatus = ExitUnknown GlobalStatus = ExitUnknown
print("UNKNOWN: Authentication Error") print("UNKNOWN: Authentication Error")