#!/usr/bin/perl  

# Purpose: 
#      box: App Server 
#   action: Grab onErr from latest sessmgr.log for current hour 
#   result: Email onErr lines found to agilent.

use Carp;
use Net::FTP;

&initvars();

#######
sub initvars{

chomp($hostname = qx[hostname]);

   # from log file:
   # Tue Aug 11 20:54:31 2009 
   # Fri Aug 7 15:13:11
   # Sat Aug 8 00:09:56 2009 

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

   $CurrentHour = sprintf "%s %s %d %02d",$weekday,$month,$date,$hour;  # zero fill hour, not date
#print "time:$CurrentHour\n";

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

#########
# main

    my $PROBLEMS = "/tmp/QueryFeed.Error.problems";

    @files = </opt/AgilentNGN/log/sessmgr.log*>; 
    @logs = reverse sort @files;      #most current now lastA

     foreach $log (@logs) {

         open IN, "<$log" or croak "Can't open file $log for read: $!\n";
         open OUT, ">>$PROBLEMS" or croak "Can't open file QueryFeed.Error.problems for write: $!\n";

         while (defined ($line = <IN>)) {

             if ( ($line =~ /$CurrentHour/) && ($line =~ /QueryFeed.onError/) ){
                 print OUT $line; 
#debug                 print  $line; 
             }
         }
         close(IN);
         close(OUT);
   }# foreach log

### summarize and mail

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

   my $badcount = line_count($PROBLEMS);

    if ( $badcount > 0 ){  # at least one problem
         open IN, "<$PROBLEMS" or croak "Can't open file results for read: $!\n";

#         open(MAIL, "| /usr/bin/mailx -s '$hostname:  $badcount QueryFeed.onErrors found: - $HeaderTime' bill_metzger\@agilent.com  ") || die "mailx failed: $!\n ";
         open(MAIL, "| /usr/bin/mailx -s '$hostname:  $badcount QueryFeed.onErrors found: - $HeaderTime' mike_duboise\@agilent.com  ") || die "mailx failed: $!\n ";
         while (<IN>) {
               print MAIL ;
         }
         print MAIL " ";
         close(MAIL);
         close(IN);
    }else {   #no problems found

        open IN, "<$PROBLEMS" or croak "Can't open file results for read: $!\n";

         open(MAIL, "| /usr/bin/mailx -s '$hostname:  $badcount QueryFeed.onErrors found: - $HeaderTime' mike_duboise\@agilent.com  ") || die "mailx failed: $!\n ";
#        open(MAIL, "| /usr/bin/mailx -s '$hostname:  $badcount QueryFeed.onErrors found: - $HeaderTime' bill_metzger\@agilent.com  ") || die "mailx failed: $!\n ";
         while (<IN>) {
               print MAIL ;
         }
         print MAIL "no problems found ";
         print MAIL " ";
         close(MAIL);
         close(IN);
    }

#cleanup
unlink("$PROBLEMS");


