forked from itamblyn/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
status
executable file
·665 lines (553 loc) · 22.5 KB
/
status
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
#!/bin/bash
#
# status will reformat the output of the qstat command by SGE.
#
# Version 1.0 - 2004-08-23 Initial release
# This version supports SGE 5.x and 6.x
#
# Copyright (C) 2004 Reuti, email: [email protected]
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#
# If there is an unrecoverable error: display a message and exit.
#
function printExit
{
case $1 in
[iI]) echo INFO: "$2" >&2 ;;
[wW]) echo WARNING: "$2" >&2 ;;
[eE]) echo ERROR: "$2" ; exit 1 >&2 ;;
*) echo $1 >&2 ;;
esac
}
#
# System depended setups.
#
platform=`uname -s`
case $platform in
SunOS) awk_command=/usr/xpg4/bin/awk ;;
*) awk_command=awk ;;
esac
awk_path=`which $awk_command`
if [ ! -r "$awk_path" -o ! -x "$awk_path" ] ; then
printExit W "No executable awk program found."
printExit E "Please update the path to awk in \"status\" according to your installation."
fi
#
# First define some functions.
#
function usage
{
cat <<-EOF
NAME
status - Display the status of jobs
SYNTAX
status [ options ] [ <user> ]
DESCRIPTION
Displays all the running and pending of your own by default. When
a <user> is specified, only the jobs belonging to this user are
displayed.
OPTIONS
-a
Display the jobs of all users. May not be used, when there is
a dedicated user specified. This also implies the output of
the owner of the jobs.
-c
Calculate a statistic of the listed jobs.
-g
Group output by owner. Only applicable if the owner is listed,
either by the -o switch or implied by -a. This is a short form
of "-s owner,running". Can't be used together with the -s option.
-h
Get this help page.
-j
Include jobs which are suspended.
-l
Suppress the output of the list of jobs. Maybe useful, in case
you are only interested in the statistic with the -c switch.
-m
Display the machine instead of the name of the queue the job is
running in under the column "running in". Only meaningful for
SGE 6.0 or above.
-n
Suppress the listing of the individual owner, mostly useful when
all jobs are listed. Take precedence of any other implied options.
If used togehther with -o, only the later one is relevant.
-o
List the owner of the individual job. Take precedence of any
other implied option. If used together with -s, only the later
one is relevant.
-s [ job | name | none | owner | running | slots | time ]
Sort the output according to the given columns. If you want to
sort inside 2 or more columns, you have to separate them by a
",", i.e. "-s owner,running".
The option "none" will supersede any other column names and
set the complete sort order to none.
The column "owner" can only be used, if the owner of the
job is displayed at all. Otherwise it will be ignored.
The sort by "running" will only be used for the running
jobs, not for the waiting once.
This will overwrite the default sort order, which is:
running jobs waiting jobs
------------- -------------
default: running none
-g: owner,running owner
Version 1.0 23 August 2004 status(1)
EOF
exit 0
}
#
# Analyze the given parameters to the command.
#
while getopts :acghjlmnos: options ; do
case $options in
a) CMDOPT_A="1" ;;
c) CMDOPT_C="1" ;;
g) CMDOPT_G="1" ;;
h) usage ;;
j) CMDOPT_J="1" ;;
l) CMDOPT_L="1" ;;
m) CMDOPT_M="1" ;;
n) CMDOPT_N="1"
unset CMDOPT_O ;;
o) CMDOPT_O="1"
unset CMDOPT_N ;;
s) CMDOPT_S="1"
sort_request="$OPTARG" ;;
\?) printExit E "Invalid option: -$OPTARG." ;;
esac
done
#
# Shift the arguments up to the input without the option prefix "-".
# This should be the user name.
#
shift $((OPTIND-1))
#
# Test, whether was a user specified.
#
if [ -n "$CMDOPT_A" ] ; then
if [ -n "$1" ] ; then
printExit E "You may only specify -a *or* an user, but not both."
else
myuser="*"
listuser="1"
fi
else
if [ -n "$1" ] ; then
myuser="$1"
else
myuser="$USER"
fi
fi
#
# Range of selected jobs to be included.
#
if [ -n "$CMDOPT_J" ] ; then
jobrange="rs"
else
jobrange="r"
fi
#
# Decide, whether the owner should be included in the output.
#
if [ -n "$CMDOPT_N" ] ; then
unset listuser
fi
if [ -n "$CMDOPT_O" ] ; then
listuser="1"
fi
#
# Grouping of owner requested?
#
if [ -n "$CMDOPT_G" -a -z "$listuser" ] ; then
printExit E "Option -g not applicable, without listing the owner of the jobs."
fi
#
# Get the version of the installed SGE.
#
version=`qstat -help 2>&1 | head -1 | cut -f 2 -d " "`
case $version in
5*) qstat_running='qstat -u "${myuser:-*}" -s "$jobrange" -r'
qstat_waiting='qstat -u "${myuser:-*}" -g d -s p -r'
running_in_column="all"
;;
6*) qstat_running='qstat -u "${myuser:-*}" -g t -s "$jobrange" -r'
qstat_waiting='qstat -u "${myuser:-*}" -g d -s p -r'
if [ -n "$CMDOPT_M" ] ; then
running_in_column="machine"
else
running_in_column="queue"
fi
;;
*) printExit E "This version of SGE ($version) is not supported."
;;
esac
#
# Assemble the sort commands as requested.
#
if [ -n "$CMDOPT_G" ] ; then
if [ -n "$CMDOPT_S" ] ; then
printExit E "You can't use the -g (grouped) switch together with the -s (sort) option."
else
sort_request="owner,running"
fi
else
if [ -z "$CMDOPT_S" ] ; then
sort_request="running"
fi
fi
sort_command=`$awk_command -v sort_request="$sort_request" -v listuser="$listuser" '
BEGIN { field_position["job"]=" -k 1,1"
field_position["name"]=" -k 3,3"
field_position["slots"]=" -k 2,2"
if (listuser)
{
field_position["owner"]=" -k 4,4"
field_position["running"]=" -k 7,7"
field_position["time"]=" -k 5,5 -k 6,6"
}
else
{
field_position["running"]=" -k 6,6"
field_position["time"]=" -k 4,4 -k 5,5"
}
sort_count=split(sort_request, sort_fields, ",")
for (field=1; field<=sort_count; field++)
{
if (sort_fields[field]=="none")
{
sort_command_running=""
sort_command_waiting=""
if (sort_count>1)
{ printf "WARNING: Sort set to none, supersede all other column names given.\n" > "/dev/stderr" }
break
}
if (sort_fields[field]=="owner" && ! listuser)
{
printf "WARNING: Sort by owner ignored, because owner not displayed.\n" > "/dev/stderr"
continue
}
if (field_position[sort_fields[field]]=="")
{ printf "WARNING: Sort column name \""sort_fields[field]"\" ignored.\n" > "/dev/stderr" }
else
{
sort_command_running=sort_command_running field_position[sort_fields[field]]
if (sort_fields[field] != "running")
{ sort_command_waiting=sort_command_waiting field_position[sort_fields[field]] }
}
}
if (sort_command_running)
{ sort_command_running="sort -b " sort_command_running }
else
{ sort_command_running="cat" }
if (sort_command_waiting)
{ sort_command_waiting="sort -b " sort_command_waiting }
else
{ sort_command_waiting="cat" }
print sort_command_running"|"sort_command_waiting } '`
sort_command_running=${sort_command%|*}
sort_command_waiting=${sort_command#*|}
#
# Now do it.
#
#
# First the already running jobs.
#
eval $qstat_running | $awk_command '
BEGIN { firstrun=1
ignore=0
}
function printy()
{
if (listuser)
{
printf "%6s %2d %-25s %-10s %19s %-10s %s\n",
jobid, procs, substr(jobname, 1, 25), owner, starttime, masternode, ext | sort_command
}
else
{
printf "%6s %2d %-25s %19s %-10s %s\n",
jobid, procs, substr(jobname, 1, 25), starttime, masternode, ext | sort_command
}
}
/MASTER *[0-9]*$/ { if (firstrun)
{
if (listing)
{
if (listuser)
{
printf "Running jobs:\n"
printf "job-ID # name owner start time running in\n"
printf "-----------------------------------------------------------------------------\n"
}
else
{
printf "Running jobs for user: %s\n", user
printf "job-ID # name start time running in\n"
printf "------------------------------------------------------------------\n"
}
}
firstrun=0
}
else
{
if (listing)
{ printy() }
}
ignore=0
jobid=$1
owner=$4
procs=1
starttime=$6" "$7
if (running_in_column=="all")
{ masternode=$8 }
else if (running_in_column=="machine")
{ masternode=substr($8, index($8, "@")+1) }
else if (running_in_column=="queue")
{ masternode=substr($8, 1, index($8, "@")-1) }
else
{ masternode="Unknown" }
ext=match($5, "d") ? "stalled" : ""
if (match($5, "s"))
{
if (ext)
{ ext=ext", suspended" }
else
{ ext="suspended"}
}
if (match($5, "S"))
{
if (ext)
{ ext=ext", suspended by queue" }
else
{ ext="suspended by queue"}
}
if (ext)
{ ext="("ext")" }
if (listuser)
{
serial[owner]++
parallel[owner]+=0
serial_slots[owner]++
parallel_slots[owner]+=0
}
else
{
serial[user]++
parallel[user]+=0
serial_slots[user]++
parallel_slots[user]+=0
}
}
/SLAVE *[0-9]*$/ { ignore=1 }
ignore { next }
/^ *Full jobname:/ { jobname=$3 }
/^ *Granted PE:/ { procs=$4
if (listuser)
{
serial[owner]--
parallel[owner]++
serial_slots[owner]--
parallel_slots[owner]+=procs
}
else
{
serial[user]--
parallel[user]++
serial_slots[user]--
parallel_slots[user]+=procs
}
}
END { if (! firstrun)
{
if (listing)
{
printy()
close(sort_command)
print ""
}
if (statistic)
{
printf("\n")
printf(" running #jobs/#slots\n")
printf("Owner serial parallel total\n")
printf("---------------------------------------\n")
if (listuser)
{
user_sort="sort -k 1,1"
for (owner in serial)
{
total_serial+=serial[owner]
total_parallel+=parallel[owner]
total_serial_slots+=serial_slots[owner]
total_parallel_slots+=parallel_slots[owner]
printf("%-10s %3d/%3d %3d/%3d %3d/%3d\n",
owner, serial[owner], serial_slots[owner], parallel[owner], parallel_slots[owner],
serial[owner]+parallel[owner], serial_slots[owner]+parallel_slots[owner]) | user_sort
}
close(user_sort)
printf("---------------------------------------\n")
printf("Sum %3d/%3d %3d/%3d %3d/%3d\n",
total_serial, total_serial_slots, total_parallel, total_parallel_slots,
total_serial+total_parallel, total_serial_slots+total_parallel_slots)
}
else
{
printf("%-10s %3d/%3d %3d/%3d %3d/%3d\n",
user, serial[user], serial_slots[user], parallel[user], parallel_slots[user],
serial[user]+parallel[user], serial_slots[user]+parallel_slots[user])
}
print ""
}
}
} ' user="$myuser" statistic="$CMDOPT_C" running_in_column="$running_in_column" \
listuser="$listuser" sort_command="$sort_command_running" listing="$((1-${CMDOPT_L:-0}))"
#
# Now the waiting jobs.
#
eval $qstat_waiting | $awk_command '
BEGIN { firstrun=1 }
function printy()
{
if (listuser)
{
printf "%6s %2d %-25s %-10s %19s %s\n",
jobid, procs, substr(jobname, 1, 25), owner, submittime, ext | sort_command
}
else
{
printf "%6s %2d %-25s %19s %s\n",
jobid, procs, substr(jobname, 1, 25), submittime, ext | sort_command
}
}
/^ *[0-9]+ */ { if (firstrun)
{
if (listing)
{
if (listuser)
{
printf "Waiting jobs:\n"
printf "job-ID # name owner submit time \n"
printf "------------------------------------------------------------------\n"
}
else
{
printf "Waiting jobs for user: %s\n", user
printf "job-ID # name submit time \n"
printf "-------------------------------------------------------\n"
}
}
firstrun=0
}
else
{
if (listing)
{ printy() }
}
jobid=$1
owner=$4
procs=1
submittime=$6" "$7
ext=match($5, "E") ? "Error" : ""
if (match($5, "h"))
{
if (ext)
{ ext=ext", hold" }
else
{ ext="hold"}
}
if (ext)
{ ext="("ext")" }
if (listuser)
{
serial[owner]++
parallel[owner]+=0
serial_slots[owner]++
parallel_slots[owner]+=0
}
else
{
serial[user]++
parallel[user]+=0
serial_slots[user]++
parallel_slots[user]+=0
}
}
/^ *Full jobname:/ { jobname=$3 }
/^ *Requested PE:/ { procs=$4
if (listuser)
{
serial[owner]--
parallel[owner]++
serial_slots[owner]--
parallel_slots[owner]+=procs
}
else
{
serial[user]--
parallel[user]++
serial_slots[user]--
parallel_slots[user]+=procs
}
}
END { if (! firstrun)
{
if (listing)
{
printy()
close(sort_command)
print ""
}
if (statistic)
{
printf("\n")
printf(" waiting #jobs/#slots\n")
printf("Owner serial parallel total\n")
printf("---------------------------------------\n")
if (listuser)
{
user_sort="sort -k 1,1"
for (owner in serial)
{
total_serial+=serial[owner]
total_parallel+=parallel[owner]
total_serial_slots+=serial_slots[owner]
total_parallel_slots+=parallel_slots[owner]
printf("%-10s %3d/%3d %3d/%3d %3d/%3d\n",
owner, serial[owner], serial_slots[owner], parallel[owner], parallel_slots[owner],
serial[owner]+parallel[owner], serial_slots[owner]+parallel_slots[owner]) | user_sort
}
close(user_sort)
printf("---------------------------------------\n")
printf("Sum %3d/%3d %3d/%3d %3d/%3d\n",
total_serial, total_serial_slots, total_parallel, total_parallel_slots,
total_serial+total_parallel, total_serial_slots+total_parallel_slots)
}
else
{
printf("%-10s %3d/%3d %3d/%3d %3d/%3d\n",
user, serial[user], serial_slots[user], parallel[user], parallel_slots[user],
serial[user]+parallel[user], serial_slots[user]+parallel_slots[user])
}
print ""
}
}
} ' user="$myuser" statistic="$CMDOPT_C" \
listuser="$listuser" sort_command="$sort_command_waiting" listing="$((1-${CMDOPT_L:-0}))"
#
# So, that's all
#
exit 0