Last active
September 27, 2017 08:03
-
-
Save mosquito/8c79f13cb4b2b26a7af72df4608a857d to your computer and use it in GitHub Desktop.
Check status of rabbitmq node
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from ast import literal_eval | |
from contextlib import contextmanager | |
from subprocess import check_output | |
import re | |
import sys | |
def parse_output(input_string): | |
input_string = input_string.replace("\"", '\'') | |
input_string = re.sub('([a-zA-Z\@\<\>\_\"\']+)', r'"\1"', input_string) | |
input_string = re.sub('(\{[a-zA-Z\@\<\>\_\"\']+?),', r'\1:', input_string) | |
return literal_eval(input_string) | |
@contextmanager | |
def log(action): | |
sys.stderr.write(action) | |
sys.stderr.write("... ") | |
sys.stderr.flush() | |
try: | |
yield | |
sys.stderr.write('OK\n') | |
except: | |
sys.stderr.write('FAIL\n') | |
sys.stderr.flush() | |
raise | |
exit(1) | |
def main(): | |
with log("Checking node health check"): | |
output = check_output("rabbitmqctl node_health_check", shell=True).decode() | |
assert "Health check passed" in output | |
with log("Checking cluster status"): | |
output = "" | |
res = enumerate(check_output("rabbitmqctl cluster_status", shell=True).decode().splitlines()) | |
current_node = None | |
for idx, line in res: | |
if not idx: | |
current_node = line.replace('Cluster status of node', '').strip() | |
continue | |
output += line | |
output = [item for item in parse_output(output) if 'running_nodes' in item][0]['running_nodes'] | |
assert current_node in output | |
if __name__ == '__main__': | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment