From 584cc86b567cbe0b4d3846ec1cf83b7887d3eba5 Mon Sep 17 00:00:00 2001 From: "Steve L. Nyemba" Date: Wed, 1 Feb 2017 07:15:11 -0600 Subject: [PATCH] analysis of anomalies @TODO: road test --- src/api/index.py | 18 +++++++++++------- src/api/static/js/dashboard.js | 7 +++++-- test/TestML.py | 12 +++++++----- 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/src/api/index.py b/src/api/index.py index 043ffa1..c3973c0 100644 --- a/src/api/index.py +++ b/src/api/index.py @@ -23,7 +23,7 @@ import monitor import Queue from utils.transport import * from utils.workers import ThreadManager, Factory -from utils.ml import ML,AnomalyDetection +from utils.ml import ML,AnomalyDetection,AnalyzeAnomaly import utils.params as SYS_ARGS import atexit @@ -51,7 +51,7 @@ def procs(id): try: gReader = factory.instance(type=class_read,args=p) data = gReader.read() - ahandler = AnomalyDetection() + ahandler = AnalyzeAnomaly() learn = {} for row in data['learn'] : label = row['label'] @@ -66,15 +66,19 @@ def procs(id): # Let us determine if this is a normal operation or not # We will update the status of the information ... # + for row in r[label] : - + index = r[label].index(row) if row['label'] in learn: id = row['label'] px = ahandler.predict([row],learn[id]) if px : - px = px[0] - row['anomaly'] = px[1]==1 - print row + + # row['anomaly'] = px[1]==1 + print "" + print label,' *** ',index + row = dict(row,**px) + r[label][index] =row # # @TODO: # Compile a report here that will be sent to the mailing list @@ -243,7 +247,7 @@ if __name__== '__main__': # ThreadManager.start(CONFIG) if 'port' not in SYS_ARGS.PARAMS : SYS_ARGS.PARAMS['port'] = 5000 - PORT = SYS_ARGS.PARAMS['port'] + PORT = int(SYS_ARGS.PARAMS['port']) app.run(host='0.0.0.0',port=PORT,debug=True,threaded=True) diff --git a/src/api/static/js/dashboard.js b/src/api/static/js/dashboard.js index 430dc61..59bbeb4 100644 --- a/src/api/static/js/dashboard.js +++ b/src/api/static/js/dashboard.js @@ -7,7 +7,8 @@ monitor.processes.fetch = function(){ } -monitor.processes.init = function(x){ +monitor.processes.init = function (x) { + var r = JSON.parse(x.responseText) monitor.processes.summary.init(r) var keys = jx.utils.keys(r) @@ -79,6 +80,7 @@ monitor.processes.render = function(label,data) { var id = jx.dom.get.value('latest_processes_label') var app = item.label monitor.processes.trend.init(id, app) + if (item.anomaly == true) { jx.dom.show('has_anomaly') } else { @@ -155,7 +157,7 @@ monitor.processes.trend.render = function (logs, key,label) { // var _y = {} var cpu = {yAxisID:'0', label: 'CPU Usage (%)', data: [] ,backgroundColor:'transparent',borderColor:COLORS[187],fill:false,borderWidth:1} var mem = {yAxisID:'0',label : 'Memory Usage(%)',data:[],backgroundColor:'transparent',borderColor:COLORS[32],fill:false,borderWidth:1} - var proc= {yAxisID:'1',label : 'Proc Count',data:[],backgroundColor:'transparent',borderColor:COLORS[42],fill:false,borderWidth:1} + var proc= {yAxisID:'1',label : 'Proc Count',data:[],backgroundColor:'transparent',borderColor:COLORS[542],fill:false,borderWidth:1} jx.utils.patterns.visitor(logs,function(item){ x = new Date(item.year,item.month-1,item.day,item.hour,item.minute) y = item[key] @@ -280,6 +282,7 @@ monitor.sandbox.init = function () { jx.dom.hide('inspect_sandbox') var httpclient = HttpClient.instance() httpclient.get('/sandbox', function (x) { + var r = JSON.parse(x.responseText) if (r.length > 0){ monitor.sandbox.render(r); diff --git a/test/TestML.py b/test/TestML.py index 00047f9..8395b41 100644 --- a/test/TestML.py +++ b/test/TestML.py @@ -1,5 +1,5 @@ from utils import transport -from utils.ml import ML, AnomalyDetection +from utils.ml import ML, AnomalyDetection, AnalyzeAnomaly import unittest import json import os @@ -57,7 +57,7 @@ class TestML(unittest.TestCase): features = CONFIG['learner']['anomalies']['features'] label = CONFIG['learner']['anomalies']['label'] x = lhandler.learn(data,'label',app,features,label) - print x + def test_Predict(self): ref = CONFIG['store']['class']['read'] @@ -68,14 +68,16 @@ class TestML(unittest.TestCase): info = data['learn'] app = CONFIG['monitor']['processes']['config']['apps'][0] - print [app] - lhandler = AnomalyDetection() + + lhandler = AnalyzeAnomaly() features = CONFIG['learner']['anomalies']['features'] label = CONFIG['learner']['anomalies']['label'] #x = lhandler.learn(data,'label',app,features,label) data = data['apps'] xo = ML.Filter('label',app,data) - print app,xo + info = ML.Filter('label',app,info) + + lhandler.predict(xo,info[0]) if __name__ == '__main__' : unittest.main()