#!/usr/bin/perl  

#ModulePurpose: Check lan configurations for all CS, WST, RSP
#ModuleName:   check_lan_configs 
#TimeRequired:  1 min

#Problems Report:
#   --- 22 lan configs issues- 08.11.2009 
#   m001        speed:100  Full-Duplex  negotiation:Off
#   m002        speed:100  Full-Duplex  negotiation:Off
#   m003        speed:100  Full-Duplex  negotiation:Off

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
#configurable
  qx[list_processors -sw > $sitelist];
  qx[cat /etc/opt/OSSInstall/servers.conf >> $sitelist];
#  qx[list_processors -s | grep m014 > $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/:.';
} #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 {
  print "lanconfig check \n";

     my ($p, $box);
     my $local_problems = "$TmpDir/lan_problems";
     my $local_details  = "$TmpDir/lan_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 local_problems for write: $!\n";
    open DETAILS, ">>$local_details" or croak "Can't open local_details for write: $!\n";

    # check each box 
    open SITE, "<$sitelist" or croak "Can't open sitelist file for read $!\n";
    while (defined ($box = <SITE>)) {

        my ($speed, $duplex, $config) = ();  #zero out for each box 
        my @arr = (); 

        chomp($box);
        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 (firewall)
        if ( $p->ping($box) ) {  #pingable

            qx[remsh $box "/usr/sbin/lanadmin -x 0" > $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 lies 
                     $line =~ s/[=|.]/ /g,$line;  # remove . and =
                     $line =~ s/\s+/ /g,$line;
                     @arr = split(/\s+/,$line);
#  Speed = 100 Full-Duplex.
#  Autonegotiation = Off.
#       or
#  Current config  = 100 Full-Duplex MANUAL

                     if ($arr[0] eq "Speed"){
                          $speed = $arr[1];
                          $duplex = $arr[2];
                     }
                     elsif  ( $line =~ /Auto/){
                          $config = $arr[-1];
                     }
                     elsif ($arr[0] eq "Current"){
                          $speed = $arr[2];
                          $duplex = $arr[3];  
                          $config = $arr[-1];
                     }    
             } #after all of junk1 has been read

             if ( ($speed == 10) || ($duplex =~ /[H|h]alf/) || ($config =~ /[O|o]n/) ){
                   printf PROBLEMS  "     %-10s  speed:%3s  %-11s  negotiation:%s\n", $box, $speed, $duplex, $config ;
             } else {
                   printf DETAILS  "     %-10s  speed:%3s  %-11s  negotiation:%s\n", $box, $speed, $duplex, $config ;
             }

        } else {
               printf PROBLEMS "$10s NOT pingable\n",$box ;
               printf  "$10s NOT pingable\n",$box ;
        }
        $p->close();
        unlink("$local_junk1") || print $!;
  } #for each box in sitelist

    #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 lan configs issues -$today \n";

    if ( $badcount > 0 ){  # at least one problem
         #format and header
         print JUNK1 $header;
             print JUNK1 "    Should be 100 Full, Auto Off (switch settings:100FD Auto On)\n";
             qx[grep -v ping $local_problems >> $local_junk1];
             qx[grep    ping $local_problems >> $local_junk1];
         close(JUNK1);

         print JUNK2 $header;
             print JUNK2 "    Should be 100 Full, Auto Off (switch settings:100FD Auto On)\n";
             qx[grep -v ping $local_details >> $local_junk2];
         close(JUNK2);
    }else {
         print JUNK2 $header;
             qx[grep -v ping $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("$sitelist") || print $!;

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

