Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
initial commit, codebase is mail_stats
Browse files Browse the repository at this point in the history
  • Loading branch information
Felix Richter authored and Felix Richter committed Jan 30, 2011
0 parents commit df68f22
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 0 deletions.
5 changes: 5 additions & 0 deletions bin/run.sh
@@ -0,0 +1,5 @@
#!/bin/bash
dirname="`dirname "$0"`"
mongod &
BINDIR=$dirname/../src
python2 $BINDIR/main.py "$@"
95 changes: 95 additions & 0 deletions 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()
Binary file added src/mail_shiny.pyc
Binary file not shown.
45 changes: 45 additions & 0 deletions 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 change: 1 addition & 0 deletions template/irc.mustache
@@ -0,0 +1 @@
Hello IRC
14 changes: 14 additions & 0 deletions 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 changes: 10 additions & 0 deletions 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.