Skip to content

Commit

Permalink
Merge pull request #30 from Napsty/python3
Browse files Browse the repository at this point in the history
Python3 compatibility
  • Loading branch information
Napsty authored Oct 2, 2018
2 parents 9f8b22a + 6aebd1a commit df56d73
Showing 1 changed file with 24 additions and 19 deletions.
43 changes: 24 additions & 19 deletions check_esxi_hardware.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,15 +256,20 @@
#@ Author : Peter Newman
#@ 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 time
import pywbem
import re
import pkg_resources
from optparse import OptionParser,OptionGroup

version = '20180411'
version = '20181001'

NS = 'root/cimv2'
hosturl = ''
Expand Down Expand Up @@ -481,7 +486,7 @@ def urlised_serialnumber(vendor,country,SerialNumber):

def verboseoutput(message) :
if verbose:
print "%s %s" % (time.strftime("%Y%m%d %H:%M:%S"), message)
print(time.strftime("%Y%m%d %H:%M:%S"), message)

# ----------------------------------------------------------------------

Expand Down Expand Up @@ -532,14 +537,14 @@ def getopts() :

# check input arguments
if len(sys.argv) < 2:
print "no parameters specified\n"
print("no parameters specified\n")
parser.print_help()
sys.exit(-1)
# if first argument starts with 'https://' we have old-style parameters, so handle in old way
if re.match("https://",sys.argv[1]):
# check input arguments
if len(sys.argv) < 5:
print "too few parameters\n"
print("too few parameters\n")
parser.print_help()
sys.exit(-1)
if len(sys.argv) > 5 :
Expand All @@ -557,7 +562,7 @@ def getopts() :
mandatories = ['host', 'user', 'password']
for m in mandatories:
if not options.__dict__[m]:
print "mandatory parameter '--" + m + "' is missing\n"
print("mandatory parameter '--" + m + "' is missing\n")
parser.print_help()
sys.exit(-1)

Expand Down Expand Up @@ -612,7 +617,7 @@ def getopts() :
on_windows = False
import signal
def handler(signum, frame):
print 'UNKNOWN: Execution time too long!'
print('UNKNOWN: Execution time too long!')
sys.exit(ExitUnknown)

if cimport:
Expand Down Expand Up @@ -670,19 +675,19 @@ def handler(signum, frame):
if vendor=='auto':
try:
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 ):
print "UNKNOWN: %s" %args
print("UNKNOWN: {}".format(args))
sys.exit (ExitUnknown)
elif ( args[1].find('ThreadPool --- Failed to enqueue request') >= 0 ):
print "UNKNOWN: %s" %args
print("UNKNOWN: {}".format(args))
sys.exit (ExitUnknown)
else:
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")
GlobalStatus = ExitUnknown
print "UNKNOWN: Authentication Error"
print("UNKNOWN: Authentication Error")
sys.exit (GlobalStatus)
else:
man=c[0][u'Manufacturer']
Expand All @@ -701,19 +706,19 @@ def handler(signum, frame):
verboseoutput("Check classe "+classe)
try:
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 ):
print "UNKNOWN: %s" %args
print("UNKNOWN: {}".format(args))
sys.exit (ExitUnknown)
elif ( args[1].find('ThreadPool --- Failed to enqueue request') >= 0 ):
print "UNKNOWN: %s" %args
print("UNKNOWN: {}".format(args))
sys.exit (ExitUnknown)
else:
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")
GlobalStatus = ExitUnknown
print "UNKNOWN: Authentication Error"
print("UNKNOWN: Authentication Error")
sys.exit (GlobalStatus)
else:
# GlobalStatus = ExitOK #ARR
Expand Down Expand Up @@ -937,12 +942,12 @@ def handler(signum, frame):
perf = ''

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 :
print "UNKNOWN: %s" % (ExitMsg) #ARR
print("UNKNOWN: %s" % (ExitMsg)) #ARR

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)

0 comments on commit df56d73

Please sign in to comment.