commit 364e6745d0fe3f10c5e146348741fda06081ba98 Author: Pedro de Oliveira Date: Mon Nov 28 00:25:11 2016 +0000 first commit diff --git a/myping-client.py b/myping-client.py new file mode 100755 index 0000000..188058b --- /dev/null +++ b/myping-client.py @@ -0,0 +1,71 @@ +#!/usr/bin/env python2 +# -*- coding: utf-8 -*- + +import pymongo +import cherrypy +import simplejson as json +import datetime +import os.path +import time + +current_dir = os.path.dirname(os.path.abspath(__file__)) + +client = pymongo.MongoClient("localhost", 27017) +db = client.myping + +class Values: + + def index(self, **params): + return cherrypy.lib.static.serve_file(os.path.join(current_dir, 'public/index.html')) + + def values(self, items, host, **params): + values = { 'ranges': [], 'values': [] } + + for record in db.my_collection.find({"host": host}).sort('$natural', pymongo.DESCENDING).limit(int(items)): + dt = time.mktime(record['date'].timetuple()) * 1000 + values['ranges'].append([dt, record['min'], record['max']]) + values['values'].append([dt, record['avg']]) + + values['ranges'].reverse() + values['values'].reverse() + + cherrypy.response.headers['Content-Type']= 'application/json' + return json.dumps(values) + + def update(self, timestamp, host, **params): + values = { 'ranges': [], 'values': [] } + + date = datetime.datetime.fromtimestamp(int(timestamp)) + + for record in db.my_collection.find({"host": host, 'date': {'$gt': date}}).sort('$natural', pymongo.DESCENDING).limit(100): + dt = time.mktime(record['date'].timetuple()) * 1000 + values['ranges'].append([dt, record['min'], record['max']]) + values['values'].append([dt, record['avg']]) + + values['ranges'].reverse() + values['values'].reverse() + + cherrypy.response.headers['Content-Type']= 'application/json' + return json.dumps(values) + +if __name__ == '__main__': + + + + mapper = cherrypy.dispatch.RoutesDispatcher() + mapper.connect('home',"/",controller=Values(), action='index') + mapper.connect('values',"/api/values/:items/:host", controller=Values(), action='values') + mapper.connect('update',"/api/update/:timestamp/:host", controller=Values(), action='update') + + cherrypy.tree.mount(None,config= + { + "/": + { + "request.dispatch": mapper, + }, + } + ) + + cherrypy.server.socket_host = '0.0.0.0' + cherrypy.engine.start() + cherrypy.engine.block() diff --git a/myping-server.py b/myping-server.py new file mode 100755 index 0000000..8ca5e8d --- /dev/null +++ b/myping-server.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python2 +# -*- coding: utf-8 -*- + +import pymongo +import subprocess +import re +import datetime + +client = pymongo.MongoClient("localhost", 27017) +db = client.myping + +hosts = [ + "DeadBSD.org", + "nausea.DeadBSD.org", + "helvete.DeadBSD.org", + "gamito.DeadBSD.org", + "www.nos.pt", + "192.168.5.1", + ] + +arguments = [ + "fping", + "-C", "20", + "-q", + "-B1", + "-r1", + "-i10", + ] + +for h in hosts: + arguments.append(h) + +dt = datetime.datetime.now() + +p = subprocess.Popen(arguments, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) +output = p.communicate()[0] + +for h in hosts: + obj = { 'date': dt, 'host': h } + match = re.search("" + h + " +: (.*)?", output) + if match: + result = match.group(1) + times = map(float, result.split()) + obj['min'] = min(times) + obj['avg'] = sum(times) / float(len(times)) + obj['max'] = max(times) + print obj + db.my_collection.insert_one(obj) + diff --git a/public/index.html b/public/index.html new file mode 100755 index 0000000..9ef4365 --- /dev/null +++ b/public/index.html @@ -0,0 +1,133 @@ + + + + + + + + + + + + + + + + + + + + +
+
+
+
+
+
+ + diff --git a/server.sh b/server.sh new file mode 100755 index 0000000..32049e3 --- /dev/null +++ b/server.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +while :; do ./myping-server.py; sleep 60; done \ No newline at end of file