mirror of
https://github.com/opinkerfi/nagios-plugins.git
synced 2024-12-22 17:13:52 +01:00
Refactor - support for lsdrive added
This commit is contained in:
parent
831156308b
commit
38a207065e
@ -4,7 +4,7 @@ from pynag.Plugins import PluginHelper, ok, warning, critical, unknown
|
||||
from pynag.Utils import runCommand
|
||||
from collections import namedtuple
|
||||
|
||||
valid_queries = "lsarray lsmdiskgrp"
|
||||
valid_queries = "lsarray lsmdiskgrp lsdrive"
|
||||
|
||||
p = PluginHelper()
|
||||
p.add_option("-H", "--hostname", help="Hostname or ip address", dest="hostname")
|
||||
@ -16,71 +16,107 @@ p.add_option("--test", help="Run this plugin in test mode", dest="test", action=
|
||||
p.parse_arguments()
|
||||
|
||||
if not p.options.hostname:
|
||||
p.parser.error("Required options -H is missing")
|
||||
p.parser.error("Required options -H is missing")
|
||||
if p.options.query not in valid_queries.split():
|
||||
p.parser.error("%s does not look like a valid query. Use -L for a list of valid queries" % (p.options.query))
|
||||
p.parser.error("%s does not look like a valid query. Use -L for a list of valid queries" % p.options.query)
|
||||
if p.options.list_queries is True:
|
||||
p.parser.error("Valid Queries: %s" % (valid_queries))
|
||||
p.parser.error("Valid Queries: %s" % valid_queries)
|
||||
|
||||
query = p.options.query
|
||||
|
||||
|
||||
# Connect to remote storwize and run a connect
|
||||
command = "ssh %s@%s %s" % (p.options.user, p.options.hostname, p.options.query)
|
||||
if p.options.test:
|
||||
command = "cat tests/%s.txt" % (p.options.query)
|
||||
return_code,stdout, stderr = runCommand(command)
|
||||
def run_query():
|
||||
""" Connect to a remote storwize box and run query """
|
||||
command = "ssh %s@%s %s" % (p.options.user, p.options.hostname, p.options.query)
|
||||
if p.options.test:
|
||||
command = "cat tests/%s.txt" % (p.options.query)
|
||||
return_code, stdout, stderr = runCommand(command)
|
||||
|
||||
if return_code != 0:
|
||||
p.status(unknown)
|
||||
p.add_summary("Got error %s when trying to log into remote storwize box" % return_code)
|
||||
p.add_long_output("\ncommand:\n===============\n%s" % command)
|
||||
p.add_long_output("\nStandard output:\n==============\n%s" % (stdout))
|
||||
p.add_long_output("\nStandard stderr:\n==============\n%s" % (stderr))
|
||||
p.exit()
|
||||
if return_code != 0:
|
||||
p.status(unknown)
|
||||
p.add_summary("Got error %s when trying to log into remote storwize box" % return_code)
|
||||
p.add_long_output("\ncommand:\n===============\n%s" % command)
|
||||
p.add_long_output("\nStandard output:\n==============\n%s" % (stdout))
|
||||
p.add_long_output("\nStandard stderr:\n==============\n%s" % (stderr))
|
||||
p.exit()
|
||||
if stderr:
|
||||
p.status(unknown)
|
||||
p.add_summary("Error when connecting to storwize: %s" % stderr)
|
||||
p.exit()
|
||||
|
||||
if stderr:
|
||||
p.status(unknown)
|
||||
p.add_summary("Error when connecting to storwize: %s" % stderr)
|
||||
p.exit()
|
||||
# Parse the output of run query and return a list of "rows"
|
||||
lines = stdout.splitlines()
|
||||
top_line = lines.pop(0)
|
||||
headers = top_line.split()
|
||||
Row = namedtuple('Row', ' '.join(headers))
|
||||
rows = []
|
||||
for i in lines:
|
||||
i = i.strip()
|
||||
columns = i.split()
|
||||
# When running lsdrive spare disks do not have same number of columns as other columns.
|
||||
if query == 'lsdrive':
|
||||
columns = columns[0:2] + [None] + columns[2:] + [None, None]
|
||||
if columns[3] == 'spare':
|
||||
columns = columns[0:5] + [None, None, None] + columns[5:]
|
||||
row = Row(*columns)
|
||||
rows.append(row)
|
||||
return rows
|
||||
|
||||
|
||||
def check_lsmdiskgrp():
|
||||
p.add_summary("%s diskgroups found" % (len(rows)))
|
||||
p.add_metric("number of groups", len(rows))
|
||||
for row in rows:
|
||||
if row.status != 'online':
|
||||
p.status(critical)
|
||||
p.add_summary("group %s is %s." % (row.name, row.status))
|
||||
p.add_long_output("%s: used: %s out of %s" % (row.name, row.used_capacity, row.capacity))
|
||||
# Add a performance metric
|
||||
metric_name = "%s_capacity" % row.name
|
||||
p.add_metric(metric_name, value=row.used_capacity, max=row.capacity)
|
||||
|
||||
lines = stdout.splitlines()
|
||||
top_line = lines.pop(0)
|
||||
headers = top_line.split()
|
||||
Row = namedtuple('Row', ' '.join(headers))
|
||||
rows = []
|
||||
for i in lines:
|
||||
i = i.strip()
|
||||
columns = i.split()
|
||||
row = Row(*columns)
|
||||
rows.append(row)
|
||||
|
||||
if query == 'lsmdiskgrp':
|
||||
p.add_summary("%s diskgroups found" % (len(rows)))
|
||||
p.add_metric("number of groups" % len(rows))
|
||||
for row in rows:
|
||||
if row.status != 'online':
|
||||
p.status(critical)
|
||||
p.add_summary("group %s is %s." % (row.name, row.status))
|
||||
p.add_long_output("%s: used: %s out of %s" % (row.name, row.used_capacity, row.capacity))
|
||||
# Add a performance metric
|
||||
metric_name = "%s_capacity" % (row.name)
|
||||
p.add_metric(metric_name, value=row.used_capacity, max=row.capacity)
|
||||
elif query == 'lsarray':
|
||||
p.add_summary("%s arrays found" % (len(rows)))
|
||||
p.add_metric("number of arrays", len(rows))
|
||||
for row in rows:
|
||||
if row.status != 'online':
|
||||
p.add_summary("array %s is %s." % (row.name, row.status))
|
||||
p.status(critical)
|
||||
# Add some performance metrics
|
||||
metric_name = row.mdisk_name + "_capacity"
|
||||
p.add_metric(metric_name, value=row.capacity)
|
||||
|
||||
else:
|
||||
p.status(unknown)
|
||||
p.add_summary("unsupported query: %s. See -L for list of valid queries" % (query))
|
||||
p.exit()
|
||||
def check_lsdrive():
|
||||
p.add_summary("%s drives found" % (len(rows)))
|
||||
p.add_metric("number of drives", len(rows))
|
||||
for row in rows:
|
||||
if row.status != 'online':
|
||||
p.add_summary("drive %s is %s" % (row.id, row.status))
|
||||
|
||||
|
||||
p.check_all_metrics()
|
||||
p.exit()
|
||||
|
||||
def check_lsarray():
|
||||
p.add_summary("%s arrays found" % (len(rows)))
|
||||
p.add_metric("number of arrays", len(rows))
|
||||
for row in rows:
|
||||
if row.status != 'online':
|
||||
p.add_summary("array %s is %s." % (row.name, row.status))
|
||||
p.status(critical)
|
||||
# Add some performance metrics
|
||||
metric_name = row.mdisk_name + "_capacity"
|
||||
p.add_metric(metric_name, value=row.capacity)
|
||||
|
||||
|
||||
def main():
|
||||
if query == 'lsmdiskgrp':
|
||||
check_lsmdiskgrp()
|
||||
elif query == 'lsarray':
|
||||
check_lsarray()
|
||||
elif query == 'lsdrive':
|
||||
check_lsdrive()
|
||||
else:
|
||||
p.status(unknown)
|
||||
p.add_summary("unsupported query: %s. See -L for list of valid queries" % query)
|
||||
p.exit()
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
rows = run_query()
|
||||
main()
|
||||
except KeyError, e:
|
||||
p.status(unknown)
|
||||
p.add_summary("Unhandled exception when running plugin")
|
||||
p.add_long_output(str(e))
|
||||
p.check_all_metrics()
|
||||
p.exit()
|
||||
|
Loading…
Reference in New Issue
Block a user