Skip to content

Instantly share code, notes, and snippets.

@jappa
Last active July 23, 2019 09:27
Show Gist options
  • Save jappa/4a6c7264f42fe48231ec to your computer and use it in GitHub Desktop.
Save jappa/4a6c7264f42fe48231ec to your computer and use it in GitHub Desktop.
Parse slurm accounting output
from datetime import timedelta
import sys
import os
import re
import math
from subprocess import Popen, PIPE, check_output
def get_timedelta(date_str):
# Returns timedelta object from string in [DD-[hh:]]mm:ss format
days = 0
hours = 0
minutes = 0
seconds = 0
if date_str.count('-') == 1:
days = int(date_str.split('-')[0])
date_str = date_str.partition('-')[2]
if date_str.count(':') == 2:
hours = int(date_str.split(':')[0])
date_str = date_str.partition(':')[2]
try:
minutes=int(date_str.split(':')[0])
seconds=int(date_str.split(':')[1])
except:
pass
return timedelta( days=days,
hours=hours,
minutes=minutes,
seconds=seconds
)
total_hours = 0
#with os.popen('sacct --noheader --format JobID,Jobname,Start,Elapsed,ncpus,nnodes,partition,state -A ZENOTECH-COM --state=CD,F,CA --starttime=030115 --endtime=040115') as f:
with os.popen('sacct --parsable2 --noheader --format JobID,Jobname,Start,Elapsed,ncpus,nnodes,partition,state -A ZENOTECH-COM --state=CANCELLED,COMPLETED,FAILED,NODE_FAIL,TIMEOUT --start=2015-10-01-00:00:00 --end=2015-10-31-23:59:59') as f:
try:
for line in f:
new_line = re.sub(' +',' ',line.strip())
new_line = re.sub(r'\|',' ',new_line)
job_id = new_line.split()[0]
try:
job_name = new_line.split()[1]
job_start = new_line.split()[2]
job_state = new_line.split()[7]
job_time = new_line.split()[3]
job_time = get_timedelta(job_time).total_seconds()/3600
job_time = int(math.ceil(job_time))
job_cores = new_line.split()[4]
total_hours += job_time*int(job_cores)
print(job_start + ' ' + job_name+ ' ' + str(job_time) + ' ' + job_cores + ' ' + job_state)
except:
pass
except:
print("G")
print(str(total_hours)+' '+ str(total_hours*0.08))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment