#!/usr/bin/perl  

#ModulePurpose: Look for BPP product option mismatch 
#ModuleName: bpp.check.options 
#TimeRequired:   7 minutes for 890 BPPs

# Problem Report:
#  --- 3 BPP option issues
#  m006ilm1p3        MIC-3390  1  --option incorrect
#  m008ilm15p4       MIC-3390  1  --option incorrect
#  m014scp1p4        CPCI-736  2  --option incorrect

# Expected
# ADVANTECH MIC-3390       should be option 2
# FORCE COMPUTERS CPCI-736 should be option 1

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 check {
# grep BPP option, and compare to option in ETR

     print "bpp option check\n";

      my $bpplist        = "$TmpDir/bpplist";    
#customizeable
      my $etrfile        = "/h/bmetzger/s/tmp/all/wm.etr";    
      my $local_problems = "$TmpDir/option_problems";
      my $local_details  = "$TmpDir/option_details";
      my $local_junk1    = "$TmpDir/junk1";    # temp file for problems
      my $local_junk2    = "$TmpDir/junk2";    # temp file for details

    unlink("$bpplist");

     #create list of boxes to check
#customizeable
#      qx[list_processors -b | grep -E "m006ilm1p3|m008ilm15p4|m014scp1p4" >> $bpplist];
      qx[list_processors -b > $bpplist];

      open (IN, "<$bpplist") or croak "Can't open bpplist for read: $!\n";
      foreach my $box (<IN>) {
          chomp($box);

          open ETR, "<$etrfile" or croak "Can't open etrfile for read $!\n";
          open (PROBLEMS, ">>$local_problems") or croak "Can't open local_problems for write: $!\n";

          $p = Net::Ping->new("icmp");       #need icmp or fails to tra1 and tra2
          if ( $p->ping($box) ) {
                   #   bpp model       : ADVANTECH MIC-3390
                   #   bpp model       : FORCE COMPUTERS CPCI-736
                   $bpp = qx[remsh $box status |grep model];   
                   chomp $bpp;
                   my @BPParr = split(/\s+/,$bpp); 
                   $model = $BPParr[-1];

                   # J6741A 3 0030D30D2DC2 172.17.248.33 m006ilm1p3 acceSS7_ILM 1 
                   $etr = qx[grep $box $etrfile | grep J6741A];   
                   my @ETRarr = split(/\s+/,$etr); 
                   $option = $ETRarr[-1];  

                   if ( ($model =~ /CPCI-736/) && ($option !~ 1) ){
                         printf PROBLEMS  "%-15s %10s  %s  --option incorrect\n",$box, $model, $option ;
                   }
                   if ( ($model =~ /MIC-3390/) && ($option !~ 2) ){
                         printf PROBLEMS  "%-15s %10s  %s  --option incorrect\n",$box, $model, $option ;
                   }
           } else {
                 printf PROBLEMS  "$box  NOT pingable\n" ;
           }
           close (PROBLEMS);  #close write
           close (ETR);  #close write
           $p->close();
      }

 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 option issues -$today \n";
 if ( $badcount > 0 ){  # at least one problem
         #format and header
         printf JUNK1 "$header";
         qx[grep -v NOT $local_problems >> $local_junk1];
         qx[grep  NOT $local_problems >> $local_junk1];
         close(JUNK1);

         printf JUNK2 $header;
         close(JUNK2);
    } else {                         #no problems found
          #format and header
         printf JUNK2 $local_header;
         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("$bpplist");
   unlink("$local_junk1") || print $!;
   unlink("$local_junk2") || print $!;
  return($header, $local_problems, $local_details);
} #check

