1
0
mirror of https://github.com/Napsty/check_esxi_hardware.git synced 2024-11-24 03:13:50 +01:00

Merge pull request #30 from Napsty/python3

Python3 compatibility
This commit is contained in:
Claudio Kuenzler 2018-10-02 06:58:00 +02:00 committed by GitHub
commit df56d7373d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -256,7 +256,12 @@
#@ Author : Peter Newman #@ Author : Peter Newman
#@ Reason : Throw an unknown if we can't fetch the data for some reason #@ Reason : Throw an unknown if we can't fetch the data for some reason
#@--------------------------------------------------- #@---------------------------------------------------
#@ Date : 20181001
#@ Author : Claudio Kuenzler
#@ Reason : python3 compatibility
#@---------------------------------------------------
from __future__ import print_function
import sys import sys
import time import time
import pywbem import pywbem
@ -264,7 +269,7 @@ import re
import pkg_resources import pkg_resources
from optparse import OptionParser,OptionGroup from optparse import OptionParser,OptionGroup
version = '20180411' version = '20181001'
NS = 'root/cimv2' NS = 'root/cimv2'
hosturl = '' hosturl = ''
@ -481,7 +486,7 @@ def urlised_serialnumber(vendor,country,SerialNumber):
def verboseoutput(message) : def verboseoutput(message) :
if verbose: if verbose:
print "%s %s" % (time.strftime("%Y%m%d %H:%M:%S"), message) print(time.strftime("%Y%m%d %H:%M:%S"), message)
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
@ -532,14 +537,14 @@ def getopts() :
# check input arguments # check input arguments
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(-1)
# 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(-1)
if len(sys.argv) > 5 : if len(sys.argv) > 5 :
@ -557,7 +562,7 @@ def getopts() :
mandatories = ['host', 'user', 'password'] mandatories = ['host', 'user', 'password']
for m in mandatories: for m in mandatories:
if not options.__dict__[m]: if not options.__dict__[m]:
print "mandatory parameter '--" + m + "' is missing\n" print("mandatory parameter '--" + m + "' is missing\n")
parser.print_help() parser.print_help()
sys.exit(-1) sys.exit(-1)
@ -612,7 +617,7 @@ if os_platform != "win32":
on_windows = False on_windows = False
import signal import signal
def handler(signum, frame): def handler(signum, frame):
print 'UNKNOWN: Execution time too long!' print('UNKNOWN: Execution time too long!')
sys.exit(ExitUnknown) sys.exit(ExitUnknown)
if cimport: if cimport:
@ -670,19 +675,19 @@ ExitMsg = ""
if vendor=='auto': if vendor=='auto':
try: try:
c=wbemclient.EnumerateInstances('CIM_Chassis') c=wbemclient.EnumerateInstances('CIM_Chassis')
except pywbem.cim_operations.CIMError,args: except pywbem.cim_operations.CIMError as args:
if ( args[1].find('Socket error') >= 0 ): if ( args[1].find('Socket error') >= 0 ):
print "UNKNOWN: %s" %args print("UNKNOWN: {}".format(args))
sys.exit (ExitUnknown) sys.exit (ExitUnknown)
elif ( args[1].find('ThreadPool --- Failed to enqueue request') >= 0 ): elif ( args[1].find('ThreadPool --- Failed to enqueue request') >= 0 ):
print "UNKNOWN: %s" %args print("UNKNOWN: {}".format(args))
sys.exit (ExitUnknown) sys.exit (ExitUnknown)
else: else:
verboseoutput("Unknown CIM Error: %s" % args) verboseoutput("Unknown CIM Error: %s" % args)
except pywbem.cim_http.AuthError,arg: except pywbem.cim_http.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")
sys.exit (GlobalStatus) sys.exit (GlobalStatus)
else: else:
man=c[0][u'Manufacturer'] man=c[0][u'Manufacturer']
@ -701,19 +706,19 @@ 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,args: except pywbem.cim_operations.CIMError as args:
if ( args[1].find('Socket error') >= 0 ): if ( args[1].find('Socket error') >= 0 ):
print "UNKNOWN: %s" %args print("UNKNOWN: {}".format(args))
sys.exit (ExitUnknown) sys.exit (ExitUnknown)
elif ( args[1].find('ThreadPool --- Failed to enqueue request') >= 0 ): elif ( args[1].find('ThreadPool --- Failed to enqueue request') >= 0 ):
print "UNKNOWN: %s" %args print("UNKNOWN: {}".format(args))
sys.exit (ExitUnknown) sys.exit (ExitUnknown)
else: else:
verboseoutput("Unknown CIM Error: %s" % args) verboseoutput("Unknown CIM Error: %s" % args)
except pywbem.cim_http.AuthError,arg: except pywbem.cim_http.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")
sys.exit (GlobalStatus) sys.exit (GlobalStatus)
else: else:
# GlobalStatus = ExitOK #ARR # GlobalStatus = ExitOK #ARR
@ -937,12 +942,12 @@ if perf == '|':
perf = '' perf = ''
if GlobalStatus == ExitOK : if GlobalStatus == ExitOK :
print "OK - Server: %s %s %s%s" % (server_info, SerialNumber, bios_info, perf) print("OK - Server: %s %s %s%s" % (server_info, SerialNumber, bios_info, perf))
elif GlobalStatus == ExitUnknown : elif GlobalStatus == ExitUnknown :
print "UNKNOWN: %s" % (ExitMsg) #ARR print("UNKNOWN: %s" % (ExitMsg)) #ARR
else: else:
print "%s- Server: %s %s %s%s" % (ExitMsg, server_info, SerialNumber, bios_info, perf) print("%s - Server: %s %s %s%s" % (ExitMsg, server_info, SerialNumber, bios_info, perf))
sys.exit (GlobalStatus) sys.exit (GlobalStatus)