This file is indexed.

/usr/bin/qsched is in gridengine-client 8.1.9+dfsg-4+deb9u2.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  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
#!/usr/bin/perl

# Copyright (C) 2010-2011 University of Leeds
#
# 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 3 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, see <http://www.gnu.org/licenses/>.

# User command to interrogate contents of schedule.last file
#
# $Id: qsched 1826 2011-06-30 11:15:34Z issmcd $

use strict;
use IO::File;
use File::stat;
use File::Basename;
use Getopt::Long;
use POSIX qw(floor);
use XML::Simple;
use Env qw(USER SGE_ROOT SGE_CELL);

my $sched_data     = "${SGE_ROOT}/${SGE_CELL}/common/schedule.last";
my $max_attempts   = 2;
my $retry_interval = 1;
my $jids;
my $old_seconds    = 1200;
my $trim_string    = '\.example.com$';
my @suffix_two     = ("", "K", "M", "G", "T");
my @suffix_ten     = ("", "k", "m", "g", "t");

my %minimum_free;
$minimum_free{"h_vmem"} = expand_suffix("1G");
$minimum_free{"slots"}  = 1;

my %opts;
$opts{user}     = $USER;
GetOptions(
    'u|user=s'    => \$opts{user},
    'a|all'       => \$opts{all},
    'f|full'      => \$opts{full},
    'j|jid=s'     => \$opts{jid},
    's|sort:s'    => \$opts{sort},
    'd|debug'     => \$opts{debug},
    'h|help'      => \$opts{help},
    'q|queues'    => \$opts{queues},
);

$opts{all} = 1 if ($opts{user} eq '*');

if ($opts{help}) {
    print "Usage: ". basename($0) ." [OPTION...]\n";
    print "\n";
    print "  -u <user>      - view jobs belonging to user\n";
    print "  -a             - view jobs belonging to all users (synonym for '-u *')\n";
    print "  -j <jid_list>  - comma-separated list of JIDs to view\n";
    print "  -f             - print all available data for each job\n";
    print "  -s j           - sort by JID\n";
    print "  -q             - sort by queue";
    print "  -d             - print debugging info\n";
    print "  -h             - print this message\n";
    exit 0;
}

if ($opts{jid}) {
    $jids = proc_jid_list($opts{jid});
}

my $fh = open_file($sched_data);
warn_and_exit("ABORT: Could not read scheduler data") if (! $fh);

my $queue = queue_data();
my $sched = sched_data($fh); # WARNING: data lags other sources by a few minutes

if ($opts{queues}) {
    print_queues($sched, $queue);
} else {
    my $users = job_to_user($queue);
    print_data($sched, $users);
}

# Warn if data file was rather old
my $time = time() - stat($fh)->mtime;
if ($time > $old_seconds) {
    warn_and_exit("WARNING: Scheduler data is $time seconds old - out of date");
}

exit 0;

sub job_to_user {
    my($q) = @_;
    my %users;

    foreach my $entry (@{$q->{job_info}->{job_list}}) {
        $users{$entry->{JB_job_number}} = $entry->{JB_owner};
    }
    foreach my $entry (@{$q->{queue_info}->{job_list}}) {
        $users{$entry->{JB_job_number}} = $entry->{JB_owner};
    }

    return \%users;
}

#DEBUG - still need to:
#  - format output properly
#  - switches to control minimum_free settings
#  - include job times
#  - too big: split into functions
sub print_queues {
    my($sched, $queue) = @_;

    # Sort reservations by queue
    my %qreserv;
    foreach my $job (sort keys %{$sched}) {
        foreach my $task (sort keys %{$sched->{$job}}) {
            foreach my $qinst (keys %{$sched->{$job}{$task}{Q}}) {
                push(@{$qreserv{$qinst}}, { job => $job, task => $task });
            }
        }
    }

    # Check each queue instance
    foreach my $qinst (sort keys %{$queue->{queue_info}->{"Queue-List"}}) {
        my $qresource = $queue->{queue_info}->{"Queue-List"}->{$qinst}->{resource};

        my $report = 1;

        foreach my $res (keys %{$qresource}) {
            if (defined($minimum_free{$res})) {
                if (expand_suffix($qresource->{$res}{content}) < expand_suffix($minimum_free{$res})) {
                    $report = 0;
                }
            }
        }

        if ($report) {
            print trim_irrelev($qinst) .":";

            # Resources
            foreach my $res (keys %{$qresource}) {
                print " $res=". $qresource->{$res}{content};
            }

            # Scheduled jobs
            foreach my $j (@{$qreserv{trim_irrelev($qinst)}}) {
                print " ". $j->{job} .".". $j->{task};
            }

            print "\n";
        }
    }
}

