-
Notifications
You must be signed in to change notification settings - Fork 2
/
slurm-cluster-status.py
executable file
·67 lines (56 loc) · 1.92 KB
/
slurm-cluster-status.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python3
import argparse
import subprocess
import time
STATE_MAP = {
"BOOT_FAIL": "failed",
"CANCELLED": "failed",
"COMPLETED": "success",
"CONFIGURING": "running",
"COMPLETING": "running",
"DEADLINE": "failed",
"FAILED": "failed",
"NODE_FAIL": "failed",
"OUT_OF_MEMORY": "failed",
"PENDING": "running",
"PREEMPTED": "failed",
"RUNNING": "running",
"RESIZING": "running",
"SUSPENDED": "running",
"TIMEOUT": "failed",
"UNKNOWN": "running"
}
def fetch_status(batch_id):
"""fetch the status for the batch id"""
sacct_args = ["sacct", "-j", batch_id, "-o", "State", "--parsable2",
"--noheader"]
try:
output = subprocess.check_output(sacct_args).decode("utf-8").strip()
except Exception:
# If sacct fails for whatever reason, assume its temporary and return 'running'
output = 'UNKNOWN'
# Sometimes, sacct returns nothing, in which case we assume it is temporary
# and return 'running'
if not output:
output = 'UNKNOWN'
# The first output is the state of the overall job
# See
# https://stackoverflow.com/questions/52447602/slurm-sacct-shows-batch-and-extern-job-names
# for details
job_status = output.split("\n")[0]
# If the job was cancelled manually, it will say by who, e.g "CANCELLED by 12345"
# We only care that it was cancelled
if job_status.startswith("CANCELLED by"):
job_status = "CANCELLED"
# Otherwise, return the status
try:
return STATE_MAP[job_status]
except KeyError:
raise NotImplementedError(f"Encountered unknown status '{job_status}' "
f"when parsing output:\n'{output}'")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("batch_id", type=str)
args = parser.parse_args()
status = fetch_status(args.batch_id)
print(status)