Permalink
Browse files

Initial Commit for Rabbithole

Rabbithole pushes every now and then status informations about the
rabbitmq database into the network.
  • Loading branch information...
Felix Richter Felix Richter
Felix Richter authored and Felix Richter committed Feb 3, 2011
0 parents commit 8151e5bb0bf7c6bfd908f73139752d50561ab6a6
Showing with 145 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +4 −0 README.md
  3. +5 −0 bin/run.sh
  4. +14 −0 conf/example.json
  5. +35 −0 src/main.py
  6. +86 −0 src/rabbithole.py
@@ -0,0 +1 @@
+*.pyc
@@ -0,0 +1,4 @@
+Rabbithole
+=========
+A Genericore plugin which provides informations about the current status of
+the rabbitmq running on the local host.
@@ -0,0 +1,5 @@
+#!/bin/bash
+echo basedir $0
+BINDIR="`dirname $0`/../src"
+
+python2 "$BINDIR/main.py" $@
@@ -0,0 +1,14 @@
+{
+ "rabbithole": {
+ "amqp": {
+ "connection": {
+ "login": "guest",
+ "password": "guest",
+ "host": "127.0.0.1"
+ },
+ "out": {
+ "exchange": "rabbithole"
+ }
+ }
+ }
+}
@@ -0,0 +1,35 @@
+#!/usr/bin/env python2
+import json,time
+from rabbithole import Rabbithole
+import logging
+import genericore as gen
+MODULE_NAME='rabbithole'
+
+log = logging.getLogger(MODULE_NAME)
+PROTO_VERSION = 1
+DESCRIPTION = 'Periodically sends Status informations of Rabbitmq'
+
+
+# set up instances of needed modules
+conf = gen.Configurator(PROTO_VERSION,DESCRIPTION)
+amqp = gen.auto_amqp(MODULE_NAME)
+s = Rabbithole(MODULE_NAME) # the magic mail parsing class
+
+conf.configure([amqp,s]) #set up parser and eval parsed stuff
+
+# start network connections
+amqp.create_connection()
+
+log.info('Starting up infinite loop')
+print ' Sending Messages in Intervals. To exit press CTRL+C'
+try:
+ while True:
+ log.info("Collecting from Database")
+ ret = s.collect()
+ data = { 'type' : 'backbone', 'subtype' : 0, 'data' : ret}
+ log.debug("writing data to queue : %s" % data)
+ amqp.publish(json.dumps(data))
+ log.info('Sleeping for %d Seconds' % s.getRepeat())
+ time.sleep(s.getRepeat())
+except Exception as e:
+ print "something happened :( " + str(e)
@@ -0,0 +1,86 @@
+#!/usr/bin/python2
+
+import logging, subprocess
+from multiprocessing import Pool
+from genericore import Configurable
+log = logging.getLogger('Rabbithole')
+
+DEFAULT_CONFIG= {
+ 'repeat' : 30,
+ 'vhost' : '/', # TODO currently unsupported
+ 'cmds' : [
+ 'status',
+ 'list_vhosts',
+ 'list_users',
+ 'list_permissions',
+ 'list_queues',
+ 'list_exchanges',
+ 'list_bindings',
+ 'list_connections',
+ 'list_channels',
+ 'list_consumers' ]
+}
+
+def call_external(cmd):
+ ''' calls the command for rabbitmq, use with caution!'''
+ log.debug('calling ' + 'rabbitmqctl' + cmd)
+ out = subprocess.Popen(
+ ['rabbitmqctl',
+ cmd],
+ stdout=subprocess.PIPE).communicate()[0]
+ return { 'cmd' : cmd ,'output' : out }
+
+class Rabbithole(Configurable):
+ mac_list = {}
+ def __init__(self,MODULE_NAME='rabbithole',config=None):
+ self.NAME = MODULE_NAME
+ newconf = { MODULE_NAME : DEFAULT_CONFIG }
+ Configurable.__init__(self,newconf)
+ self.load_conf(config)
+
+ def getRepeat(self):
+ ''' getter convenience function for long config path'''
+ return self.config[self.NAME]['repeat']
+
+ def parallel_call(self):
+ cmds = self.config[self.NAME]['cmds']
+ try:
+ p = Pool(10)
+ ret = p.map(call_external, cmds)
+
+ p.terminate()
+ return ret
+ except Exception as e:
+ log.warning("Something happened,falling back to original data: "+ str(e))
+ return []
+
+ def process_output(self,output):
+ '''will pull the data together in one dictionary,
+ TODO parse the output of every command differently'''
+ ret = {}
+ for i in output :
+ ret[i['cmd']] = i['output']
+ return ret
+
+ def collect(self):
+ output = self.parallel_call()
+ log.debug(str(output))
+ ret = self.process_output(output)
+ return ret
+
+ def print_output(self,data):
+ for k,v in data.items():
+ print str(k) + ' --> ' + str(v)
+ print
+ def populate_parser(self,parser):
+ parser.add_argument('--repeat',type=int,dest='repeat',help='Seconds between Scans',metavar='SECS') #TODO add this to configuration
+
+ def eval_parser(self,parsed):
+ '''does not allow endless without delay'''
+ self.repeat = parsed.repeat if parsed.repeat else self.getRepeat()
+
+if __name__ == '__main__' :
+ rab = Rabbithole()
+ logging.basicConfig(level=logging.INFO)
+ out = rab.collect()
+ rab.print_output(out)

0 comments on commit 8151e5b

Please sign in to comment.