Skip to content

Instantly share code, notes, and snippets.

@ubiquitousthey
Last active December 10, 2015 23:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ubiquitousthey/4513128 to your computer and use it in GitHub Desktop.
Save ubiquitousthey/4513128 to your computer and use it in GitHub Desktop.
Cleanup RabbitMQ Virtual Hosts
#!/usr/bin/env python
import httplib
import base64
import json
auth = base64.encodestring('{0}:{1}'.format('guest','guest'))
headers = {'Content-type':'application/json','Authorization':'Basic {0}'.format(auth)}
def remove_vhost(vhost):
print('Deleting vhost: {0}'.format(vhost))
conn = httplib.HTTPConnection('localhost',15672)
conn.request('DELETE', '/api/vhosts/{0}'.format(vhost), headers=headers)
def get_connected_vhosts():
data = get_rest_data('/api/connections')
return [rmq_conn['vhost'] for rmq_conn in data]
def get_vhosts_with_messages():
data = get_rest_data('/api/queues')
return [q['vhost'] for q in data if q['messages'] > 0]
def get_all_vhosts():
return get_rest_data('/api/vhosts')
def get_rest_data(url):
conn = httplib.HTTPConnection('localhost',15672)
conn.request('GET',url,headers=headers)
data = json.loads(conn.getresponse().read())
return data
vhosts = get_all_vhosts()
connected_vhosts = get_connected_vhosts()
messaged_vhosts = get_vhosts_with_messages()
vhosts_to_save = connected_vhosts + messaged_vhosts
vhosts_to_save.append('/')
for vhost in vhosts:
vhost_name = vhost['name']
if vhost_name not in vhosts_to_save:
remove_vhost(vhost_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment