3 from datetime import datetime
14 # Generic function to run a command, removes top item from output as this is always either 'listing vhosts', 'listing queue' etc
15 def get_command(command):
17 command = shlex.split(command)
19 proc = subprocess.Popen(command,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=False)
20 output = proc.communicate()
22 # Output[0] comes as a tuple so items are first split at \n before first and last items are removed because first items descripes the output and last item is and empty item
23 out = output[0].split('\n')[1:-1]
29 # Returns a list of rabbitmq virtual hosts
32 command = '/usr/sbin/rabbitmqctl list_vhosts'
33 vhosts = get_command(command)
39 # Generates autodiscovery json output for virtualhost and its queues
48 command = '/usr/sbin/rabbitmqctl list_queues -p ' + vhost + ' name'
49 queues = get_command(command)
51 # only if queues are not empty
57 inst = {"{#VHOST}":vhost, "{#QUEUE}":queue}
58 json_list.append(inst)
60 result = {"data":json_list}
61 print json.dumps(result,sort_keys=True,indent=2)
65 # Generates autodiscovery json output for virtualhost and its queues
74 command = '/usr/sbin/rabbitmqctl list_exchanges -p ' + vhost + ' name'
76 exchanges = get_command(command)
78 # default and empty exchanges should be ignored
79 default_and_empty_exchanges = ['amq.direct', 'amq.fanout', 'amq.match', 'amq.topic', 'amq.headers', 'amq.rabbitmq.trace', 'amq.rabbitmq.log', '']
81 # only if exchanges are not empty
84 for exchange in exchanges:
86 # Ensures that non of the exchanges from default_and_empty_exchanges are included
87 if all(x != exchange for x in default_and_empty_exchanges):
90 inst = {"{#VHOST}":vhost, "{#EXCHANGE}":exchange}
91 json_list.append(inst)
93 result = {"data":json_list}
94 print json.dumps(result,sort_keys=True,indent=2)
98 if __name__ == '__main__':
102 parser= argparse.ArgumentParser()
104 parser.add_argument('-lh', '--list_vhosts', action='store_true', help='lists virtual hosts')
105 parser.add_argument('-lq', '--list_queues', action='store_true', help='lists virtual hosts and their queues')
106 parser.add_argument('-le', '--list_exchanges', action='store_true', help='lists virtual hosts and their exchanges')
108 args = parser.parse_args()
111 vhosts = get_vhosts()
115 elif args.list_queues:
118 elif args.list_exchanges:
125 except Exception as e: