#!/usr/bin/perl  

#ModulePurpose: Check hardware mirrors on CS and BuCS
#ModuleName:    check_hardware_mirrors
#TimeRequired:  1 minutes

# reference:  http://docs.hp.com/en/J6369-90026/ch05s02.html

#Problems Report:
# --- 4 hardware mirror issues -08.17.2009 
#     m7server  /dev/ciss5 Auto-Fail is disabled 
#     m7server  /dev/ciss5 RAID not configured 
#     m7altsrv  /dev/ciss5 Auto-Fail is disabled 
#     m7altsrv  /dev/ciss5 RAID not configu
 
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[cat /etc/opt/OSSInstall/servers.conf > $sitelist];
#  qx[cat /etc/opt/OSSInstall/servers.conf | grep m7server > $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{    #(main program)
############################

     print "hardware mirror status \n";

     ($p, $box) =();
     $local_problems = "$TmpDir/mirror_problems";
     $local_details  = "$TmpDir/mirror_details";
     $local_junk1    = "$TmpDir/junk1";    # temp file for problems
     $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";

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

    # check each box 
    while (defined ($box = <CS>)) {
          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
          if ( $p->ping($box) ) {  #pingable
                  qx[remsh $box "/usr/sbin/ioscan -kfn | grep RAID" > $local_junk1 ];
       
                  open JUNK1, "<$local_junk1" or croak "Can't open junk1 for read $!\n";

#ext_bus     4  0/4/1/0/4/0    ciss      CLAIMED     INTERFACE    PCI-X SmartArray 6404 RAID Controller
#ext_bus     5  0/4/1/0/5/0    ciss      CLAIMED     INTERFACE    PCI-X SmartArray 6404 RAID Controller 

                  while (my $line = <JUNK1>) {
                        chomp $line;
                        next if ($line =~ /^$/);  #blank links 
                        $line =~ s/\s+/ /g,$line;
                        my @arr = split(/\s+/,$line);
                        my $devicefile = "/dev/" . "$arr[3]" . "$arr[1]"; #join to make /dev/ciss4

                        qx[remsh $box "/usr/sbin/saconfig $devicefile" > $local_junk2 ];
                        open JUNK2, "<$local_junk2" or croak "Can't open junk2 for read $!\n";

                        while (my $line2 = <JUNK2>) {
                             chomp $line2;
                             my ($quota, $status, $configured) = ""; # reset for each line
                             my @arr = (); 

# Auto-Fail Missing Disks at Boot     = enabled
# Auto-Fail Missing Disks at Boot     = disabled
#  External       1    0    72.8 GB        OK
#  External       1    1    72.8 GB        OK
# No physical drive detected

                            next if ($line2 =~ /^$/);  #blank links
                            $line2 =~ s/\s+/ /g,$line2;
                            @arr = split(/\s+/,$line2);
                            if ( ($line2 =~ /^Auto-Fail/) && (lc(@arr[-1]) ne "enabled") ) {
                                       printf PROBLEMS "   $box  $devicefile Auto-Fail is $arr[-1] \n";
             printf "   $box  $devicefile Auto-Fail is $arr[-1] \n";
                            } 

                            if ( $line2 =~ /No physical drive detected/) {
                                       $configured = "no";
                                  printf PROBLEMS "   $box  $devicefile RAID not configured \n";
             printf "   $box  $devicefile RAID not configured \n";
                            } 

                            # assume it has disk
                            if ( $line2 =~ /^External/) {
                                 $id = @arr[2];
                                 $status = @arr[-1];
                                 if ( $status ne "OK" ) {
                                     printf "   $box  $devicefile id:$id External Disk status: $arr[-1] \n";
                                 }
                            }
                  } #end of reading JUNK2
              } #end of JUNK1
         } else {
               printf PROBLEMS "    $10s NOT pingable\n",$box ;
               printf  "$10s NOT pingable\n",$box ;
        } #box is pingable
        $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 hardware mirror issues -$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);
    }else {
         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 $!;

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



