forked from kcl-nmssys/slurm-influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·310 lines (266 loc) · 13 KB
/
main.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#!/usr/bin/python3
#
# Get various Slurm metrics['partition'] and feed them into an InfluxDB time-series database
# Xand Meaden, King's College London
import datetime
import grp
import influxdb
import json
import ldap
import pyslurm
import pwd
import re
import socket
import struct
import sys
import time
import yaml
def tres_to_dict(tres_csv):
resources = {}
for resource in tres_csv.split(','):
[k, v] = resource.split('=')
resources[k] = v
return resources
try:
with open('config.yaml') as fh:
config = yaml.load(fh, Loader=yaml.SafeLoader)
except:
sys.stderr.write('Failed to load configuration\n')
sys.exit(1)
try:
client = influxdb.InfluxDBClient(host=config['influxdb_host'], port=config['influxdb_port'], username=config['influxdb_username'], password=config['influxdb_password'], ssl=config['influxdb_ssl'], verify_ssl=config['influxdb_verify_ssl'])
except:
sys.stderr.write('Failed to connect to InfluxDB\n')
sys.exit(2)
try:
pyslurmnode = pyslurm.node()
except:
sys.stderr.write('Failed to get Slurm data\n')
sys.exit(3)
if config['user_lookup']:
try:
ldap_c = ldap.initialize('ldaps://%s:636' % config['ldap_hostname'])
ldap_c.simple_bind_s(config['ldap_username'], config['ldap_password'])
except:
sys.stderr.write('Failed to bind to LDAP\n')
sys.exit(4)
groups = config['groups']
partitions = pyslurm.partition().get()
node_partitions = {}
metrics = {}
metrics['partition'] = {}
metrics['partition']['cpu_total'] = {}
metrics['partition']['cpu_usage'] = {}
metrics['partition']['cpu_usage_pc'] = {}
metrics['partition']['gpu_total'] = {}
metrics['partition']['gpu_usage'] = {}
metrics['partition']['gpu_usage_pc'] = {}
metrics['partition']['mem_total'] = {}
metrics['partition']['mem_usage'] = {}
metrics['partition']['mem_usage_pc'] = {}
metrics['partition']['jobs_running'] = {}
metrics['partition']['jobs_pending'] = {}
metrics['partition']['queue_time'] = {}
metrics['partition']['queue_jobs'] = {}
metrics['user'] = {}
metrics['user']['cpu_usage'] = {}
metrics['user']['gpu_usage'] = {}
metrics['user']['mem_usage'] = {}
metrics['user']['jobs_running'] = {}
metrics['user']['jobs_pending'] = {}
metrics['user']['queue_time'] = {}
metrics['user']['queue_jobs'] = {}
metrics['group'] = {}
metrics['group']['cpu_usage'] = {}
metrics['group']['gpu_usage'] = {}
metrics['group']['mem_usage'] = {}
metrics['group']['jobs_running'] = {}
metrics['group']['jobs_pending'] = {}
metrics['group']['queue_time'] = {}
metrics['group']['queue_jobs'] = {}
if config['user_lookup']:
metrics['ldap_attrib'] = {}
metrics['ldap_attrib']['cpu_usage'] = {}
metrics['ldap_attrib']['gpu_usage'] = {}
metrics['ldap_attrib']['mem_usage'] = {}
metrics['ldap_attrib']['jobs_running'] = {}
metrics['ldap_attrib']['jobs_pending'] = {}
metrics['ldap_attrib']['queue_time'] = {}
metrics['ldap_attrib']['queue_jobs'] = {}
user_ids = {}
user_groups = {}
user_ldap = {}
now = datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ')
# Setup data structures, with stats set to 0
for part in list(partitions.keys()) + ['ALL']:
if part != 'ALL':
hl = pyslurm.hostlist()
hl.create(partitions[part]['nodes'])
for node in hl.get_list():
node = node.decode() # Python3-ism or pyslurm bug?
if node not in node_partitions:
node_partitions[node] = []
node_partitions[node].append(part)
metrics['partition']['cpu_total'][part] = 0
metrics['partition']['cpu_usage'][part] = 0
metrics['partition']['cpu_usage_pc'][part] = 0
metrics['partition']['gpu_total'][part] = 0
metrics['partition']['gpu_usage'][part] = 0
metrics['partition']['gpu_usage_pc'][part] = 0
metrics['partition']['mem_total'][part] = 0
metrics['partition']['mem_usage'][part] = 0
metrics['partition']['mem_usage_pc'][part] = 0
metrics['partition']['jobs_running'][part] = 0
metrics['partition']['jobs_pending'][part] = 0
metrics['partition']['queue_time'][part] = 0
metrics['partition']['queue_jobs'][part] = 0
for group in groups:
metrics['group']['cpu_usage'][group] = 0
metrics['group']['gpu_usage'][group] = 0
metrics['group']['mem_usage'][group] = 0
metrics['group']['jobs_running'][group] = 0
metrics['group']['jobs_pending'][group] = 0
metrics['group']['queue_time'][group] = 0
metrics['group']['queue_jobs'][group] = 0
members = grp.getgrnam(group)[3]
for user in members:
if user not in user_groups:
user_groups[user] = []
user_groups[user].append(group)
# Go through all the nodes and get their cpu/gpu/memory usage and store for each partition they belong to
nodes = pyslurmnode.get()
for node in nodes:
node_data = nodes.get(node)
metrics['partition']['cpu_total']['ALL'] += node_data['cpus']
metrics['partition']['cpu_usage']['ALL'] += node_data['alloc_cpus']
metrics['partition']['cpu_usage_pc']['ALL'] = 100 * (float(metrics['partition']['cpu_usage']['ALL']) / float(metrics['partition']['cpu_total']['ALL']))
metrics['partition']['mem_total']['ALL'] += node_data['real_memory'] * 1048576
metrics['partition']['mem_usage']['ALL'] += node_data['alloc_mem'] * 1048576
metrics['partition']['mem_usage_pc']['ALL'] = 100 * (float(metrics['partition']['mem_usage']['ALL']) / float(metrics['partition']['mem_total']['ALL']))
gpu_total = 0
gpu_usage = 0
if node_data['gres']:
gres_total = pyslurm.node().parse_gres(node_data['gres'][0])
gres_usage = pyslurm.node().parse_gres(node_data['gres_used'][0])
for g in gres_total:
is_gpu = re.match(r'^gpu:([0-9]+)\(?', g)
if is_gpu:
gpu_total = int(is_gpu.group(1))
if gpu_total > 0:
for g in gres_usage:
is_gpu = re.match(r'^gpu:(?:[^:]*:?)([0-9]+)\(?', g)
if is_gpu:
gpu_usage = int(is_gpu.group(1))
metrics['partition']['gpu_total']['ALL'] += gpu_total
metrics['partition']['gpu_usage']['ALL'] += gpu_usage
if metrics['partition']['gpu_total']['ALL'] > 0:
metrics['partition']['gpu_usage_pc']['ALL'] = 100 * (float(metrics['partition']['gpu_usage']['ALL']) / metrics['partition']['gpu_total']['ALL'])
if node in node_partitions:
for part in node_partitions[node]:
metrics['partition']['cpu_total'][part] += node_data['cpus']
metrics['partition']['cpu_usage'][part] += node_data['alloc_cpus']
metrics['partition']['cpu_usage_pc'][part] = 100 * (float(metrics['partition']['cpu_usage'][part]) / metrics['partition']['cpu_total'][part])
metrics['partition']['mem_total'][part] += node_data['real_memory'] * 1048576
metrics['partition']['mem_usage'][part] += node_data['alloc_mem'] * 1048576
metrics['partition']['mem_usage_pc'][part] = 100 * (float(metrics['partition']['mem_usage'][part]) / metrics['partition']['mem_total'][part])
metrics['partition']['gpu_total'][part] += gpu_total
metrics['partition']['gpu_usage'][part] += gpu_usage
if metrics['partition']['gpu_total'][part] > 0:
metrics['partition']['gpu_usage_pc'][part] = 100 * (float(metrics['partition']['gpu_usage'][part]) / metrics['partition']['gpu_total'][part])
# Now go through the jobs list to see user-specific stuff
jobs = pyslurm.job().get()
for job in jobs:
job = jobs.get(job)
if job['user_id'] not in user_ids:
user = pwd.getpwuid(job['user_id'])[0]
user_ids[job['user_id']] = user
metrics['user']['cpu_usage'][user] = 0
metrics['user']['gpu_usage'][user] = 0
metrics['user']['mem_usage'][user] = 0
metrics['user']['jobs_running'][user] = 0
metrics['user']['jobs_pending'][user] = 0
metrics['user']['queue_time'][user] = 0
metrics['user']['queue_jobs'][user] = 0
else:
user = user_ids[job['user_id']]
if config['user_lookup']:
if user not in user_ldap:
result_id = ldap_c.search(config['ldap_userbase'], ldap.SCOPE_SUBTREE, '(%s=%s)' % (config['ldap_username_attrib'], user), [config['ldap_grouping_attrib']])
result_type, result_data = ldap_c.result(result_id, 0)
if result_data == []:
user_ldap[user] = 'unknown'
else:
user_ldap[user] = result_data[0][1][config['ldap_grouping_attrib']][0]
if user_ldap[user] not in metrics['ldap_attrib']['jobs_running']:
metrics['ldap_attrib']['jobs_running'][user_ldap[user]] = 0
metrics['ldap_attrib']['jobs_pending'][user_ldap[user]] = 0
metrics['ldap_attrib']['cpu_usage'][user_ldap[user]] = 0
metrics['ldap_attrib']['gpu_usage'][user_ldap[user]] = 0
metrics['ldap_attrib']['mem_usage'][user_ldap[user]] = 0
metrics['ldap_attrib']['queue_jobs'][user_ldap[user]] = 0
metrics['ldap_attrib']['queue_time'][user_ldap[user]] = 0
if job['job_state'] == 'RUNNING':
metrics['partition']['jobs_running']['ALL'] += 1
metrics['partition']['jobs_running'][job['partition']] += 1
tres_alloc = tres_to_dict(job['tres_alloc_str'])
cpu = int(tres_alloc['cpu'])
mem = 0
if 'mem' in tres_alloc:
m = re.match('^[0-9]+[MGT]$', tres_alloc['mem'])
if m:
mem = float(m.group(1))
if tres_alloc.group(2) == 'G':
mem *= 1024
elif tres_alloc.group(2) == 'T':
mem *= 1048576
mem *= 1048576
mem = int(mem)
gpu = 0
if 'tres_per_node' in job and job['tres_per_node']:
tres_per_node = re.match(r'gpu:([0-9]+)', job['tres_per_node'])
if tres_per_node:
gpu = int(tres_per_node.group(1)) * job['num_nodes']
metrics['user']['jobs_running'][user] += 1
metrics['user']['cpu_usage'][user] += cpu
metrics['user']['gpu_usage'][user] += gpu
metrics['user']['mem_usage'][user] += mem
queue_time = job['start_time'] - job['submit_time']
metrics['user']['queue_jobs'][user] += 1
metrics['user']['queue_time'][user] = (float(metrics['user']['queue_time'][user] + queue_time)) / metrics['user']['queue_jobs'][user]
metrics['partition']['queue_jobs']['ALL'] += 1
metrics['partition']['queue_time']['ALL'] = (float(metrics['partition']['queue_time']['ALL'] + queue_time)) / metrics['partition']['queue_jobs']['ALL']
metrics['partition']['queue_jobs'][job['partition']] += 1
metrics['partition']['queue_time'][job['partition']] = (float(metrics['partition']['queue_time'][job['partition']] + queue_time)) / metrics['partition']['queue_jobs'][job['partition']]
if user in user_groups:
for group in user_groups[user]:
metrics['group']['jobs_running'][group] += 1
metrics['group']['cpu_usage'][group] += cpu
metrics['group']['gpu_usage'][group] += gpu
metrics['group']['mem_usage'][group] += mem
metrics['group']['queue_jobs'][group] += 1
metrics['group']['queue_time'][group] = (float(metrics['group']['queue_time'][group] + queue_time)) / metrics['group']['queue_jobs'][group]
if config['user_lookup']:
metrics['ldap_attrib']['jobs_running'][user_ldap[user]] += 1
metrics['ldap_attrib']['cpu_usage'][user_ldap[user]] += cpu
metrics['ldap_attrib']['gpu_usage'][user_ldap[user]] += gpu
metrics['ldap_attrib']['mem_usage'][user_ldap[user]] += mem
metrics['ldap_attrib']['queue_jobs'][user_ldap[user]] += 1
metrics['ldap_attrib']['queue_time'][user_ldap[user]] = (float(metrics['ldap_attrib']['queue_time'][user_ldap[user]] + queue_time)) / metrics['ldap_attrib']['queue_jobs'][user_ldap[user]]
elif job['job_state'] == 'PENDING':
metrics['partition']['jobs_pending']['ALL'] += 1
for partition in job['partition'].split(','):
if partition in metrics['partition']['jobs_pending']:
metrics['partition']['jobs_pending'][partition] += 1
metrics['user']['jobs_pending'][user] += 1
if user in user_groups:
for group in user_groups[user]:
metrics['group']['jobs_pending'][group] += 1
if config['user_lookup']:
metrics['ldap_attrib']['jobs_pending'][user_ldap[user]] += 1
payload = []
for grouping in ['partition', 'user', 'group', 'ldap_attrib']:
for reading in ['cpu_total', 'cpu_usage', 'cpu_usage_pc', 'gpu_total', 'gpu_usage', 'gpu_usage_pc', 'mem_total', 'mem_usage', 'mem_usage_pc', 'jobs_running', 'jobs_pending', 'queue_time']:
if reading in metrics[grouping] and len(metrics[grouping][reading]) > 0:
for key in metrics[grouping][reading].keys():
payload.append({'measurement': '%s_%s' % (grouping, reading), 'time': now, 'fields': {reading: float(metrics[grouping][reading][key])}, 'tags': {grouping: key}})
client.write_points(payload, database=config['influxdb_database'])