#!/bin/ksh93
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r720 src/43haes/usr/sbin/cluster/sa/tsmserver/sbin/cl_tsm_server_query.sh 1.2.1.1 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2010 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
# @(#)29	1.2.1.1  src/43haes/usr/sbin/cluster/sa/tsmserver/sbin/cl_tsm_server_query.sh, hacmp.assist, 61haes_r720, 1541B_hacmp720 10/5/15 12:23:12

###############################################################################
#
# NAME:
#       cl_tsm_server_query
#
# PURPOSE:
#       Discover PowerHA SystemMirror configuration for TSM server
#	instance. This script is used in SMIT discovery screens.
#
# ARGUMENTS:
#       -i TSM instance name
#       -q Application id 
#	-L to list the TSM server instances
#	-I to list the TSM server instances from other node 
#
# RETURNS
#  0 on success
#  1 on Failure
#
###############################################################################

. /usr/es/sbin/cluster/sa/tsmserver/sbin/cl_tsmserverGlobals

function instanceList {
	[[ $VERBOSE_LOGGING == "high" ]] && set -x
	nodes=$*
	
	local_node=$(get_local_nodename)
	
	for node in $nodes
	do
		if [[ "$node" == "$local_node" ]]; then
			if [[ -e $TSM_SRV_DIR/db2/bin/db2ilist ]]; then
				print "\n(Node \"$node\"): $TSM_SRV_DIR/db2/bin/db2ilist" >>$KLIB_DEFAULT_LOGFILE
				$TSM_SRV_DIR/db2/bin/db2ilist 2>>$KLIB_DEFAULT_LOGFILE | tee -a $KLIB_DEFAULT_LOGFILE
			else
				print >>$KLIB_DEFAULT_LOGFILE
				/usr/bin/dspmsg -s 1 tsmserversa.cat 9999 '# Warning: DB2 could not be found on node "%1$s".\n' "$node" | tee -a $KLIB_DEFAULT_LOGFILE
			fi
		else
			cl_rsh $node "[[ -e $TSM_SRV_DIR/db2/bin/db2ilist ]]"
			if (( $? == 0 )); then
				print "\ncl_rsh \"$node\" \"$TSM_SERVER_HA_SBIN/cl_tsm_server_query -I\"" >>$KLIB_DEFAULT_LOGFILE
				cl_rsh "$node" "$TSM_SERVER_HA_SBIN/cl_tsm_server_query -I" 2>>$KLIB_DEFAULT_LOGFILE | tee -a $KLIB_DEFAULT_LOGFILE
			else
				print >>$KLIB_DEFAULT_LOGFILE
				/usr/bin/dspmsg -s 1 tsmserversa.cat 9999 '# Warning: DB2 could not be found on node "%1$s".\n' "$node" | tee -a $KLIB_DEFAULT_LOGFILE
			fi
		fi
	done
	print >>$KLIB_DEFAULT_LOGFILE

	return 0
}

###############
#   main
###############

[[ "VERBOSE_LOGGING" == "high" ]] && set -x
	
typeset nodes list instance application_id

while getopts q:i:n:IL opt
do
	case $opt in
	q) # application_id of the instance
	   application_id=$OPTARG
	   ;;

	i) # TSM server instance name
	   instance=$OPTARG
	   ;;
	
	L) # List instances
	   list="Y"
	   ;;

	n) # List of nodes
	   nodes=$OPTARG
	   ;;

	I) # calling from other node for instances
	   local=$(get_local_nodename)
	   instanceList $local
	   exit 0
	   ;;

	esac
done
	
# Discovery for Instance List

[[ -n $list ]] && {

	[[ -z $nodes ]] && {
		nodes=$(clvt query node)
	}
	instanceList "$nodes"| sort -u
	exit 0
}

# Discovery for Application name
[[ -n $instance ]]&& {
echo "#application_name:primary"
local_node=$(get_local_nodename)
echo $instance"_"$local_node":"$local_node
exit 0
}

# Discovery for modifying the Smart Assist Application
[[ -n $application_id ]] && {
echo "#primary:takeover:instance:instance_dir:VG_list:serviceip:prefix:user:password"
		
rg=$(clodmget -q "name=RESOURCE_GROUP and application_id=$application_id" -f value -n HACMPsa_metadata)
[[ -z $rg ]]&& exit 1

instance_dir=$(clodmget -q "name=INSTANCE_DIRECTORY and application_id=$application_id" -f value -n HACMPsa_metadata)
instance=$(clodmget -q "name=INSTANCE_NAME and application_id=$application_id" -f value -n HACMPsa_metadata)
user=$(clodmget -q "name=INSTANCE_USER and application_id=$application_id" -f value -n HACMPsa_metadata)
password=$(clodmget -q "name=INSTANCE_PASSWORD and application_id=$application_id" -f value -n HACMPsa_metadata)

nodes=$(clvt query resource_group $rg | grep -w NODES|awk -F= '{ print $2 }' | sed -e "s/\"//g")
VG_list=$(clvt query resource_group $rg | grep VOLUME_GROUP|awk -F= '{ print $2 }' | sed -e "s/\"//g")
serviceip=$(clvt query resource_group $rg | grep SERVICE_LABEL|awk -F= '{ print $2 }' | sed -e "s/\"//g")
prefix=$(clodmget -q "ip_label=$serviceip" -f netmask -n HACMPadapter)

takeover=""
primary=""
for n in $nodes
do
	if [[ -z $primary ]]  
	then
		primary=$n
	else
		takeover=$(echo "$takeover $n")
	fi
done
	
echo $primary":"$takeover":"$instance":"$instance_dir":"$VG_list":"$serviceip":"$prefix":"$user":"$password
exit 0
}	
