1
0
mirror of https://github.com/opinkerfi/nagios-plugins.git synced 2024-11-25 03:43:46 +01:00
nagios-plugins/check_hpacucli/check_hpacucli.py

363 lines
11 KiB
Python
Raw Normal View History

2010-09-03 02:52:15 +02:00
#!/usr/bin/python
#
# Copyright 2010, Pall Sigurdsson <palli@opensource.is>
#
# check_hpacucli.py is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# check_hpacucli.py is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# About this script
2013-05-27 18:42:27 +02:00
#
2010-09-03 21:51:50 +02:00
# This script will check the status of Smart Array Raid Controller
2013-05-27 18:42:27 +02:00
# You need the hpacucli binary in path (/usr/sbin/hpacucli is a good place)
2010-09-03 21:51:50 +02:00
# hpacucli comes with the Proliant Support Pack (PSP) from HP
2010-09-03 02:52:15 +02:00
2010-09-03 03:17:56 +02:00
debugging = False
2010-09-03 02:52:15 +02:00
# No real need to change anything below here
2013-05-27 18:58:58 +02:00
version = "1.1"
ok = 0
warning = 1
critical = 2
unknown = 3
not_present = -1
2010-09-03 02:52:15 +02:00
nagios_status = -1
state = {}
state[not_present] = "Not Present"
state[ok] = "OK"
state[warning] = "Warning"
state[critical] = "Critical"
state[unknown] = "Unknown"
2013-05-27 18:58:58 +02:00
longserviceoutput = "\n"
perfdata = ""
summary = ""
sudo = False
2010-09-03 02:52:15 +02:00
from sys import exit
from sys import argv
2013-05-27 18:58:58 +02:00
from os import getenv, putenv, environ
2010-09-03 02:52:15 +02:00
import subprocess
def print_help():
2013-05-27 18:58:58 +02:00
print "check_hpacucli version %s" % version
print "This plugin checks HP Array with the hpacucli command"
print ""
print "Usage: %s " % argv[0]
print "Usage: %s [--help]" % argv[0]
print "Usage: %s [--version]" % argv[0]
print "Usage: %s [--path </path/to/hpacucli>]" % argv[0]
print "Usage: %s [--no-perfdata]" % argv[0]
print "Usage: %s [--no-longoutput]" % argv[0]
print ""
2010-09-03 02:52:15 +02:00
def error(errortext):
2013-05-27 18:58:58 +02:00
print "* Error: %s" % errortext
print_help()
print "* Error: %s" % errortext
exit(unknown)
2010-09-03 02:52:15 +02:00
2013-05-27 18:58:58 +02:00
def debug(debugtext):
global debugging
if debugging:
print debugtext
2010-09-03 02:52:15 +02:00
def runCommand(command):
2013-05-27 18:58:58 +02:00
""" Run command from the shell prompt. Exit Nagios style if unsuccessful"""
proc = subprocess.Popen(command,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
2013-05-27 18:42:27 +02:00
stdout, stderr = proc.communicate('through stdin to stdout')
if proc.returncode > 0:
2013-05-27 18:58:58 +02:00
print "Error %s: %s\n command was: '%s'"\
% (proc.returncode, stderr.strip(), command)
debug("results: %s" % (stdout.strip()))
if proc.returncode == 127: # File not found, lets print path
path = getenv("PATH")
2010-09-03 03:12:53 +02:00
print "Check if your path is correct %s" % (path)
if stderr.find('Password:') == 0 and command.find('sudo') == 0:
2013-05-27 18:42:27 +02:00
print "Check if user is in the sudoers file"
2010-09-03 03:12:53 +02:00
if stderr.find('sorry, you must have a tty to run sudo') == 0 and command.find('sudo') == 0:
2013-05-27 18:42:27 +02:00
print "Please remove 'requiretty' from /etc/sudoers"
exit(unknown)
else:
return stdout
2010-09-03 02:52:15 +02:00
2013-05-27 18:58:58 +02:00
2010-09-03 02:52:15 +02:00
def end():
2013-05-27 18:42:27 +02:00
global summary
global longserviceoutput
global perfdata
global nagios_status
2013-05-27 18:58:58 +02:00
print "%s - %s | %s" % (state[nagios_status], summary, perfdata)
2013-05-27 18:42:27 +02:00
print longserviceoutput
2013-05-27 18:58:58 +02:00
if nagios_status < 0:
nagios_status = unknown
2013-05-27 18:42:27 +02:00
exit(nagios_status)
2010-09-03 02:52:15 +02:00
2013-05-27 18:58:58 +02:00
2010-09-03 02:52:15 +02:00
def add_perfdata(text):
2013-05-27 18:42:27 +02:00
global perfdata
text = text.strip()
perfdata = perfdata + " %s " % (text)
2010-09-03 02:52:15 +02:00
2013-05-27 18:58:58 +02:00
2010-09-03 02:52:15 +02:00
def add_long(text):
2013-05-27 18:42:27 +02:00
global longserviceoutput
longserviceoutput = longserviceoutput + text + '\n'
2010-09-03 02:52:15 +02:00
2013-05-27 18:58:58 +02:00
2010-09-03 02:52:15 +02:00
def add_summary(text):
2013-05-27 18:42:27 +02:00
global summary
summary = summary + text
2010-09-03 02:52:15 +02:00
2013-05-27 18:58:58 +02:00
2010-09-03 02:52:15 +02:00
def set_path(path):
2013-05-27 18:42:27 +02:00
current_path = getenv('PATH')
2013-05-27 18:58:58 +02:00
if current_path.find('C:\\') > -1: # We are on this platform
2013-05-27 18:42:27 +02:00
if path == '':
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\Compaq\Hpacucli\Bin"
2013-05-27 18:58:58 +02:00
else:
path = ';' + path
else: # Unix/Linux, etc
if path == '':
path = ":/usr/sbin"
else:
path = ':' + path
current_path = "%s%s" % (current_path, path)
2013-05-27 18:42:27 +02:00
environ['PATH'] = current_path
def run_hpacucli(run_type='controllers', controller=None):
2013-05-27 18:58:58 +02:00
if run_type == 'controllers':
command = "hpacucli controller all show detail"
elif run_type in ('logicaldisks', 'physicaldisks'):
2013-05-27 18:42:27 +02:00
if 'Slot' not in controller:
2013-05-27 18:58:58 +02:00
add_summary("Controller not found")
2013-05-27 18:42:27 +02:00
end()
2013-05-27 18:58:58 +02:00
identifier = 'slot=%s' % (controller['Slot'])
command = "hpacucli controller %s %s all show detail"
if run_type == 'logicaldisks':
subcommand = 'ld'
elif run_type == 'physicaldisks':
subcommand = 'pd'
else:
end()
return
command = command % (identifier, subcommand)
2013-05-27 18:58:58 +02:00
debug(command)
if sudo:
command = "sudo " + command
2013-05-27 18:42:27 +02:00
output = runCommand(command)
# Some basic error checking
2013-05-27 18:58:58 +02:00
error_strings = ['Permission denied']
2013-05-27 18:42:27 +02:00
error_strings.append('Error: You need to have administrator rights to continue.')
for error in error_strings:
2013-05-27 18:58:58 +02:00
if output.find(error) > -1 and output.find("sudo") != 0:
2013-05-27 18:42:27 +02:00
command = "sudo " + command
print command
output = runCommand(command)
output = output.split('\n')
objects = []
my_object = None
for i in output:
if len(i) == 0:
continue
if i.strip() == '':
continue
if i.startswith('Note:'):
continue
2013-05-27 18:58:58 +02:00
if run_type == 'controllers' and i[0] != ' ': # space on first line
if my_object and not my_object in objects:
objects.append(my_object)
2013-05-27 18:42:27 +02:00
my_object = {}
my_object['name'] = i
2013-05-27 18:58:58 +02:00
elif run_type == 'logicaldisks' and i.find('Logical Drive:') > 0:
if my_object and not my_object in objects:
objects.append(my_object)
2013-05-27 18:42:27 +02:00
my_object = {}
my_object['name'] = i.strip()
2013-05-27 18:58:58 +02:00
elif run_type == 'physicaldisks' and i.find('physicaldrive') > 0:
if my_object and not my_object in objects:
objects.append(my_object)
2013-05-27 18:42:27 +02:00
my_object = {}
my_object['name'] = i.strip()
else:
i = i.strip()
2013-05-27 18:58:58 +02:00
if i.find(':') < 1:
continue
2013-05-27 18:42:27 +02:00
i = i.split(':')
2013-05-27 18:58:58 +02:00
if i[0] == '':
continue # skip empty lines
if len(i) == 1:
continue
2013-05-27 18:42:27 +02:00
key = i[0].strip()
2013-05-27 18:58:58 +02:00
value = ' '.join(i[1:]).strip()
2013-05-27 18:42:27 +02:00
my_object[key] = value
2013-05-27 18:58:58 +02:00
if my_object and not my_object in objects:
objects.append(my_object)
2013-05-27 18:42:27 +02:00
return objects
2010-09-03 02:52:15 +02:00
2013-05-27 18:58:58 +02:00
2010-09-03 02:52:15 +02:00
controllers = []
2013-05-27 18:58:58 +02:00
2010-09-03 02:52:15 +02:00
def check_controllers():
2013-05-27 18:42:27 +02:00
global controllers
status = -1
controllers = run_hpacucli()
if len(controllers) == 0:
add_summary("No Disk Controllers Found. Exiting...")
global nagios_state
nagios_state = unknown
end()
2013-05-27 18:58:58 +02:00
add_summary("Found %s controllers" % (len(controllers)))
2013-05-27 18:42:27 +02:00
for i in controllers:
2013-05-27 18:58:58 +02:00
controller_status = check(i, 'Controller Status', 'OK')
2013-05-27 18:42:27 +02:00
status = max(status, controller_status)
2013-05-27 18:58:58 +02:00
cache_status = check(i, 'Cache Status')
2013-05-27 18:42:27 +02:00
status = max(status, cache_status)
controller_serial = 'n/a'
cache_serial = 'n/a'
2013-05-27 18:58:58 +02:00
if 'Serial Number' in i:
2013-05-27 18:42:27 +02:00
controller_serial = i['Serial Number']
2013-05-27 18:58:58 +02:00
if 'Cache Serial Number' in i:
2013-05-27 18:42:27 +02:00
cache_serial = i['Cache Serial Number']
2013-05-27 18:58:58 +02:00
add_long("%s" % (i['name']))
add_long("- Controller Status: %s (sn: %s)"
% (state[controller_status], controller_serial))
add_long("- Cache Status: %s (sn: %s)"
% (state[cache_status], cache_serial))
2013-05-27 18:42:27 +02:00
if controller_status > ok or cache_status > ok:
2013-05-27 18:58:58 +02:00
add_summary(";%s on %s;" % (state[controller_status], i['name']))
2013-05-27 18:42:27 +02:00
add_summary(', ')
return status
2010-09-03 02:52:15 +02:00
def check_logicaldisks():
2013-05-27 18:42:27 +02:00
global controllers
if len(controllers) < 1:
controllers = run_hpacucli()
logicaldisks = []
for controller in controllers:
2013-05-27 18:58:58 +02:00
for ld in run_hpacucli(run_type='logicaldisks',
controller=controller):
logicaldisks.append(ld)
2013-05-27 18:42:27 +02:00
status = -1
2013-05-27 18:58:58 +02:00
add_long("\nChecking logical Disks:")
add_summary("%s logicaldisks" % (len(logicaldisks)))
2013-05-27 18:42:27 +02:00
for i in logicaldisks:
2013-05-27 18:58:58 +02:00
ld_status = check(i, 'Status')
2013-05-27 18:42:27 +02:00
status = max(status, ld_status)
if i.get('Status') == 'Failed':
status = max(status, critical)
2013-05-27 18:42:27 +02:00
mount_point = i['Mount Points']
2013-05-27 18:58:58 +02:00
add_long("- %s (%s) = %s" % (i['name'], mount_point, state[ld_status]))
2013-05-27 18:42:27 +02:00
add_summary(". ")
2010-09-03 02:52:15 +02:00
2013-05-27 18:58:58 +02:00
2010-09-03 02:52:15 +02:00
def check_physicaldisks():
2013-05-27 18:42:27 +02:00
global controllers
2013-05-27 18:58:58 +02:00
disktype = 'physicaldisks'
2013-05-27 18:42:27 +02:00
if len(controllers) < 1:
controllers = run_hpacucli()
disks = []
for controller in controllers:
for disk in run_hpacucli(run_type=disktype, controller=controller):
2013-05-27 18:58:58 +02:00
disks.append(disk)
2013-05-27 18:42:27 +02:00
status = -1
2013-05-27 18:58:58 +02:00
add_long("\nChecking Physical Disks:")
add_summary("%s %s" % (len(disks), disktype))
2013-05-27 18:42:27 +02:00
for i in disks:
2013-05-27 18:58:58 +02:00
disk_status = check(i, 'Status')
2013-05-27 18:42:27 +02:00
status = max(status, disk_status)
size = i['Size']
firmware = i['Firmware Revision']
interface = i['Interface Type']
serial = i['Serial Number']
model = i['Model']
2013-05-27 18:58:58 +02:00
add_long("- %s, %s, %s = %s" %
(i['name'], interface, size, state[disk_status])
)
2013-05-27 18:42:27 +02:00
if disk_status > ok:
2013-05-27 18:58:58 +02:00
error_str = "-- Replace drive, firmware=%s, model=%s, serial=%s"
add_long(error_str % (firmware, model, serial))
2013-05-27 18:42:27 +02:00
if status > ok:
2013-05-27 18:58:58 +02:00
add_summary("(errors)")
2010-09-03 02:52:15 +02:00
add_summary(". ")
2013-05-27 18:58:58 +02:00
def check(my_object, field, valid_states=None):
2013-05-27 18:42:27 +02:00
if valid_states is None:
valid_states = ['OK']
state = -1
global nagios_status
2013-05-27 18:58:58 +02:00
if field in my_object:
2013-05-27 18:42:27 +02:00
if my_object[field] in valid_states:
state = ok
else:
state = warning
nagios_status = max(nagios_status, state)
return state
2010-09-03 02:52:15 +02:00
2010-09-03 03:12:53 +02:00
2010-09-03 21:47:19 +02:00
def parse_arguments():
2013-05-27 18:42:27 +02:00
arguments = argv[1:]
while len(arguments) > 0:
arg = arguments.pop(0)
if arg == '--help':
print_help()
exit(ok)
elif arg == '--path':
path = arguments.pop(0)
set_path(path)
elif arg == '--debug':
global debugging
debugging = True
elif arg == '--sudo':
global sudo
sudo = True
else:
print_help()
exit(unknown)
2010-09-03 02:52:15 +02:00
2010-09-03 21:47:19 +02:00
def main():
2013-05-27 18:42:27 +02:00
parse_arguments()
set_path('')
check_controllers()
check_logicaldisks()
check_physicaldisks()
end()
2010-09-03 21:47:19 +02:00
if __name__ == '__main__':
2013-05-27 18:42:27 +02:00
main()