#!/usr/bin/perl 

#ModulePurpose: Check for elemeents which are OOCs 
#ModuleName: comms_check
#TimeRequired: 1 min

#Problems Report: - standard p7cs
#  Sites        :   10 total,   10 in comms (100.0%),    0 not in comms (  0.0%)
#  AMCs         :  134 total,  134 in comms (100.0%),    0 not in comms (  0.0%)
#  BPPs         :  886 total,  883 in comms ( 99.7%),    3 not in comms (  0.3%)
#  EPPs         :  130 total,  130 in comms (100.0%),    0 not in comms (  0.0%)
#  Wrkstn/AppSvr:   10 total,   10 in comms (100.0%),    0 not in comms (  0.0%)
 



#Uses /usr/contrib/bin/p7cs  which calls /usr/contrib/bin/p7commsstatus 

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) = InitVars();

  $SSH_OPTIONS=" -o BatchMode=yes -o ConnectTimeout=2";
  $ENV{'PATH'} = 'misc_tools:/usr/bin:/usr/local/bin:/usr/sbin/:/usr/ucb:/etc/:/opt/platform7/lbin/:.';
  &initvars();

  #create list of boxes to check
  qx[/opt/platform7/lbin/list_processors -w > $sitelist];

### 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

   $SSH_OPTIONS=" -o BatchMode=yes -o ConnectTimeout=2";
   $ENV{'PATH'} = '/usr/bin:/usr/local/bin:/usr/sbin/:/usr/ucb:/etc/:/opt/platform7/lbin/:.';
} #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 {
# check for elements which are OOC 

     print "comms check \n";

     my $commsfile = "/var/opt/platform7/tmp/eng_logging_C32_0";
     my $local_problems = "$TmpDir/comms_problems";
     my $local_details  = "$TmpDir/comms_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 file $local_problems for write: $!\n";
    open DETAILS, ">>$local_details" or croak "Can't open file $local_details for write: $!\n";

 # if file exists, and owned by root, dumpstatus will fail.
   if (-e $commsfile) { unlink("$commsfile") };
 
   qx[/usr/contrib/bin/p7cs  > $local_details];
   open COMMS, "<$commsfile" or croak "Can't open C32 for read: $!\n";
   my $badcount = 0;
   while (<COMMS>) {$badcount++ if (/not/) }; 

    #gotta close, so lines can be counted
    close(DETAILS);  
    close(PROBLEMS); 

    my $detailcount = line_count($local_details);
    my $box_count = scalar(@sitelist);

    # 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 elements are out of comms\n";

    if ( $badcount > 0 ){  # at least one problem

         #format and header 
         print JUNK1 $header;
         qx[grep not $commsfile >> $local_junk1];
         close(JUNK1);
    }

    #  Format local_details, if needed
       print JUNK2 $header;
       system("grep -v status $local_details >> $local_junk2");
       close(JUNK2);  

   copy($local_junk1,$local_problems) or die "junk1 cannot be copied";
   copy($local_junk2,$local_details) or die "junk2 cannot be copied";

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

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

