#!/usr/bin/perl
###########################################################################
# Sun Proprietary/Confidential Code
#
# File:           verifyConfig_lib
#
# ident  "@(#)arrayConfigLib 1.4     05/05/06 SMI"
#
#
# Copyright 2004, Sun Microsystems, Inc. All rights reserved.
###########################################################################

sub getVars {

        $HOSTSFILE = "/etc/hosts";
        $DEBUG     = "TRUE" if ($ENV{'DEBUG'});
        $VERBOSE   = "TRUE" if ($ENV{'VERBOSE'});
        $LOGIN     = "root";
        $PASSWD    = "sun1"; 
        $DefaultCacheState = "writebehind";
        #$SYS_LOGFILE  = "/opt/se6320_conf/etc/syslog.conf";
        $SYS_LOGFILE  = "/opt/array_conf/etc/syslog.conf";
        $ARRAY_SYSLOG = "/etc/syslog.conf";

        %SYS = (
             blocksize   => "64k",
             cache       => "auto",
             mirror      => "auto",
             mp_support  => "mpxio",
             naca        => "off",
             rd_ahead    => "on",
             recon_rate  => "med",
             fc_topology => "auto",
             fc_speed    => "auto",
             disk_scrubber => "on",
             ondg          => "befit"
          );

         %SET = (
              bootmode   => "auto",
              bootdelay  => "3",
              netmask    => "255.255.255.0",
              gateway    => "192.168.0.1",
              tftphost   => "192.168.0.2",
              tftpfile   => "/var/crash/$name",
              hostname   => $name,
              loglevel   => "3",
              logto      => '*',
              rarp       => "on"
          );

        %NTP = (
              server     => "192.168.0.2",
              poll       => "unicast",
              interval   => "60"
          );  

}

sub openConnection {
undef(@responce);

my($array_name) = @_;

use Net::Telnet();
	$t = Net::Telnet->new(Timeout => 300,Prompt => '/\S*:\/:/',Errmode => "return");
        return(1) if(&checkcmdstatus("Telnet New"));

        $t->open(Host => $array_name);
        if ($t->errmsg()) {
            return 1;
        }

        return(1) if(&checkcmdstatus("telnet open"));
	$t->login($LOGIN,$PASSWD);
        if(&checkcmdstatus(Login)) 
        {
           print "ERROR : Login failed !!!\n";
           return(1);
        }
}

sub closeConnection {
undef(@responce);
        $t->close;
        return 0;
}

