|
#!/bin/env python |
|
import itertools |
|
import subprocess |
|
import getpass |
|
import time |
|
import smtplib |
|
|
|
class SlurmJobs(): |
|
def __init__(self): |
|
self.__get_jobs() |
|
self.__parse_jobs() |
|
self.__get_states() |
|
self.__compile_report() |
|
|
|
def __call__(self): |
|
return self |
|
|
|
def __get_jobs(self): |
|
whoami = getpass.getuser() |
|
cmd = "get_slurm_jobs" ## change to C executable |
|
try: |
|
slurm_output = subprocess.Popen([cmd], stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
|
output, err = slurm_output.communicate() |
|
self.__my_jobs_raw = [i for i in output.split("\n\n") if whoami in i] |
|
except: |
|
print "Compile C program before running this script" |
|
raise SystemExit |
|
|
|
def __parse_jobs(self): |
|
jobs = {} |
|
for job in self.__my_jobs_raw: |
|
job_dict = {} |
|
for line in job.split("\n"): |
|
for item in line.split(): |
|
item = item.split("=") |
|
job_dict[item[0]] = item[1] |
|
jobs[job_dict["JobId"]] = job_dict |
|
self.my_jobs = jobs |
|
|
|
def __get_states(self): |
|
self.__job_states = {} |
|
for jobid in self.my_jobs: |
|
job = self.my_jobs[jobid] |
|
state = job["JobState"] |
|
if state in self.__job_states: |
|
self.__job_states[state] += 1 |
|
else: |
|
self.__job_states[state] = 1 |
|
|
|
def __compile_report(self): |
|
ReportString = "" |
|
## header |
|
ReportString += "SLURM Report\n" |
|
ReportString += "{0}\n\n".format(time.ctime(time.time())) |
|
|
|
## Overall status |
|
ReportString += "-- Job States --\n" |
|
for state in self.__job_states: |
|
ReportString += "{0}: {1}\n".format(state.capitalize(), self.__job_states[state]) |
|
ReportString += "\n" |
|
|
|
## add the report for each job |
|
for state, jobids in itertools.groupby(self.my_jobs, lambda j: self.my_jobs[j]["JobState"]): |
|
ReportString += "--- {0} ---\n".format(state.capitalize()) |
|
for jobid in list(jobids): |
|
job = self.my_jobs[jobid] |
|
ReportString += " {0} ({1}): {2} in {3}\n".format(job["Name"], |
|
job["JobId"], |
|
job["RunTime"], |
|
job["WorkDir"]) |
|
ReportString += "\n" |
|
|
|
self.JobReport = ReportString |
|
|
|
def send_report(self, email): |
|
server = smtplib.SMTP("localhost") |
|
msg = self.JobReport |
|
from_addr = email |
|
to_addr = email |
|
|
|
server.sendmail(from_addr, to_addr, msg) |
|
server.quit() |
|
|
|
if __name__ == "__main__": |
|
jobs = SlurmJobs() |
|
jobs.send_report("[email protected]")) |