-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tstatus
executable file
·360 lines (318 loc) · 10.2 KB
/
Tstatus
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
#!/usr/local/bin/perl5 -w
#
# $Header: /home/red/bin/Tstatus,v 1.5 1997/10/28 19:09:18 red Exp red $
#
# "Qstatus" created by red
#
# $Log: Tstatus,v $
# Revision 1.5 1997/10/28 19:09:18 red
# Added an option to specify the update time between 'tstat -v' runs
# Added An Icon name and widget title.
#
# Revision 1.4 1997/10/23 23:00:03 red
# Additions by Bob Proulx for background operation and inproved options
# parsing.
#
# Revision 1.3 1997/10/23 22:11:01 red
# Fixed the unitialized variable caused in $cmdline.
#
# Revision 1.2 1997/04/14 17:46:52 red
# Working right now.
#
# Revision 1.1 1997/04/12 18:56:49 red
# Initial revision
#
use strict;
use English;
use Tk;
use Tk::Xrm;
use Tk::DialogBox;
use subs qw(handler show_usage update get_tstats cleanup_and_die);
use POSIX qw(strftime);
use vars qw($opt_e $opt_p $opt_u $opt_f $opt_n $opt_x $opt_r);
use Getopt::Std;
#-------------------------------------------------------------------
# General Variables and runtime information.
#-------------------------------------------------------------------
use vars qw($update $header_string $Rev $RunDate $DirName $ProgName);
use vars qw(@fields $job $status $queue $cmdline $label_string);
use vars qw($QueueEmpty %Current %Queue @tinfo $machine @title);
my $FONT = '-*-Helvetica-Medium-R-Normal--*-140-*-*-*-*-*-*';
my $tlog = "/net/hpesfem/home/red/tbroker/quiddich.log";
#Widgets
use vars qw($main $top $ltop $tstats $header $w_update $w_quit $listbox);
$RunDate = strftime '%Y/%m/%d %H:%M:%S', localtime;
$Rev = (split(' ', '$Revision: 2 $', 3))[1];
$0 =~ m!(.*)/!; $ProgName = $'; $DirName = $1; $DirName = '.' unless $DirName;
$SIG{'HUP'} = \&handler;
$SIG{'INT'} = \&handler;
$SIG{'QUIT'} = \&handler;
$SIG{'TERM'} = \&handler;
#--------------------------------------------------------------
# General utility subroutines
#--------------------------------------------------------------
sub handler
{
my($sig) = @_;
warn "$ProgName:INFO: Caught a SIG$sig -- shutting down\n";
exit(0);
}
sub show_usage
{
print "$ProgName:\n";
print " Options:\n";
print " -e : Enable extended queue tracking options\n";
print " -p : enable pages.\n";
print " -f : Run in foreground.\n";
print " -u <update> : Update time in seconds (default is 1 sec).\n";
print " -x : Verbose degug information.\n";
print " -r [hostname]: Show tstats from remote machine. Note you\n";
print " must have remsh permission to this machine.\n";
print " -n : Don't exit when all jobs are done.\n";
print "\n";
print "$ProgName $Rev\t\t$RunDate\n\n";
print " Put up a small xwindow log to show the status of running\n";
print " Taskbroker jobs.\n";
print " In the Tstatus window inputing:\n";
print " Control-C : Exits the program.\n";
print " Control-Q : Exits the program.\n";
print " Control-U : Run a tstat and update immediately.\n";
print "\n";
exit 0;
}
sub cleanup_and_die {
my $msg=$_[0];
close(INFO);
die "$msg";
}
#----------------------------------------------------------------------------------
# Main subroutine to run the 'tinfo' command and gather the information about the
# running jobs.
#----------------------------------------------------------------------------------
sub get_tstats {
my ($rest, $index, $save);
undef @tinfo;
if ($opt_r) {
open(INFO,"remsh $opt_r -n 'tinfo' |")
|| cleanup_and_die "Cannot open pipe to tinfo\n";
}
else {
open(INFO,"tinfo |") || cleanup_and_die "Cannot open pipe to tinfo\n";
}
$index=0;
$save="";
while(<INFO>){
chop;
/^\s*$/ && do { undef @tinfo; close(INFO); return; };
s/\s+/ /g;
s/(.*)\s+'(.*)'/$1/ && do {
$save=$2;
s/\'//g;
s/\s/:/g;
};
$opt_x && print "Tinfo $.: $_\n";
$tinfo[$index]=$_;
$title[$index]=$save;
$index++;
}
close(INFO);
}
sub manage_queue {
my $message;
my $datestamp=`date +%m%d%YT%H%M%S`;
# remove completed jobs
for my $jkey (keys %Queue){
if($Queue{$jkey}->{Running}==1 && !exists($Current{$jkey})){
$message .= "Job Complete:\n";
$message .= " ... removing completed job $jkey.\n";
$message .= " ... command=$Queue{$jkey}->{Command}\n";
if (defined($opt_p)){
system "fcpager moore,floyd \"$message\"";
}
$Queue{$jkey}->{Running}=0;
$Queue{$jkey}->{Delete}=$datestamp;
}
}
for my $jkey (keys %Current){
if (exists $Queue{$jkey}){
$Queue{$jkey}->{Running}=1;
#$message .= " ... job $job exists in queue leaving it there\n";
} else {
#insert into queue
#$message .= " ... Insert job $jkey\n";
$Queue{$jkey}->{Running}=1;
$Queue{$jkey}->{Insert}=$datestamp;
$Queue{$jkey}->{Machine}=$Current{$jkey}->{Machine};
$Queue{$jkey}->{Command}=$Current{$jkey}->{Command};
my $message_string =
sprintf("%3s %-12s %-15s %-40s", $Queue{$jkey}->{Running},
$jkey,
$Queue{$jkey}->{Machine},
$Queue{$jkey}->{Command});
if (defined($ltop)){
$listbox->insert('end',$message_string);
}
}
}
if (defined($message)){
my $job_dialog = $top->DialogBox( -title => 'Queue:',
-buttons => ["OK"] );
$job_dialog->add("Label", -text => $message)->pack();
$job_dialog->Show;
}
if (!keys %Current){
$QueueEmpty=1;
&exit unless (defined($opt_n));
}
}
sub update {
my($index);
# Update the counter every 1 second.
#$opt_x && print "Update...\n";
&get_tstats;
$index=0;
undef %Current;
if (defined(@tinfo)){
$label_string = "";
for ($index=0;$index<=$#tinfo;++$index){
$queue="";
$job="";
my $stamp=`date`; chomp $stamp;
undef ($machine);
$cmdline=" ";
@fields=split(/:/,$tinfo[$index]);
if ($fields[0] =~ /Q/){
#queued task
$queue=$fields[3] . " queue";
$job=$fields[2];
$cmdline="Submit " . $title[$index];
}
else {
#running task
#queued task
$job=$fields[3];
$queue=$fields[4];
$machine=$fields[2];
$cmdline="Run " . $title[$index];
my $user=(split(".",$job))[0];
$Current{$job}->{User}=$user;
$Current{$job}->{Machine}=$machine;
$Current{$job}->{Command}=$cmdline;
$Current{$job}->{Command}=~ s/^Run\s+//;
}
$opt_x && print "Queue=$queue\n";
$opt_x && $machine && print "Machine=$machine\n";
$opt_x && print "Job=$job\n";
unless(defined($cmdline)) { $cmdline="-No Title-"; }
if (length($cmdline) > 33 ){
$cmdline=substr($cmdline,0,33);
$cmdline .= "...";
}
if (length($queue) > 15 ){
$cmdline=substr($cmdline,0,15);
}
$opt_x && print "Cmdline=$cmdline\n";
if (defined($machine)){
$label_string .= sprintf("%-12s %-15s %-40s\n", $job, $machine, $cmdline);
}
else {
$label_string .= sprintf("%-12s %-15s %-40s\n", $job, $queue, $cmdline);
}
$opt_x && print "$label_string\n";
}
} else {
$label_string = "No Jobs";
$QueueEmpty=1;
}
if (defined($opt_e) && defined(@tinfo)){ manage_queue(); }
$top->after($update, \&update);
}
#----------------------------------------------------------------------------------
#
# Main program
#
#
# parse options...
#
my @SAVEDARGV = @ARGV;
if (&getopts('pefnxr:u:') == 0) {
&show_usage;
}
#print "# $ProgName $Rev\t\t$RunDate\n\n";
unless($opt_u){
$update=1000;
} else {
if ( $opt_u !~ /\d+/ ){
die "Invalid number specified for the update time: $opt_u\n";
}
$update=int($opt_u*1000);
if ( $update == 0 ) {
print "Bad update time conversion\n";
}
elsif ($update < 100) {
print "Setting update time to minimum of 0.1 seconds\n";
$update=100;
}
else {
print "Update time set at ",($update/1000)," seconds\n";
}
}
if (defined($opt_p) && !defined($opt_e)){
die "Page mode only available with extended option mode '-e'\n";
}
if (defined($opt_e)){
my $user=`whoami`;
chomp $user;
if ($user ne "red"){
print " This option is experimental. Please see Floyd Moore before\n";
print " using it so I can gauge the need\n";
undef $opt_e;
}
}
# get first update...
undef $QueueEmpty;
&get_tstats;
unless (defined(@tinfo)) {
print "No Tasks Found\n";
}
if (!defined($opt_f)) {
exec 'nohupd', '-n', $0, '-f', @SAVEDARGV;
die "$ProgName: Error: exec of \"nohupd\" failed: $!\n";
}
$top = MainWindow->new;
$top->bind('<Control-c>' => \&exit);
$top->bind('<Control-q>' => \&exit);
$top->bind('<Control-u>' => \&update);
$top->title('Tstatus');
$top->iconname('Tstatus');
# Define the header
$header_string = sprintf("%-12s %-15s %-40s", "Which?", "Where?", "What?");
$header = $top->Label( -textvariable => \$header_string,
-relief => 'groove')
->pack( -side => 'top',
-fill => 'y');
$tstats = $top->Label( -textvariable => \$label_string)
->pack( -side => 'top',
-fill => 'y');
$w_update = $top->Button(-text => 'Update',
-command => \&update)
->pack( -side => 'left',
-fill => 'y',
-expand => 'y');
$w_quit = $top->Button(-text => 'Quit',
-command => sub { exit(0); })
->pack( -side => 'right',
-fill => 'y',
-expand => 'y');
if (defined($opt_e)){
#create the listbox...
$ltop = $top->Toplevel(); # create a new window
$ltop->title('Tbd History');
$listbox=$ltop->Scrolled('Listbox',
"width" => 60,
"height" => 5,
-scrollbars => 'se')
->pack();
}
&update;
MainLoop;