#!/usr/bin/perl  

#ModulePurpose: Check EPP input data rates using TSD tool
#ModuleName:    check_epp_data_rates
#TimeRequired:  3 hours !

# todo:  
#  - run from cron
#  - send all to details
#  sort -rn on details

#Problems Report:
#  --- 1 EPP InputData rate > 5000000 bits/sec -08.21.2009 
#  m003     21/08/09 15:53:43 : EPP PORT 0 : 8251274 bits/sec 

#Details Report:
#  --- 0 EPP InputData rate > 42000000 bits/sec -08.21.2009 
#  m001     21/08/09 16:33:14 : EPP PORT 0 : 2698733 bits/sec 
#  m001     21/08/09 16:34:38 : EPP PORT 0 : 5352970 bits/sec 


 
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 epps to check
#configurable
   $eppScript = "eppstats.ksh";
   $epplist = "$TmpDir/epplist";
#  qx[/opt/platform7/lbin/list_processors -e |grep m003ilm4p8 > $epplist];
  qx[/opt/platform7/lbin/list_processors -e > $epplist];

### 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/:.';
} #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{    #(main program)
############################

     print "epp InputData rates \n";

     ($p, $box) =();
     $local_problems = "$TmpDir/epp_problems";
     $local_details  = "$TmpDir/epp_details";
     $local_junk1    = "$TmpDir/junk1";    # temp file for problems
     $local_junk2    = "$TmpDir/junk2";    # temp file for details

    $THRESHOLD = 42000000;  # EPPstats are in bits/sec  8,239,438 bits/sec

    # open local output files
    open PROBLEMS, ">>$local_problems" or croak "Can't open local_problems for write: $!\n";
    open DETAILS, ">>$local_details" or croak "Can't open local_details for write: $!\n";

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

    # pre-check
    # if TSD script is missing,
    if (! -e "misc_tools/$eppScript") {
         printf PROBLEMS  "$eppScript is missing\n";
     printf   "$eppScript is missing\n";
         break;
    } else {

         # check each epp 
         while (defined ($epp = <EPP>)) {
               chomp($epp);

               open PROBLEMS, ">>$local_problems" or croak "Can't open local_problems for write: $!\n";
               open DETAILS, ">>$local_details" or croak "Can't open local_details for write: $!\n";

               $p = Net::Ping->new("icmp");   #need icmp or fails to tra1 and tra2
               if ( $p->ping($epp) ) {  #pingable

                        if($epp =~ m/(\w\d{3})/ ){     #extract site name from epp
                           $site = $1;   
                        }   

                        qx[rcp -p misc_tools/$eppScript $site:/tmp/wm ];
                        qx[remsh $site "/tmp/wm/$eppScript $epp" > $local_junk1 ];
       
                        open JUNK1, "<$local_junk1" or croak "Can't open junk1 for read $!\n";
                        while (my $line = <JUNK1>) {
                              chomp $line;
                              next if ($line =~ /^$/);  #blank links 
                              $line =~ s/\s+/ /g,$line;
                              my @arr = split(/\s+/,$line);

# debug                        if ($arr[7] > 0 ){
#                                 printf DETAILS  "%-8s @arr ",$site;
#                                 print DETAILS "\n";
#                              }
                              if ($arr[7] > $THRESHOLD ){  # 8,239,438 bits/sec
                                 printf PROBLEMS "%-8s @arr ",$site;
                                 printf PROBLEMS "\n";
                              }
              } #end of JUNK1
         } else {
               printf PROBLEMS "    $10s NOT pingable\n",$box ;
               printf  "$10s NOT pingable\n",$box ;
        } #box is pingable
    } #eppScript exists
        $p->close();
  } #for each box in sitelist
   unlink("$local_junk1") || print $!;
   unlink("$local_junk2") || print $!;

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

    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 EPP InputData rate > $THRESHOLD bits/sec -$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;
             qx[cat  $local_details >> $local_junk2];
         close(JUNK2);
    }else {
         print JUNK2 $header;
             qx[cat  $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 $!;
   unlink("$epplist") || print $!;

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



