devinteske

qacctr

Jul 24th, 2024
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 4.66 KB | None | 0 0
  1. #!/usr/bin/perl
  2. ############################################################ IDENT(1)
  3. #
  4. # $Title: Extract a time-range of SGE accounting records $
  5. #
  6. ############################################################ INCLUDES
  7.  
  8. use strict;
  9. use warnings;
  10. use File::ReadBackwards;
  11. use Getopt::Std;
  12.  
  13. ############################################################ CONFIGURATION
  14.  
  15. our $DEFAULT_FILE = "/sge/default/common/accounting";
  16. our @ACCOUNTING = qw/qname hostname group owner job_name job_number account
  17.         priority submission_time start_time end_time failed exit_status
  18.         ru_wallclock ru_utime ru_stime ru_maxrss ru_ixrss ru_ismrss ru_idrss
  19.         ru_isrss ru_minflt ru_majflt ru_nswap ru_inblock run_oublock ru_msgsnd
  20.         ru_msgrcv ru_nsignals ru_nvcsw ru_nivcsw project department granted_pe
  21.         slots task_number cpu mem io category iow pe_taskid maxvmem arid
  22.         ar_submission_time/;
  23.  
  24. ############################################################ GLOBALS
  25.  
  26. our $VERSION = '$Version: 1.1 $';
  27.  
  28. (our $pgm = $0) =~ s#.*/##g; # Program basename
  29.  
  30. #
  31. # Global exit status
  32. #
  33. our $SUCCESS = 0;
  34. our $FAILURE = 1;
  35.  
  36. #
  37. # Command-line options
  38. #
  39. our $opt_f = $DEFAULT_FILE;     # file
  40. our $opt_h = 0;                 # help
  41. our $opt_n = 0;                 # max
  42. our $opt_v = 0;                 # version
  43.  
  44. #
  45. # Command-line arguments
  46. #
  47. our $START;
  48. our $HYPHEN;
  49. our $STOP;
  50.  
  51. #
  52. # Miscellaneous
  53. #
  54. our $fh = undef;
  55. our $record = 0;
  56. our $started = 0;
  57.  
  58. ############################################################ FUNCTIONS
  59.  
  60. sub usage
  61. {
  62.         my ($fmt) = @_;
  63.         my $optfmt = "\t%-10s %s\n";
  64.         my $argfmt = "\t%-9s %s\n";
  65.  
  66.         select STDERR;
  67.         if ($fmt) {
  68.                 shift @_;
  69.                 printf "%s: $fmt\n", $pgm, @_;
  70.         }
  71.         printf "Usage: %s [-hv] [-f file] [-n max] start - stop\n", $pgm;
  72.         printf "Options:\n";
  73.         printf $optfmt, "-f file", "Accounting file. Default: $DEFAULT_FILE";
  74.         printf $optfmt, "-h", "Print this usage statement and exit.";
  75.         printf $optfmt, "-n max", "Produce at most max records.";
  76.         printf $optfmt, "-v", "Print version information and exit.";
  77.         printf "Arguments:\n";
  78.         printf $argfmt, "start",
  79.                 "Stop producing records when this time is reached.";
  80.         printf $argfmt, "", "Can be 0 to produce all records.";
  81.         printf $argfmt, "-", "A literal hyphen/dash.";
  82.         printf $argfmt, "stop",
  83.                 "Start producing records when this time is reached.";
  84.         printf $argfmt, "", "Can be 0 to produce records immediately.";
  85.         exit $FAILURE;
  86. }
  87.  
  88. ############################################################ MAIN
  89.  
  90. #
  91. # Process command-line options
  92. #
  93. getopts('f:hn:v') or &usage; # NOTREACHED
  94. &usage() if $opt_h;
  95. if ($opt_v)
  96. {
  97.         $VERSION =~ s/^\$Version: //;
  98.         $VERSION =~ s/ \$//;
  99.         print "$VERSION\n";
  100.         exit $SUCCESS;
  101. }
  102.  
  103. #
  104. # Check command-line options
  105. #
  106. &usage("-n requires a number") unless $opt_n =~ m/^\d+$/; # NOTREACHED
  107.  
  108. #
  109. # Process command-line arguments
  110. #
  111. &usage("Too few arguments") if $#ARGV < 2; # NOTREACHED
  112. &usage("Too many arguments") if $#ARGV > 2; # NOTREACHED
  113. ($START, $HYPHEN, $STOP) = @ARGV;
  114.  
  115. #
  116. # Check command-line arguments
  117. #
  118. &usage("arg1 must be a number") unless $START =~ m/^\d+$/; # NOTREACHED
  119. &usage("arg2 must be a hyphen/dash") unless $HYPHEN eq "-"; # NOTREACHED
  120. &usage("arg3 must be a number") unless $STOP =~ m/^\d+$/; # NOTREACHED
  121. $started = 1 if $STOP == 0;
  122.  
  123. #
  124. # Open the file
  125. #
  126. $fh = File::ReadBackwards->new($opt_f) or die "$opt_f: $!";
  127.  
  128. #
  129. # Read the file
  130. #
  131. while (1)
  132. {
  133.         my $exit = 0;
  134.         my $line = $fh->readline() or last;
  135.         my $n = 0;
  136.         my %acct = ( );
  137.  
  138.         #
  139.         # Check max
  140.         #
  141.         $record++;
  142.         last if $opt_n gt 0 && $record gt $opt_n;
  143.  
  144.         #
  145.         # Parse one more line
  146.         #
  147.         foreach (split /:/, $line)
  148.         {
  149.                 last unless $n <= $#ACCOUNTING;
  150.                 $acct{$ACCOUNTING[$n++]} = $_;
  151.         }
  152.  
  153.         if ($started == 0)
  154.         {
  155.                 # Check to see if we should start
  156.                 next unless $acct{end_time} gt 0;
  157.                 $started = 1 if $acct{end_time} le $STOP;
  158.         }
  159.         elsif ($acct{end_time} gt 0)
  160.         {
  161.                 # Check to see if we should stop
  162.                 exit $SUCCESS if $acct{end_time} lt $START;
  163.         }
  164.  
  165.         print $line if $started;
  166. }
  167.  
  168. #
  169. # Bye
  170. #
  171. exit $SUCCESS;
  172.  
  173. ################################################################################
  174. # END
  175. ################################################################################
Add Comment
Please, Sign In to add comment