-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslurmdashboard.py
364 lines (305 loc) · 12.6 KB
/
slurmdashboard.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
from flask import Flask, render_template, jsonify
import subprocess
import json
from datetime import datetime, timedelta
from collections import Counter
import logging
import sys
app = Flask(__name__)
# Set up logging
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s %(levelname)s: %(message)s',
handlers=[
logging.StreamHandler(sys.stdout)
]
)
def run_command(command):
try:
app.logger.debug(f"Executing command: {command}")
result = subprocess.run(command, shell=True, capture_output=True, text=True)
if result.stderr:
app.logger.error(f"Command stderr: {result.stderr}")
app.logger.debug(f"Command output: {result.stdout}")
return result.stdout
except Exception as e:
app.logger.error(f"Error executing command: {str(e)}")
return str(e)
def get_current_jobs():
# Get current jobs using squeue with a nice format
squeue_cmd = "squeue --format='%i|%u|%P|%j|%T|%M|%l|%D|%C|%N' --noheader"
output = run_command(squeue_cmd)
jobs = []
job_stats = {'total': 0, 'running': 0, 'pending': 0}
for line in output.strip().split('\n'):
if line:
job_id, user, partition, name, state, time, time_limit, nodes, cpus, nodelist = line.split('|')
jobs.append({
'job_id': job_id,
'user': user,
'partition': partition,
'name': name,
'state': state,
'time': time,
'time_limit': time_limit,
'nodes': nodes,
'cpus': cpus,
'nodelist': nodelist
})
job_stats['total'] += 1
if state == 'RUNNING':
job_stats['running'] += 1
elif state == 'PENDING':
job_stats['pending'] += 1
return {'jobs': jobs, 'stats': job_stats}
def get_top_users():
# Get top users using sreport with CPU metrics
end_date = datetime.now()
start_date = end_date - timedelta(days=30)
# Format dates as Month/Day-Hour:Minute
start_str = start_date.strftime('%m/%d-%H:%M')
end_str = end_date.strftime('%m/%d-%H:%M')
# Get CPU usage
sreport_cmd = f"sreport user top start={start_str} end={end_str} TopCount=50 -t hourper --tres=cpu -n"
app.logger.debug(f"Executing command: {sreport_cmd}")
output = run_command(sreport_cmd)
app.logger.debug(f"Raw sreport output:\n{output}")
users = []
for line in output.strip().split('\n'):
if not line or line.startswith('-') or 'Cluster' in line or 'Login' in line:
continue
try:
# Split by whitespace and get fields
# Expected format: cluster login proper_name account tres_name usage
fields = [f for f in line.split() if f] # Remove empty strings
if len(fields) < 6: # Need at least all required fields
app.logger.debug(f"Skipping line with insufficient fields: {line}")
continue
# Find the account field by looking for the TRES field (cpu) and taking the field before it
try:
tres_index = fields.index('cpu')
account = fields[tres_index - 1]
login = fields[1]
usage = fields[-1] # Last field contains the usage data
except ValueError:
app.logger.debug(f"Could not find 'cpu' field in line: {line}")
continue
app.logger.debug(f"Processing line for user {login}:")
app.logger.debug(f" Raw usage field: {usage}")
app.logger.debug(f" Account: {account}")
# Extract usage percentage and hours from the format "1051(16.23%)"
try:
if '(' in usage:
hours_str, percent_str = usage.split('(')
hours = float(hours_str)
usage_percent = float(percent_str.rstrip('%)'))
app.logger.debug(f" Parsed hours: {hours}, percent: {usage_percent}")
else:
hours = float(usage)
usage_percent = 0.0
app.logger.debug(f" Parsed hours (no percent): {hours}")
except (ValueError, IndexError) as e:
app.logger.error(f" Error parsing usage values: {str(e)}")
hours = 0.0
usage_percent = 0.0
user_data = {
'user': login.rstrip('+'), # Remove + from truncated names
'account': account,
'cpu_percent': usage_percent,
'cpu_hours': hours
}
app.logger.debug(f" Adding user data: {user_data}")
users.append(user_data)
except Exception as e:
app.logger.error(f"Error processing line: {str(e)}")
continue # Skip malformed lines
# Sort users by CPU percentage
users.sort(key=lambda x: x['cpu_percent'], reverse=True)
app.logger.debug("Final sorted users data:")
for user in users[:5]:
app.logger.debug(f" {user}")
return {
'users': users[:5], # Top 5 users
'period': {
'start': start_date.strftime('%Y-%m-%d'),
'end': end_date.strftime('%Y-%m-%d')
}
}
def get_cluster_usage():
# Get cluster usage for the last month using sreport
end_date = datetime.now()
start_date = end_date - timedelta(days=30) # Default to last 30 days
# Format dates with hours, minutes, seconds
start_str = start_date.strftime('%Y-%m-%dT%H:%M:%S')
end_str = end_date.strftime('%Y-%m-%dT%H:%M:%S')
# Get cluster utilization
sreport_cmd = f"sreport cluster utilization start={start_str} end={end_str} -t Hours -n"
output = run_command(sreport_cmd)
try:
# Parse the text output
lines = output.strip().split('\n')
if not lines:
return {"error": "No data available", "data": []}
# Get the line with the numbers (should be the last non-empty line)
data_line = None
for line in lines:
if line.strip() and not line.startswith('-') and 'Cluster' not in line:
data_line = line
if not data_line:
return {"error": "No data available", "data": []}
# Split the line into fields and convert to numbers
parts = [p for p in data_line.split() if p]
if len(parts) >= 7:
cluster = parts[0]
allocated = float(parts[1])
down = float(parts[2])
plnd_down = float(parts[3])
idle = float(parts[4])
reserved = float(parts[5])
reported = float(parts[6])
# Calculate utilization
if reported > 0:
utilization = (allocated / reported) * 100
else:
utilization = 0
# Create a single data point
data_point = {
'timestamp': end_date.strftime('%Y-%m-%dT%H:%M:%S'),
'utilization': round(utilization, 2),
'allocated_hours': allocated,
'total_hours': reported,
'idle_hours': idle,
'down_hours': down
}
return {
'data': [data_point],
'period': {
'start': start_str,
'end': end_str
}
}
except Exception as e:
app.logger.error(f"Error parsing cluster usage: {str(e)}")
return {"error": "Could not parse sreport output", "data": []}
def get_node_status():
# Get node status using sinfo with a detailed format
sinfo_cmd = "sinfo --format='%n|%P|%t|%C|%m|%e|%d' --noheader"
output = run_command(sinfo_cmd)
nodes = {}
node_stats = {'total': 0, 'allocated': 0, 'idle': 0, 'down': 0, 'reserved': 0}
for line in output.strip().split('\n'):
if line:
nodename, partition, state, cpus, memory, free_mem, disk = line.split('|')
# Parse CPU info (format: alloc/idle/other/total)
cpu_parts = cpus.split('/')
# If node already exists, just add the partition
if nodename in nodes:
if partition not in nodes[nodename]['partitions']:
nodes[nodename]['partitions'].append(partition)
continue
# Create new node entry
nodes[nodename] = {
'name': nodename,
'partitions': [partition],
'state': state,
'cpus': {
'allocated': cpu_parts[0],
'idle': cpu_parts[1],
'other': cpu_parts[2],
'total': cpu_parts[3]
},
'memory': memory,
'free_memory': free_mem,
'disk': disk
}
# Update statistics only once per node
node_stats['total'] += 1
if 'alloc' in state.lower():
node_stats['allocated'] += 1
elif 'idle' in state.lower():
node_stats['idle'] += 1
elif 'down' in state.lower():
node_stats['down'] += 1
elif 'resv' in state.lower():
node_stats['reserved'] += 1
return {'nodes': list(nodes.values()), 'stats': node_stats}
def get_hourly_usage():
# Get CPU usage for the last 24 hours in 30-minute intervals
end_date = datetime.now()
start_date = end_date - timedelta(hours=24)
data_points = []
current = end_date
# Collect data for each 30-minute interval
while current >= start_date:
interval_end = current
interval_start = current - timedelta(minutes=30)
# Format dates
start_str = interval_start.strftime('%Y-%m-%dT%H:%M:%S')
end_str = interval_end.strftime('%Y-%m-%dT%H:%M:%S')
# Get cluster utilization for this interval
sreport_cmd = f"sreport cluster utilization start={start_str} end={end_str} -t Hours -p -n"
output = run_command(sreport_cmd)
try:
# Parse pipe-separated values
for line in output.strip().split('\n'):
if not line or line.startswith('-') or 'Cluster' in line:
continue
parts = line.strip().split('|')
if len(parts) >= 7:
allocated = float(parts[1])
down = float(parts[2])
plnd_down = float(parts[3])
idle = float(parts[4])
reserved = float(parts[5])
reported = float(parts[6])
# Calculate utilization
if reported > 0:
utilization = (allocated / reported) * 100
else:
utilization = 0
# Create data point
data_point = {
'timestamp': interval_end.strftime('%Y-%m-%dT%H:%M:%S'),
'utilization': round(utilization, 2),
'allocated': allocated,
'total': reported,
'idle': idle,
'down': down
}
data_points.append(data_point)
break # Take only the first valid line for this interval
except Exception as e:
app.logger.error(f"Error parsing interval {start_str} to {end_str}: {str(e)}")
current = interval_start
# Sort data points by timestamp
data_points.sort(key=lambda x: x['timestamp'])
return {
'data': data_points,
'period': {
'start': start_date.strftime('%Y-%m-%dT%H:%M:%S'),
'end': end_date.strftime('%Y-%m-%dT%H:%M:%S')
}
}
@app.route('/')
def index():
return render_template('index.html')
@app.route('/nodes')
def nodes_page():
return render_template('nodes.html')
@app.route('/api/jobs')
def jobs():
return jsonify(get_current_jobs())
@app.route('/api/usage')
def usage():
return jsonify(get_cluster_usage())
@app.route('/api/top_users')
def top_users():
return jsonify(get_top_users())
@app.route('/api/nodes')
def nodes():
return jsonify(get_node_status())
@app.route('/api/hourly_usage')
def hourly_usage():
return jsonify(get_hourly_usage())
if __name__ == '__main__':
app.run(host='pc059.cern.ch', port=5000)