sub print_data {
    my($data,$users) = @_;

    # Obtain a sorted list of jobs/tasks into %order
    my %order;
    if ($opts{jid}) {
        foreach my $jid (@{$jids}) {
            if ($jid =~ /^(\d+)\.(\d+)$/) {
                my $job  = $1;
                my $task = $2;
                push(@{$order{0}}, [ $job, $task ]);
            } elsif ($jid =~ /^(\d+)$/) {
                my $job = $1;
                next if (! exists $data->{$job});
                foreach my $task (sort {$a <=> $b} keys %{$data->{$job}}) {
                    push(@{$order{0}}, [ $job, $task ]);
                }
            }
        }
    } else {
        if ($opts{sort} eq "j") {
            # Print job/task data in order of job id
            foreach my $job (sort {$a <=> $b} keys %{$data}) {
                foreach my $task (sort {$a <=> $b} keys %{$data->{$job}}) {
                    push(@{$order{0}}, [ $job, $task ]) if (match_username($users->{$job}));
                }
            }
        } else {
            # Print job/task data in order of start_time
            foreach my $job (sort {$a <=> $b} keys %{$data}) {
                foreach my $task (sort {$a <=> $b} keys %{$data->{$job}}) {
                    push(@{$order{$data->{$job}{$task}{start_time}}}, [ $job, $task ]) if (match_username($users->{$job}));
                }
            }
        }
    }

    # Find calculate derived data and formatting information
    my %format;
    $format{jobid}  = 6;
    $format{time}   = 19;
    $format{slots}  = 5;
    $format{h_vmem} = 6;
    $format{user}   = 4;
    foreach my $key (keys %{order}) {
        foreach my $entry (@{$order{$key}}) {
            my $job  = ${$entry}[0];
            my $task = ${$entry}[1];

            # jobid
            my $len = length($job .".". $task);
            $format{jobid} = $len if ($len > $format{jobid});

            # slots
            $data->{$job}{$task}{slots} = sum_entry($data->{$job}{$task}{Q}, "slots");
            my $len = length($data->{$job}{$task}{slots});
            $format{slots} = $len if ($len > $format{jobid});

            # h_vmem
            $data->{$job}{$task}{h_vmem} = human_readable(sum_entry($data->{$job}{$task}{H}, "h_vmem") / $data->{$job}{$task}{slots});
            my $len = length($data->{$job}{$task}{h_vmem});
            $format{h_vmem} = $len if ($len > $format{jobid});

            # user
            my $len = 0;
            $len = length($users->{$job}) if (exists $users->{$job});
            $format{user} = $len if ($len > $format{user});
        }
    }

    if (! $opts{full}) {
        print
            sprintf("%-". $format{jobid}  ."s", "job-ID") ."  ".
            sprintf("%-". $format{user}   ."s", "user")   ."  ".
            sprintf("%-". $format{time}   ."s", "start")  ."  ".
            sprintf("%-". $format{time}   ."s", "end")    ."  ".
            sprintf("%-". $format{slots}  ."s", "slots")  ."  ".
            sprintf("%-". $format{h_vmem} ."s", "h_vmem") ."\n";
        print
            "-" x $format{jobid}  ."  ".
            "-" x $format{user}   ."  ".
            "-" x $format{time}   ."  ".
            "-" x $format{time}   ."  ".
            "-" x $format{slots}  ."  ".
            "-" x $format{h_vmem} ."\n";
    }

    # Print sorted list of jobs/tasks
    foreach my $key (sort {$a <=> $b} keys %{order}) {
        foreach my $entry (@{$order{$key}}) {
            print_job_data(${$entry}[0], ${$entry}[1], $data, \%format, $users);
        }
    }
}

sub print_job_data {
    my($job,$task,$data,$format,$users) = @_;

    return if (! exists $data->{$job});
    return if (! exists $data->{$job}{$task});

    my $user   = $users->{$job};
    my $slots  = $data->{$job}{$task}{slots};
    my $h_vmem = $data->{$job}{$task}{h_vmem};

    if ($opts{full}) {
        my $start = localtime($data->{$job}{$task}{start_time});
        my $end   = localtime($data->{$job}{$task}{end_time});

        print "Job ${job}.${task}\n";
        print "    user      = $user\n";
        print "    start     = $start\n";
        print "    end       = $end\n";
        print "    slots     = $slots\n";
        print "    h_vmem    = $h_vmem\n";

        print "    pe        =". print_entry($data->{$job}{$task}{P});
        print "    queue     =". print_entry($data->{$job}{$task}{Q});
        print "    resources =". print_entry($data->{$job}{$task}{H});

        print "\n";

    } else {
        my $start = print_datetime($data->{$job}{$task}{start_time});
        my $end   = print_datetime($data->{$job}{$task}{end_time});

        print
            sprintf("%-". $format->{jobid}  ."s", "${job}.${task}") ."  ".
            sprintf("%-". $format->{user}   ."s", $user)            ."  ".
            sprintf("%-". $format->{time}   ."s", $start)           ."  ".
            sprintf("%-". $format->{time}   ."s", $end)             ."  ".
            sprintf("%".  $format->{slots}  ."s", $slots)           ."  ".
            sprintf("%".  $format->{h_vmem} ."s", $h_vmem)          ."\n";
    }
}

sub sched_data {
    my($fh) = @_;
    my %data;

    while (<$fh>) {
        if (/^([^:]+):([^:]+):([^:]+):([^:]+):([^:]+):([^:]+):([^:]+):([^:]+):([^:]+)$/) {
            my $job           = $1;
            my $task          = $2;
            my $state         = $3;
            my $time          = $4;
            my $length        = $5;
            my $record_type   = $6;
            my $record_object = $7;
            my $record_key    = $8;
            my $record_value  = $9;
            chomp($record_value);

            if ($state eq "RESERVING") {
                $data{$job}{$task}{start_time} = $time;
                $data{$job}{$task}{end_time} = $time + $length;

                $record_object = trim_irrelev($record_object) if ($record_type eq "Q");
                $record_object = trim_irrelev($record_object) if ($record_type eq "H");
                $record_value  = trim_irrelev($record_value);

                $data{$job}{$task}{$record_type}{$record_object}{$record_key} = human_readable($record_value);
            }
        }
    }

    return \%data;
}

sub open_file {
    my($file) = @_;

    my $attempts = 0;
    do {
        $attempts++;

        my $fh = IO::File->new;
        $fh->open("< $file") && return $fh;

        plog("Failed opening $file");
        sleep $retry_interval;
    } while ($attempts < $max_attempts);

    return 0;
}

sub plog {
    my($txt) = @_;
    chomp($txt);

    if ($opts{debug}) {
        print "$txt\n";
    }
}

sub warn_and_exit {
    my($txt) = @_;
    chomp($txt);

    print "$txt\n";
    exit 1;
}

sub trim_irrelev {
    my($txt) = @_;

    $txt =~ s/\.0+$//;
    $txt =~ s/${trim_string}//;
    return $txt;
}

sub print_entry {
    my($data) = @_;
    my $str;

    foreach my $obj (sort keys %{$data}) {
        $str .= " $obj (";
        my $num = 0;
        foreach my $key (sort keys %{$data->{$obj}}) {
            $str .= ", " if ($num > 0);
            $str .= "$key = $data->{$obj}{$key}";
            $num++;
        }
        $str .= ")";
    }
    $str .= "\n";
}

sub sum_entry {
    my($data,$key) = @_;
    my $sum = 0;

    foreach my $obj (sort keys %{$data}) {
        $sum += expand_suffix($data->{$obj}{$key});
    }

    return $sum;
}

sub proc_jid_list {
    my($txt) = @_;
    my @jid = split(/,/,$txt);
    return \@jid;
}

sub human_readable {
    my($value) = @_;
    my(@base_two, @base_ten);

    @base_two = contract_suffix($value, 1024);
    @base_ten = contract_suffix($value, 1000);

    if ($base_two[1] > $base_ten[1]) {
         # value divides more neatly in base 2
        if ($#suffix_two < $base_two[1]) {
            return $value;
        } else {
            return $base_two[0] . $suffix_two[$base_two[1]];
        }
    } else {
         # value divides more neatly in base 10
        if ($#suffix_ten < $base_ten[1]) {
            return $value;
        } else {
            return $base_ten[0] . $suffix_ten[$base_ten[1]];
        }
    }
}

# Return the value with n*base subtracted from it, and n
sub contract_suffix {
    my($value, $base) = @_;

    my $new_value = $value / $base;
    if ($new_value != 0 && $new_value == POSIX::floor($value / $base)) {
        my($value, $nbase) = contract_suffix($new_value, $base);
        return ($value, $nbase+1);
    } else {
        return ($value, 0);
    }

}

sub print_datetime {
    my($time) = @_;
    my @d = localtime($time);
    return sprintf("%02d", $d[4]+1) ."/".
           sprintf("%02d", $d[3])   ."/".
           ($d[5] +1900) ." ".
           sprintf("%02d", $d[2]) .":".
           sprintf("%02d", $d[1]) .":".
           sprintf("%02d", $d[0]);
}

sub queue_data {
    my $xs = XML::Simple->new(ForceArray => [ 'job_list' ]);

    # Obtain queue instance data
    my $fh = IO::File->new;
    $fh->open("qstat -F h_vmem,slots -f -ext -u \\\* -xml |") || die("Problem executing qstat: $!");

    my $doc = $xs->XMLin($fh);

    return $doc;
}

sub expand_suffix {
    my($number) = @_;

    if ($number =~ /^([0-9.]+)(\S+)?$/) {
        my $amount    = $1;
        my $multipler = $2;

        for(my $i = 0; $i <= $#suffix_ten; $i++) {
            return $number * 1000**$i if ($multipler eq $suffix_ten[$i]);
        }

        for(my $i = 0; $i <= $#suffix_two; $i++) {
            return $number * 1024**$i if ($multipler eq $suffix_two[$i]);
        }
    }

    return $number;
}

sub match_username {
    my($user) = @_;

    if ($opts{all} || $user eq $opts{user}) {
        return 1;
    }

    return 0;
}