#!/usr/bin/perl 

#ModulePurpose: Grab latest AMC alarms and date
#               number of errors to check for is configurable (currently set to 4)
#ModuleName: checkfor_amc_alarms

# Takes a long time (45 min)to run

# Problems report
# m004ilm4p10  Check mgnt LANs 01 Jul 2009 15:53:34 
# m004int29p10 Fan 2 too slow 01 Jul 2009 15:58:25 
# m002ilm9p10  Card 5 voltage 08 Jul 2009 15:10:09 

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

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

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


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

###################
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 {
# grab latest alarms from each AMC 

     print "amc alarm check \n";

     my ($p, $site, $amc);
     my $local_problems = "$TmpDir/amc_problems";
     my $local_details  = "$TmpDir/amc_details";
     my $errors_log     = "$TmpDir/amcErrors"; # latest events for each AMC
     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 problems for write: $!\n";
    open DETAILS, ">>$local_details" or croak "Can't open file details for write: $!\n";

    open SITE, "<$sitelist" or croak "Can't open sitelist file for read $!\n";

    # check each site 
     while (defined ($site = <SITE>)) {
        chomp($site);
        $sitetime = qx[remsh $site "date"];  
        my ($sitedow, $sitemonth, $siteday, $sitetime, $sitetz, $siteyear) = split(/\s+/,$sitetime);  
        $siteday = sprintf("%02d", $siteday);   # zero fill to match AMC day

        #Timestamp from AMC: 15 May 2009 21:57:29
        #date: Tue Jun  9 02:27:25 GMT 2009 

        open AMC, "<$amclist" or die $! ;
            while ($amc = <AMC>) {
                 chomp($amc);

                 if ($amc =~ /$site/) {
                      $p = Net::Ping->new();      # don't use icmp here
                      if ( $p->ping($amc) ) {
                           open ERRORS, ">>$errors_log" or croak "Can't open amcErrors file for write $!\n";

                           # want to ignore (seen when AMC is rebooted)
                             # HW Alarms Not Detect 09 Jun 2009 17:40:15 - Current
                             # PPS Not Detected 09 Jun 2009 17:40:15 - Current
                             # Appn running 09 Jun 2009 17:56:58 - Current
                             # HW Alarms Not Detect 09 Jun 2009 17:57:13 - Current
                             # PPS Not Detected 09 Jun 2009 17:57:08 - Current
                             # Using last config 09 Jun 2009 17:57:03 - Current
                             # No link to card 2 09 Jun 2009 19:01:53 - Current

                           # eventlogEvLogAlarmText.499=Fan 6 failed
                           qx[remsh $site -n  "remsh $amc  'smc-cmd -e eventlogEvLogAlarmText'" | 
                                              grep -vE "User|End|Detect|Using|Appn|card" | tail -3 >> $errors_log ];
# configurable  tail -3 >  # number of errors to check for
                           close (ERRORS);  #close write

                           open ERRORS, "<$errors_log" or croak "Can't open amcErrors file for read $!\n";
                           while (my $error = <ERRORS>) {
                                 my $timestamp = "";    #reset for each error

                                 $error =~ s/[.|=]/ /g ; 
                                 my ($junk1, $eventnumb, @text) = split(/\s+/,$error);
                                 $timestamp = qx[remsh $site "remsh $amc  'smc-cmd -e  eventlogEvLogTimestamp.$eventnumb'" |
                                                              grep -v End |cut -d"." -f2 | cut -d"=" -f2];  

                                 chomp $timestamp;
                                 my $size = length $timestamp;

                                 if ($size == 0) {
                                     print PROBLEMS "   $amc  is Hosed\n";    #try rebooting AMC
                                 } else {
                                    #eventlogEvLogTimestamp.499=15 May 2009 21:57:29
                                    #Timestamp: 15 May 2009 21:57:29
                                    my ($errday, $errmonth, $erryear, $errtime) = split(/\s+/,$timestamp);

#    print "siteday:$siteday  errday:$errday   sitemonth:$sitemonth  errmonth:$errmonth\n";

                                    if ( ($siteday =~ /$errday/) && ($sitemonth =~ /$errmonth/) ){
#     print          "$site $amc  @text $timestamp\n";
                                          print PROBLEMS "   $amc  @text $timestamp \n";
                                          print          "   $amc  @text $timestamp \n";
# debug                                   print PROBLEMS "   $site $amc $eventnumb @text $timestamp\n";
                                    }else {
                                          print "$amc @text $timestamp  - Old \n";
#                                          print "$site $amc $eventnumb @text $timestamp - Old\n";
                                    }
                                } #could log into AMC (timestamp size was not 0)
                           } # process all error lines
                           close (ERRORS);  #close read

                    } else {
                      printf PROBLEMS  "   $amc  NOT pingable\n" ;
                    }
                    $p->close();
                    unlink("$errors_log")     || print $!;
                 } # amc exists at current site 
                 next;
             } #for each amc in amclist
           close(DETAILS);  
   } #foreach box in sitelist


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

    qx[sort -u $local_problems > $local_junk1];
    move($local_junk1,$local_problems) or die "junk1 cannot be copied";

    # 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 current AMC alarms or issues found -$today\n";

    if ( $badcount > 0 ){  # at least one problem
         #format and header 
         print JUNK1 $header;
             qx[grep -v ping $local_problems >> $local_junk1];
             qx[grep    ping $local_problems >> $local_junk1];
         close(JUNK1);

       print JUNK2 $header;
       close(JUNK2);  
    }

    #  Format local_details, if needed
       print JUNK2 $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";

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

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

