

#======================================================
# SCMS-SM VCS agents
# SubscriberManager agent - offline
# Created: Aug 2004
#======================================================
# Developed to offline the SM resource
# the scripts runs the following algorithm:
# - writes the offline to the last state file
# - calls the SM to move to standby state
# - returns the time till standby to the VCS
#======================================================
# Modification list:
# 5/8/2004: created
#
#======================================================
# Messages Used:
# MSGID               Message
#------------------------------------------------------
# 37034: SubscriberManager:% s: offline: invalid path to the SM '$SmBinPathname'
# 37035: SubscriberManager:% s: offline: parameters: $SmBinPathname, $State, $DebugLevel
# 37036: SubscriberManager:% s: offline: $SmBinPathname/p3cluster failed and returned $sysRc
# 37037: SubscriberManager:% s: offline: p3cluster -standby result: $cluResult
# 37038: SubscriberManager:% s: offline: timeout is $timeout
# 37039: SubscriberManager:% s: offline: the pattern was not found: $cluResult
# 37040: SubscriberManager:% s: offline: CLU failed or timed out
# 37041:
#======================================================


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

use SmOperations;
use Args;
#
# 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", "offline",$category_id);

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

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

$TMP_FILE = "/tmp/SubscriberManagerOffline.txt";
$sysRc = system("su - pcube -c \"$SmBinPathname/p3cluster --standby > $TMP_FILE\"");

if ($sysRc != 0) 
{
   if ($DebugLevel > 0) 
   {
      VCSAG_LOG_MSG("W"," $SmBinPathname/p3cluster failed and returned $sysRc",37036,"$SmBinPathname", "$sysRc");
   }
   
   exit 0;
}


open TMP_FILE or die "Can't find file: $TMP_FILE !\n";
read TMP_FILE, $cluResult, 16384;
close TMP_FILE;

if ($DebugLevel > 1) 
{
   VCSAG_LOG_MSG("I","p3cluster -standby result: $cluResult",37037,"$cluResult");
}

$success = grep("Command terminated successfully",$cluResult);

if ($success > 0)
{
     # the CLU terminated successfully
     # look for the sentence: the SM will become standby within XXX seconds
     @words = split / /, $cluResult;
     $withinFound = 0;
     $found = 0;
     $timeout = 0;
     foreach $word (@words)
     {
          if ($withinFound == 0)
          {
               # look for the word within
               if ($word eq "within") 
               {
                    $withinFound = 1;
                    $found = 1;
               }
          }
          else
          {
               # when within is found the next word is the timeout !!!
               $timeout = $word;
               $withinFound = 0;
               if ($DebugLevel > 1)
               {
                  VCSAG_LOG_MSG("I"," timeout is $timeout",37038,"$timeout");
               }
          }
     }
     if ($found == 0)
     {
          # the pattern was not found - warn !!!
         VCSAG_LOG_MSG("W"," the pattern was not found: $cluResult",37039,"$cluResult");
         return 0;
     }
     exit $timeout;	
}
else
{
   # CLU failed or timed out
   VCSAG_LOG_MSG("W"," CLU failed or timed out",37040);
   
   exit 0;
}

