# offline
#
# Copyright(C) 2003 Sybase Inc.  ALL RIGHTS RESERVED.
# UNPUBLISHED -- RIGHTS RESERVED UNDER THE COPYRIGHT
# LAWS OF THE UNITED STATES.  USE OF A COPYRIGHT NOTICE
# IS PRECAUTIONARY ONLY AND DOES NOT IMPLY PUBLICATION
# OR DISCLOSURE.
#
# THIS SOFTWARE CONTAINS CONFIDENTIAL INFORMATION AND
# TRADE SECRETS OF SYBASE INC.  USE, DISCLOSURE,
# OR REPRODUCTION IS PROHIBITED WITHOUT THE PRIOR
# EXPRESS WRITTEN PERMISSION OF SYBASE INC.
#

#
# make the script work under any of csh, sh or perl
#
eval '(exit $?0)' && eval 'exec /opt/VRTSvcs/bin/perl5 -S $0 ${1+"$@"}'
& eval 'exec /opt/VRTSvcs/bin/perl5 -S $0 $argv:q'
	if 0;

# This script offlines an HAase instance. If the database is
# not running, it does nothing.
#
 
use sybhautil;
($res_name,$Sybase_home,$Dataserver_name,$Backupserver_name,$Monserver_name,$Textserver_name,$Secondary_companion_name,$Dataserver_login_file,$RUN_server_file,$Thorough_probe_cycle,$Thorough_probe_script,$Debug,$Log_max_size,$Failback_strategy) = @ARGV;

my($log_message);
my($ase_status);
my($group);
my($failover)=0;
my($retval);
my($ypcheck);
my($srvtimeout);
my($offlinetimeout);
my($auxtimeout);
my($start_time)=0;
my($end_time)=0;

# initialize environment
init_env();

# log purpose
$log_message = sprintf "VCS:2000200:HAase:$res_name:offline:call offline.";
system("$HALOG -add C \"$log_message\" -msgid 2000200 -parameters $res_name");

#
# Retrieve the attribute value for OfflineTimeout
#
($retval, $offlinetimeout) = getAttrInfo($res_name, $RES_TYPE,$OFFLINETIMEOUT);

if (!$retval)
{
	$offlinetimeout=60;
	$log_message = sprintf "VCS:2000811:HAase:$res_name:clean:Fail to retrieve value for attribute $OFFLINETIMEOUT, use default value $offlinetimeout.";
	system("$HALOG -add C \"$log_message\" -msgid 2000811 -parameters $res_name");
}

# 
# Reserving 10% of the total offlinetimeout for auxiliary server shutdown
#
$auxtimeout = 0.1 * $offlinetimeout;

#
# Check if the dataserver is on primary node
#
($ase_status,$group) = IsASEonPrimary($res_name);
if($ase_status == 1) 
{
        $log_message = sprintf "VCS:2000201:HAase:$res_name:offline:HAase service $Dataserver_name is on primary node.";
	system("$HALOG -add C \"$log_message\" -msgid 2000201 -parameters $res_name");

	# initialize RUN file for backup server or monitor server
	($retval, $RUN_srv_file, $RUN_backup_srv_file, $RUN_monitor_srv_file)=init_resource();
	if (!$retval)
	{
		$log_message = sprintf "VCS:156001:HAase:$res_name:offline:HAase resource $res_name cannot be initialized.";
		system("$HALOG -add C \"$log_message\" -msgid 156001 -parameters $res_name");
		exit 0;
	}

	#
	# Retrieve login info. for user with ha role
	#
	($retval, $Salogin, $Sapswd) = get_login($Dataserver_login_file);
	if ($retval==0)
	{
		$log_message = sprintf "VCS:2000601:HAase:$res_name:offline:Cannot get login info. to offline $Dataserver_name, exit...";
		system("$HALOG -add C \"$log_message\" -msgid 2000601 -parameters $res_name");
		exit 1;
	}

	#
	# Stop the auxiliary servers if they exist. If $auxtimeout is less than 0,
	# VCS will automatically stop the offline script.   
	#
	$start_time=time;
	stop_auxiliary_servers($auxtimeout);
	$end_time=time;

	#
	# Now actually stop ASE
	#
	$srvtimeout=$offlinetimeout - ($end_time - $start_time);
	$retval = shutdown_server($Dataserver_name,$Salogin, $Sapswd,$srvtimeout);
	if ($retval==0)
	{
		$log_message = sprintf "VCS:2000801:HAase:$res_name:offline:Cannot shutdown $Dataserver_name normally."; 
		system("$HALOG -add C \"$log_message\" -msgid 2000801 -parameters $res_name");
	}

	#
	# Try forced shutdown if dataserver is still up.
	# isql command is "shutdown with nowait\n"
	#
	if(IsServerPresent($res_name, $Dataserver_name) != 0)
	{
		$srvtimeout=int($offlinetimeout/4);
		($retval) = shutdown_server($CLEANSERVER, $Salogin, $Sapswd, $srvtimeout);
		if ($retval==0)
		{
			$log_message = sprintf "VCS:2000871:HAase:$res_name:offline:Cannot shutdown $Dataserver_name forcefully.";
			system("$HALOG -add C \"$log_message\" -msgid 2000871 -parameters $res_name");
		}

		#
		# check if dataserver is still up.
		#
		if(IsServerPresent($res_name, $Dataserver_name) != 0) 
		{
			$log_message = sprintf "VCS:156005:HAase:$res_name:offline:HAase service $Dataserver_name cannot be stopped by isql shutdown";
			system("$HALOG -add C \"$log_message\" -msgid 156005 -parameters $res_name $Dataserver_name");

			$log_message = sprintf "VCS:156006:HAase:$res_name:offline:clean scripts should remove the process.";
			system("$HALOG -add C \"$log_message\" -msgid 156006 -parameters $res_name");
		}
		else 
		{
			$log_message = sprintf "VCS:156007:HAase:$res_name:offline:HAase service $Dataserver_name has been stopped";
			system("$HALOG -add C \"$log_message\" -msgid 156007 -parameters $res_name $Dataserver_name");
		}
	}


	exit 5;
}
elsif ($ase_status == 0)
{
	$log_message = sprintf "VCS:2000202:HAase:$res_name:offline:ASE is on secondary node.";
	system("$HALOG -add C \"$log_message\" -msgid 2000202 -parameters $res_name");
	$retval=connect_companion($companion, $failover);

        if ($retval==0)
        {
                $log_message = sprintf "VCS:2000204:HAase:$res_name:offline:primary server cannot failback to primary host.";
                system("$HALOG -add C \"$log_message\" -msgid 2000204 -parameters $res_name");
        }

        exit 15;
}
else
{
	$log_message = sprintf "VCS:2000203:HAase:$res_name:offline:ASE is in transition or it is an error.";
	system("$HALOG -add C \"$log_message\" -msgid 2000203 -parameters $res_name");

        exit 0;
}

