Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- ############################################################ IDENT(1)
- #
- # $Title: Extract a time-range of SGE accounting records $
- #
- ############################################################ INCLUDES
- use strict;
- use warnings;
- use File::ReadBackwards;
- use Getopt::Std;
- ############################################################ CONFIGURATION
- our $DEFAULT_FILE = "/sge/default/common/accounting";
- our @ACCOUNTING = qw/qname hostname group owner job_name job_number account
- priority submission_time start_time end_time failed exit_status
- ru_wallclock ru_utime ru_stime ru_maxrss ru_ixrss ru_ismrss ru_idrss
- ru_isrss ru_minflt ru_majflt ru_nswap ru_inblock run_oublock ru_msgsnd
- ru_msgrcv ru_nsignals ru_nvcsw ru_nivcsw project department granted_pe
- slots task_number cpu mem io category iow pe_taskid maxvmem arid
- ar_submission_time/;
- ############################################################ GLOBALS
- our $VERSION = '$Version: 1.1 $';
- (our $pgm = $0) =~ s#.*/##g; # Program basename
- #
- # Global exit status
- #
- our $SUCCESS = 0;
- our $FAILURE = 1;
- #
- # Command-line options
- #
- our $opt_f = $DEFAULT_FILE; # file
- our $opt_h = 0; # help
- our $opt_n = 0; # max
- our $opt_v = 0; # version
- #
- # Command-line arguments
- #
- our $START;
- our $HYPHEN;
- our $STOP;
- #
- # Miscellaneous
- #
- our $fh = undef;
- our $record = 0;
- our $started = 0;
- ############################################################ FUNCTIONS
- sub usage
- {
- my ($fmt) = @_;
- my $optfmt = "\t%-10s %s\n";
- my $argfmt = "\t%-9s %s\n";
- select STDERR;
- if ($fmt) {
- shift @_;
- printf "%s: $fmt\n", $pgm, @_;
- }
- printf "Usage: %s [-hv] [-f file] [-n max] start - stop\n", $pgm;
- printf "Options:\n";
- printf $optfmt, "-f file", "Accounting file. Default: $DEFAULT_FILE";
- printf $optfmt, "-h", "Print this usage statement and exit.";
- printf $optfmt, "-n max", "Produce at most max records.";
- printf $optfmt, "-v", "Print version information and exit.";
- printf "Arguments:\n";
- printf $argfmt, "start",
- "Stop producing records when this time is reached.";
- printf $argfmt, "", "Can be 0 to produce all records.";
- printf $argfmt, "-", "A literal hyphen/dash.";
- printf $argfmt, "stop",
- "Start producing records when this time is reached.";
- printf $argfmt, "", "Can be 0 to produce records immediately.";
- exit $FAILURE;
- }
- ############################################################ MAIN
- #
- # Process command-line options
- #
- getopts('f:hn:v') or &usage; # NOTREACHED
- &usage() if $opt_h;
- if ($opt_v)
- {
- $VERSION =~ s/^\$Version: //;
- $VERSION =~ s/ \$//;
- print "$VERSION\n";
- exit $SUCCESS;
- }
- #
- # Check command-line options
- #
- &usage("-n requires a number") unless $opt_n =~ m/^\d+$/; # NOTREACHED
- #
- # Process command-line arguments
- #
- &usage("Too few arguments") if $#ARGV < 2; # NOTREACHED
- &usage("Too many arguments") if $#ARGV > 2; # NOTREACHED
- ($START, $HYPHEN, $STOP) = @ARGV;
- #
- # Check command-line arguments
- #
- &usage("arg1 must be a number") unless $START =~ m/^\d+$/; # NOTREACHED
- &usage("arg2 must be a hyphen/dash") unless $HYPHEN eq "-"; # NOTREACHED
- &usage("arg3 must be a number") unless $STOP =~ m/^\d+$/; # NOTREACHED
- $started = 1 if $STOP == 0;
- #
- # Open the file
- #
- $fh = File::ReadBackwards->new($opt_f) or die "$opt_f: $!";
- #
- # Read the file
- #
- while (1)
- {
- my $exit = 0;
- my $line = $fh->readline() or last;
- my $n = 0;
- my %acct = ( );
- #
- # Check max
- #
- $record++;
- last if $opt_n gt 0 && $record gt $opt_n;
- #
- # Parse one more line
- #
- foreach (split /:/, $line)
- {
- last unless $n <= $#ACCOUNTING;
- $acct{$ACCOUNTING[$n++]} = $_;
- }
- if ($started == 0)
- {
- # Check to see if we should start
- next unless $acct{end_time} gt 0;
- $started = 1 if $acct{end_time} le $STOP;
- }
- elsif ($acct{end_time} gt 0)
- {
- # Check to see if we should stop
- exit $SUCCESS if $acct{end_time} lt $START;
- }
- print $line if $started;
- }
- #
- # Bye
- #
- exit $SUCCESS;
- ################################################################################
- # END
- ################################################################################
Add Comment
Please, Sign In to add comment