Skip to content
Browse files

initial commit, codebase is mail_stats

  • Loading branch information...
0 parents commit df68f2209fbf37fbd2c7ba1694dc3254d8b5e166 Felix Richter committed Jan 30, 2011
Showing with 170 additions and 0 deletions.
  1. +5 −0 bin/run.sh
  2. +95 −0 src/mail_shiny.py
  3. BIN src/mail_shiny.pyc
  4. +45 −0 src/main.py
  5. +1 −0 template/irc.mustache
  6. +14 −0 template/mail.mustache
  7. +10 −0 template/snmp.mustache
5 bin/run.sh
@@ -0,0 +1,5 @@
+#!/bin/bash
+dirname="`dirname "$0"`"
+mongod &
+BINDIR=$dirname/../src
+python2 $BINDIR/main.py "$@"
95 src/mail_shiny.py
@@ -0,0 +1,95 @@
+#!/usr/bin/python2
+from genericore import Configurable
+import pystache,cherrypy,argparse
+import logging, sys
+from os import path
+
+MODULE_NAME = 'mail_stats'
+
+log = logging.getLogger(MODULE_NAME)
+
+DEFAULT_CONFIG = {
+ MODULE_NAME : {
+ "http" : {
+ "socket_port" : 8080,
+ "socket_host" : "0.0.0.0"
+ },
+ "template" : {
+ "engine" : "mustache", #currently only mustache is supported
+ #TODO register modules
+ "files" : {
+ "mail" : path.join( path.dirname(sys.argv[0]) + "/../template/mail.mustache") ,
+ "irc" : path.join( path.dirname(sys.argv[0]) + "/../template/irc.mustache") ,
+ "snmp" : path.join( path.dirname(sys.argv[0]) + "/../template/snmp.mustache")
+ }
+ }
+ }
+}
+
+class mail_shiny(Configurable): #TODO pull out the HTTP server Component
+ stats = {'mail' : {}, 'irc' : {}, 'snmp' : {}}
+ def __init__(self,conf=None):
+ Configurable.__init__(self,DEFAULT_CONFIG)
+ self.load_conf(conf)
+ cherrypy.server.__dict__.update(self.config[MODULE_NAME]["http"])
+
+ def process(self,stats):
+ log.debug("Received Stats: " + str(stats))
+ self.stats[stats['type']] = stats['data']
+
+ def create_connection(self):
+ cherrypy.tree.mount(self)
+ cherrypy.server.quickstart()
+ cherrypy.engine.start()
+ self.load_templates()
+
+ def populate_parser(self,parser):
+ parser.add_argument('--http-port',type=int,dest='http_port',help='Http Server host port',metavar='PORT')
+ parser.add_argument('--http-host',dest='http_host',help='Http Server host Address',metavar='ADDR')
+ #TODO template options for irc and mail
+ #TODO refactor this piece
+
+ def eval_parser(self,parsed):
+ conf = self.config[MODULE_NAME]
+ cherrypy.server.socket_port = parsed.http_port if parsed.http_port else conf['http']['socket_port']
+ cherrypy.server.socket_host = parsed.http_host if parsed.http_host else conf['http']['socket_host']
+
+ def load_templates(self):
+ self.templates = {}
+ for k,v in self.config[MODULE_NAME]['template']['files'].items():
+ log.debug("Loading Template:" + str(v) )
+ f = open(v)
+ self.templates[k] = f.read()
+ f.close()
+
+ # path functions
+ def index(self):
+ """ will be called when a client requests '/' """
+ # TODO write status "LED"s when mails and irc comes in
+ return "Check out : <a href='mail'>Mail</a> or <a href='irc'>IRC</a> or <a href='mail'>SNMP User</a>stats"
+ index.exposed=True
+
+ # TODO make this more generic if necessary
+ def mail(self):
+ self.load_templates()
+ if not self.stats['mail']:
+ return "No Mail Statistics received yet!"
+ return pystache.render(self.templates['mail'],self.stats['mail'])
+ mail.exposed=True
+ def snmp(self):
+ self.load_templates()
+ if not self.stats['snmp']:
+ return "No SNMP Statistics received yet!"
+ stache = {'num_clients' : 0, 'mlist' : []}
+ stache['num_clients'] = len(self.stats['snmp'])
+ for mac,ips in self.stats['snmp'].items():
+ stache['mlist'].append({'mac': mac, 'ips' : ', '.join(ips)})
+ return pystache.render(self.templates['snmp'],stache)
+ snmp.exposed=True
+
+ def irc(self):
+ return pystache.render(self.templates['irc'],self.stats)
+ irc.exposed=True
+
+ def close_connection(self):
+ cherrypy.engine.exit()
BIN src/mail_shiny.pyc
Binary file not shown.
45 src/main.py
@@ -0,0 +1,45 @@
+#!/usr/bin/env python2
+import sys,json
+from mail_shiny import mail_shiny
+import logging
+import genericore as gen
+log = logging.getLogger('mail_shiny')
+PROTO_VERSION = 1
+DESCRIPTION = 'Makes Statistics shiny'
+
+
+# set up instances of needed modules
+conf = gen.Configurator(PROTO_VERSION,DESCRIPTION)
+amqp1 = gen.auto_amqp()
+amqp2 = gen.auto_amqp()
+s = mail_shiny() # the magic mail parsing class
+
+conf.configure([amqp1,amqp2,s]) #set up parser and eval parsed stuff
+
+#TODO fix this thingy...
+amqp1.load_conf({'amqp' : {"in" : { "exchange" : "mail_stats"}}})
+amqp2.load_conf({'amqp' : {"in" : { "exchange" : "snmp_src"}}})
+# start network connections
+s.create_connection()
+amqp1.create_connection()
+amqp2.create_connection()
+
+# main method
+def cb (ch,method,header,body):
+ log.debug ( "Header %r" % (header,))
+ log.debug ( "Body %r" % (body,))
+ try:
+ entry = s.process(json.loads(body))
+ except Exception as e:
+ print 'Something just fuckin happened ' + str(e)
+ raise e
+
+amqp1.consume(cb)
+amqp2.consume(cb)
+print "waiting for messages"
+try:
+ amqp1.start_loop()
+except:
+ amqp1.close_connection()
+ amqp2.close_connection()
+ s.close_connection()
1 template/irc.mustache
@@ -0,0 +1 @@
+Hello IRC
14 template/mail.mustache
@@ -0,0 +1,14 @@
+Hello, Mail!
+
+<table>
+ {{#data}}
+ {{#num_mail_abs}}
+ <tr><td><strong>Number of Mails</strong></td><td></td></tr>
+ <tr><td>last 30 days</td><td>{{month}}</td></tr>
+ <tr><td>last 7 days</td><td>{{week}}</td></tr>
+ <tr><td>last day</td><td>{{day}}</td></tr>
+ <tr><td>last hour</td><td>{{hour}}</td></tr>
+ <tr><td>last 5 minutes</td><td>{{5minutes}}</td></tr>
+ {{/num_mail_abs}}
+ {{/data}}
+</table>
10 template/snmp.mustache
@@ -0,0 +1,10 @@
+snmp current clients:<br/>
+
+Active Clients: {{num_clients}} <br/>
+
+<table>
+ <tr><td><strong>Mac Address</strong></td><td>IPs</td></tr>
+ {{#mlist}}
+ <tr><td>{{mac}}</td><td>{{ips}}</td></tr>
+ {{/mlist}}
+</table>

0 comments on commit df68f22

Please sign in to comment.
Something went wrong with that request. Please try again.