Update megacli-status to python3

This commit is contained in:
Nico Schottelius 2021-08-13 10:43:13 +02:00
parent 57d3771399
commit fa45565eb0
1 changed files with 39 additions and 39 deletions

View File

@ -1,4 +1,4 @@
#!/usr/bin/python #!/usr/bin/python3
# $Id: megaclisas-status,v 1.68 2016/10/21 14:38:56 root Exp root $ # $Id: megaclisas-status,v 1.68 2016/10/21 14:38:56 root Exp root $
# #
# Written by Adam Cecile <gandalf@NOSPAM.le-vert.net> # Written by Adam Cecile <gandalf@NOSPAM.le-vert.net>
@ -40,7 +40,7 @@ Outputs = {}
# Startup # Startup
def print_usage(): def print_usage():
print 'Usage: megaraid-status [--nagios|--debug|--notemp]' print('Usage: megaraid-status [--nagios|--debug|--notemp]')
# We need root access to query # We need root access to query
if __name__ == '__main__': if __name__ == '__main__':
@ -49,7 +49,7 @@ if __name__ == '__main__':
except AttributeError: except AttributeError:
root_or_admin = ctypes.windll.shell32.IsUserAnAdmin() !=0 root_or_admin = ctypes.windll.shell32.IsUserAnAdmin() !=0
if not root_or_admin: if not root_or_admin:
print '# This script requires Administrator privileges' print('# This script requires Administrator privileges')
sys.exit(5) sys.exit(5)
# Check command line arguments to enable nagios or not # Check command line arguments to enable nagios or not
@ -102,19 +102,19 @@ for megabin in "MegaCli64","MegaCli","megacli", "MegaCli.exe":
if (megaclipath != None): if (megaclipath != None):
dbgprint ('Will use MegaCLI from here: '+str(megaclipath)) dbgprint ('Will use MegaCLI from here: '+str(megaclipath))
break break
# Check binary exists (and +x), if not print an error message # Check binary exists (and +x), if not print an error message
if (megaclipath != None): if (megaclipath != None):
if os.path.exists(megaclipath) and os.access(megaclipath, os.X_OK): if os.path.exists(megaclipath) and os.access(megaclipath, os.X_OK):
pass pass
else: else:
if nagiosmode: if nagiosmode:
print 'UNKNOWN - Cannot find '+megaclipath print('UNKNOWN - Cannot find '+megaclipath)
else: else:
print 'Cannot find ' + megaclipath + 'in your PATH. Please install it.' print('Cannot find ' + megaclipath + 'in your PATH. Please install it.')
sys.exit(3) sys.exit(3)
else: else:
print 'Cannot find "MegaCli64","MegaCli" or "megacli" or "MegaCli.exe" in your PATH. Please install it.' print('Cannot find "MegaCli64","MegaCli" or "megacli" or "MegaCli.exe" in your PATH. Please install it.')
sys.exit(3) sys.exit(3)
@ -130,7 +130,7 @@ def returnWdthFromArrayCol(glarray,idx):
# Get command output # Get command output
def getOutput(cmd): def getOutput(cmd):
lines = [] lines = []
if ( Outputs.has_key(cmd) ): if cmd in Outputs:
dbgprint ("Got Cached value: "+str(cmd)) dbgprint ("Got Cached value: "+str(cmd))
lines = Outputs[cmd] lines = Outputs[cmd]
else: else:
@ -141,7 +141,7 @@ def getOutput(cmd):
lines.append(line.strip()) lines.append(line.strip())
Outputs[cmd] = lines Outputs[cmd] = lines
return lines return lines
def returnControllerNumber(output): def returnControllerNumber(output):
for line in output: for line in output:
if re.match(r'^Controller Count.*$',line.strip()): if re.match(r'^Controller Count.*$',line.strip()):
@ -274,7 +274,7 @@ def returnHBAInfo(table,output,controllerid):
cmd = '%s -AdpBbuCmd -GetBbuStatus -a%d -NoLog' % (megaclipath, controllerid) cmd = '%s -AdpBbuCmd -GetBbuStatus -a%d -NoLog' % (megaclipath, controllerid)
output = getOutput(cmd) output = getOutput(cmd)
controllerbbu = returnBBUStatus(output) controllerbbu = returnBBUStatus(output)
if controllermodel != 'Unknown': if controllermodel != 'Unknown':
table.append([ 'c'+str(controllerid), controllermodel, controllerram, str(controllertemp), str(controllerbbu), str('FW: '+controllerrev) ]) table.append([ 'c'+str(controllerid), controllermodel, controllerram, str(controllertemp), str(controllerbbu), str('FW: '+controllerrev) ])
@ -469,9 +469,9 @@ def returnDiskInfo(output,controllerid):
dbgprint('Disk Info: '+str(arrayid)+' '+str(diskid)+' '+str(oldenclid)) dbgprint('Disk Info: '+str(arrayid)+' '+str(diskid)+' '+str(oldenclid))
if subfstate == 'Rebuild': if subfstate == 'Rebuild':
cmd = '%s pdrbld -showprog -physdrv\[%s:%s\] -a%d -NoLog' % (megaclipath, enclid, slotid, controllerid) cmd = '%s pdrbld -showprog -physdrv\[%s:%s\] -a%d -NoLog' % (megaclipath, enclid, slotid, controllerid)
output = getOutput(cmd) output = getOutput(cmd)
percent = returnRebuildProgress(output) percent = returnRebuildProgress(output)
fstate = str('Rebuilding (%d%%)' % (percent)) fstate = str('Rebuilding (%d%%)' % (percent))
if (( NestedLDTable[controllerid][int(arrayindex)] == True) and (spanid != False)): if (( NestedLDTable[controllerid][int(arrayindex)] == True) and (spanid != False)):
sarrayid = str(arrayid)+"s"+spanid sarrayid = str(arrayid)+"s"+spanid
@ -566,7 +566,7 @@ bad = False
if printcontroller: if printcontroller:
if controllernumber: if controllernumber:
if not nagiosmode: if not nagiosmode:
print '-- Controller information --' print('-- Controller information --')
i = 0 i = 0
controllerid = 0 controllerid = 0
@ -585,25 +585,25 @@ if printcontroller:
# Header # Header
if ( i == 0 ): if ( i == 0 ):
if not nagiosmode: if not nagiosmode:
print hbafmt % ("-- ID","H/W Model","RAM","Temp","BBU", "Firmware") print(hbafmt % ("-- ID","H/W Model","RAM","Temp","BBU", "Firmware"))
if not nagiosmode: if not nagiosmode:
print hbafmt % ( print(hbafmt % (
hba[0], hba[0],
hba[1], hba[1],
hba[2], hba[2],
hba[3], hba[3],
hba[4], hba[4],
hba[5]) hba[5]))
i += 1 i += 1
if not nagiosmode: if not nagiosmode:
print '' print('')
else: else:
print "No MegaRAID or PERC adapter detected on your system!" print("No MegaRAID or PERC adapter detected on your system!")
exit(1) exit(1)
if printarray: if printarray:
if not nagiosmode: if not nagiosmode:
print '-- Array information --' print('-- Array information --')
controllerid = 0 controllerid = 0
pcipath = '' pcipath = ''
@ -682,9 +682,9 @@ if printarray:
# Header # Header
if ( i == 0 ): if ( i == 0 ):
if not nagiosmode: if not nagiosmode:
print ldfmt % ("-- ID", "Type", "Size", "Strpsz", "Flags", "DskCache", "Status", "OS Path", "CacheCade", "InProgress" ) print(ldfmt % ("-- ID", "Type", "Size", "Strpsz", "Flags", "DskCache", "Status", "OS Path", "CacheCade", "InProgress" ))
if not nagiosmode: if not nagiosmode:
print ldfmt % ( print(ldfmt % (
arrayinfo[0], arrayinfo[0],
arrayinfo[1], arrayinfo[1],
arrayinfo[2], arrayinfo[2],
@ -694,7 +694,7 @@ if printarray:
arrayinfo[6], arrayinfo[6],
arrayinfo[7], arrayinfo[7],
arrayinfo[8], arrayinfo[8],
arrayinfo[9]) arrayinfo[9]))
dbgprint("Array state : "+arrayinfo[6]) dbgprint("Array state : "+arrayinfo[6])
if arrayinfo[6] not in [ 'Optimal', 'N/A' ]: if arrayinfo[6] not in [ 'Optimal', 'N/A' ]:
bad = True bad = True
@ -705,7 +705,7 @@ if printarray:
i += 1 i += 1
controllerid += 1 controllerid += 1
if not nagiosmode: if not nagiosmode:
print '' print('')
controllerid = 0 controllerid = 0
while controllerid < controllernumber: while controllerid < controllernumber:
@ -716,7 +716,7 @@ while controllerid < controllernumber:
if totaldrivenumber: if totaldrivenumber:
if not nagiosmode: if not nagiosmode:
print '-- Disk information --' print('-- Disk information --')
i = 0 i = 0
dlen = 0 ; mlen = 0 ; flen = 0 dlen = 0 ; mlen = 0 ; flen = 0
@ -767,11 +767,11 @@ if totaldrivenumber:
# Header # Header
if ( i == 0 ): if ( i == 0 ):
if not nagiosmode: if not nagiosmode:
print drvfmt % ( print(drvfmt % (
"-- ID", "Type", "Drive Model", "Size", "Status", "Speed", "Temp", "Slot ID", "LSI Device ID") "-- ID", "Type", "Drive Model", "Size", "Status", "Speed", "Temp", "Slot ID", "LSI Device ID"))
# Drive information # Drive information
if not nagiosmode: if not nagiosmode:
print drvfmt % ( print(drvfmt % (
str('c'+str(controllerid)+'u'+array[0]+'p'+array[1]), # c0p0 str('c'+str(controllerid)+'u'+array[0]+'p'+array[1]), # c0p0
array[2], # HDD/SDD array[2], # HDD/SDD
array[3], # Model Information (Variable len) array[3], # Model Information (Variable len)
@ -780,11 +780,11 @@ if totaldrivenumber:
array[6], # Speed array[6], # Speed
array[7], # Temp array[7], # Temp
str('['+array[8]+':'+array[9]+']'), # Slot ID str('['+array[8]+':'+array[9]+']'), # Slot ID
array[10]) # LSI ID array[10])) # LSI ID
i = i + 1 i = i + 1
controllerid += 1 controllerid += 1
if not nagiosmode: if not nagiosmode:
print '' print('')
controllerid = 0 controllerid = 0
totalconfdrivenumber = 0 totalconfdrivenumber = 0
@ -811,7 +811,7 @@ dbgprint('Total Unconfigured Drives : ' + str(totalunconfdrivenumber))
if totalunconfdrivenumber: if totalunconfdrivenumber:
if not nagiosmode: if not nagiosmode:
print '-- Unconfigured Disk information --' print('-- Unconfigured Disk information --')
controllerid = 0 controllerid = 0
while controllerid < controllernumber: while controllerid < controllernumber:
@ -844,11 +844,11 @@ if totalunconfdrivenumber:
# Header # Header
if ( i == 0 ): if ( i == 0 ):
if not nagiosmode: if not nagiosmode:
print drvfmt % ( print(drvfmt % (
"-- ID", "Type", "Drive Model", "Size", "Status", "Speed", "Temp", "Slot ID", "LSI Device ID") "-- ID", "Type", "Drive Model", "Size", "Status", "Speed", "Temp", "Slot ID", "LSI Device ID"))
# Drive information # Drive information
if not nagiosmode: if not nagiosmode:
print drvfmt % ( print(drvfmt % (
str('c'+str(controllerid)+'uXpY'), # cXpY str('c'+str(controllerid)+'uXpY'), # cXpY
array[0], # HDD/SDD array[0], # HDD/SDD
array[1], # Model Information (Variable len) array[1], # Model Information (Variable len)
@ -857,19 +857,19 @@ if totalunconfdrivenumber:
array[4], # Speed array[4], # Speed
array[5], # Temp array[5], # Temp
str('['+array[6]+':'+array[7]+']'), # Slot ID str('['+array[6]+':'+array[7]+']'), # Slot ID
array[8]) # LSI ID array[8])) # LSI ID
i = i + 1 i = i + 1
controllerid += 1 controllerid += 1
if not nagiosmode: if not nagiosmode:
print '' print('')
if nagiosmode: if nagiosmode:
if bad: if bad:
print 'RAID ERROR - Arrays: OK:'+str(nagiosgoodarray)+' Bad:'+str(nagiosbadarray)+' - Disks: OK:'+str(nagiosgooddisk)+' Bad:'+str(nagiosbaddisk) print('RAID ERROR - Arrays: OK:'+str(nagiosgoodarray)+' Bad:'+str(nagiosbadarray)+' - Disks: OK:'+str(nagiosgooddisk)+' Bad:'+str(nagiosbaddisk))
sys.exit(2) sys.exit(2)
else: else:
print 'RAID OK - Arrays: OK:'+str(nagiosgoodarray)+' Bad:'+str(nagiosbadarray)+' - Disks: OK:'+str(nagiosgooddisk)+' Bad:'+str(nagiosbaddisk) print('RAID OK - Arrays: OK:'+str(nagiosgoodarray)+' Bad:'+str(nagiosbadarray)+' - Disks: OK:'+str(nagiosgooddisk)+' Bad:'+str(nagiosbaddisk))
else: else:
if bad: if bad:
print '\nThere is at least one disk/array in a NOT OPTIMAL state.' print('\nThere is at least one disk/array in a NOT OPTIMAL state.')
sys.exit(1) sys.exit(1)