#!/usr/bin/ksh93
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/usr/sbin/cluster/sa/db2/sbin/cl_db2ver_collector.sh 1.6 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2005,2008 
# 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 
# @(#)61      1.6 src/43haes/usr/sbin/cluster/sa/db2/sbin/cl_db2ver_collector.sh, hacmp.assist, 61haes_r714 12/8/08 08:37:05

#
# Name:         clver_db2ver_collector
#
# Description:  This script is used by clverify to gather
#             	the rhosts information for each accessible DB2
#             	instance on the local node.  The script also collects
#		the db2nodes.cfg file as well as information related
#		to configured DB2 instances on the local node
#
#             	The DB2 instance home directory must be mounted
#             	at the time the script is executed, otherwise information
#		related to DB2 volume groups will not be discovered
#		this information is only accessible by connecting
#		to the DB2 database and performing a "db2 get db cfg"
#		for the databases defined to the configured DB2 instance
#
# Arguments:	n/a
#
# Returns:	1 if unsuccessful (environment variables not set properly)
#		0 if successful
#

echo "#type:instance:ip_label:rhost_user:instance_owner:insthome:db2nodes_cfg_ip_label"

DISCOVERY_FILE="/tmp/db2.disc.$$"

# Determine if the environment has the CLVERIFY_ODMDIR set, if not
# abort verification, no information will be reported to stdout since
# this would be interpretted by clverify as collected data
if [[ -z $CLVERIFY_DCD ]]; then
	exit 1
fi

#
# If the caller (clverify) has not set the NODENAME, then abort
#
if [[ -z $CLVERIFY_NODENAME ]]; then
	exit 1
fi

typeset -i num_instances
num_instances=$(ODMDIR=$CLVERIFY_DCD odmget -q sa_type=SA_DB2Instance HACMPgroup | wc -l)

# If there are no DB2 instances defined to HACMP, then abort this script
if (( $num_instances == 0 )); then
	exit 0
fi

CLUSTER_NODE=$CLVERIFY_NODENAME
echo "CLUSTER_NODE=$VERIFY_NODENAME" > $DISCOVERY_FILE

#
# typeset -L, represents left justify, removing leading spaces
#
# As lslpp can't be used to check the installation of DB2, from DB2 V9.X onwards.
# From DB2 V9.X onwards, we have 'db2ls' command, e.g. as below
# /usr/local/bin/db2ls -> /opt/IBM/db2/V9.5/install/db2ls
# From the above output, the installation path for DB2 is /opt/IBM/db2/V9.5
# The installation path is parsed accordingly.
#
typeset -L INSTALL_DIR
lslpp -cw '*bin/db2ilist' 1>/dev/null 2>&1
[[ $? != 0 ]] && {
	INSTALL_DIR=$(ls -l /usr/local/bin/db2ls | cut -d ">" -f 2 | cut -d "install" -f 1)
	export DSE_INSTALL_DIR="$INSTALL_DIR"
}

/usr/es/sbin/cluster/sa/db2/sbin/cl_db2discovery -q >> $DISCOVERY_FILE 2>/dev/null

# Now source the discovery file
. $DISCOVERY_FILE
FPATH=/usr/es/lib/ksh93/db2/
rm -f $DISCOVERY_FILE

for instance in $(KLIB_DB2_disc_get_instances); do
	homedir=$(KLIB_DB2_disc_get_instance_value $instance "INSTHOME")
	inst_owner=$(KLIB_DB2_disc_get_instance_value $instance "INSTANCE_OWNER_USER")

	if [[ -n $homedir && -f $homedir/.rhosts ]]; then
		found=false
		while read node user; do
			echo "RHOST:$instance:$node:$user:$inst_owner:$homedir: "
			found=true
		done < $homedir/.rhosts
		$found || echo "RHOST:$instance: : : : : "
	else
		if [[ -d $homedir/sqllib ]]; then
			echo "RHOST:$instance: : : : : "
		fi
	fi

	if [[ -n $homedir && -f $homedir/sqllib/db2nodes.cfg ]]; then
		found=false
		while read dbpartitionum ip_label logical_port netname resourcename; do
			echo "DB2NODES:$instance:none:none:$inst_owner:$homedir:$ip_label"
			found=true
		done < $homedir/sqllib/db2nodes.cfg
		$found || echo "DB2NODES:$instance:none:none:$inst_owner:$homedir: "
	else
		if [[ -d $homedir/sqllib ]]; then
			echo "DB2NODES:$instance:none:none:$inst_owner:$homedir: "
		fi
	fi
done

unset DSE_INSTALL_DIR
