#!/usr/bin/perl  

#ModulePurpose: Find "abandonConfig with IOPP" messages for current day in ifpclogs
#ModuleName:    iopp_messages 
#TimeRequired:  1 minute 

# Problem Report:


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 {

   # format of log file
   #  Mar  9 04:08:02 m003ilm5p4 tMEB: cgf: HWConfigMsgHandler: IOPP verifyHardware returns ERROR
   #  Apr 12 04:59:17 m004ilm4p8 tMEB: cgf - abandonConfig with IOPP because of error code 3

   # for log file search
  # my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
    ($minute,$hour,$date,$year) = (localtime)[1,2,3,5];
    $year += 1900;

    @weekdays = (Sun,Mon,Tue,Wed,Thur,Fri,Sat);
    $day      = (localtime)[6];
    $today    = $weekdays[$day];

    @months = (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sept,Oct,Nov,Dec);
    $mon  = (localtime)[4];
    $month = $months[$mon];

#   $CurrentDay = sprintf "%s %s %d %02d",$weekday,$month,$date,$hour;  # zero fill hour, not date
   $CurrentDay = sprintf "%s %s",$month,$date;  

   # for header
   ($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 {
#########
# Look for IOPP messages in ifpc.log

  print "IOPP messages\n";

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

 #create list of boxes to check
# configurable
  qx[list_processors -s  > $sitelist];
#  qx[list_processors -s |grep m008 > $sitelist];

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

       open (DETAILS, ">>$local_details") or croak "Can't open local_details for write: $!\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) ) {
                qx[remsh $box "cd /var/opt/platform7/tmp; grep -ih iopp ifpclog.\*" > $local_junk1];   

                open JUNK1, "<$local_junk1" or croak "Can't open junk1 for read $!\n";
                while (my $line = <JUNK1>) {
                     next if ($line =~ /^$/);   # blank lines

#stopped
#                     if ( ($line =~ /IOPP/) && ($line =~ /$CurrentDay/) ) {
                     if ( $line =~ /IOPP/) {
                        printf PROBLEMS "   $line";
      printf "$line";
                     }
                }
       } else {
                 printf PROBLEMS  "$box  NOT pingable\n" ;
       }
       $p->close();
   } #foreach box
   close (PROBLEMS);  #close write
   unlink("$local_junk1") || print $!;

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

         printf JUNK2 $header;
         close(JUNK2);
    } else {             #no problems found
         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 $!;
   unlink("$sitelist") || print $!;

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

