From 344a8c9c71dd1d2133c9632b88fcb54956cf8f07 Mon Sep 17 00:00:00 2001 From: Edileuton Henrique de Oliveira <eho09@c3sl.ufpr.br> Date: Fri, 21 Feb 2014 15:05:48 -0300 Subject: [PATCH] Add elapsedtime function that verifies if have passed 24 hours since the last sucessful execution Signed-off-by: Edileuton Henrique de Oliveira <eho09@c3sl.ufpr.br> --- windows-collect/src/datasidAgent.py | 83 +++++++++++++++++++---------- 1 file changed, 54 insertions(+), 29 deletions(-) diff --git a/windows-collect/src/datasidAgent.py b/windows-collect/src/datasidAgent.py index 128087f..959c2bf 100644 --- a/windows-collect/src/datasidAgent.py +++ b/windows-collect/src/datasidAgent.py @@ -27,7 +27,7 @@ import subprocess import urllib import collect import glob -from time import localtime, strftime +from time import localtime, strftime, time from xml.etree.ElementTree import tostring # ========================================== @@ -36,7 +36,8 @@ from xml.etree.ElementTree import tostring # TODO: Check compatibility with 64bit Windows (Program Files(x86) folder) DATASIDPATH = os.environ["ProgramFiles"] + "\\DataSID" PREVIOUSPATH = DATASIDPATH + "\\data\\previous" -URL = "http://bisimmcdev.c3sl.ufpr.br/axis2/services/DataSID" +URL = "http://simmc.c3sl.ufpr.br/axis2/services/DataSID" +LASTEXEC = DATASIDPATH + "\\data\\lastexec.txt" # Log lifetime in days LOGLIFETIME = 30 @@ -98,11 +99,11 @@ def callClient(args): # Get proxy information from Windows registry def getProxyInfo(): - phost = collect.getRegistryValue("HKEY_LOCAL_MACHINE", "SOFTWARE\\DataSID", "ProxyHost")[:-1] - pport = collect.getRegistryValue("HKEY_LOCAL_MACHINE", "SOFTWARE\\DataSID", "ProxyPort")[:-1] - puser = collect.getRegistryValue("HKEY_LOCAL_MACHINE", "SOFTWARE\\DataSID", "ProxyUser")[:-1] - ppass = collect.getRegistryValue("HKEY_LOCAL_MACHINE", "SOFTWARE\\DataSID", "ProxyPass")[:-1] - return (phost, pport, puser, ppass) + phost = collect.getRegistryValue("HKEY_LOCAL_MACHINE", "SOFTWARE\\DataSID", "ProxyHost")[:-1] + pport = collect.getRegistryValue("HKEY_LOCAL_MACHINE", "SOFTWARE\\DataSID", "ProxyPort")[:-1] + puser = collect.getRegistryValue("HKEY_LOCAL_MACHINE", "SOFTWARE\\DataSID", "ProxyUser")[:-1] + ppass = collect.getRegistryValue("HKEY_LOCAL_MACHINE", "SOFTWARE\\DataSID", "ProxyPass")[:-1] + return (phost, pport, puser, ppass) # Check for updates def verifyUpdate(): @@ -119,7 +120,7 @@ def verifyUpdate(): (returnCode, output) = callClient(args) if returnCode == 0: return 0 - + if returnCode == 4: updatelink = output[0].split("\n")[0] return updatelink @@ -184,12 +185,25 @@ def callUpdater(): # Wait to be killed out = proc.communicate() +# Check if have passed 24 hours since the last sucessful execution +def elapsedTime(): + if not os.path.exists(LASTEXEC): + return True + + lastexec = file(LASTEXEC , "r").read() + + # 86400 seconds = 24 hours + if time() - float(lastexec) > 86400: + return True + else: + return False + + # ========================================== # Main program # ========================================== # Open the logfile log = Log() - # Delete old log files try: deleteOldLogs() @@ -204,7 +218,6 @@ except: log.close() sys.exit(1) - # Write version conf files try: writeVersionConf(version) @@ -258,19 +271,26 @@ if(updatelink): else: log.write("UPDATE: No updates are available.") +# Check if have passed 24 hours since the last sucessful execution +if not elapsedTime(): + # If everything ran ok, exit with success status + log.write("EXIT(0): Success, not elapsed time.") + log.close() + sys.exit(0) + # Collect data try: collectData = collect.collect() except Exception, e: - log.write("ERROR(8): Failed to collect data: " + str(e)) + log.write("ERROR(8): Failed to collect data. " + str(e)) log.close() sys.exit(8) # Write the collect-data.xml file try: writeCollectData(collectData, "collect-data.xml") -except: - log.write("ERROR(9): Failed to write collect-data.xml.") +except Exception, e: + log.write("ERROR(9): Failed to write collect-data.xml. " + str(e)) log.close() sys.exit(9) @@ -282,8 +302,8 @@ except clientError, e: log.write("Client message: " + str(e.errorMsg)) log.close() sys.exit(10) -except: - log.write("ERROR(11): Failed to get the proxy information from Windows registry.") +except Exception, e: + log.write("ERROR(11): Failed to get the proxy information from Windows registry. " + str(e)) log.close() sys.exit(11) @@ -295,28 +315,33 @@ if os.path.isdir(PREVIOUSPATH): log.write("ERROR(12): Failed to collect net usage: " + str(e)) log.close() sys.exit(12) - # Write the collect-data.xml file + # Write the collect-data.xml file try: writeCollectData(netCollectData, "net-collect-data.xml") - except: - log.write("ERROR(13): Failed to write net-collect-data.xml.") + except Exception, e: + log.write("ERROR(13): Failed to write net-collect-data.xml. " + str(e)) log.close() sys.exit(13) - + try: sendData(" --send-net-usage ", "net-collect-data.xml") - shutil.rmtree(PREVIOUSPATH) + shutil.rmtree(PREVIOUSPATH) except clientError, e: - log.write("ERROR(10): Failed to send net usage data. Client returned " + str(e.code) + ".") - log.write("Client message: " + str(e.errorMsg)) - log.close() - sys.exit(10) - except: - log.write("ERROR(11): Failed to get the proxy information from Windows registry.") - log.close() - sys.exit(11) + log.write("ERROR(10): Failed to send net usage data. Client returned " + str(e.code) + ".") + log.write("Client message: " + str(e.errorMsg)) + log.close() + sys.exit(10) + except Exception, e: + log.write("ERROR(11): Failed to get the proxy information from Windows registry. " + str(e)) + log.close() + sys.exit(11) + +# Record the last sucess execution +lastExecFile = file(LASTEXEC , "w") +lastExecFile.write(str(time())) +lastExecFile.close() # If everything ran ok, exit with success status log.write("EXIT(0): Success.") log.close() -sys.exit(0) +sys.exit(0) \ No newline at end of file -- GitLab