#!/usr/bin/sh
LD_LIBRARY_PATH=/opt/SUNWrasag/lib/so:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH

/opt/SUNWrasag/perl/perl5 -I/opt/SUNWrasag/lib<<'EODATA' 
#
#  RAS Agent
#
use Net::Telnet();
use Net::Ping;
use ConfigFile;
require "ras_telnet.pl";
require "sdt.pl";

$HOME   = "/opt/SUNWrasag";
$PROMPT = '/\w*:\/:\<\d+\>/';

($renv, $devices) = ConfigFile->read("$HOME/rasagent.conf");
print "\n    Starting to read Configuration file!\n\n";

$cnt  = 0;
$fail = 0;

$ok = 0;
if (open(O,">$HOME/data/test")) {
  print O "test"; close(O);
  if (open(O, "$HOME/data/test")) {
    $l = <O>;
    $ok = 1 if ($l eq "test");
    close(O);
  }
} 
if (!$ok) {
  print "-- Error: Cannot write to $HOME/data/test: $! \n";
  $fail++;
}
  

if (!-x $renv->{mailer}) {
  print "-- Error: Invalid mailer $renv->{mailer}: Cannot execute!\n";
  $fail++;
}

if ($renv->{email} && $renv->{email} !~ /.*\@.*/) {
  print "-- Error: Invalid email address $renv->{email} \n";
  $fail++;
}

if ($renv->{error_email} && $renv->{error_email} !~ /.*\@.*/) {
  print "-- Error: Invalid email address $renv->{error_email} \n";
  $fail++;
}

if ($renv->{admin_email}) {
  @emails = split(/ *, */, $renv->{admin_email});
  foreach $em (@emails) {
    if (index($em, ":") >= 0) {
       ($e, $opt) = split(/ *: */, $em);
    } else {
        $e = $em; $opt = "lctp";
    }
    if ($e !~ /.*\@.*/) {
       print "-- Error: Invalid admin_email address $e \n";
       $fail++;
    }
    if ($opt !~ /^[lctp]*$/) {
       print "-- Error: Invalid admin_email options ($opt) for $e \n";
       $fail++;
    }
  }
}

open(O,"/opt/SUNWrasag/bin/disk_inquiry 2>/dev/null|");
$st = "";
$disk_inq_flag = 0;
while ($l = <O>) {
   $l =~ s/\0//g;
   next if ($l =~ /^ *$/);
   $st .= $l;
   next if ($l =~ /\tVendor\t/ || $l =~ / SCSI /);
   $disk_inq_flag++;
   chop($l);
   @v = split(/ *\t */, $l);
   $serial = &rtrim(&ltrim($v[3]));
   $SERIAL{$serial} = 1;
}
if (!$disk_inq_flag) {
   print "Could not find any storage with 'disk_inquiry'\n"; 
} else {
   $c = 0;
   foreach $k (keys %SERIAL) {
     $c++;
   }
   print "Found '$disk_inq_flag' volumes and '$c' distinct storage devices with 'disk_inquiry'\n";
}
close(O);


foreach $device (@$devices) {
   $cnt++;
   $ip = $device->{ip} || "N/A";
   $name = $device->{name} || "N/A";
   print "\n--> Device$cnt: Reading $device->{type} $device->{_name}: ip= $ip , name= $name \n";

   if ($device->{serial} && $disk_inq_flag) {
      $found = 0; $pat = "";
      $pat = "0*" if (substr($device->{serial},0,1) eq "0");
      foreach $x (keys %SERIAL) {
         if ($x =~ /^$pat$device->{serial}/) {
            $found = 1;
            last;
         }
      }
      if (!$found) {
         print "     -- Warning: Could not find device with serial# $device->{serial} with 'disk_inquiry'.\n";
      }
   }
   if (lc(substr($device->{_name},0,6)) ne "device") {
     $fail++;
     print "     -- Error: The section names in between brackets '[ ]' ".
           "must start with the string 'device', it is '$device->{_name}'.\n";
   }
   
   if ($FOUND{$device->{_name}}) {
     $fail++;
     print "     -- Error: The section names in between brackets '[ ]' must be unique, ".
           "$device->{_name} is twice.\n";
   } else {
     $FOUND{$device->{_name}} = 1;
   }

   if (uc(substr($device->{type},0,2)) eq "T3") {
      if ($device->{ip}) {
        if (&ping($device->{ip}, $PROMPT, 4)) {
          print "     -- t300 $device->{_name} is reachable.\n";
        } else {
          print "     -- Error: $device->{_name} is NOT reachable after 4 seconds!\n";
          $fail++;
        }
      } else {
        err("     -- T300 IP not found for $device->{_name} in config file!");
      }
   }
   foreach $f1 (keys %$device) {
      if (substr(uc($f1),0,3) eq "SDT") {
         $f2 = $device->{$f1};
         if ($f2 !~ /^ *$/) {
           ($err, $in_range) = &sdt_format($f2);
           if ($err) {
              print "    -- Error: Problem with format of $f1=$f2: $err \n";
              $fail++;
           }
         }
      }
      if (uc(substr($f1, 0, 10)) eq "LUN_DEVICE") {
        $f2 = $device->{$f1};
        open(SYS, $f2);
        $len = sysread(SYS, $bytes, 1);
        close(SYS);
        if (!$len) {
          $fail++;
          @stats = stat($f2);
          $uid_name = getpwuid($stats[4]);                               # uid
          $gid_name = getgrgid($stats[5]);                               # gid
          @time  = localtime($stats[9]); $time[4]++; $time[5] += 1900;   # mtime
          $f3 = "";
          if (-l $f2) {
             $f3 = "( ". readlink($f2) . " )";
          }
          print <<EOF;
     -- Error: Problem reading data-$f1: $f2 $f3
          uid: $uid_name/$stats[4], gid: $gid_name/$stats[5], last-mod: $time[4]/$time[3]/$time[5]
EOF
          
        }
      }
   }

}
print "\n\n";
if ($fail) {
  print "    ****** $fail test(s) failed! ******\n";
} else {
  print "  ****** Test completed: no errors ******\n";
}

   
sub ltrim {
  my($s) = @_;

  $s =~ /^( *)/;
  if ($1) {
    substr($s,length($1));
  } else {
    $s;
  }
}

sub rtrim {
  my($s) = @_;

  $s =~ /( *)$/;
  if ($1) {
    substr($s,0,0 - length($1));
  } else {
    $s;
  }
}




EODATA

