| 
						
						
							
								
							
						
						
					 | 
					 | 
					@ -8,6 +8,28 @@ from utils.transport import *
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					from utils.ml import AnomalyDetection,ML
 | 
					 | 
					 | 
					 | 
					from utils.ml import AnomalyDetection,ML
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					from utils.params import PARAMS
 | 
					 | 
					 | 
					 | 
					from utils.params import PARAMS
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					import time
 | 
					 | 
					 | 
					 | 
					import time
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					class BaseLearner(Thread):
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
						def __init__(self,lock) :
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
							Thread.__init__(self)
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
							path = PARAMS['path']
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
							self.name = self.__class__.__name__.lower()
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
							if os.path.exists(path) :
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
								f = open(path)
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
								self.config = json.loads(f.read())
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
								f.close()
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
							else:
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
								self.config = None
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
							self.lock = lock
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
							self.factory 	= DataSourceFactory()
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
							self.quit	= False
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
						"""
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
							This function is designed to stop processing gracefully
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
							
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
						"""
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
						def stop(self):
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
							self.quit = True
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
							
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
							
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					"""
 | 
					 | 
					 | 
					 | 
					"""
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
						This class is intended to apply anomaly detection to various areas of learning
 | 
					 | 
					 | 
					 | 
						This class is intended to apply anomaly detection to various areas of learning
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
						The areas of learning that will be skipped are :
 | 
					 | 
					 | 
					 | 
						The areas of learning that will be skipped are :
 | 
				
			
			
		
	
	
		
		
			
				
					| 
						
						
						
							
								
							
						
					 | 
					 | 
					@ -16,16 +38,10 @@ import time
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
						@TODO:
 | 
					 | 
					 | 
					 | 
						@TODO:
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
							- Find a way to perform dimensionality reduction if need be
 | 
					 | 
					 | 
					 | 
							- Find a way to perform dimensionality reduction if need be
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					"""
 | 
					 | 
					 | 
					 | 
					"""
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					class Anomalies(Thread) :
 | 
					 | 
					 | 
					 | 
					class Anomalies(BaseLearner) :
 | 
				
			
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
						def __init__(self,lock):		
 | 
					 | 
					 | 
					 | 
						def __init__(self,lock):		
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
							Thread.__init__(self)
 | 
					 | 
					 | 
					 | 
							BaseLearner.__init__(self,lock)
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
							path = PARAMS['path']
 | 
					 | 
					 | 
					 | 
							if self.config :
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
							self.name = self.__class__.__name__.lower()
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
							if os.path.exists(path) :
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
								f = open(path)
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
								self.config = json.loads(f.read())
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
								f.close()
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
							
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
								#
 | 
					 | 
					 | 
					 | 
								#
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
								# Initializing data store & factory class
 | 
					 | 
					 | 
					 | 
								# Initializing data store & factory class
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
								#
 | 
					 | 
					 | 
					 | 
								#
 | 
				
			
			
		
	
	
		
		
			
				
					| 
						
						
						
							
								
							
						
					 | 
					 | 
					@ -34,9 +50,9 @@ class Anomalies(Thread) :
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
								self.rclass	= self.config['store']['class']['read']
 | 
					 | 
					 | 
					 | 
								self.rclass	= self.config['store']['class']['read']
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
								self.wclass	= self.config['store']['class']['write']		
 | 
					 | 
					 | 
					 | 
								self.wclass	= self.config['store']['class']['write']		
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
								self.rw_args	= self.config['store']['args']
 | 
					 | 
					 | 
					 | 
								self.rw_args	= self.config['store']['args']
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
								self.factory 	= DataSourceFactory()
 | 
					 | 
					 | 
					 | 
								# self.factory 	= DataSourceFactory()
 | 
				
			
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
								self.quit	= False
 | 
					 | 
					 | 
					 | 
								self.quit	= False
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
								self.lock 	= lock
 | 
					 | 
					 | 
					 | 
								# self.lock 	= lock
 | 
				
			
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
						def format(self,stream):
 | 
					 | 
					 | 
					 | 
						def format(self,stream):
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
							pass
 | 
					 | 
					 | 
					 | 
							pass
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
						def stop(self):
 | 
					 | 
					 | 
					 | 
						def stop(self):
 | 
				
			
			
		
	
	
		
		
			
				
					| 
						
						
						
							
								
							
						
					 | 
					 | 
					@ -46,39 +62,57 @@ class Anomalies(Thread) :
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
							DELAY = self.config['delay'] * 60
 | 
					 | 
					 | 
					 | 
							DELAY = self.config['delay'] * 60
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
							reader 	= self.factory.instance(type=self.rclass,args=self.rw_args)
 | 
					 | 
					 | 
					 | 
							reader 	= self.factory.instance(type=self.rclass,args=self.rw_args)
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
							data	= reader.read()
 | 
					 | 
					 | 
					 | 
							data	= reader.read()
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
							key	= 'apps'
 | 
					 | 
					 | 
					 | 
							key	= 'apps@'+self.id
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
							rdata	= data[key]
 | 
					 | 
					 | 
					 | 
							if key in data:
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
							features = ['memory_usage','cpu_usage']
 | 
					 | 
					 | 
					 | 
								rdata	= data[key]
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
							yo = {"1":["running"],"name":"status"}				
 | 
					 | 
					 | 
					 | 
								features = ['memory_usage','cpu_usage']
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
							while self.quit == False :
 | 
					 | 
					 | 
					 | 
								yo = {"1":["running"],"name":"status"}				
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
								print ' *** ',self.name, ' ' , str(datetime.today())
 | 
					 | 
					 | 
					 | 
								while self.quit == False :
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
								for app in self.apps:
 | 
					 | 
					 | 
					 | 
									print ' *** ',self.name, ' ' , str(datetime.today())
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
									print '\t',app,str(datetime.today()),' ** ',app
 | 
					 | 
					 | 
					 | 
									for app in self.apps:
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
									logs = ML.Filter('label',app,rdata)
 | 
					 | 
					 | 
					 | 
										print '\t',app,str(datetime.today()),' ** ',app
 | 
				
			
			
				
				
			
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
										logs = ML.Filter('label',app,rdata)
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
										
 | 
					 | 
					 | 
					 | 
										
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
									if logs :
 | 
					 | 
					 | 
					 | 
										if logs :
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
										handler = AnomalyDetection()
 | 
					 | 
					 | 
					 | 
											handler = AnomalyDetection()
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
										value 	= handler.learn(logs,'label',app,features,yo)
 | 
					 | 
					 | 
					 | 
											value 	= handler.learn(logs,'label',app,features,yo)
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
										if value is not None:
 | 
					 | 
					 | 
					 | 
											if value is not None:
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
											value = dict(value,**{"features":features})
 | 
					 | 
					 | 
					 | 
												value = dict(value,**{"features":features})
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
											value = dict({"id":self.id},**value)
 | 
					 | 
					 | 
					 | 
												value = dict({"id":self.id},**value)
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
											#r[id][app] = value
 | 
					 | 
					 | 
					 | 
												#r[id][app] = value
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
											self.lock.acquire()
 | 
					 | 
					 | 
					 | 
												self.lock.acquire()
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
											writer = self.factory.instance(type=self.wclass,args=self.rw_args)
 | 
					 | 
					 | 
					 | 
												writer = self.factory.instance(type=self.wclass,args=self.rw_args)
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
											writer.write(label='learn',row=value)
 | 
					 | 
					 | 
					 | 
												writer.write(label='learn',row=value)
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
											self.lock.release()
 | 
					 | 
					 | 
					 | 
												self.lock.release()
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
								#
 | 
					 | 
					 | 
					 | 
									#
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
								if 'MONITOR_CONFIG_PATH' in os.environ :
 | 
					 | 
					 | 
					 | 
									if 'MONITOR_CONFIG_PATH' in os.environ :
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
									break
 | 
					 | 
					 | 
					 | 
										break
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
								time.sleep(DELAY)
 | 
					 | 
					 | 
					 | 
									time.sleep(DELAY)
 | 
				
			
			
				
				
			
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
							print ' *** Exiting ',self.name.replace('a','A')
 | 
					 | 
					 | 
					 | 
							print ' *** Exiting ',self.name.replace('a','A')
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
									
 | 
					 | 
					 | 
					 | 
									
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
							
 | 
					 | 
					 | 
					 | 
							
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					"""
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
						Let's estimate how many files we will have for a given date
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
						y = ax + b with y: number files, x: date, y: Number of files
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					"""		
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					class Regression(BaseLearner):
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
						def __init__(self,lock):
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
							BaseLearner.__init__(self)
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
							self.folders 	= self.config['folders']
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
							self.id 	= self.config['id']
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
						def run(self):
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
							DELAY = self.config['delay'] * 60
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
							reader 	= self.factory.instance(type=self.rclass,args=self.rw_args)
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
							data	= reader.read()
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
							if 'folders' in data :
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
								data = ML.Filter('id',self.id,data['folders'])
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
								xo  	= ML.Extract(['date'],data)
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
								yo	= ML.Extract(['count'],data)
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
								numpy.linalg.lstsq(xo, yo, rcond=-1)
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
							
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					class Regression(Thread):
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
						def __init__(self,params):
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
							pass
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					if __name__ == '__main__' :
 | 
					 | 
					 | 
					 | 
					if __name__ == '__main__' :
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
						lock = RLock()
 | 
					 | 
					 | 
					 | 
						lock = RLock()
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
						thread = Anomalies(lock)
 | 
					 | 
					 | 
					 | 
						thread = Anomalies(lock)
 | 
				
			
			
		
	
	
		
		
			
				
					| 
						
						
						
					 | 
					 | 
					
 
 |