

#======================================================
# SCMS-SM VCS agents
# SubscriberManager agent - offline
# Created: Aug 2004
#======================================================
# Developed to start the SM resource
# the scripts runs the following algorithm:
# - checks if the SM process is up
# - if YES it checks if the SM is running
# - if no it restarts the SM
#======================================================
# Modification list:
# 5/8/2004: created
#
#======================================================
# Messages Used:
# MSGID               Message
#------------------------------------------------------
# 37042: SubscriberManager:% s: open: invalid path to the SM '$SmBinPathname'
# 37043: SubscriberManager:% s: open: parameters: $SmBinPathname, $State, $DebugLevel
# 37044: SubscriberManager:% s: open: the SM is not up ...
# 37045: 
#======================================================

eval 'exec $VCS_HOME/bin/perl5 -S $0 ${1+"$@"}'
        if 0;
($ResID, $SmBinPathname, $State, $DebugLevel) = @ARGV;

use SmOperations;

#
# Set the OS locale
#
$ENV{"LANG"}="C";
$vcs_home =$ENV{"VCS_HOME"} || "/opt/VRTSvcs";
my ( $log_message );

$SmBinPathname = cleanString($SmBinPathname);

use ag_i18n_inc;
VCSAG_SET_ENVS ("SubscriberManager", "open",$category_id);

if (($SmBinPathname eq "") || ( ! -d $SmBinPathname )) {
   VCSAG_LOG_MSG("E"," invalid path to the SM '$SmBinPathname'",37042,"$SmBinPathname");
   exit 1;
}

if ($DebugLevel > 0) 
{
   VCSAG_LOG_MSG("I"," parameters: $SmBinPathname, $State, $DebugLevel",37043,"$SmBinPathname","$State", "$DebugLevel");
}

sub moveToVcsState {

   # get the state the VCS thinks the SM is in
   my $myState =  $State;

   # if not mapped => check the State parameter to check the wanted state
   if (($myState == 0) ||
       ($myState == 1) ||
       ($myState == 4) ||
       ($myState == 5))
   {
      # offline
      $vcsState = "standby";
   }
   elsif (($myState == 2) ||
         ($myState == 3))
   {
      #online
      $vcsState = "active";
   }
   else
   {
      return;
   }


   %args1 = ("Operation" => "su - pcube -c \"$SmBinPathname/p3cluster --$vcsState\"" ,
             "DontReturnResult" => "1",
             "DebugLevel" => $DebugLevel,
             "ResID" => $ResID,
             "maxRetries" => "5");
   
   runOperation(\%args1);

}

# these paraeters define how long and in what rate the SM is polled
$startUpTimeout = 30;
$initialSleep   = 5;
$pollInterval   = 5;

# if the process is running 
if (smProcessExists() > 0) 
{
   %args = ("Timeout" => ($startUpTimeout - $initialSleep),
            "PollInterval" => $pollInterval,
            "SmBinPathname" => $SmBinPathname,
            "DebugLevel" => $DebugLevel,
            "ResID" => $ResID);
   # SM Process was run
   if (waitForSmUp(\%args) > 0) 
   {
      # SM is up and running
      moveToVcsState();
      exit 0;
   }
   else
   {
      # the operation timed-out
      if ($DebugLevel > 0)
      {
         VCSAG_LOG_MSG("C"," the SM is not up ...",37044);
      }
      exit 1;
   }
}
else
{
   %args = ("Timeout" => ($startUpTimeout - $initialSleep),
            "PollInterval" => $pollInterval,
            "InitialSleep" => $initialSleep,
            "SmBinPathname" => $SmBinPathname,
            "DebugLevel" => $DebugLevel,
            "ResID" => $ResID);
   # call the SM to start
   if (smStartAndWait(\%args) > 0)
   {
      # SM is up and running
      moveToVcsState();
      exit 0;
   }
   else
   {
      # the operation timed-out
      if ($DebugLevel > 0)
      {
         VCSAG_LOG_MSG("C"," the SM is not up ...",37044);
      }
      exit 1;
   }
}


