123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- """
- 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['monitor'] :
-
- className = CONFIG['monitor'][key]['class']
- ref = "".join(["monitor.",className,"()"])
- ref = eval(ref)
- HANDLERS[key] = {"class":ref,"config":CONFIG['monitor'][key]["config"]}
- f.close()
- #
- #
- from threading import Thread, RLock
- p = CONFIG['store']['args']
- class_read = CONFIG['store']['class']['read']
- class_write= CONFIG['store']['class']['write']
- factory = DataSourceFactory()
- #gWriter = factory.instance(type='CouchdbWritera',args=p)
- #gReader = factory.instance(type='CouchdbReader',args=p)
- p['qid'] = HANDLERS['processes']['config'].keys()
- gReader = factory.instance(type=class_read,args=p)
- gWriter = factory.instance(type=class_write,args=p)
- mthread = monitor.Monitor(HANDLERS,gWriter,'processes',)
- @app.route('/get/<id>')
- def procs(id):
- try:
- d = gReader.read()
-
- r = {}
- for label in d :
- index = len(d[label]) - 1
- r[label] = d[label][index]
-
- except Exception, e:
- print e
- r = []
- return json.dumps(r)
- """
- This function/endpoint will assess n-virtual environments and return the results
- @TODO: Should this be stored for future mining (I don't think so but could be wrong)
- """
- @app.route('/sandbox')
- def sandbox():
- if 'sandbox' in HANDLERS:
- handler = HANDLERS['sandbox']['class']
- conf = HANDLERS['sandbox']['config']
- r = []
- for id in conf:
- handler.init(conf[id])
- r.append (dict(handler.composite(),**{"label":id}))
- else:
- print 'Oops'
- r = []
- return json.dumps(r)
- @app.route('/trends')
- def trends ():
- id = request.args.get('id')
- app = request.args.get('app').strip()
- p = CONFIG['store']['args']
- class_read = CONFIG['store']['class']['read']
- p['qid'] =[id] #HANDLERS['processes']['config'].keys()
- gReader = factory.instance(type=class_read,args=p)
-
- r = gReader.read()
- if id in r:
- r = r[id] #--matrix
- series = []
- for row in r:
-
- series += [item for item in row if str(item['label'])== app]
- if len(series) > 12 :
- beg = len(series) - 13
- series = series[beg:]
- return json.dumps(series)
- else:
- return "[]"
- @app.route('/download',methods=['POST'])
- def requirements():
- stream = request.form['missing']
-
- stream = "\n".join(json.loads(stream))
- headers = {"content-disposition":"attachment; filename=requirements.txt"}
- return Response(stream,mimetype='text/plain',headers=headers)
- @app.route('/dashboard')
- def dashboard():
- context = PARAMS['context']
- return render_template('dashboard.html',context=context)
- """
- This function is designed to trigger learning for anomaly detection
- @TODO: forward this to a socket i.e non-blocking socket
- """
- @app.route('/learn')
- def learn():
- app = request.args.get('app')
- id = request.args.get('id')
- p = CONFIG['store']['args']
- class_read = CONFIG['store']['class']['read']
- p['qid'] =[id] #HANDLERS['processes']['config'].keys()
- gReader = factory.instance(type=class_read,args=p)
-
- r = gReader.read()
- r = r[id]
- r = ML.Filter('label',app,r)
- label = ML.Extract(['status'],r)
- r = ML.Extract(['cpu_usage','memory_usage'],r)
-
- if __name__== '__main__':
-
- mthread.start()
- app.secret_key = 'A0Zr98j/3yX R~XHH!jmN]LWX=?RT'
- app.run(host='0.0.0.0',debug=True,threaded=True)
-
|