#!/bin/bash
# BEGIN_ICS_COPYRIGHT8 ****************************************
# Copyright (c) 2003 InfiniCon Systems, Inc.  All rights reserved.
#
# THIS SOFTWARE IS PROVIDED BY INFINICON SYSTEMS, INC. ("INFINICON") TO EACH
# PERSON OR COMPANY ("RECIPIENT") ON AN "AS IS" BASIS.  ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL INFINICON BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED OR ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
# OF THE POSSIBILITY OF SUCH DAMAGE.
# Any agreements between InfiniCon and the Recipient shall apply to Recipient's
# use of the Software.
# END_ICS_COPYRIGHT8   ****************************************

# [ICS VERSION STRING: @(#) ./bin/iba_showmc 7_4_3_0_2 [01/26/18 09:02]

# This script provides a summary of multicast membership
# it uses saquery to gather the information from the SM

trap "exit 1" SIGHUP SIGTERM SIGINT

# optional override of defaults
if [ -f /etc/sysconfig/fastfabric.conf ]
then
	. /etc/sysconfig/fastfabric.conf
elif [ -f /etc/fastfabric.conf ]
then
	. /etc/fastfabric.conf
fi

. /opt/iba/ib_tools/defaults
. /opt/iba/ib_tools/ff_funcs

show_mc()
{
	# $1 = hca
	# $2 = port
	# $3 = showname (y or n)
	if [ "$port" -eq 0 ]
	then
		port_opts="-h $hca"	# default port to 1st active
	else
		port_opts="-h $hca -p $port"
	fi
	echo "Fabric $hca:$port Multicast Information:"
	/sbin/iba_saquery $port_opts -o mcmember 2>/dev/null | grep GID: | sed -e 's/GID: //' | while read mgid 
	do
		/sbin/iba_saquery $port_opts -o mcmember -m $mgid|head -4|grep -v 'PortGid:'
		if [ "$showname" = "y" ]
		then
			/sbin/iba_saquery $port_opts -o mcmember -m $mgid|grep 'PortGid:'|while read heading portgid rest
			do
				echo "$heading $portgid $rest"
				portguid=$(echo $portgid|cut -f 2 -d :)
				if [ $(($portguid)) -ne 0 ]
				then
					echo -n "  Name: "
					/sbin/iba_saquery $port_opts -o desc -g $(echo $portgid|cut -f 2 -d :)
				fi
			done
		else
			/sbin/iba_saquery $port_opts -o mcmember -m $mgid|grep 'PortGid:'
		fi
		echo
	done
}

Usage_full()
{
	echo "Usage: iba_showmc [-v] [-t portsfile] [-p ports]" >&2
	echo "              or" >&2
	echo "       iba_showmc --help" >&2
	echo "   --help - produce full help text" >&2
	echo "   -v - verbose output, show name for each member" >&2
	echo "   -t portsfile - file with list of local HCA ports used to access" >&2
	echo "                  fabric(s) for analysis, default is $CONFIG_DIR/iba/ports" >&2
	echo "   -p ports - list of local HCA ports used to access fabric(s) for analysis" >&2
	echo "              default is 1st active port" >&2
	echo "              This is specified as hca:port" >&2
	echo "                 0:0 = 1st active port in system" >&2
	echo "                 0:y = port y within system" >&2
	echo "                 x:0 = 1st active port on HCA x" >&2
	echo "                 x:y = HCA x, port y" >&2
	echo "              The first HCA in the system is 1.  The first port on an HCA is 1." >&2
	echo " Environment:" >&2
	echo "   PORTS - list of ports, used in absence of -t and -p" >&2
	echo "   PORTS_FILE - file containing list of ports, used in absence of -t and -p" >&2
	echo "for example:" >&2
	echo "   iba_showmc" >&2
	echo "   iba_showmc -p '1:1 1:2 2:1 2:2'" >&2
	exit 0
}

Usage()
{
	echo "Usage: iba_showmc [-v]" >&2
	echo "              or" >&2
	echo "       iba_showmc --help" >&2
	echo "   -v - verbose output, show name for each member" >&2
	echo "   --help - produce full help text" >&2
	echo "for example:" >&2
	echo "   iba_showmc" >&2
	exit 2
}

if [ x"$1" = "x--help" ]
then
	Usage_full
fi

status=ok
showname=n
while getopts p:t:v param
do
	case $param in
	p)	export PORTS="$OPTARG";;
	t)	export PORTS_FILE="$OPTARG";;
	v)	showname=y;;
	?)	Usage;;
	esac
done
shift $((OPTIND -1))
if [ $# -ge 1 ]
then
	Usage
fi

check_ports_args fabric_info

for hca_port in $PORTS
do
	hca=$(expr $hca_port : '\([0-9]*\):[0-9]*')
	port=$(expr $hca_port : '[0-9]*:\([0-9]*\)')
	if [ "$hca" = "" -o "$port" = "" ]
	then
		echo "iba_showmc: Error: Invalid port specification: $hca_port" >&2
		status=bad
		continue
	fi

	show_mc "$hca" "$port" "$showname"
done

if [ "$status" != "ok" ]
then
	exit 1
else
	exit 0
fi
