forked from bgietzel/htcondor-sysview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuserjobs.py
executable file
·269 lines (234 loc) · 7.37 KB
/
userjobs.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
#!/usr/bin/env python
import sys, os, time, string
import memcache
verbose = False
mc = None
PANDA_URL='http://bigpanda.cern.ch/job?pandaid='
CONDOR_URL='http://www.mwt2.org/sys/view/job_info/'
def mc_init():
global mc
if not mc:
mc = memcache.Client(['mc.mwt2.org:11211'])
def mc_get(key):
mc_init()
try:
r = mc.get(key)
except:
r = None
if verbose:
print "GET", key, r
return r
def HMS(x):
x = int(x)
s, x = x%60, int(x/60)
m, x = x%60, int(x/60)
return "%02d:%02d:%02d" % (x,m,s)
def strip_chars(x):
valid_chars = "-_.%s%s" % (string.ascii_letters, string.digits)
if x is None:
x = "None"
return ''.join(c for c in x if c in valid_chars)
def write_header(ofile=sys.stdout):
ofile.write("""<html><head><title>User/Job summary for MWT2</title></head>
<script src='sorttable.js'></script>
<style type="text/css">
th, td {
padding: 3px !important;
}
table.sortable thead {
background-color:#eee;
color:#666666;
font-weight: bold;
cursor: default;
}
</style>
<body>
<h3>User/Job summary for MWT2</h3>
<h3>Generated on %s</h3>
The table is sortable.""" % time.ctime())
def mask_eq(k1, k2, mask):
return ((mask&1 or k1[0]==k2[0])
and (mask&2 or k1[1]==k2[1])
and (mask&4 or k1[2]==k2[2]))
def merge_keys(keys, mask=0):
groups = []
keys = keys[:] # copy
while keys:
k1, keys = keys[0], keys[1:]
group = [k1]
for k2 in keys[:]:
if mask_eq(k1, k2, mask):
group.append(k2)
keys.remove(k2)
groups.append(group)
return groups
def write_job_table(jobs, group, ofile=sys.stdout, merge_mask=0):
fields = ["User", "Type", "Site", "Panda ID", "Condor ID", "Walltime", "CPU time", "%Efficiency"]
merge_user = merge_mask & 1
merge_type = merge_mask & 2
merge_site = merge_mask & 4
ofile.write("<table class='sortable'><thead><tr>")
for i, field in enumerate(fields):
text = field
if i < 3:
merge_field = merge_mask & (1<<i)
link = merge_mask^(1<<i) # or ''
ofile.write("<th align='center'>%s</th>" % text)
ofile.write("</tr></thead>")
ofile.write("<tbody>")
rows=[]
for keys in group:
user, jobtype, site = keys
for job in jobs[keys]:
jobid = job[0]
try:
walltime = job[2][0]
except TypeError, e:
walltime=0
try:
cputime = job[2][1]
except TypeError, e:
cputime=0
if walltime:
effcy = 100.0 * cputime / walltime
else:
effcy = 0
try:
panda_id=panda_condor_map[jobid]
except KeyError, e:
panda_id=None
rows.append((-walltime, """<tr><td>%s</td>
<td>%s</td>
<td align='center'>%s</td>
<td align='right'><a href='%s%s'>%s</a></td>
<td align='right'><a href='%s%s.html'>%s</a></td>
<td sorttable_customkey='%s' align='right'>%s</td>
<td sorttable_customkey='%s' align='right'>%s</td>
<td align='right'>%.2f</td>
</tr>\n""" % (user, jobtype, site, PANDA_URL, jobid, jobid, CONDOR_URL, panda_id, panda_id, walltime, HMS(walltime), cputime, HMS(cputime), effcy)))
rows.sort()
for row in rows:
ofile.write(row[1])
ofile.write("</table>")
def write_table(jobs, ofile=sys.stdout, merge_mask=0):
fields = ["User", "Type", "Site", "#Jobs", "Walltime", "CPU time", "%Efficiency"]
merge_user = merge_mask & 1
merge_type = merge_mask & 2
merge_site = merge_mask & 4
ofile.write("<table class='sortable'><thead><tr>")
for i, field in enumerate(fields):
text = field
if i < 3:
merge_field = merge_mask & (1<<i)
link = merge_mask^(1<<i) # or ''
if merge_field:
text += '<a href=userjobs%s.html>(show)</a>' % link
else:
text += '<a href=userjobs%s.html>(hide)</a>' % link
ofile.write("<th align='center'>%s</th>" % text)
ofile.write("</tr></thead>")
ofile.write("<tbody>")
keys = jobs.keys()
keys.sort()
groups = merge_keys(keys, merge_mask)
rows = []
for group in groups:
data = []
for key in group:
user, jobtype, site = key
#if user is None:
# continue
data += jobs[key]
if merge_user:
user = ""
if merge_type:
jobtype = ""
if merge_site:
site = ""
njobs = len(data)
#job_index_filename= '_'.join([ strip_chars(k) for k in key]) + ".html"
job_index_filename=strip_chars("%s_%s_%s.html" % ( user, jobtype,site))
ji_file = open("/var/local/sysview/sys/." + job_index_filename, 'w')
write_header(ji_file)
#write_job_table(data, key, ji_file, merge_mask)
write_job_table(jobs, group, ji_file, merge_mask)
write_footer(ji_file)
ji_file.close()
walltime = sum([x[2][0] for x in data if x[2] is not None])
cputime = sum([x[2][1] for x in data if x[2] is not None])
if walltime:
effcy = 100.0 * cputime / walltime
else:
effcy = 0
rows.append((-walltime, """<tr><td>%s</td>
<td>%s</td>
<td align='center'>%s</td>
<td align='right'><a href='.%s'>%d</a></td>
<td sorttable_customkey='%s' align='right'>%s</td>
<td sorttable_customkey='%s' align='right'>%s</td>
<td align='right'>%.2f</td>
</tr>\n""" % (user, jobtype, site, job_index_filename, njobs, walltime, HMS(walltime), cputime, HMS(cputime), effcy)))
rows.sort()
for row in rows:
ofile.write(row[1])
ofile.write("</table>")
def write_footer(ofile=sys.stdout):
ofile.write("</body></html>\n")
panda_condor_map = mc_get('MWT2.panda_condor_map')
if not panda_condor_map:
sys.exit(1)
condor_ids = panda_condor_map.values()
condor_ids.sort()
jobs = {} # key is (user, type, site)
def getsite(host):
if not host:
return '??'
#site = host[:2] # host.split('-')[0]
if '@taub' in host:
return 'uiuc (taub)'
if '@golub' in host:
return 'uiuc (golub)'
hostw = host.split('.')
try:
site = hostw[1]
except IndexError:
raise IndexError, 'cannot identify site for host %s' % host
if site == 'mwt2':
if 'uc3' in hostw[0]:
return 'uc3'
else:
return 'uc'
if site == 'dmz':
return 'illinoisLX'
return site
for condor_id in condor_ids:
host = mc_get('%s.host' % condor_id)
# Defaults
user = 'Unknown'
times = (0, 0)
site = getsite(host)
panda_id = mc_get('%s.panda_id' % condor_id)
info = mc_get('%s.info' % panda_id)
if info:
user = info[0]
jobtype = info[1]
else:
user = jobtype = None
times = mc_get('%s.times' % condor_id)
#if user is None:
# user = 'unknown'
#if times is None or times[0] is None or times[1] is None:
# continue
## print condor_id, panda_id, user, times
key = (user, jobtype, site)
if not jobs.has_key(key):
jobs[key] = []
jobs[key].append((panda_id, host, times))
for merge_mask in xrange(8):
fname = 'userjobs%d.html' % merge_mask
ofile = open("/var/local/sysview/sys/." + fname, 'w') # open as hidden file
write_header(ofile)
write_table(jobs, ofile, merge_mask)
write_footer(ofile)
ofile.close()
os.rename("/var/local/sysview/sys/."+fname, "/var/local/sysview/sys/"+fname)