-
Notifications
You must be signed in to change notification settings - Fork 1
/
snapshot_slurm_accounting.py
executable file
·277 lines (220 loc) · 10.3 KB
/
snapshot_slurm_accounting.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/usr/bin/env python3
#===============================================================================
#
# snapshot_slurm_accounting.py - Copies the given month/year's Slurm accounting data
# into a separate file.
#
# ARGS:
# 1st: BillingConfig.xlsx file
#
# SWITCHES:
# --billing_root: Location of BillingRoot directory (overrides BillingConfig.xlsx)
# [default if no BillingRoot in BillingConfig.xlsx or switch given: current working dir]
# --year: Year of snapshot requested. [Default is this year]
# --month: Month of snapshot requested. [Default is last month]
#
# OUTPUT:
# An Slurm accounting file with only entries with end_dates within the given
# month. This file, named SlurmAccounting.<YEAR>-<MONTH>.txt, will be placed in
# <BillingRoot>/<YEAR>/<MONTH>/ if BillingRoot is given or in the current
# working directory if not.
#
# ASSUMPTIONS:
# Dependent on xlrd module.
#
# AUTHOR:
# Keith Bettinger
#
#==============================================================================
#=====
#
# IMPORTS
#
#=====
import datetime
import argparse
import os.path
import subprocess
import tempfile
# Simulate an "include billing_common.py".
SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
exec(compile(open(os.path.join(SCRIPT_DIR, "billing_common.py"), "rb").read(), os.path.join(SCRIPT_DIR, "billing_common.py"), 'exec'))
#=====
#
# CONSTANTS
#
#=====
# From billing_common.py
global SLURMACCOUNTING_PREFIX
global SLURMACCOUNTING_DELIMITER
global SUBDIR_RAWDATA
SLURM_ACCT_COMMAND_NAME = ["sacct"]
SLURM_ACCT_STATE_SWITCHES = ["--state=CA,CD,DL,F,NF,PR,TO,OOM,RQ"]
# We will collect both all the fields from Slurm records
# and just the ones we need for reports within our system.
SLURM_ACCT_FIELDS_ALL_SWITCHES = ["--format=ALL"]
SLURM_ACCT_FIELDS_MIN_SWITCHES = ["--format=User,JobName,Account,WCKey,NodeList,NCPUS,ElapsedRaw,JobID,JobIDRaw,MaxVMSize,Submit,Start,End"]
SLURM_ACCT_OTHER_SWITCHES = ["--allusers","--parsable2","--allocations","--duplicates",
"--delimiter=%s" % SLURMACCOUNTING_DELIMITER]
#=====
#
# FUNCTIONS
#
#=====
# From billing_common.py
global argparse_get_parent_parser
global argparse_get_year_month
global argparse_get_billingroot_billingconfig
global get_subdirectory
def get_slurm_accounting(slurm_output_pathname, slurm_field_switches, filter_returns=False):
slurm_accounting_file = open(slurm_output_pathname, "w")
###
#
# Steps to get Slurm accounting for only this month:
# 1. Get Slurm accounting for <MN>/01/<YR> through <MN+1>/01/<YR>.
# - This data includes jobs running during the month but ending after, so...
# 2. Get Slurm account for <MN+1>/01/<YR> through now (no End date needed).
# 3. Subtract the lines from 2. from the lines in 1.
#
###
temp_filename_prefix = "%s.%d-%02d" % (SLURMACCOUNTING_PREFIX, year, month)
#
# "1. Get Slurm accounting for <MN>/01/<YR> through <MN+1>/01/<YR>."
#
# COMMAND LINE: sacct SACCT_SWITCHES | fgrep -v PENDING | tr -d \r
#
(slurm_this_month_temp_file, slurm_this_month_temp_filename) = \
tempfile.mkstemp(".txt", "%s-thisMonth." % temp_filename_prefix)
print("Getting Slurm accounting for %d-%02d to %s" % (year, month, slurm_this_month_temp_filename))
# Create the start and end dates switches to the command.
slurm_command_starttime_switch = ["--starttime", "%02d/01/%02d" % (month, year - 2000)]
# If we are in the month we are querying for, use the current date
if month == todays_month and year == todays_year:
slurm_command_endtime_switch = ["--endtime", datetime.datetime.today().strftime("%m/%d/%y-%H:%M:%S")]
else:
slurm_command_endtime_switch = ["--endtime", "%02d/01/%02d" % (next_month, next_month_year - 2000)]
slurm_command_list = SLURM_ACCT_COMMAND_NAME + SLURM_ACCT_STATE_SWITCHES + \
slurm_field_switches + \
SLURM_ACCT_OTHER_SWITCHES + \
slurm_command_starttime_switch + slurm_command_endtime_switch
if args.verbose:
print(slurm_command_list)
sacct_process = subprocess.Popen(slurm_command_list, stdout=subprocess.PIPE)
fgrep_command_list = ['fgrep', '-v', '%sPENDING%s' % (SLURMACCOUNTING_DELIMITER, SLURMACCOUNTING_DELIMITER)]
if filter_returns:
fgrep_process = subprocess.Popen(fgrep_command_list,
stdin=sacct_process.stdout,
stdout=subprocess.PIPE)
# Removing return characters embedded within lines
tr_command_list = ['tr', '-d', "\r"]
tr_process = subprocess.Popen(tr_command_list,
stdin=fgrep_process.stdout,
stdout=slurm_this_month_temp_file)
(tr_process_stdout, tr_process_stderr) = tr_process.communicate()
else:
fgrep_process = subprocess.Popen(fgrep_command_list,
stdin=sacct_process.stdout,
stdout=slurm_this_month_temp_file)
(fgrep_process_stdout, fgrep_process_stderr) = fgrep_process.communicate()
(sacct_process_stdout, sacct_process_stderr) = sacct_process.communicate()
#
# "2. Get Slurm account for <MN+1>/01/<YR> through now (no End date needed)."
#
# COMMAND LINE: sacct SACCT_SWITCHES | fgrep -v PENDING | tr -d \r
#
(slurm_next_month_temp_file, slurm_next_month_temp_filename) = tempfile.mkstemp(".txt",
"%s-nextMonth." % temp_filename_prefix)
print("Getting Slurm accounting for %d-%02d to %s" % (next_month_year, next_month, slurm_next_month_temp_filename))
# Create the start date switch to the command.
slurm_command_starttime_switch = ["--starttime", "%02d/01/%02d" % (next_month, next_month_year - 2000)]
slurm_command_list = SLURM_ACCT_COMMAND_NAME + SLURM_ACCT_STATE_SWITCHES + \
slurm_field_switches + \
SLURM_ACCT_OTHER_SWITCHES + \
slurm_command_starttime_switch + ["--noheader"]
if args.verbose:
print(slurm_command_list)
sacct_process = subprocess.Popen(slurm_command_list, stdout=subprocess.PIPE)
fgrep_command_list = ['fgrep', '-v', '%sPENDING%s' % (SLURMACCOUNTING_DELIMITER, SLURMACCOUNTING_DELIMITER)]
if filter_returns:
fgrep_process = subprocess.Popen(fgrep_command_list,
stdin=sacct_process.stdout,
stdout=subprocess.PIPE)
# Removing return characters embedded within lines
tr_command_list = ['tr', '-d', "\r"]
tr_process = subprocess.Popen(tr_command_list,
stdin=fgrep_process.stdout,
stdout=slurm_next_month_temp_file)
(tr_process_stdout, tr_process_stderr) = tr_process.communicate()
else:
fgrep_process = subprocess.Popen(fgrep_command_list,
stdin=sacct_process.stdout,
stdout=slurm_next_month_temp_file)
(fgrep_process_stdout, fgrep_process_stderr) = fgrep_process.communicate()
(sacct_process_stdout, sacct_process_stderr) = sacct_process.communicate()
#
# 3. Subtract the lines from 2. from the lines in 1.
#
print("Subtracting the two files")
ret_val = subprocess.call(["awk", "{if (f==1) { r[$0] } else if (! ($0 in r)) { print $0 } }",
"f=1", slurm_next_month_temp_filename, "f=2", slurm_this_month_temp_filename],
stdout=slurm_accounting_file)
#=====
#
# SCRIPT BODY
#
#=====
parser = argparse.ArgumentParser(parents=[argparse_get_parent_parser()])
parser.add_argument("-a", "--all_only", action="store_true",
default=False,
help='Only output the complete accounting file [default = output both all and min]')
parser.add_argument("-n", "--min_only", action="store_true",
default=False,
help='Only output the minimum accounting file [default = output both all and min]')
args = parser.parse_args()
#
# Sanity-check arguments.
#
# Get year/month-related arguments
(year, month, begin_month_timestamp, end_month_timestamp) = argparse_get_year_month(args)
# What month is it today?
today = datetime.date.today()
todays_month = today.month
todays_year = today.year
# Calculate next month for range of this month.
if month != 12:
next_month = month + 1
next_month_year = year
else:
next_month = 1
next_month_year = year + 1
# Get BillingRoot and BillingConfig arguments
(billing_root, billing_config_file) = argparse_get_billingroot_billingconfig(args, year, month)
# Build path to the output subdirectory within BillingRoot for the storage data output
output_subdir = get_subdirectory(billing_root, year, month, SUBDIR_RAWDATA, create_if_nec=True)
#
# Print summary of arguments.
#
print("TAKING SLURM ACCOUNTING FILE SNAPSHOT OF %02d/%d:" % (month, year))
print(" BillingConfigFile: %s" % billing_config_file)
print(" BillingRoot: %s" % billing_root)
# Create output accounting pathnames.
slurm_accounting_filename_all = "%s.%d-%02d.all.txt" % (SLURMACCOUNTING_PREFIX, year, month)
slurm_accounting_pathname_all = os.path.join(output_subdir, slurm_accounting_filename_all)
slurm_accounting_filename_min = "%s.%d-%02d.txt" % (SLURMACCOUNTING_PREFIX, year, month)
slurm_accounting_pathname_min = os.path.join(output_subdir, slurm_accounting_filename_min)
print()
if not args.min_only: print(" SlurmAccountingFile (all): %s" % slurm_accounting_pathname_all)
if not args.all_only: print(" SlurmAccountingFile (min): %s" % slurm_accounting_pathname_min)
print()
if not args.min_only:
print("GETTING SLURM ACCOUNTING - ALL FIELDS")
get_slurm_accounting(slurm_accounting_pathname_all, SLURM_ACCT_FIELDS_ALL_SWITCHES, filter_returns=False)
else:
print("**SKIPPING** SLURM ACCOUNTING - ALL FIELDS")
print()
if not args.all_only:
print("GETTING SLURM ACCOUNTING - MINIMUM FIELDS")
get_slurm_accounting(slurm_accounting_pathname_min, SLURM_ACCT_FIELDS_MIN_SWITCHES, filter_returns=True)
else:
print("**SKIPPING** SLURM ACCOUNTING - MINIMUM FIELDS")
print()