#!/opt/SUNWstade/bin/perl -I/opt/SUNWstade/lib

use strict;
use Getopt::Std;
use System;
use Util;

my $local_p = $INC[0];
my($ix) = rindex($local_p, "/");
my $HOME = substr($local_p,0,$ix);
System->set_home($HOME);

my($renv, $devices, $hosts, $notifs) = PDM::ConfigFile->read();

my($sve_path) = $renv->{GSV_SVE_PKGDIR};

if ($sve_path = 'undef') {
   $sve_path = '/opt/svengine';
}


$|=1;
my($ps)   =  System->find_command("ps");
my($grep) =  System->find_command("grep");
my $Error;
my $OffLineQuestion = "WARNING - Volume data will be offline ". 
                      "while ve_diag is running.\n Continue ? [N]: ";
my %Options;
my $ok = getopts('fvo:', \%Options);
my $numargs=keys(%Options);


my %ve_diagParameters=undef;
@ve_diagParameters{"dev","target","sddiag","sudiag"}
			= (undef,undef,"enable","enable");


my @tmpParams = split(/\|/,$Options{"o"});
#loop through params
foreach my $i (@tmpParams){
  (my $key, my $value) = split(/=/,$i);

  my $match=undef;
  foreach my $validOption (keys(%ve_diagParameters)){
    if ($key eq $validOption){
      $match = $validOption;
    }
  }
#if the key does not match a valid option then the match does not get difined
  if(! (defined($match))){
    print "$key is an invalid option.\n".
          "ve_diag did not execute.\n";
    exit(1);
  } else  { #finally stick the value in the hash
      $ve_diagParameters{$key}=$value;
  }
}

foreach my $option (keys(%ve_diagParameters)){
  next if (!($option));
  if(!defined($ve_diagParameters{$option})){
    print "$option is not specified.\n";
    printUsage();
    exit (1);
  }
}


if(!defined($ve_diagParameters{dev})){ 
  printUsage();
  exit(1);
}
if((!defined($ve_diagParameters{target})) 
			&& ($ve_diagParameters{sddiag} =~ /enable/i)){
  print "Must specify target for sddiag to run.\n";
  printUsage();
  exit(1);
}

if($ve_diagParameters{dev} ne "v1" && $ve_diagParameters{dev} ne "v2"){
  print "$ve_diagParameters{dev} is an invalid SignOn Path for VE's in SE 6910's and SE 6960's\n";
  print "This does not apear to be a supported configuration of VE's.\n";
  print "This diagnostic can only run properly on VE's in SE 6910 and ";
  print "SE 6960 configurations.\n";
  exit(-1);
}

#if($ve_diagParameters{initiator} !~ /I0{0,5}1|I0{0,5}2/){
#  print "$ve_diagParameters{initiator} is an invalid initiator for VE's in SE 6910's and SE 6960's\n";
#  print "This does not apear to be a supported configuration of VE's.\n";
#  print "This diagnostic can only run properly on VE's in SE 6910 and ";
#  print "SE 6960 configurations.\n";
#  exit(-1);
#}

if($Options{"v"}){
  print "checking slicd\n";
}
open(O, "$ps -ef | $grep \"slicd\" | $grep  -v grep|");
my @l = <O>; close(O);
$ps = "@l";
if($Options{"v"}){
  print @l;
}
if($ps){
  print "slicd is running, continuing.\n";
}else{
  print "slicd is not running, unable to continue!\n";
  print "Start the Slic daemon and re-run this test.\n";
  print "Consult the SV Router Installation and Users Guide for instructions on ";
  print "how to start the Slicd Daemon.\n"; 
  exit(-1);
    
}

my @vicoms;

foreach my $dev (@$devices) {
   if ($dev->{type} eq "ve") {
     push(@vicoms, $dev);
     if($Options{"v"}){
       print "pinging $dev->{name}\n";
     }
     my $active = Util->ping($dev->{ipno});
     if (!$active) {
       print "Error: Unable to ping $dev->{name}\n";
       print "Reccomended Action:\n";
       print "\tVerify all VE's are powered on.\n";
       print "\tVerify Ethernet connectivity. \n";
       print "\t\tCheck Ethernet cables.\n";
       print "\t\tCheck Ethernet Hub in rack.\n";
       exit(1);
     }
  }
}
if(@vicoms == 0){
  print "Warning: No VE's found in Config file. \n";
  print "Reccomended Action:\n";
  print "\tVerify vicom discovery in maitain devices section of StorAde.\n";
  print "\tVerify vicom discovery has been  pushed to slave in maintain devices section of StorADE\n";
  exit(1);
}


if($ve_diagParameters{sddiag} =~ /enable/i){
  my $sd_command;
  if( -x "$sve_path/sduc/sddiag"){ $sd_command="$sve_path/sduc/sddiag";}
  else{ $Error .= "Cannot execute $sve_path/sduc/sddiag\n";}
  
  if(defined( $sd_command)){
    my $command = $sd_command." -d ".$ve_diagParameters{dev}." -t ".$ve_diagParameters{target};
    if($Options{"v"}){
      print "running sddiag:\n";
    }
    my @results=`$command`;
    my $result = $?;
    print $result . "\n";
    foreach my $line (@results){
      if($line =~ /fail/i){
        $result = -1;
      }
      print $line . "\n";
    }
  
    if($result != 0){
      $Error .= "sddiag failed\n";
    }
    if($Options{"v"}){
      print "sddiag completed\n";
    }
    my @results=`$command`;
  }
}

#this Code has been taken out due to possible Data Couptions issues
#if($ve_diagParameters{sudiag} =~ /enable/i){
  
#  my $su_command;
#  if( -x "$sve_path/sduc/sudiag"){ $su_command="$sve_path/sduc/sudiag";}
#  else{ $Error .= "cannot $sve_path/sduc/sudiag\n";}

#  if(defined( $su_command)){
#    if($Options{"v"}){
#      print "running sudiag\n";
#    }
#    my $result = system($su_command." -d ".$ve_diagParameters{dev});
#    if($result != 0){
#      $Error .= "sudiag failed\n";
#    }
#  }
#}
if(defined($Error)){
  print $Error;
  print "Recommended Action:\n";
  print "\tVerify all VE's are powered on.\n";
  print "\tVerify slicd is running correctly\n";
  print "\t\t(see SV's Instalation and Users Guide).\n";
  print "\tVerify fiber cables are connected securely to VE's and \n";
  print "fibrechannel switches.\n";
  exit(-1);
}else{
  print "vediag did not detect any errors. \n";
  exit(0);
}

sub printUsage{
  print "Usage: vediag -o dev= [login]|target=[target]|sddiag=[enable|disable]\n";
}
