#! /usr/bin/ksh

#
# @(#)01        1.1  src/bos/usr/sbin/drmgr/sample_scripts/sysadmin_fail_cpu_add.sh.S, cmdcfg, bos720 8/22/02 11:44:33
#
# COMPONENT_NAME: (CMDCFG)
#
# FUNCTIONS: 
#
# ORIGINS: 27
#
# (C) COPYRIGHT International Business Machines Corp. 2000, 2002
# All Rights Reserved
# Licensed Materials - Property of IBM
# 
# US Government Users Restricted Rights - Use, duplication or
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
#
#
# This script will allow an administrator to stop cpu being added
# dynamically, when the current number of cpus in the system is 1.
# This script could be useful to stop the system moving from 
# uniprocessor to multiprocessor environment. This might be
# necessitized by some applications which donot adapt to
# this change.
#
#


# FILE NAME: sysadmin_fail_cpu_add.sh
#
# FILE DESCRIPTION:  See above. The failure is achieved in
# 	checkacquire phase. Here the number of cpus in the
# 	system is calculated and if it is 1, cpu addition
# 	is unconditionally failed.
#
#	Go to XXXX to review the failing script code.
#


################################# dbg #######################################
#
# NAME: dbg()
#
# DESCRIPTION: Write the debug message to debug file
#
# INPUT:
#	Message to write to debug file
#
# OUTPUT:
#	Message echoed to the debug file.
#
# RETURN VALUE DESCRIPTION:
#	None
#
##############################################################################

dbg()
{
	echo $1 >> ${DBG_FILE_NAME}
}

############################## process_scriptinfo ############################
#
# NAME: process_scriptinfo()
#
# DESCRIPTION: Process 'scriptinfo' command from drmgr
#
# INPUT:
#	The various environment variables set by drmgr
#
# OUTPUT:
#	Output name=value pairs to stdout
#	Various pieces of information about the DLPAR script.
#
# RETURN VALUE DESCRIPTION:
#	0 : success
#	Else failure.
#
##############################################################################

process_scriptinfo()
{
	echo "DR_SCRIPTINFO=AIX DR ksh example script"
	echo "DR_VERSION=1"
	echo "DR_DATE=10182002"
	echo "DR_VENDOR=IBM"
	echo "DR_TIMEOUT=10"
	return 0
}

############################## process_register   ############################
#
# NAME: process_register()
#
# DESCRIPTION: Process 'register' command from drmgr
#
# INPUT:
#	The various environment variables set by drmgr
#
# OUTPUT:
#	Output name=value pairs to stdout
#	List of all the resources supported by this DLPAR script.
#
# RETURN VALUE DESCRIPTION:
#	0 : success
#	Else failure.
#
##############################################################################

process_register()
{
	echo "DR_RESOURCE=cpu"
	echo "DR_RESOURCE=mem"
	return 0
}

############################## process_usage      ############################
#
# NAME: process_usage()
#
# DESCRIPTION: Process 'usage' command from drmgr
#
# INPUT:
#	The various environment variables set by drmgr
#	resource name input variable
#
# OUTPUT:
#	Output name=value pairs to stdout
#	Writes the how this resource is being used by the application
#	associated with this DLPAR script.
#
# RETURN VALUE DESCRIPTION:
#	0 : success
#	Else failure.
#
##############################################################################

process_usage()
{
	case	"$1" in
		"cpu")
			echo "DR_USAGE=cpu binding for performance"
			;;
		"mem")
			echo "DR_USAGE=Shared(Pinned) memory for app XYZ"
			;;
		*)
			echo "DR_ERROR=Script does not use Resource $1"
			;;
	esac
	return 0
}

############################## process_checkrelease ##########################
#
# NAME: process_checkrelease()
#
# DESCRIPTION: Process 'checkrelease' command from drmgr
#
# INPUT:
#	The various environment variables set by drmgr
#	resource name input variable
#
# OUTPUT:
#	Any debug information using DR debug name=value pairs
#	such as DR_LOG_DEBUG="..."
#	Any error message in the form of DR_ERROR name=value pair
#
#
# RETURN VALUE DESCRIPTION:
#	0 : success
#	10 : Command not implemented for this resource.
#	Else DLPAR operatin is not ok with the DLPAR script/associated app.
#
##############################################################################

process_checkrelease()
{
	case	"$1" in
		"cpu")
			dbg "Resource : cpu"
			# Do all the cpu related checks here and determine
			# whether DLPAR remove can proceed. 
			;;
		"mem")
			dbg "Resource : mem"
			# Do all the memory  related checks here and determine
			# whether DLPAR remove can proceed. 
			;;
		*)
			echo "DR_ERROR=Script does not support Resource $1"
			;;
	esac

	return 0
}

