#!/bin/ksh -p
# $Id: //depot/stable/explorer/tools/firelink#2 $
#
# Explorer Version:
EXP_VERSION=4.0
#
# Source tools functions
. ${EXP_LIB}/exp_tools

# Run if specified to do so
script=${0##*/}
which_gate_exit $script default all

##########
#
# There are two packages that explorer will check to see if
# the firelink module should be run.
#
#   SUNWwcfmr - Sunfirelink Fabric Manager Root Package
#   This package is required on the fabric manager admin station.
#
#   SUNWrsmpr -  Sunfirelink RSM Proxy Root Package
#   This package is required on each firelink RSM cluster node.
#
# This script should only be run if one of these two packages
# are installed.
#
##########

TEXT=`gettext "Sun Firelink Interconnect not installed"`
require "pkginfo -q SUNWwcfmr || pkginfo -q SUNWrsmpr" "${TEXT}" 

# This function is used to collect specific firelink data
# on the firelink RSM cluster node
firelink_rsm_cluster_nodes()
{
	OUTPUT_DIR="firelink/cnodes"

	WRSMCONF="/usr/platform/sun4u/sbin/wrsmconf"
	WRSMSTAT="/usr/platform/sun4u/sbin/wrsmstat"

	get_cmd "$WRSMCONF topology" $OUTPUT_DIR/wrsmconf-topology
	get_cmd "$WRSMCONF check" $OUTPUT_DIR/wrsmconf-check
	get_cmd "$WRSMCONF info" $OUTPUT_DIR/wrsmconf-info

	get_cmd "$WRSMSTAT controller" $OUTPUT_DIR/wrsmstat-controller
	get_cmd "$WRSMSTAT wrsm" $OUTPUT_DIR/wrsmstat-wrsm
	get_cmd "$WRSMSTAT route" $OUTPUT_DIR/wrsmstat-route

	# It is possible for a node to have multiple firelink rsm controllers
	# For each controller run wrsmconf dump -c #

	ctlrlist=`$WRSMCONF info | /usr/bin/awk '/controller/ {print $2}'`;
	for c in $ctlrlist; do
		$WRSMCONF dump -c $c -f /tmp/wrsmconf-dump.c$c
		get_file "/tmp/wrsmconf-dump.c$c" $OUTPUT_DIR
		/usr/bin/rm /tmp/wrsmconf-dump.c$c
	done

	# Controller information is save in the directory /etc/wrsm that is used
	# to initialize the controllers at boot time.

	get_dir recursive /etc/wrsm $OUTPUT_DIR/etc-wrsm

	# Find out what version of java is being used with the FM proxy
	PROXYPKG=SUNWwrsmp
	USER_PROXYPKG=SUNWrsmpu
	USER_BASEDIR=`/usr/bin/pkgparam ${USER_PROXYPKG} BASEDIR 2>/dev/null`
	JRE_HOME="$USER_BASEDIR/$PROXYPKG/jre_home.cfg"
	. $JRE_HOME
	get_cmd "$JAVA_BIN/java -version 2>&1" $OUTPUT_DIR/java-version

}

# This function is used to collect specific firelink data
# on the firelink Fabric Manager Admin Station
firelink_fm_nodes()
{
	OUTPUT_DIR="firelink/FM"
	PKG_NAME="SUNWwcfmu"
	PKG_NAME_DIR="SUNWwcfm"
	BASEDIR=`/usr/bin/pkgparam $PKG_NAME BASEDIR`
	FM_BIN="${BASEDIR}/${PKG_NAME_DIR}/bin"
	CONFIG_FILE_LOC="${BASEDIR}/${PKG_NAME_DIR}/config/wcfm_base_data_dir.cfg"
	WCFM_BASE_DATA_DIR=`cat $CONFIG_FILE_LOC`

	get_dir recursive $WCFM_BASE_DATA_DIR $OUTPUT_DIR/fabrics

	get_cmd "$FM_BIN/list_fabrics" $OUTPUT_DIR/list-fabrics

	for FABRICS in `ls $WCFM_BASE_DATA_DIR`
	do
    		get_cmd "$FM_BIN/wcfmstat $FABRICS" $OUTPUT_DIR/wcfmstat-$FABRICS
	done

	# Find out what version of java is being used with fabric manager
	JRE_HOME="${BASEDIR}/${PKG_NAME_DIR}/config/jre_home.cfg"
	. $JRE_HOME
	get_cmd "$JAVA_BIN/java -version 2>&1" $OUTPUT_DIR/java-version

}

# Start data collection for Firelink RSM cluster nodes
pkginfo -q SUNWrsmpr
if [ $? -eq 0 ]
then
	firelink_rsm_cluster_nodes; 
fi

# Start data collection for Firelink Fabric Manager
pkginfo -q SUNWwcfmr
if [ $? -eq 0 ]
then
	firelink_fm_nodes;
fi

