From 854df8a99dd9e3ebc11efcf75d344380b4c2db49 Mon Sep 17 00:00:00 2001 From: steve Date: Thu, 2 Feb 2017 14:57:40 -0600 Subject: [PATCH] File monitor --- src/monitor.py | 47 ++++++++++++++++++++++++++++++--------- test/TestServerMonitor.py | 3 ++- 2 files changed, 38 insertions(+), 12 deletions(-) diff --git a/src/monitor.py b/src/monitor.py index 9f0c837..fa45dfa 100755 --- a/src/monitor.py +++ b/src/monitor.py @@ -214,9 +214,13 @@ class DetailProcess(Analysis): ma += [ dict(now, **self.format(row)) for row in matrix] return ma - +""" + This class evaluates a list of folders and provides detailed informaiton about age/size of each file + Additionally the the details are summarized in terms of global size, and oldest file. +""" class FileWatch(Analysis): - def __init__(self,conf): + def __init__(self,folders): + self.folders = folders pass def split(self,row): @@ -228,16 +232,29 @@ class FileWatch(Analysis): print x size = float(x[0].replace('K','').replace('KB','')) / 1000 elif 'M' in x[0] : - size = x[0].replace('MB','') + size = float(x[0].replace('MB','').replace('M','')) elif 'G' in x[0] : - size = x[0].replace('GB','') * 1000 + size = float(x[0].replace('GB','').replace('G','')) * 1000 elif 'T' in x[0] : - pass + size = float(x[0].replace('TB','').replace('T','')) * 1000000 + else : + # + # Size provided in bytes we are converting into MB + size = float(x[0]) / 1000000 month = months.index(x[1]) + 1 - day = x[2] - print [' ** ',x[4]] - #hour,minute = x[3].split(':') - year = x[4] + day = int(x[2]) + age = -1 + hour=minute = 0 + if ':' in x[3] : + hour,minute = x[3].split(':') + if re.match('^\d+$',x[4]): + year = int(x[4]) + else: + year = datetime.datetime.today().year + file_date = datetime.datetime(year,month,day,int(hour),int(minute)) + size = round(size,2) + #file_date = datetime.datetime(year,month,day,hour,minute) + age = (file_date - datetime.datetime.now()).days return {"size":size,"age":age} return None @@ -247,9 +264,17 @@ class FileWatch(Analysis): handler = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE) ostream = handler.communicate()[0].split('\n') - print [self.split(stream) for stream in ostream if stream.strip() != ''] - pass + return [self.split(stream) for stream in ostream if stream.strip() != ''] + def composite(self): + d = [] #-- matrix of details (age,size) + s = {} #-- summary of the + for folder in self.folders: + if os.path.exists(folder): + d += self.evaluate(folder) + xo = np.array(ML.Extract(['size','age'],d)) + s[folder] = [np.sum(xo[:,0]),np.max(xo[:,1])] + return {"summary":s,"details":d} class Monitor (Thread): def __init__(self,pConfig,pWriter,id='processes') : Thread.__init__(self) diff --git a/test/TestServerMonitor.py b/test/TestServerMonitor.py index 5183988..cb42264 100644 --- a/test/TestServerMonitor.py +++ b/test/TestServerMonitor.py @@ -58,8 +58,9 @@ class TestMonitorServer(unittest.TestCase): p.start() def test_JMX(self): conf =CONFIG['monitor']['folder'] + path =os.environ['FILE_PATH'] fw = FileWatch(conf) - print fw.evaluate("/Users/steve/tmp") + print fw.evaluate(path) if __name__ == '__main__' : unittest.main()