index.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. """
  2. This is a RESTful interface implemented using Flask micro framework.
  3. The API is driven by configuration that is organized in terms of the monitoring classes
  4. We designed the classes to be reusable (and powered by labels):
  5. 'monitoring-type':
  6. 'class':'<class-name>'
  7. 'config':<labeled-class-specific-configuration>'
  8. """
  9. from flask import Flask, session, request, redirect, Response
  10. from flask.templating import render_template
  11. from flask_session import Session
  12. import sys
  13. import os
  14. import json
  15. import re
  16. import monitor
  17. import Queue
  18. PARAMS = {'context':''}
  19. if len(sys.argv) > 1:
  20. N = len(sys.argv)
  21. for i in range(1,N):
  22. value = None
  23. if sys.argv[i].startswith('--'):
  24. key = sys.argv[i].replace('-','')
  25. if i + 1 < N:
  26. value = sys.argv[i + 1] = sys.argv[i+1].strip()
  27. if key and value:
  28. PARAMS[key] = value
  29. i += 2
  30. app = Flask(__name__)
  31. f = open(PARAMS['path'])
  32. CONFIG = json.loads(f.read())
  33. HANDLERS= {}
  34. for key in CONFIG :
  35. if key == "monitor":
  36. continue
  37. className = CONFIG[key]['class']
  38. ref = "".join(["monitor.",className,"()"])
  39. ref = eval(ref)
  40. #ref.init(CONFIG[key]['config'])
  41. HANDLERS[key] = {"class":ref,"config":CONFIG[key]["config"]}
  42. f.close()
  43. #
  44. #
  45. from threading import Timer,Thread
  46. ProcessQueue = Queue.LifoQueue()
  47. mthread = monitor.Monitor(HANDLERS,ProcessQueue,'processes')
  48. mthread.start()
  49. #(Timer(10,mthread.run)).start()
  50. #mthread = Process(target=monitor.Monitor,args=(HANDLERS,ProcessQueue,'processes'))
  51. #mthread.start()
  52. @app.route('/get/<id>')
  53. def procs(id):
  54. if id in HANDLERS and len(mthread.logs)>0:
  55. # r = ProcessQueue.get(block=True,timeout=15)
  56. index = len(mthread.logs) -1
  57. r = mthread.logs[index]
  58. return json.dumps(r)
  59. else:
  60. return "[]"
  61. pass
  62. @app.route('/trends')
  63. def trends ():
  64. id = request.args.get('id')
  65. # key = request.args.get('key')
  66. handler = monitor.mapreducer()
  67. r = handler.filter(id,mthread.logs)
  68. r = handler.run(r,handler.mapper,handler.reducer)
  69. return json.dumps(r)
  70. @app.route('/dashboard')
  71. def dashboard():
  72. context = PARAMS['context']
  73. return render_template('dashboard.html',context=context)
  74. if __name__== '__main__':
  75. app.secret_key = 'A0Zr98j/3yX R~XHH!jmN]LWX=?RT'
  76. app.run(host='0.0.0.0',debug=True,threaded=True)