#!/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/fabric_info 7_4_3_0_2 [01/26/18 09:02]
# This script provides a quick summary of fabric configuration
# 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

if [ -f /opt/iba/ib_tools/defaults ]
then
	. /opt/iba/ib_tools/defaults
fi
if [ -f /opt/iba/ib_tools/ff_funcs ]
then
	. /opt/iba/ib_tools/ff_funcs
	ff_available=y
else
	ff_available=n
fi

# if ff is not available, we can only support a subset of the capability

show_info()
{
	# $1 = hca
	# $2 = port
	if [ "$port" -eq 0 ]
	then
		port_opts="-h $hca"	# default port to 1st active
	else
		port_opts="-h $hca -p $port"
	fi
	if [ "$ff_available" = y ]
	then
		echo "Fabric $hca:$port Information:"
	else
		echo "Fabric Information:"
	fi
	if /sbin/iba_saquery $port_opts -o sminfo 2>/dev/null | grep Guid: >/dev/null 2>&1
	then
		# SM supports sminfo
		/sbin/iba_saquery $port_opts -o sminfo|grep "Guid:"|while read trash lid trash guid trash state
		do
			echo "SM:" `/sbin/iba_saquery $port_opts -g $guid -o desc|head -1` "Guid: $guid State: $state"
		done
	else
		# SM does not support sminfo
		# this is a cheat, assume SM is LID 1
		echo "SM:" `/sbin/iba_saquery $port_opts -l 1 -o desc|head -1` "Guid:" `/sbin/iba_saquery $port_opts -l 1 -o nodeguid|head -1`
	fi
	echo "Number of CAs:" `/sbin/iba_saquery $port_opts -t ca -o nodeguid|sort -u|wc -l`
	echo "Number of CA Ports:" `/sbin/iba_saquery $port_opts -t ca -o desc|wc -l`
	echo "Number of Switch Chips:" `/sbin/iba_saquery $port_opts -t sw -o nodeguid|sort -u|grep -v 'No Records Returned'|wc -l`
	echo "Number of Links:" $(($(/sbin/iba_saquery $port_opts -o link|wc -l) / 2))
	# check for any 1x links
	echo "Number of 1x Ports:" `/sbin/iba_saquery $port_opts -o portinfo|grep 'LinkWidth'|grep 1x|wc -l`
	echo "-------------------------------------------------------------------------------"
}

Usage_ff_full()
{
	echo "Usage: fabric_info [-t portsfile] [-p ports]" >&2
	echo "              or" >&2
	echo "       fabric_info --help" >&2
	echo "   --help - produce full help text" >&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 "   fabric_info" >&2
	echo "   fabric_info -p '1:1 1:2 2:1 2:2'" >&2
	exit 0
}

Usage_ff()
{
	echo "Usage: fabric_info" >&2
	echo "              or" >&2
	echo "       fabric_info --help" >&2
	echo "   --help - produce full help text" >&2
	echo "for example:" >&2
	echo "   fabric_info" >&2
	exit 2
}

Usage_basic()
{
	echo "Usage: fabric_info" >&2
	exit 2
}

Usage()
{
	if [ "$ff_available" = y ]
	then
		Usage_ff
	else
		Usage_basic
	fi
}

if [ "$ff_available" = y  -a  x"$1" = "x--help" ]
then
	Usage_ff_full
fi

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

if [ "$ff_available" = y ]
then
	check_ports_args fabric_info
else
	PORTS='0:0'
fi

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 "fabric_info: Error: Invalid port specification: $hca_port" >&2
		status=bad
		continue
	fi

	show_info "$hca" "$port"
done

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