From 10c414706c2c77225c014673f03b8e3166b82ff2 Mon Sep 17 00:00:00 2001 From: ndarshan Date: Thu, 10 Apr 2014 11:15:53 +0530 Subject: Server-addons: Server-side plugin for volume status. Modified the existing volume utilization server plugin to include status information as well. Refactored the plugin with an argument -o|--option to get the specific volume status. Change-Id: I2b6f8612e32390fb20917098a255de1ee6b3a170 Signed-off-by: ndarshan Signed-off-by: Sahina Bose --- plugins/check_vol_server.py | 127 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100755 plugins/check_vol_server.py (limited to 'plugins/check_vol_server.py') diff --git a/plugins/check_vol_server.py b/plugins/check_vol_server.py new file mode 100755 index 0000000..87ca269 --- /dev/null +++ b/plugins/check_vol_server.py @@ -0,0 +1,127 @@ +#!/usr/bin/python +import sys +import commands +import random +import argparse +import livestatus +import os +from glusternagios import utils + +_NRPEPath = "/usr/lib64/nagios/plugins/check_nrpe" + + +def _getListHosts(args): + table = livestatus.readLiveStatus("GET hostgroups\nColumns: members\n" + "Filter: name = " + + args.hostgroup + "\n") + tab1 = table[0] + list_hosts = tab1[0].split(",") + #First take a random host from the group and send the request + return list_hosts + + +def _getHostAddress(host): + # Get the address of the host + host_address = livestatus.checkLiveStatus("GET hosts\nColumns: address\n" + "Filter: display_name = " + + host + "\n") + return host_address.rstrip() + + +def _getVolUtilizationNRPECommand(args): + return ("check_vol_utilization -a " + args.volume + " " + + str(args.warning) + " " + str(args.critical)) + + +def _getVolStatusNRPECommand(args): + return ("check_vol_status -a " + args.volume) + + +def _getNRPEBaseCmd(host): + return _NRPEPath + " -H " + host + " -c "; + + +def execNRPECommand(command): + status, output = commands.getstatusoutput(command) + return os.WEXITSTATUS(status), output + + +def showVolumeOutput(args): + list_hosts = _getListHosts(args) + host = random.choice(list_hosts) + #Get the address of the host + host_address = _getHostAddress(host) + + if args.option == 'status': + command = _getVolStatusNRPECommand(args) + elif args.option == 'utilization': + command = _getVolUtilizationNRPECommand(args) + + status, output = execNRPECommand(_getNRPEBaseCmd(host_address) + command) + + if status != utils.PluginStatusCode.UNKNOWN: + return status, output + for host in list_hosts: + status, output = execNRPECommand(_getNRPEBaseCmd(_getHostAddress(host)) + + command) + if status != utils.PluginStatusCode.UNKNOWN: + return status, output + break + return status, output + + #if success return from here + if "Volume Utilization" in output: + return status, output + #radom host is not able to execute the command + #Now try to iterate through the list of hosts + #in the host group and send the command until + #the command is successful + for host in list_hosts: + status, output = execNRPECommand(host, args) + #if success return from here + if "Volume Utilization" in output: + return status, output + break + return status, output + + +def parse_input(): + parser = argparse.ArgumentParser( + usage='%(prog)s [-h] -w ' + ' -c [-o|--option]') + parser.add_argument( + "hostgroup", + help="Name of the hostgroup to which the volume belongs") + parser.add_argument( + "volume", + help="Name of the volume being queried") + parser.add_argument( + "-w", + "--warning", + action="store", + type=int, + default=70, + help="Warning Threshold in percentage") + parser.add_argument( + "-c", + "--critical", + action="store", + type=int, + default=90, + help="Critical Threshold in percentage") + parser.add_argument('-o', '--option', + action='store', + help='the volume option to check', + choices=['utilization', + 'status']) + args = parser.parse_args() + if args.critical <= args.warning: + print "UNKNOWN:Critical must be greater than Warning." + sys.exit(utils.PluginStatusCode.UNKNOWN) + return args + +if __name__ == '__main__': + args = parse_input() + status, output = showVolumeOutput(args) + print output + exit(status) -- cgit