


#======================================================
# SCMS-SM VCS agents
# SubscriberManager agent - online
# Created: Aug 2004
#======================================================
# Developed to online the SM resource
# the scripts runs the following algorithm:
# - writes the online to the last state file
# - calls the SM to move to active state
# - returns the time till active to the VCS
#======================================================
# Modification list:
# 5/8/2004: created
#
#======================================================
# Messages Used:
# MSGID               Message
#------------------------------------------------------
# 37026: SubscriberManager:% s: online: invalid path to the SM '$SmBinPathname'
# 37027: SubscriberManager:% s: online: parameters: $SmBinPathname, $State, $DebugLevel
# 37028: SubscriberManager:% s: online: $SmBinPathname/p3cluster failed and returned $sysRc
# 37029: SubscriberManager:% s: online: p3cluster -active result: $cluResult
# 37030: SubscriberManager:% s: online: timeout is $timeout
# 37031: SubscriberManager:% s: online: the pattern was not found: $cluResult
# 37032: SubscriberManager:% s: online: CLU failed or timed out
# 37033:
#======================================================

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", "online",$category_id);

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

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

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

if ($sysRc != 0)
{
	if ($DebugLevel > 0)
	{
		VCSAG_LOG_MSG("W","$SmBinPathname/p3cluster failed and returned $sysRc",37028,"$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 -active result: $cluResult",37029,"$cluResult");
}

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

if ($success > 0)
{
	#the CLU terminated successfully
	#look for the sentence: the SM will become active 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",37030,"$timeout");
			}
		}
	}
	if ($found == 0)
	{
		#the pattern was not found - warn !!!
		VCSAG_LOG_MSG("W","the pattern was not found: $cluResult",37031,"$cluResult");
		return 0;
	}
	exit $timeout;  
}
else
{
	#CLU failed or timed out
    VCSAG_LOG_MSG("I","CLU failed or timed out",37032);

	exit 0;
}