sub getArrayList {
        my $result = `grep array $HOSTSFILE 2>&1`;
        my (@list) = split(/\n/,$result);
        foreach my $line (@list) {
            next if ($line =~/^#/i);
            next if ($line =~/^\s+/i);
            next if ($line !~/^\d+/);
            my ($ip,$brickname) = split(' ',$line);
            $result = `/usr/sbin/ping $brickname 1 2>&1`;
            if ($result !~/alive/i)
              {
                print ("Warning : $brickname is offline \n"); 
              } else {
                push(@ARRAYLIST,$brickname);
              } 
        }
}

sub runCommand {
undef(@responce);

	local($command) = @_;
	(@responce) = $t->cmd($command);
        return(1) if(&checkcmdstatus($command));
        return 0;
}

sub setSysDefaults {

my($command);

     $command = "sys mirror off";
     return (1) if(&runCommand($command));

     foreach $key (sort keys %SYS) {
       next if($key =~ /blocksize/);
       if($key =~ /fc_speed/) {
          $command = "sys $key force"; 
          return (1) if(&runCommand($command));
       }
       $command = "sys $key $SYS{$key}";
       return (1) if(&runCommand($command));
     }
     return 0;
}

sub checkSysDefaults {
local $command = "sys list";
my $return = 0;

     return 1 if(&runCommand($command));

     foreach $key (sort keys %SYS) {
       next if($key =~ /fc_speed/);
       if(!grep(/$key\s+:\s+$SYS{$key}/,@responce)){
          print "Warning: System parameter \"$key\" is different from the default value \"$SYS{$key}\" \n";
          my (@currentValue) = grep(/$key\s+:\s+/,@responce);
          print "Current value : @currentValue \n";
          $return = 1;
       }  
     }
     return $return;
}

sub setSetDefaults {
my($command);

     foreach $key (sort keys %SET) {

          $command = "set hostname $name";
          return (1) if(&runCommand($command));

          $command = "set tftpfile /var/crash/$name";
          return (1) if(&runCommand($command));

          $command = "set $key $SET{$key}";
          return (1) if(&runCommand($command));
     }
     return 0;
}


sub checkSetDefaults {
local $command = "set";
$return = 0;
$SET{tftpfile} = "/var/crash/$name";

     return 1 if(&runCommand($command));

     foreach $key (sort keys %SET) {
       next if($key =~ /logto/);
       #next if($key =~ /tftpfile/);
       if(!grep(/$key\s+$SET{$key}/,@responce)){
          print "Warning: System setup  parameter \"$key\" is different from the default value $SET{$key} \n"; 
          my (@currentValue) = grep(/$key\s+/,@responce);
          print "Current value : @currentValue \n";
          $return = 1;
       } 
     }
     return $return;
}


sub checkcmdstatus {
my($cmd_str) = @_;

     $cmd_out = join(' ',@responce);
     if($msg = $t->errmsg()) 
     {
         print "$msg \n";
         return(1);
     }
     if(grep(/ERROR/i,@responce))
      { 
        $err_msg = join(' ',@responce);
        print "$err_msg \n";
        return 1;
      }
     return 0;
}

sub checkSyslog {

        $command = "ls -l etc";
        if(&runCommand($command))
        {
             print ("Error : Could not find etc directory on Array \n");
             return 1 ;
        }

        foreach $line (@responce) {
           if ($line =~ /syslog.conf\s+/) {
                 $syslog_found = "true";
                 last;
           }
        }

        if($syslog_found) {

           $command = "cat /etc/syslog.conf";
           &runCommand($command);
           open(temp, ">/tmp/syslog.$$");
           print temp @responce;
           close temp;
           $result = `diff /opt/se6320_conf/etc/syslog.conf /tmp/syslog.$$`;
           `rm /tmp/syslog.$$` if(-e "/tmp/syslog.$$");
        } else { 
           print ("WARNING : Array syslog.conf file not found, can not check syslog.conf file \n"); 
           return 1;
        }

        if ($result) {
           print ("WARNING : Array syslog.conf file different from /opt/se6320_conf/etc/syslog.conf file \n");
           return 1;
         }
        return 0;
}


sub checkVolExists {
my(@result);
my $return = 0;

        my($command);
        $command = "vol mode";
        &runCommand($command);
        return 1 if(&checkcmdstatus("$command"));

         (@result) = @responce;

         if($JbodFound) { 
            if(grep(/^tray0_pool1/,@result)) {
               #print "Found volume tray0_pool1 \n";
            } else {
               print "Warning : Volume tray0_pool1 could not found \n";
               $return = 0;
            }

            if(grep(/^tray1_pool1/,@result)) {
              # print "Found volume tray1_pool1 \n";
            } else {
               print "Warning : Volume tray1_pool1 could not found \n";
               $return = 0;
            }

            if(grep(/^tray2_pool1/,@result)) {
              # print "Found volume tray2_pool1 \n";
            } else {
               print "Warning : Volume tray2_pool1 could not found \n";
               $return = 0;
            }

         } else {
            if(grep(/^tray0_pool1/,@result)) {
              # print "Found volume tray0_pool1 \n";
            } else {
               print "Warning : Volume tray0_pool1 could not found \n";
               $return = 0;
            }

            if(grep(/^tray1_pool1/,@result)) {
              # print "Found volume tray1_pool1 \n";
            } else {
               print "Warning : Volume tray1_pool1 could not found \n";
               $return = 0;
            }
         }
        return $return;
}

sub checkVolStatus {
my(@result);
my $return = 0;
        my($command);
        $command = "vol mode";
        &runCommand($command);

       (@result) = @responce;

    foreach $line (@result) {
         next if($line !~ /^tray\S+/);

         if($JbodFound) {
            $volstring = "tray[0123]_pool[12]";
         } else {
            $volstring = "tray[01]_pool[12]";
         }
            if($line =~/$volstring/){
                ($lun_found,$mntstatus,$cachestatus,$mirror_status) = split(/\s+/,$line);
                if ($mntstatus =~/yes/) {
                   # print "volume $lun_found is mounted \n";
                } else {
                    print "Warning : volume $lun_found is not mounted \n";
                    $return = 0;
                }

                if ($cachestatus =~/writebehind/) {
                   # print "volume cache status $cachestatus \n";
                } else {
                    print "Warning : volume cache status  is not properly set \n";
                    $return = 0;
                }

            }
    }
        return 1 if(&checkcmdstatus("$command"));
        return $return;
}

sub checkFruStatus {
my  $command = "fru stat";
my $return = 0;

    return 1 if (&runCommand($command));

    foreach $line (@responce) {
      chomp($line);
      next if($line !~ /^u\d+d\d+|^u\d+ctr|^u\d+l\d+/);
           ($fru,$status,$state) = split(/\s+/,$line);
           $JbodFound = 1 if($fru =~/u3ctrl/);
           next if($status eq ready) && ($state eq enabled);
           print "Warning : $fru status not ready/enabled \n";
           $return = 1;
      }
    return $return;
}

sub checkNtpSetting {
my $command = "ntp";
my $return = 0;
    return 1 if (&runCommand($command));

     foreach $key (sort keys %NTP) {
       if(!grep(/$key\s+$NTP{$key}/,@responce)){
          print "Warning: NTP parameter \"$key\" is different from the default value \"$NTP{$key}\" \n";
          @currentValue = grep(/$key\s+/,@responce);
          print "Current value : @currentValue \n";
          $return = 1; 
       }
     }
      return $return;
}

sub setNtpDefaults {
my($command);

     foreach $key (sort keys %NTP) {
          $command = "set $key $NTP{$key}";
          return (1) if(&runCommand($command));
     }
     return 0;
}

sub copySyslog
{
  my $ftp;

use Net::FTP;

  if(-e "$SYS_LOGFILE")  {

      unless ($ftp = Net::FTP->new($name)) {
         return 1;
      }

      unless ($ftp->login($LOGIN, $PASSWD)) {
         return 1;
      }

      unless ($ftp->put($SYS_LOGFILE, $ARRAY_SYSLOG)) {
         return 1;
      }
      $ftp->quit;
  } else {
    print "Warning : $SYS_LOGFILE not found , can not copy to array \n";
    return 1;
  }

  return 0;
}

###############################
sub createVolumes {
%VOLUMES = (
             tray0_pool1 => "u1d1-7",
             tray0_pool2 => "u1d8-13",
             tray1_pool1 => "u2d1-7",
             tray1_pool2 => "u2d8-13",
          );
my($command);

    $command = "global_standby list";
    return (1) if(&runCommand($command));

    @result = @responce;
    foreach $line (@result) {
         next if($line =~/^\s+/);
         next if($line =~/^Global/);
         my($drive,$rest) = split(/\s+/,$line);
            if($drive =~ /u\d+d\d+/) {
                $command = "global_standby remove $drive";
                return (1) if(&runCommand($command));
            }
    }

                $command = "global_standby add u1d14,u2d14";
                return (1) if(&runCommand($command));

                foreach $key (sort keys %VOLUMES) {
                    $command = "vol add $key data $VOLUMES{$key} raid 5";
                    return (1) if(&runCommand($command));
                } 
                $VOL_INIT_TIMEOUT = "36000";
                $cmd="vol init tray0_pool1,tray0_pool2,tray1_pool1,tray1_pool2 parallel";
                (@responce) = $t->cmd(String => "vol init tray0_pool1,tray0_pool2,tray1_pool1,tray1_pool2 parallel",
                                Prompt => '/\[N\]:/');
                #return (1) if(&runCommand($command));
                $cmd="y";
                print "$cmd \n";
                (@responce) = $t->cmd(String => "y",Prompt => '/\S*:\/:/',
                                 Timeout => 36000
                                );
                #return (1) if(&runCommand($command));

                $checkvolint = &checkProcList;

           while($checkvolint == -1) {
                     sleep(300);
                     $checkvolint = &checkProcList;
           } 

                foreach $key (sort keys %VOLUMES) {
                    $command = "vol mount $key";
                    return (1) if(&runCommand($command));
                }

}

sub checkProcList {

# retun values -1 if vol init is going on
# return 1 if error while executing the command
# return 0 no val list is running

      $command = "proc list";
      return (1) if(&runCommand($command));

      foreach $line (@responce)
       {

         next if($line =~/^\s/);
         next if($line =~/VOLUME\s+CMD_REF/);
         return (-1) if ($line =~/vol init/);
       }
      return 0;
}


sub deleteVolumes {
my($vol) = @_;
my($command);

    $command = "vol mode";
    return (1) if(&runCommand($command));
    my(@result) = @responce ;
    foreach $line (@result) {
         next if($line =~/^\s+/);
         next if($line =~/^volume/);
         my($lun_found,$mntstatus,$cachestatus,$mirror_status) = split(/\s+/,$line);
            if ($mntstatus =~/yes/) {
                $command = "vol unmount $lun_found";
                return (1) if(&runCommand($command));
            }
             $command = "vol -y remove $lun_found";
             return (1) if(&runCommand($command));
    }
    return 0;
}

sub createSlices {
my($command);

    $command = "vol list";
    return (1) if(&runCommand($command));

    my(@result) = @responce ;

    foreach $line (@result) {
         next if($line =~/^\s+/);
         next if($line =~/^volume/);
         ($lun_found,$capacity,$units,$rest) = split(/\s+/,$line);
         ($capacity,$decimal) = split(/\./,$capacity);

         $command = "volslice create v".$lun_found." -z ".$capacity."gb ".$lun_found;
         return (1) if(&runCommand($command));
    }

    $command = "lun perm all_lun rw all_wwn";
    return (1) if(&runCommand($command));
    return 0;
}
1;
