#!/usr/bin/perl 

#ModulePurpose: Check for AMCs which have incorrect LAN settings
#ModuleName: amc_lan_check
#TimeRequired: 1 min

# Uses lanconfigs_errors file created by daily cronjob (which can take 3hours + to run)
# /h/bmetzger/s/amc/getAMClaninfo.sh

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::stat;
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'} = '/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 "AMC LAN check \n";

     # collected nightly from cron.
     my $amc_errorsfile = "/h/bmetzger/s/amc/lanconfigs_errors";

     my $local_problems = "$TmpDir/amc_problems";
     my $local_details  = "$TmpDir/amc_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 errors file is missing, or created > one days ago.....
   if ( (! -e $amc_errorsfile) || (-C $amc_errorsfile > 1) ) {
       #    $date_string = ctime(stat($amc_errorsfile)->mtime);   #find time file was modified 
         printf PROBLEMS  "   $amc_errorsfile missing or old\n";
   }
 
    $filesize = -s $amc_errorsfile;   #check the size
    if ($filesize > 10) {
         open IN, "<$amc_errorsfile" or croak "Can't open amc_errors file for read: $!\n";
         foreach my $line (<IN>) {
             printf PROBLEMS  "   $line";
         } 
    }
   close (IN);

    #gotta close, so lines can be counted
    close(DETAILS);  
    close(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 $badcount = line_count($local_problems);
    my $header = "\n--- $badcount AMC lan issues  -$today \n";

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

         #format and header 
         print JUNK1 $header;
         qx[cat $local_problems >> $local_junk1];
         close(JUNK1);
    }

   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

