def main(): Script.registerSwitch("f:", "File=", "Get output for jobs with IDs from the file") Script.registerSwitch("g:", "JobGroup=", "Get output for jobs in the given group") # Registering arguments will automatically add their description to the help menu Script.registerArgument(["JobID: DIRAC Job ID"], mandatory=False) sws, args = Script.parseCommandLine(ignoreErrors=True) import DIRAC from DIRAC.Interfaces.API.Dirac import Dirac, parseArguments from DIRAC.Core.Utilities.Time import toString, date, day dirac = Dirac() jobs = [] for sw, value in sws: if sw.lower() in ("f", "file"): if os.path.exists(value): jFile = open(value) jobs += jFile.read().split() jFile.close() elif sw.lower() in ("g", "jobgroup"): group = value jobDate = toString(date() - 30 * day) result = dirac.selectJobs(jobGroup=value, date=jobDate) if not result["OK"]: if "No jobs selected" not in result["Message"]: print("Error:", result["Message"]) DIRAC.exit(-1) else: jobs += result["Value"] for arg in parseArguments(args): jobs.append(arg) if not jobs: print("Warning: no jobs selected") Script.showHelp() DIRAC.exit(0) result = dirac.deleteJob(jobs) if result["OK"]: print("Deleted jobs %s" % ",".join([str(j) for j in result["Value"]])) exitCode = 0 else: print(result["Message"]) exitCode = 2 DIRAC.exit(exitCode)
def main(): Script.registerSwitch("f:", "File=", "Get status for jobs with IDs from the file") Script.registerSwitch("g:", "JobGroup=", "Get status for jobs in the given group") # Registering arguments will automatically add their description to the help menu Script.registerArgument(["JobID: DIRAC Job ID"], mandatory=False) sws, args = Script.parseCommandLine(ignoreErrors=True) from DIRAC import exit as DIRACExit from DIRAC.Core.Utilities.Time import toString, date, day from DIRAC.Interfaces.API.Dirac import Dirac, parseArguments dirac = Dirac() exitCode = 0 jobs = [] for key, value in sws: if key.lower() in ("f", "file"): if os.path.exists(value): jFile = open(value) jobs += jFile.read().split() jFile.close() elif key.lower() in ("g", "jobgroup"): jobDate = toString(date() - 30 * day) # Choose jobs no more than 30 days old result = dirac.selectJobs(jobGroup=value, date=jobDate) if not result["OK"]: print("Error:", result["Message"]) DIRACExit(-1) jobs += result["Value"] if len(args) < 1 and not jobs: Script.showHelp(exitCode=1) if len(args) > 0: jobs += parseArguments(args) result = dirac.getJobStatus(jobs) if result["OK"]: for job in result["Value"]: print("JobID=" + str(job), end=" ") for status in result["Value"][job].items(): print("%s=%s;" % status, end=" ") print() else: exitCode = 2 print("ERROR: %s" % result["Message"]) DIRACExit(exitCode)
def main(): Script.registerSwitch("f:", "File=", "Get status for jobs with IDs from the file") Script.registerSwitch("g:", "JobGroup=", "Get status for jobs in the given group") Script.parseCommandLine(ignoreErrors=True) args = Script.getPositionalArgs() from DIRAC import exit as DIRACExit from DIRAC.Core.Utilities.Time import toString, date, day from DIRAC.Interfaces.API.Dirac import Dirac, parseArguments dirac = Dirac() exitCode = 0 jobs = [] for key, value in Script.getUnprocessedSwitches(): if key.lower() in ('f', 'file'): if os.path.exists(value): jFile = open(value) jobs += jFile.read().split() jFile.close() elif key.lower() in ('g', 'jobgroup'): jobDate = toString(date() - 30 * day) # Choose jobs no more than 30 days old result = dirac.selectJobs(jobGroup=value, date=jobDate) if not result['OK']: print("Error:", result['Message']) DIRACExit(-1) jobs += result['Value'] if len(args) < 1 and not jobs: Script.showHelp(exitCode=1) if len(args) > 0: jobs += parseArguments(args) result = dirac.getJobStatus(jobs) if result['OK']: for job in result['Value']: print('JobID=' + str(job), end=' ') for status in result['Value'][job].items(): print('%s=%s;' % status, end=' ') print() else: exitCode = 2 print("ERROR: %s" % result['Message']) DIRACExit(exitCode)
def main(): Script.registerSwitch("f:", "File=", "Get output for jobs with IDs from the file") Script.registerSwitch("g:", "JobGroup=", "Get output for jobs in the given group") Script.parseCommandLine(ignoreErrors=True) args = Script.getPositionalArgs() import DIRAC from DIRAC.Interfaces.API.Dirac import Dirac, parseArguments from DIRAC.Core.Utilities.Time import toString, date, day dirac = Dirac() jobs = [] for sw, value in Script.getUnprocessedSwitches(): if sw.lower() in ('f', 'file'): if os.path.exists(value): jFile = open(value) jobs += jFile.read().split() jFile.close() elif sw.lower() in ('g', 'jobgroup'): group = value jobDate = toString(date() - 30 * day) result = dirac.selectJobs(jobGroup=value, date=jobDate) if not result['OK']: if "No jobs selected" not in result['Message']: print("Error:", result['Message']) DIRAC.exit(-1) else: jobs += result['Value'] for arg in parseArguments(args): jobs.append(arg) if not jobs: print("Warning: no jobs selected") Script.showHelp() DIRAC.exit(0) result = dirac.deleteJob(jobs) if result['OK']: print('Deleted jobs %s' % ','.join([str(j) for j in result['Value']])) exitCode = 0 else: print(result['Message']) exitCode = 2 DIRAC.exit(exitCode)
errorList = [] outputDir = None group = None jobs = [] for sw, value in Script.getUnprocessedSwitches(): if sw in ( 'D', 'Dir' ): outputDir = value elif sw.lower() in ( 'f', 'file' ): if os.path.exists( value ): jFile = open( value ) jobs += jFile.read().split() jFile.close() elif sw.lower() in ( 'g', 'jobgroup' ): group = value jobDate = toString( date() - 30 * day ) # Choose jobs in final state, no more than 30 days old result = dirac.selectJobs( jobGroup = value, date = jobDate, status = 'Done' ) if not result['OK']: if not "No jobs selected" in result['Message']: print "Error:", result['Message'] DIRAC.exit( -1 ) else: jobs += result['Value'] result = dirac.selectJobs( jobGroup = value, date = jobDate, status = 'Failed' ) if not result['OK']: if not "No jobs selected" in result['Message']: print "Error:", result['Message'] DIRAC.exit( -1 ) else:
if __name__ == "__main__": from DIRAC.Interfaces.API.Dirac import Dirac, parseArguments from DIRAC.Core.Utilities.Time import toString, date, day dirac = Dirac() jobs = [] for sw, value in Script.getUnprocessedSwitches(): if sw.lower() in ('f', 'file'): if os.path.exists(value): jFile = open(value) jobs += jFile.read().split() jFile.close() elif sw.lower() in ('g', 'jobgroup'): group = value jobDate = toString(date() - 30 * day) result = dirac.selectJobs(jobGroup=value, date=jobDate) if not result['OK']: if "No jobs selected" not in result['Message']: print("Error:", result['Message']) DIRAC.exit(-1) else: jobs += result['Value'] for arg in parseArguments(args): jobs.append(arg) if not jobs: print("Warning: no jobs selected") Script.showHelp() DIRAC.exit(0)
def execute(self): """ The main agent execution method """ limitDate = date() - self._period tableList = ["MessageRepository", "FixedTextMessages", "Systems", "SubSystems"] columnsList = ["SystemName", "SubSystemName", "count(*) as entries", "FixedTextString"] cmd = "SELECT " + ', '.join(columnsList) + " FROM " \ + " NATURAL JOIN ".join(tableList) \ + " WHERE MessageTime > '%s'" % limitDate \ + " AND LogLevel in ('ERROR','FATAL','EXCEPT')" \ + " GROUP BY FixedTextID,SystemName,SubSystemName HAVING entries > %s" % self._threshold \ + " ORDER BY entries DESC LIMIT %i;" % self._limit result = self.systemLoggingDB._query(cmd) if not result['OK']: return result messageList = result['Value'] if messageList == 'None' or messageList == (): self.log.warn('The DB query returned an empty result') return S_OK() mailBody = '\n' for message in messageList: mailBody = mailBody + "Count: " + str(message[2]) + "\tError: '"\ + message[3] + "'\tSystem: '" + message[0]\ + "'\tSubsystem: '" + message[1] + "'\n" mailBody = mailBody + "\n\n-------------------------------------------------------\n"\ + "Please do not reply to this mail. It was automatically\n"\ + "generated by a Dirac Agent.\n" result = self.systemLoggingDB._getDataFromAgentTable(self.agentName) self.log.debug(result) if not result['OK']: errorString = "Could not get the date when the last mail was sent" self.log.error(errorString) return S_ERROR(errorString) else: if result['Value']: self.log.debug("date value: %s" % fromString(result['Value'][0][0][1:-1])) lastMailSentDate = fromString(result['Value'][0][0][1:-1]) else: lastMailSentDate = limitDate - 1 * day result = self.systemLoggingDB._insertDataIntoAgentTable(self.agentName, lastMailSentDate) if not result['OK']: errorString = "Could not insert data into the DB" self.log.error(errorString, result['Message']) return S_ERROR(errorString + ": " + result['Message']) self.log.debug("limitDate: %s\t" % limitDate + "lastMailSentDate: %s\n" % lastMailSentDate) if lastMailSentDate > limitDate: self.log.info("The previous report was sent less " + " than %s days ago" % self.__days) return S_OK() dateSent = toString(date()) self.log.info("The list with the top errors has been sent") result = self.systemLoggingDB._insertDataIntoAgentTable(self.agentName, dateSent) if not result['OK']: errorString = "Could not insert data into the DB" self.log.error(errorString, result['Message']) return S_ERROR(errorString + ": " + result['Message']) result = self.notification.sendMail(self._mailAddress, self._subject, mailBody) if not result['OK']: self.log.warn("The notification could not be sent") return S_OK() return S_OK("The list with the top errors has been sent")
def main(): Script.registerSwitch("D:", "Dir=", "Store the output in this directory") Script.registerSwitch("f:", "File=", "Get output for jobs with IDs from the file") Script.registerSwitch("g:", "JobGroup=", "Get output for jobs in the given group") # Registering arguments will automatically add their description to the help menu Script.registerArgument(["JobID: DIRAC Job ID or a name of the file with JobID per line"], mandatory=False) sws, args = Script.parseCommandLine(ignoreErrors=True) from DIRAC.Interfaces.API.Dirac import Dirac, parseArguments from DIRAC.Core.Utilities.Time import toString, date, day from DIRAC.Core.Utilities.File import mkDir dirac = Dirac() exitCode = 0 errorList = [] outputDir = None group = None jobs = [] for sw, value in sws: if sw in ("D", "Dir"): outputDir = value elif sw.lower() in ("f", "file"): if os.path.exists(value): jFile = open(value) jobs += jFile.read().split() jFile.close() elif sw.lower() in ("g", "jobgroup"): group = value jobDate = toString(date() - 30 * day) # Choose jobs in final state, no more than 30 days old result = dirac.selectJobs(jobGroup=value, date=jobDate, status="Done") if not result["OK"]: if "No jobs selected" not in result["Message"]: print("Error:", result["Message"]) DIRAC.exit(-1) else: jobs += result["Value"] result = dirac.selectJobs(jobGroup=value, date=jobDate, status="Failed") if not result["OK"]: if "No jobs selected" not in result["Message"]: print("Error:", result["Message"]) DIRAC.exit(-1) else: jobs += result["Value"] for arg in parseArguments(args): if os.path.isdir(arg): print("Output for job %s already retrieved, remove the output directory to redownload" % arg) else: jobs.append(arg) if not jobs: print("No jobs selected") DIRAC.exit(0) if group: if outputDir: outputDir = os.path.join(outputDir, group) else: outputDir = group if outputDir: mkDir(outputDir) else: outputDir = os.getcwd() jobs = [str(job) for job in jobs] doneJobs = os.listdir(outputDir) todoJobs = [job for job in jobs if job not in doneJobs] for job in todoJobs: result = dirac.getOutputSandbox(job, outputDir=outputDir) jobDir = str(job) if outputDir: jobDir = os.path.join(outputDir, job) if result["OK"]: if os.path.exists(jobDir): print("Job output sandbox retrieved in %s/" % (jobDir)) else: if os.path.exists("%s" % jobDir): shutil.rmtree(jobDir) errorList.append((job, result["Message"])) exitCode = 2 for error in errorList: print("ERROR %s: %s" % error) DIRAC.exit(exitCode)
def main(): Script.registerSwitch("D:", "Dir=", "Store the output in this directory") Script.registerSwitch("f:", "File=", "Get output for jobs with IDs from the file") Script.registerSwitch("g:", "JobGroup=", "Get output for jobs in the given group") Script.parseCommandLine(ignoreErrors=True) args = Script.getPositionalArgs() from DIRAC.Interfaces.API.Dirac import Dirac, parseArguments from DIRAC.Core.Utilities.Time import toString, date, day from DIRAC.Core.Utilities.File import mkDir dirac = Dirac() exitCode = 0 errorList = [] outputDir = None group = None jobs = [] for sw, value in Script.getUnprocessedSwitches(): if sw in ('D', 'Dir'): outputDir = value elif sw.lower() in ('f', 'file'): if os.path.exists(value): jFile = open(value) jobs += jFile.read().split() jFile.close() elif sw.lower() in ('g', 'jobgroup'): group = value jobDate = toString(date() - 30 * day) # Choose jobs in final state, no more than 30 days old result = dirac.selectJobs(jobGroup=value, date=jobDate, status='Done') if not result['OK']: if "No jobs selected" not in result['Message']: print("Error:", result['Message']) DIRAC.exit(-1) else: jobs += result['Value'] result = dirac.selectJobs(jobGroup=value, date=jobDate, status='Failed') if not result['OK']: if "No jobs selected" not in result['Message']: print("Error:", result['Message']) DIRAC.exit(-1) else: jobs += result['Value'] for arg in parseArguments(args): if os.path.isdir(arg): print("Output for job %s already retrieved, remove the output directory to redownload" % arg) else: jobs.append(arg) if not jobs: print("No jobs selected") DIRAC.exit(0) if group: if outputDir: outputDir = os.path.join(outputDir, group) else: outputDir = group if outputDir: mkDir(outputDir) else: outputDir = os.getcwd() jobs = [str(job) for job in jobs] doneJobs = os.listdir(outputDir) todoJobs = [job for job in jobs if job not in doneJobs] for job in todoJobs: result = dirac.getOutputSandbox(job, outputDir=outputDir) jobDir = str(job) if outputDir: jobDir = os.path.join(outputDir, job) if result['OK']: if os.path.exists(jobDir): print('Job output sandbox retrieved in %s/' % (jobDir)) else: if os.path.exists('%s' % jobDir): shutil.rmtree(jobDir) errorList.append((job, result['Message'])) exitCode = 2 for error in errorList: print("ERROR %s: %s" % error) DIRAC.exit(exitCode)