check_hpacucli.py: pep8 cleanup

This commit is contained in:
Pall Sigurdsson 2013-05-27 16:58:58 +00:00
parent f06667b55d
commit 5eb8ce9199
1 changed files with 130 additions and 102 deletions

View File

@ -23,8 +23,6 @@
debugging = False debugging = False
# No real need to change anything below here # No real need to change anything below here
version = "1.1" version = "1.1"
ok = 0 ok = 0
@ -34,8 +32,6 @@ unknown=3
not_present = -1 not_present = -1
nagios_status = -1 nagios_status = -1
state = {} state = {}
state[not_present] = "Not Present" state[not_present] = "Not Present"
state[ok] = "OK" state[ok] = "OK"
@ -56,7 +52,6 @@ from os import getenv,putenv,environ
import subprocess import subprocess
def print_help(): def print_help():
print "check_hpacucli version %s" % version print "check_hpacucli version %s" % version
print "This plugin checks HP Array with the hpacucli command" print "This plugin checks HP Array with the hpacucli command"
@ -76,6 +71,7 @@ def error(errortext):
print "* Error: %s" % errortext print "* Error: %s" % errortext
exit(unknown) exit(unknown)
def debug(debugtext): def debug(debugtext):
global debugging global debugging
if debugging: if debugging:
@ -83,11 +79,16 @@ def debug( debugtext ):
def runCommand(command): def runCommand(command):
""" Runs command from the shell prompt. Exit Nagios style if unsuccessful""" """ Run command from the shell prompt. Exit Nagios style if unsuccessful"""
proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE,stderr=subprocess.PIPE,) proc = subprocess.Popen(command,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
stdout, stderr = proc.communicate('through stdin to stdout') stdout, stderr = proc.communicate('through stdin to stdout')
if proc.returncode > 0: if proc.returncode > 0:
print "Error %s: %s\n command was: '%s'" % (proc.returncode,stderr.strip(),command) print "Error %s: %s\n command was: '%s'"\
% (proc.returncode, stderr.strip(), command)
debug("results: %s" % (stdout.strip())) debug("results: %s" % (stdout.strip()))
if proc.returncode == 127: # File not found, lets print path if proc.returncode == 127: # File not found, lets print path
path = getenv("PATH") path = getenv("PATH")
@ -100,6 +101,7 @@ def runCommand(command):
else: else:
return stdout return stdout
def end(): def end():
global summary global summary
global longserviceoutput global longserviceoutput
@ -107,22 +109,27 @@ def end():
global nagios_status global nagios_status
print "%s - %s | %s" % (state[nagios_status], summary, perfdata) print "%s - %s | %s" % (state[nagios_status], summary, perfdata)
print longserviceoutput print longserviceoutput
if nagios_status < 0: nagios_status = unknown if nagios_status < 0:
nagios_status = unknown
exit(nagios_status) exit(nagios_status)
def add_perfdata(text): def add_perfdata(text):
global perfdata global perfdata
text = text.strip() text = text.strip()
perfdata = perfdata + " %s " % (text) perfdata = perfdata + " %s " % (text)
def add_long(text): def add_long(text):
global longserviceoutput global longserviceoutput
longserviceoutput = longserviceoutput + text + '\n' longserviceoutput = longserviceoutput + text + '\n'
def add_summary(text): def add_summary(text):
global summary global summary
summary = summary + text summary = summary + text
def set_path(path): def set_path(path):
current_path = getenv('PATH') current_path = getenv('PATH')
if current_path.find('C:\\') > -1: # We are on this platform if current_path.find('C:\\') > -1: # We are on this platform
@ -130,16 +137,17 @@ def set_path(path):
path = ";C:\Program Files\Hewlett-Packard\Sanworks\Element Manager for StorageWorks HSV" path = ";C:\Program Files\Hewlett-Packard\Sanworks\Element Manager for StorageWorks HSV"
path = path + ";C:\Program Files (x86)\Compaq\Hpacucli\Bin" path = path + ";C:\Program Files (x86)\Compaq\Hpacucli\Bin"
path = path + ";C:\Program Files\Compaq\Hpacucli\Bin" path = path + ";C:\Program Files\Compaq\Hpacucli\Bin"
else: path = ';' + path else:
path = ';' + path
else: # Unix/Linux, etc else: # Unix/Linux, etc
if path == '': path = ":/usr/sbin" if path == '':
else: path = ':' + path path = ":/usr/sbin"
else:
path = ':' + path
current_path = "%s%s" % (current_path, path) current_path = "%s%s" % (current_path, path)
environ['PATH'] = current_path environ['PATH'] = current_path
def run_hpacucli(run_type='controllers', controller=None): def run_hpacucli(run_type='controllers', controller=None):
if run_type == 'controllers': if run_type == 'controllers':
command = "hpacucli controller all show detail" command = "hpacucli controller all show detail"
@ -148,12 +156,18 @@ def run_hpacucli(run_type='controllers', controller=None):
add_summary("Controller not found") add_summary("Controller not found")
end() end()
identifier = 'slot=%s' % (controller['Slot']) identifier = 'slot=%s' % (controller['Slot'])
command = "hpacucli controller %s %s all show detail"
if run_type == 'logicaldisks': if run_type == 'logicaldisks':
command = "hpacucli controller %s ld all show detail" % (identifier) subcommand = 'ld'
elif run_type == 'physicaldisks': elif run_type == 'physicaldisks':
command = "hpacucli controller %s pd all show detail" % (identifier) subcommand = 'pd'
else:
end()
return
command = command % (subcommand, identifier)
debug(command) debug(command)
if sudo: command = "sudo " + command if sudo:
command = "sudo " + command
output = runCommand(command) output = runCommand(command)
# Some basic error checking # Some basic error checking
error_strings = ['Permission denied'] error_strings = ['Permission denied']
@ -173,31 +187,41 @@ def run_hpacucli(run_type='controllers', controller=None):
continue continue
if i.startswith('Note:'): if i.startswith('Note:'):
continue continue
if run_type=='controllers' and i[0] != ' ': # No space on first line if run_type == 'controllers' and i[0] != ' ': # space on first line
if my_object and not my_object in objects: objects.append(my_object) if my_object and not my_object in objects:
objects.append(my_object)
my_object = {} my_object = {}
my_object['name'] = i my_object['name'] = i
elif run_type == 'logicaldisks' and i.find('Logical Drive:') > 0: elif run_type == 'logicaldisks' and i.find('Logical Drive:') > 0:
if my_object and not my_object in objects: objects.append(my_object) if my_object and not my_object in objects:
objects.append(my_object)
my_object = {} my_object = {}
my_object['name'] = i.strip() my_object['name'] = i.strip()
elif run_type == 'physicaldisks' and i.find('physicaldrive') > 0: elif run_type == 'physicaldisks' and i.find('physicaldrive') > 0:
if my_object and not my_object in objects: objects.append(my_object) if my_object and not my_object in objects:
objects.append(my_object)
my_object = {} my_object = {}
my_object['name'] = i.strip() my_object['name'] = i.strip()
else: else:
i = i.strip() i = i.strip()
if i.find(':') < 1: continue if i.find(':') < 1:
continue
i = i.split(':') i = i.split(':')
if i[0] == '': continue # skip empty lines if i[0] == '':
if len(i) == 1: continue continue # skip empty lines
if len(i) == 1:
continue
key = i[0].strip() key = i[0].strip()
value = ' '.join(i[1:]).strip() value = ' '.join(i[1:]).strip()
my_object[key] = value my_object[key] = value
if my_object and not my_object in objects: objects.append(my_object) if my_object and not my_object in objects:
objects.append(my_object)
return objects return objects
controllers = [] controllers = []
def check_controllers(): def check_controllers():
global controllers global controllers
status = -1 status = -1
@ -217,13 +241,15 @@ def check_controllers():
controller_serial = 'n/a' controller_serial = 'n/a'
cache_serial = 'n/a' cache_serial = 'n/a'
if i.has_key('Serial Number'): if 'Serial Number' in i:
controller_serial = i['Serial Number'] controller_serial = i['Serial Number']
if i.has_key('Cache Serial Number'): if 'Cache Serial Number' in i:
cache_serial = i['Cache Serial Number'] cache_serial = i['Cache Serial Number']
add_long("%s" % (i['name'])) add_long("%s" % (i['name']))
add_long( "- Controller Status: %s (sn: %s)" % ( state[controller_status], controller_serial ) ) add_long("- Controller Status: %s (sn: %s)"
add_long( "- Cache Status: %s (sn: %s)" % ( state[cache_status], cache_serial ) ) % (state[controller_status], controller_serial))
add_long("- Cache Status: %s (sn: %s)"
% (state[cache_status], cache_serial))
if controller_status > ok or cache_status > ok: if controller_status > ok or cache_status > ok:
add_summary(";%s on %s;" % (state[controller_status], i['name'])) add_summary(";%s on %s;" % (state[controller_status], i['name']))
@ -238,7 +264,8 @@ def check_logicaldisks():
controllers = run_hpacucli() controllers = run_hpacucli()
logicaldisks = [] logicaldisks = []
for controller in controllers: for controller in controllers:
for ld in run_hpacucli(run_type='logicaldisks', controller=controller): for ld in run_hpacucli(run_type='logicaldisks',
controller=controller):
logicaldisks.append(ld) logicaldisks.append(ld)
status = -1 status = -1
add_long("\nChecking logical Disks:") add_long("\nChecking logical Disks:")
@ -251,6 +278,7 @@ def check_logicaldisks():
add_long("- %s (%s) = %s" % (i['name'], mount_point, state[ld_status])) add_long("- %s (%s) = %s" % (i['name'], mount_point, state[ld_status]))
add_summary(". ") add_summary(". ")
def check_physicaldisks(): def check_physicaldisks():
global controllers global controllers
disktype = 'physicaldisks' disktype = 'physicaldisks'
@ -272,9 +300,12 @@ def check_physicaldisks():
interface = i['Interface Type'] interface = i['Interface Type']
serial = i['Serial Number'] serial = i['Serial Number']
model = i['Model'] model = i['Model']
add_long( "- %s, %s, %s = %s" % (i['name'], interface, size, state[disk_status]) ) add_long("- %s, %s, %s = %s" %
(i['name'], interface, size, state[disk_status])
)
if disk_status > ok: if disk_status > ok:
add_long( "-- Replace drive, firmware=%s, model=%s, serial=%s" % (firmware,model, serial)) error_str = "-- Replace drive, firmware=%s, model=%s, serial=%s"
add_long(error_str % (firmware, model, serial))
if status > ok: if status > ok:
add_summary("(errors)") add_summary("(errors)")
add_summary(". ") add_summary(". ")
@ -285,7 +316,7 @@ def check(my_object, field, valid_states = None):
valid_states = ['OK'] valid_states = ['OK']
state = -1 state = -1
global nagios_status global nagios_status
if my_object.has_key(field): if field in my_object:
if my_object[field] in valid_states: if my_object[field] in valid_states:
state = ok state = ok
else: else:
@ -294,7 +325,6 @@ def check(my_object, field, valid_states = None):
return state return state
def parse_arguments(): def parse_arguments():
arguments = argv[1:] arguments = argv[1:]
while len(arguments) > 0: while len(arguments) > 0:
@ -316,8 +346,6 @@ def parse_arguments():
exit(unknown) exit(unknown)
def main(): def main():
parse_arguments() parse_arguments()
set_path('') set_path('')