123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- """
- This is a RESTful interface implemented using Flask micro framework.
- The API is driven by configuration that is organized in terms of the monitoring classes
- We designed the classes to be reusable (and powered by labels):
- 'monitoring-type':
- 'class':'<class-name>'
- 'config':<labeled-class-specific-configuration>'
- """
- from flask import Flask, session, request, redirect, Response
- from flask.templating import render_template
- from flask_session import Session
- import time
- import sys
- import os
- import json
- import re
- import monitor
- import Queue
- from utils.transport import *
- PARAMS = {'context':''}
- if len(sys.argv) > 1:
-
- N = len(sys.argv)
- for i in range(1,N):
- value = None
- if sys.argv[i].startswith('--'):
- key = sys.argv[i].replace('-','')
-
- if i + 1 < N:
- value = sys.argv[i + 1] = sys.argv[i+1].strip()
- if key and value:
- PARAMS[key] = value
-
- i += 2
-
- app = Flask(__name__)
- f = open(PARAMS['path'])
- CONFIG = json.loads(f.read())
- HANDLERS= {}
- for key in CONFIG :
- if key == "monitor":
- continue
- className = CONFIG[key]['class']
- ref = "".join(["monitor.",className,"()"])
- ref = eval(ref)
- #ref.init(CONFIG[key]['config'])
- HANDLERS[key] = {"class":ref,"config":CONFIG[key]["config"]}
- f.close()
- #
- #
- from threading import Thread, RLock
- p = {'uri':'http://dev.the-phi.com:5984','dbname':'monitor','uid':'logs','filename':'logs.JSON'}
- factory = DataSourceFactory()
- gWriter = factory.instance(type='CouchdbWriter',args=p)
- gReader = factory.instance(type='CouchdbReader',args=p)
- mthread = monitor.Monitor(HANDLERS,gWriter,'processes',)
- mthread.start()
- #(Timer(10,mthread.run)).start()
- #mthread = Process(target=monitor.Monitor,args=(HANDLERS,ProcessQueue,'processes'))
- #mthread.start()
- @app.route('/get/<id>')
- def procs(id):
- if id in HANDLERS and len(mthread.logs)>0:
- # r = ProcessQueue.get(block=True,timeout=15)
- index = len(mthread.logs) -1
- r = mthread.logs[index]
- return json.dumps(r)
- else:
- return "[]"
- pass
- @app.route('/trends')
- def trends ():
- id = request.args.get('id')
- # key = request.args.get('key')
- global mthread
- # mLock.acquire()
-
- time.sleep(2)
- doc = gReader.read()
- doc['row']
- handler = monitor.mapreducer()
- r = handler.filter(id,logs)
- r = handler.run(r,handler.mapper,handler.reducer)
- # mLock.release()
- if 'Google Chrome' in r:
- for item in r['Google Chrome']:
- print item['hour'],item['minute']
-
- return json.dumps(r)
- @app.route('/dashboard')
- def dashboard():
- context = PARAMS['context']
- return render_template('dashboard.html',context=context)
- if __name__== '__main__':
- app.secret_key = 'A0Zr98j/3yX R~XHH!jmN]LWX=?RT'
- app.run(host='0.0.0.0',debug=True,threaded=True)
-
|