Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wall #11

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open

Wall #11

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@ nosetests.xml
.mr.developer.cfg
.project
.pydevproject
.idea

tests/mibs/.index
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ MySQL-python==1.2.5
PyYAML==3.10
SQLAlchemy==0.9.1
backports.ssl-match-hostname==3.4.0.2
distribute==0.6.49
distribute==0.6.35
expvar==0.0.2
oid-translate==0.2.2
pyasn1==0.1.7
Expand All @@ -13,3 +13,4 @@ pysnmp==4.2.5
pytz==2014.1
tornado==3.2
wsgiref==0.1.2
tornado-redis==2.4.18
49 changes: 48 additions & 1 deletion trapdoor/handlers.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
from datetime import datetime
import json
from sqlalchemy import desc, func, or_
import logging
import string
import random
import sys
from threading import Timer


import tornado
from sqlalchemy import desc, or_

from tornado import gen

from trapdoor.utils import TrapdoorHandler
from trapperkeeper.models import Notification, VarBind

import tornadoredis

def filter_query(query, host, oid, severity):
if host is not None:
query = query.filter(Notification.host == host)
Expand Down Expand Up @@ -74,6 +86,7 @@ def get(self):
"index.html", traps=traps, now=now, num_active=num_active,
host=host, oid=oid, severity=severity, offset=offset, limit=limit)


class Resolve(TrapdoorHandler):
def post(self):
host = self.get_argument("host")
Expand Down Expand Up @@ -176,3 +189,37 @@ def get(self):
traps, num_active = _get_traps(self.db, offset, limit, host, oid, severity)

self.write(json.dumps([trap.to_dict() for trap in traps]))

class MessageHandler(tornado.websocket.WebSocketHandler):

def __init__(self, *args, **kwargs):
super(MessageHandler, self).__init__(*args, **kwargs)
self.listen()

@tornado.gen.engine
def listen(self):
self.client = tornadoredis.Client()
self.client.connect()
yield tornado.gen.Task(self.client.subscribe, 'root')
self.client.listen(self.on_message)

def on_message(self, msg):
logging.info("received message %s", msg)
if msg.kind == 'message':
self.write_message(str(msg.body))
if msg.kind == 'disconnect':
# Do not try to reconnect, just send a message back
# to the client and close the client connection
self.write_message('The connection terminated '
'due to a Redis server error.')
self.close()

def on_close(self):
"""
Callback when the socket is closed. Frees up resource related to this socket.
"""
logging.info("socket closed, cleaning up resources now")
if self.client.subscribed:
self.client.unsubscribe('root')
self.client.disconnect()

5 changes: 4 additions & 1 deletion trapdoor/routes.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@

from trapdoor import handlers
from trapdoor import handlers,utils

HANDLERS = [
(r"/", handlers.Index),
(r"/resolve/?", handlers.Resolve),
(r"/resolve_all/?", handlers.ResolveAll),

#websocket
(r"/message_wall", handlers.MessageHandler),

# API
(r"/api/varbinds/(?P<notification_id>\d+)", handlers.ApiVarBinds),
(r"/api/activetraps/?", handlers.ApiActiveTraps),
Expand Down
7 changes: 7 additions & 0 deletions trapdoor/static/css/bootstrap.min.css

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions trapdoor/static/css/font-awesome.min.css

Large diffs are not rendered by default.

Binary file added trapdoor/static/fonts/fontawesome-webfont.ttf
Binary file not shown.
Binary file added trapdoor/static/fonts/fontawesome-webfont.woff
Binary file not shown.
37 changes: 37 additions & 0 deletions trapdoor/static/js/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Initiate global websocket object.
* @todo: Add user cookie for authentication.
*/
var host = location.origin.replace(/^http/, 'ws')
var ws = new WebSocket(host +"/message_wall" );

(function($){
$.extend({
playSound: function(){
return $("<embed src='"+arguments[0]+".mp3' hidden='true' autostart='true' loop='false' class='playSound'>" + "<audio autoplay='autoplay' style='display:none;' controls='controls'><source src='"+arguments[0]+".mp3' /><source src='"+arguments[0]+".ogg' /></audio>").appendTo('body');
}
});
})(jQuery);

$(document).ready(function() {
if (!window.console) window.console = {};
if (!window.console.log) window.console.log = function() {};

// Websocket callbacks:
ws.onopen = function() {
console.log("Connected...");
};
ws.onmessage = function (event) {
data = JSON.parse(event.data);
if (data.severity == "warning" || data.severity == "critical") {
setTimeout($.playSound('../static/mp3/msg'), 650);
}
console.log("New Message", data);
$('#table').load(document.URL + '# .table');
};
ws.onclose = function() {
// @todo: Implement reconnect.
console.log("Closed!");
location.reload();
};
});
6 changes: 6 additions & 0 deletions trapdoor/static/js/bootstrap.min.js

Large diffs are not rendered by default.

Loading