############################## process_prerelease ############################
#
# NAME: process_prerelease()
#
# DESCRIPTION: Process 'prerelease' command from drmgr
#
# INPUT:
#	The various environment variables set by drmgr
#	resource name input variable
#
# OUTPUT:
#	Any debug information using DR debug name=value pairs
#	such as DR_LOG_DEBUG="..."
#	Any error message in the form of DR_ERROR name=value pair
#
#
# RETURN VALUE DESCRIPTION:
#	0 : success
#	10 : Command not implemented for this resource.
#	Else DLPAR script/associated application could not release the resource
#	for DLPAR operation.
#
##############################################################################
process_prerelease()
{
	# Do any pre release actions here. One could send a signal
	# from here and wait for application do the necessary.
	# Return from here only after the desired actions have
	# taken place.
	case	"$1" in
		"cpu")
			dbg "Resource : cpu"
			# Release any cpu bindings etc here if the
			# resource being released is used by the app.
			;;
		"mem")
			dbg "Resource : mem"
			# Release application hold over any memory 
			# that is being removed.
			;;
		*)
			echo "DR_ERROR=Script does not support Resource $1"
			;;
	esac

	return 0
}

############################## process_postrelease ############################
#
# NAME: process_postrelease()
#
# DESCRIPTION: Process 'postrelease' command from drmgr
#
# INPUT:
#	The various environment variables set by drmgr
#	resource name input variable
#
# OUTPUT:
#	Any debug information using DR debug name=value pairs
#	such as DR_LOG_DEBUG="..."
#	Any error message in the form of DR_ERROR name=value pair
#
#
# RETURN VALUE DESCRIPTION:
#	0 : success
#	10 : Command not implemented for this resource.
#	Else DLPAR script/associated application could not post DLPAR operations.
#
##############################################################################
process_postrelease()
{
	# Reacquire any resource release during prerelease.
	# activate any apps quieced during prerelease.

	case	"$1" in
		"cpu")
			dbg "Resource : cpu"
			;;
		"mem")
			dbg "Resource : mem"
			;;
		*)
			echo "DR_ERROR=Script does not support Resource $1"
			;;
	esac

	return 0
}

############################## process_undoprerelease #########################
#
# NAME: process_undoprerelease()
#
# DESCRIPTION: Process 'process_undoprerelease' command from drmgr
#
# INPUT:
#	The various environment variables set by drmgr
#	resource name input variable
#
# OUTPUT:
#	Any debug information using DR debug name=value pairs
#	such as DR_LOG_DEBUG="..."
#	Any error message in the form of DR_ERROR name=value pair
#
#
# RETURN VALUE DESCRIPTION:
#	0 : success
#	10 : Command not implemented for this resource.
#	Else DLPAR script/associated application failed undorelease
#
##############################################################################
process_undoprerelease()
{
	# DLPAR operation was aborted/failed. Hence undo any
	# changes done during prerelease for this resource
	# and the application associated with the DLPAR script.
	case	"$1" in
		"cpu")
			dbg "Resource : cpu"
			;;
		"mem")
			dbg "Resource : mem"
			;;
		*)
			echo "DR_ERROR=Script does not support Resource $1"
			;;
	esac
	return 0
}

############################## process_checkacquire   #########################
#
# NAME: process_checkacquire()
#
# DESCRIPTION: Process 'process_checkacquire' command from drmgr
#
# INPUT:
#	The various environment variables set by drmgr
#	resource name input variable
#
# OUTPUT:
#	Any debug information using DR debug name=value pairs
#	such as DR_LOG_DEBUG="..."
#	Any error message in the form of DR_ERROR name=value pair
#
#
# RETURN VALUE DESCRIPTION:
#	0 : success
#	10 : Command not implemented for this resource.
#	Else DLPAR script/associated application does want this resource.
#
##############################################################################
process_checkacquire()
{
	# Do any checks prior to resource addition.
	case	"$1" in
		"cpu")
			dbg "Resource : cpu"
			# XXXX
			# 1. Check if number of cpus is 1
			num_of_cpus=`bindprocessor -q | sed 's/^.*://g' | awk '{print NF}'`

			# 2. If num of cpus is 1 in the system fail the DLPAR operation.
			if [ $num_of_cpus -eq 1 ]
			then
				echo "DR_ERROR=Only one cpu in the system and hence Failing checkacquire"
				dbg "Only one cpu in the system and hence Failing checkacquire"
				return 1
			fi
			;;
		"mem")
			dbg "Resource : mem"
			;;
		*)
			echo "DR_ERROR=Script does not support Resource $1"
			;;
	esac
	return 0
}
############################## process_preacquire     #########################
#
# NAME: process_preacquire()
#
# DESCRIPTION: Process 'process_preacquire' command from drmgr
#
# INPUT:
#	The various environment variables set by drmgr
#	resource name input variable
#
# OUTPUT:
#	Any debug information using DR debug name=value pairs
#	such as DR_LOG_DEBUG="..."
#	Any error message in the form of DR_ERROR name=value pair
#
#
# RETURN VALUE DESCRIPTION:
#	0 : success
#	10 : Command not implemented for this resource.
#	Else DLPAR script/associated application preacquire failed.
#
##############################################################################
process_preacquire()
{
	# Do all the necessary work prior to resource addition.
	case	"$1" in
		"cpu")
			dbg "Resource : cpu"
			;;
		"mem")
			dbg "Resource : mem"
			;;
		*)
			echo "DR_ERROR=Script does not support Resource $1"
			;;
	esac
	return 0
}

