# clean 
#
# 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.
#

# This script cleans the HAase resource.   

#
# 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;

use sybhautil;
($res_name,$CleanReason,$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($ypcheck);
my($cleantimeout);
my($retval);
my($srvtimeout);
my($auxtimeout);
my($engpid)=0;
my($args);
my($auxpid);

# initialize environment
init_env();

# log purpose
$log_message = sprintf "VCS:2000300:HAase:$res_name:clean:clean script starts.";
system("$HALOG -add C \"$log_message\" -msgid 2000100 -parameters $res_name");

#
# Check if the dataserver is on primary node
#
($ase_status, $group) = IsASEonPrimary($res_name);
if($ase_status == 1) 
{
	$log_message = sprintf "VCS:2000301:HAase:$res_name:clean:HAase service $Dataserver_name is on primary node.";
	system("$HALOG -add C \"$log_message\" -msgid 2000301 -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:clean:HAase resource $res_name cannot be initialized.";
		system("$HALOG -add C \"$log_message\" -msgid 156001 -parameters $res_name");
		exit 0;
	}

	#
	# Retrieve the attribute value for CleanTimeout
	#
	($retval, $cleantimeout) = getAttrInfo($res_name, $RES_TYPE, $CLEANTIMEOUT);

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

	#
	# Kill the auxiliaries if found
	#
	$auxpid = 0;
	if (($auxpid = IsServerPresent($res_name, $Backupserver_name)) != 0)
	{
		# First attempt a SIGTERM
		kill (15, $auxpid);
		sleep 2;
		if (($auxpid = IsServerPresent($res_name, $Backupserver_name)) != 0)
		{
			# If its still here, SIGKILL
			kill (9, $auxpid);
		}
	}

	# Repeat the operation for the MonitorServer
	$auxpid = 0;
	if (($auxpid = IsServerPresent($res_name, $Monserver_name)) != 0)
	{
		kill (15, $auxpid);
		sleep 2;
		if (($auxpid = IsServerPresent($res_name, $Monserver_name)) != 0)
		{
			kill (9, $auxpid);
		}
	}
 
	#
	# Try to shutdown the database the decent way.  
	#

	# Ineffective online/offline
	if ($CleanReason =~ /^[23]/) 
	{ 
		#
		# Retrieve the 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:clean:Cannot get login info. to normally shutdown $Dataserver_name.";
			system("$HALOG -add C \"$log_message\" -msgid 2000601 -parameters $res_name");
		}
		else
		{
			#
			# Now actually stop the server
			#
			$srvtimeout=int($cleantimeout/5);
			$retval=shutdown_server($Dataserver_name,$Salogin, $Sapswd,$srvtimeout);
			if ($retval==0)
			{
				$log_message = sprintf "VCS:2000812:HAase:$res_name:clean:Cannot shutdown $Dataserver_name normally.";
				system("$HALOG -add C \"$log_message\" -msgid 2000812 -parameters $res_name");
			}

			#
			# Try forced shutdown if dataserver is still up.
			#
			if(IsServerPresent($res_name, $Dataserver_name) != 0) 
			{
				$srvtimeout=int($cleantimeout/5);
				$retval=shutdown_server($CLEANSERVER, $Salogin, $Sapswd, $srvtimeout);
				if ($retval==0)
				{
					$log_message = sprintf "VCS:2000822:HAase:$res_name:clean:Cannot shutdown $Dataserver_name forcefully.";
					system("$HALOG -add C \"$log_message\" -msgid 2000822 -parameters $res_name");

				}
			}

			#
			# stop any non-zero engines and release any
			# shared memory
			#
			ase_clean_up($RUN_server_file, 1);
		}
	}

	#
	# Clean up by brute force for all cases. First send a SIGTERM followed
	# by SIGKILL to the processes.
	#
	$engpid = 0;
	if (open (PS, "$PS |"))
	{
		while (<PS>) 
		{
			next unless /\b$ServerProc\b/;
			next unless /-s\s*$Dataserver_name\b/;
			($engpid, $args) = split;
			last;
		}
		close(PS);
	}
	else
	{
		$log_message = sprintf "VCS:2000151:HAase:$res_name:Unable to pipe from $PS.";
		system("$HALOG -add C \"$log_message\" -msgid 2000151 -parameters $res_name");
		exit 0;
	}

	if ($engpid eq 0) 
	{
		ase_clean_up($RUN_server_file, 1);
		exit 0;
	}

	# Kill through SIGTERM
	if (kill (15, $engpid) == 1) 
	{
		$log_message = sprintf "VCS:156008:HAase:$res_name:clean:kill -15 of $engpid";
		system("$HALOG -add C \"$log_message\" -msgid 156008 -parameters $res_name $engpid");
		# stop any non-zero engines and release any shared memory
		ase_clean_up($RUN_server_file, 1);
	}
	
	sleep (2);
	if (kill (0, $engpid) == 1)
	{
		$log_message = sprintf "VCS:156009:HAase:$res_name:clean:kill -TERM was not successful for $engpid";
		system("$HALOG -add C \"$log_message\" -msgid 156009 -parameters $res_name $engpid");

		kill(9,$engpid);

		# stop any non-zero engines and release any shared memory
		ase_clean_up($RUN_server_file, 0);

		$log_message = sprintf "VCS:156010:HAase:$res_name:clean:Forcing the termination of $engpid";
		system("$HALOG -add C \"$log_message\" -msgid 156010 -parameters $res_name $engpid");
	}			

	exit 0;
}
elsif ($ase_status == 0)
{
        $log_message = sprintf "VCS:2000302:HAase:$res_name:clean:ASE is on secondary node.";
        system("$HALOG -add C \"$log_message\" -msgid 2000302 -parameters $res_name");
	exit 0;
}
else
{
	$log_message = sprintf "VCS:2000303:HAase:$res_name:online:ASE is in transition or it is an error.";
	system("$HALOG -add C \"$log_message\" -msgid 2000303 -parameters $res_name");

        exit 0;
}

