diff --git a/monitoring-for-openstack/oschecks/pacemaker_host_check.py b/monitoring-for-openstack/oschecks/pacemaker_host_check.py old mode 100755 new mode 100644 index 64caf80..b4eb85a --- a/monitoring-for-openstack/oschecks/pacemaker_host_check.py +++ b/monitoring-for-openstack/oschecks/pacemaker_host_check.py @@ -22,36 +22,81 @@ import argparse import os import shlex import subprocess +import re try: import utils except ImportError: from oschecks import utils +def _ok_run_script(options): + '''If there is a script to run it is executed otherwise a default message. + + Argument: + options (Object) -- main program arguments + ''' + if options.script: + script = shlex.split(options.script) + os.execvp(script[0], script) + else: + utils.ok("pacemaker resource %s is running" + % options.pacemaker_resource) + +def _check_resource_in_host(remaining, match_word, options, local_hostname): + '''Searches for resource and a local_hostname on the rest of the line + + It checks if the resource is the second or third word on the line and + search for the host on the running nodes + + Arguments: + :param remaining: (str)-- the rest of the line + :param match_word: (str)-- 'Started:'-->Clone or 'Master'-->Master/Slave + :param options: (object)-- main program arguments + :param local_hostname: -- localhost + ''' + engine = re.compile('Set: ('+options.pacemaker_resource+' \[.*\]|.* \[' + +options.pacemaker_resource+'\]) '+match_word+' (\[.*?\])') + patterns = re.search(engine, remaining) + if patterns is not None: + host_list = patterns.group(2).split()[1:-1] + for host in host_list: + if host == local_hostname: + _ok_run_script(options) + utils.ok("pacemaker resource %s doesn't on this node " + "(but on %s)" % (resource, patterns.group(2))) def _pacemaker_host_check(): parser = argparse.ArgumentParser( description='Check amqp connection of an OpenStack service.') parser.add_argument('-r', dest='pacemaker_resource', help='pacemaker resource', required=True) - parser.add_argument('-s', dest='script', required=True, + parser.add_argument('-s', dest='script', required=False, help='Script') + parser.add_argument('--crm', dest='crm', required=False, + help='Use "crm_mon -1" instead of "pcs status"', + action='store_true', default=False) options = parser.parse_args() - local_hostname = subprocess.check_output(['hostname', '-s']).strip() + if options.script and (not os.path.isfile(options.script) + or not os.access(options.script, os.X_OK)): + utils.critical('the script %s could not be read' % options.script) + local_hostname = subprocess.check_output(['hostname', '-s']).strip() try: - output = subprocess.check_output(['pcs', 'status']) + if options.crm : + output = subprocess.check_output(['crm_mon', '-1']) + else: + output = subprocess.check_output(['pcs', 'status']) except subprocess.CalledProcessError as e: utils.critical('pcs status with status %s: %s' % e.returncode, e.output) except OSError: utils.critical('pcs not found') - for line in output.splitlines(): + + for line in re.sub("\n +", " ", output).splitlines(): line = " ".join(line.strip().split()) # Sanitize separator if not line: continue - resource, remaining = line.split(None, 1) if resource == options.pacemaker_resource: agent, __, remaining = remaining.partition(' ') @@ -63,15 +108,20 @@ def _pacemaker_host_check(): utils.critical("pacemaker resource %s is not started (%s)" % (resource, status)) if current_hostname != local_hostname: - utils.ok("pacemaker resource %s doesn't on this node " + utils.ok("pacemaker resource %s doesn't run on this node " "(but on %s)" % (resource, current_hostname)) - script = shlex.split(options.script) - os.execvp(script[0], script) + _ok_run_script(options) + elif resource == 'Clone' : + _check_resource_in_host(remaining, 'Started:', options, + local_hostname) + elif resource == 'Master/Slave': + _check_resource_in_host(remaining, 'Masters:', options, + local_hostname) else: utils.critical('pacemaker resource %s not found' % options.pacemaker_resource) - def pacemaker_host_check(): utils.safe_run(_pacemaker_host_check) +