############################## process_undopreacquire #########################
#
# NAME: process_undopreacquire()
#
# DESCRIPTION: Process 'process_undopreacquire' command from drmgr
#
# INPUT:
#	The various environment variables set by drmgr
#	resource name input variable
#
# OUTPUT:
#	Any debug information using DR debug name=value pairs
#	such as DR_LOG_DEBUG="..."
#	Any error message in the form of DR_ERROR name=value pair
#
#
# RETURN VALUE DESCRIPTION:
#	0 : success
#	10 : Command not implemented for this resource.
#	Else DLPAR script/associated application undopreacquire failed.
#
##############################################################################
process_undopreacquire()
{
	# DLPAR operation has failed. So undo any activities done during
	# preacquire
	case	"$1" in
		"cpu")
			dbg "Resource : cpu"
			;;
		"mem")
			dbg "Resource : mem"
			;;
		*)
			echo "DR_ERROR=Script does not support Resource $1"
			;;
	esac
	return 0
}
############################## process_postacquire  #########################
#
# NAME: process_postacquire()
#
# DESCRIPTION: Process 'process_postacquire' command from drmgr
#
# INPUT:
#	The various environment variables set by drmgr
#	resource name input variable
#
# OUTPUT:
#	Any debug information using DR debug name=value pairs
#	such as DR_LOG_DEBUG="..."
#	Any error message in the form of DR_ERROR name=value pair
#
#
# RETURN VALUE DESCRIPTION:
#	0 : success
#	10 : Command not implemented for this resource.
#	Else DLPAR script/associated application postacquire failed.
#
##############################################################################
process_postacquire()
{
	# execute any actions required after the DLPAR add operation.
	# Egs: Increase the number of threads for the application
	#      Increase memory areas reserved for application etc.
	case	"$1" in
		"cpu")
			dbg "Resource : cpu"
			;;
		"mem")
			dbg "Resource : mem"
			;;
		*)
			echo "DR_ERROR=Script does not support Resource $1"
			;;
	esac
	return 0
}

###############################################
#            MAIN SCRIPT STARTS HERE
###############################################

script_file_name=`basename $0`
DBG_FILE_NAME=/tmp/${script_file_name}.dbg

date_and_time=`date`
dbg "------ DLPAR Script start at $date_and_time -------"

if [ $# -eq 0 ]
then
	# Atleast the command must be part of the invocation
	dbg "No command passed to the DLPAR script"
	echo "DR_ERROR=Script Usage Error"
	exit 1
fi

# Note down the command
command=$1
ret_code=0

dbg "command issued: $1"
case	"$1" in
	scriptinfo)
		process_scriptinfo
		ret_code=$?
		;;
	register)
		process_register
		ret_code=$?
		;;
	usage)
		process_usage $2 
		ret_code=$?
		;;
	checkrelease)
		process_checkrelease $2
		ret_code=$?
		;;
	prerelease)
		process_prerelease $2
		ret_code=$?
		;;
	postrelease)
		process_postrelease $2
		ret_code=$?
		;;
	undoprerelease)
		process_undoprerelease $2
		ret_code=$?
		;;
	checkacquire)
		process_checkacquire $2
		ret_code=$?
		;;
	preacquire)
		process_preacquire $2
		ret_code=$?
		;;
	undopreacquire)
		process_undopreacquire $2
		ret_code=$?
		;;
	postacquire)
		process_postacquire $2
		ret_code=$?
		;;
	*)
		dbg "unknown command: $1 issued"
		ret_code=10
		;;
esac

dbg "SCRIPT exiting with return code : $ret_code"

dbg "................DLPAR Script end  ................"

return $ret_code


