#!/bin/sh

# 
# @HP_COPYRIGHT@
#
# HISTORY
# $NOHISTORY:$
# $EndNOHISTORY$
# 
# @(#)$RCSfile: cluster_optimal,v $ $Revision: 1.4 $ (HP) $Date: 2003/10/08 22:52:04 $
# 

PATH=/usr/bin:/usr/sbin:/sbin:$PATH
export PATH

CmdName="`basename $0`"

# Global variable used to pass details to DomainNotOptimalMsg()
DomainBadDevices=""

#
# Utility Functions
#
DrdDeviceServers()
{
	drdDev="${1}"

	drdmgr -a server ${drdDev} | grep 'Server Name' | cut -d: -f2 | awk '{print $1}'
}

CfsDomainServer()
{
	cfsDmn="${1}"

	cfsmgr -a server -d ${cfsDmn} | grep 'Server Name' | cut -d= -f2 | awk '{print $1}'
}

CfsDomainDevices()
{
	cfsDmn="${1}"

	cfsmgr -a devices -d ${cfsDmn} | grep 'dsk[0-9][0-9]*[a-h]' | cut -d- -f2 | awk '{print $1}'
}

# Used to report non-optimal domain information
DomainNotOptimalMsg()
{
	dmnName="${1}"
	
	echo "${dmnName}: non-optimal configuration"
	echo "    Server = `CfsDomainServer ${dmnName}`"
	echo "    Devices = `CfsDomainDevices ${dmnName}`"
	for dev in ${DomainBadDevices}
	do
		echo "\t${dev} served by: `DrdDeviceServers ${dev} | tr -s '\n' ' '`"
	done
	echo ""

	# Clear out the handled device entries
	DomainBadDevices=""
}

# Determine if a domain is being CFS served by a
# node which can also serve the physical storage.
DomainOptimal()
{
	dmnName="${1}"
	dmnOptimal=0	# assume is optimal
	DomainBadDevices=""

	if [ ! -d /etc/fdmns/${dmnName} ]
	then
		dmnOptimal=1
	else
		dmnServer="`CfsDomainServer ${dmnName}`"

		for dev in `CfsDomainDevices ${dmnName}`
		do
			if DrdDeviceServers ${dev} | grep -qw "${dmnServer}" 
			then
				:
			else
				dmnOptimal=1
				if [ -z "${DomainBadDevices}" ]
				then
					DomainBadDevices="${dev}"
				else
					DomainBadDevices="${DomainBadDevices} ${dev}"
				fi
			fi
		done
	fi

	return $dmnOptimal
}


#
# Main Body
#

# Verify running on a cluster
[ -x /usr/sbin/clu_get_info ] && /usr/sbin/clu_get_info -q 
if [ $? -ne 0 ]
then
	echo "${CmdName}: not a cluster!"
	exit 1
fi

# Assume cluster is optimal until proven otherwise
notOptimal=0

# for each of the cluster primary file systems
# verify that they optimally served.
for dmn in cluster_root cluster_usr cluster_var
do
	if DomainOptimal ${dmn}
	then
		:
	else
		DomainNotOptimalMsg ${dmn}
		notOptimal=1
	fi
done

exit ${notOptimal}
