#!/usr/bin/perl  

#ModulePurpose: Just look for BPPs which exceed input rate of 3MB/sec (configurable)
#ModuleName: bpp_datarates 
#TimeRequired: 2 min

# historical datarates are captured by cron each night.
# Results are saved to /h/bmetzger/s/tmp/all/site* 

# Problems:
#    BPP      Period  Kb/sec     Date     Time
#m002int29p1    MIN    3040    2009-07-06 19:59:19
#m004int30p3    MIN    3052    2009-07-06 19:44:47

use lib '/h/bmetzger/s/p/hc/';
use HC qw(:ALL);  #functions available to all modules 

use English;
use Carp;
use Cwd;
use DBI;
use Net::Ping;
use File::Basename;
use File::Path;
use File::Glob;
use File::Copy;

###############3###
# define location of, and open files
##################

  my ($TmpDir,$sitelist,$global_problems,$global_details,$global_summary,$ArchiveDir) = InitVars();

  $ENV{'PATH'} = '/usr/bin:/usr/local/bin:/usr/sbin/:/usr/ucb:/etc/:/opt/platform7/lbin/:.';
  &initvars();

### checks
 ($header, $local_problems, $local_details)= &check(); 
  summarize($header, $local_problems, $local_details);

###################33
sub initvars {

   ($sys_day,$sys_mon,$sys_year) = (localtime)[3,4,5];
   $sys_mon += 1;  #jan is 0
   $sys_year += 1900;
   $today = sprintf "%02d.%02d.%d",$sys_mon,$sys_day,$sys_year;  # zero fill date

   $ENV{'PATH'} = '/usr/bin:/usr/local/bin:/usr/sbin/:/usr/ucb:/etc/:.';
} #initvars

####################
sub line_count {
# perform a  wc -l

  my $file = shift;
    open my $fh, "<", $file or die "could not open $file:$!";
    my $lines = 0;
    $lines++ while <$fh>;
    return $lines;
    close ($fh);
} #line_count

#########
sub unique_array {
# Find elements that are in one array but not another.
# find only elements in @A and not in @B
#assume @A and @B are already loaded

   $refA = shift;
   $refB = shift;
   ($asize, $bsize, $missing) = 0;

#foreach (@$refA) {
#   print "$_\n";
#}
   %seen = ();                  # lookup table to test membership of B
   @aonly = ();                 # answer

   # build lookup table
   foreach $item (@$refB) { $seen{$item} = 1 }

   # find only elements in @A and not in @B
   foreach $item (@$refA) {
       unless ($seen{$item}) {
           # it's not in %seen, so add to @aonly
           push(@aonly, $item);
print "aonly:$item\n";
       }
   }
       $asize = @$refA;
       $bsize = @$refB;
       $missing = scalar @aonly;
  return ($asize, $bsize, $missing, @aonly);
} #unique_array

#########
sub check {
# Look for high input levels on all BPPs 

     print "bpp input datarates\n";

      my $local_problems = "$TmpDir/datarates_problems";
      my $local_details  = "$TmpDir/datarates_details";
      my $local_junk1    = "$TmpDir/junk1";    # temp file for problems
      my $local_junk2    = "$TmpDir/junk2";    # temp file for details

    # open local output files
    open (PROBLEMS, ">>$local_problems") or croak "Can't open local_problems for write: $!\n";
    open (DETAILS, ">>$local_details") or croak "Can't open local_details for write: $!\n";

    @allfiles = </h/bmetzger/s/tmp/all/*.datarates>;

    foreach my $file (@allfiles){ 
        if (-C $file > 1) {   # if file created > one days ago.....
              printf   "$file is not current\n";

        }else {
               open (IN, "<$file") or croak "Can't open datarates file $file for read: $!\n";
               while (my $line = <IN>) {
                      chomp $line;
                      $line =~ s/\.0{4,}//g;
                      my @arr = split(/,/,$line);

                      if ( ($line =~ /BPP/) && ($line !~ /No/) )  {
                          $bpp = $arr[4];
                      } 
#configurable
                      # > 3000 kb/sec but ignore wild startup values
                      if ( ($line =~ /DATARATE/) && ($arr[6] >= 3000) && ($arr[6] < 11111) ){
                          printf PROBLEMS "   $bpp    $arr[5]    $arr[6]    $arr[7]\n"; 
                      } else {
                          printf DETAILS "   $bpp    $arr[5]    $arr[6]    $arr[7]\n"; 
                      } 

               } #each file
        }
   } #all globbed files
   close (PROBLEMS);
   close (DETALS);

    my $badcount = line_count($local_problems);

    # open local output files
    open (JUNK1, ">>$local_junk1") or croak "Can't open file local_junk1 for write: $!\n";
    open (JUNK2, ">>$local_junk2") or croak "Can't open file $local_junk2 for write: $!\n";

    my $header = "\n--- $badcount BPP input datarate issues -$today \n";
    my $local_header = "    BPP      Period  Kb/sec     Date     Time\n";

    if ( $badcount > 0 ){  # at least one problem
         #format and header
         printf JUNK1 "$header";
         printf JUNK1 "$local_header";
           qx[cat $local_problems >> $local_junk1];
         close(JUNK1);

         printf JUNK2 $header;
          close(JUNK2);
    } else {
          #format and header
         printf JUNK2 $header;
         printf JUNK2 "\n    No BPP input datarates exceed 3Mbps\n";
         close(JUNK2);
    }
   copy($local_junk1,$local_problems) or die "junk1 cannot be copied";
   copy($local_junk2,$local_details) or die "junk2 cannot be copied";

   # cleanup
   unlink("$local_junk1") || print $!;
   unlink("$local_junk2") || print $!;

  return($header, $local_problems, $local_details);
} #check

