#! /usr/bin/sh
#
#ident	"@(#)sanity.sh	1.8	04/12/08 SMI"
#
# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
####	------------------------------------------------------------------------
####	Updated July 1999 by Ciaran Davies
####	command-line:	driver		- name of NIC driver
####			instance	- instance of this driver to test
####			remote_machine	- name or IP for remote machine
####			broadcast_addr	- particular broadcast address for NIC	
####			run_time	- length of time to run test (secs)
####	------------------------------------------------------------------------

usage() {
	cat <<EOF
Usage:
	$0 driver remote_machine
or	$0 driver instance remote_machine
or	$0 driver instance remote_machine broadcast_address
or	$0 driver instance remote_machine broadcast_address time_to_run

	driver:			Name of the driver to check
	instance:		Instance number of the driver to check
				<default=0>
	remote_machine:		IP or hostname of a remote machine (to ping)
	broadcast_address:	Broadcast address of driver/instance
				<default read from ifconfig output>
	time_to_run:		Time in seconds to run the test for
				<default=30>

EOF
}


# Note: This value should be large enough to allow remote reply (timeout)

DEFAULTDELAY=5		# delay in between tests (secs)

# Defaults here:
drv=""
inst="0"
rmachine=""
ping=""
time_to_run=${RUNTIME:=30}
if [ $# -ge 2 ]; then
	if [ "$1" = "-h" ]; then
		usage
		exit 0
	fi

	case $# in
	2)
		drv="$1"
		rmachine="$2"
		ping=`ifconfig $drv$inst |tail +2 |head -1 |awk '{print $6}'`
	;;

	3)
		drv="$1"
		inst="$2"
		rmachine="$3"
		ping=`ifconfig $drv$inst |tail +2 |head -1 |awk '{print $6}'`
	;;
	
	4)
		drv="$1"
		inst="$2"
		rmachine="$3"
		ping="$4"
	;;

	5)
		drv="$1"
		inst="$2"
		rmachine="$3"
		ping="$4"
		time_to_run="$5"
	;;

	*)
		usage
		echo "!ERROR: Usage error."
		exit 1
	;;
	esac
	
		
else
	# Get the driver name
	while [ 1 -eq 1 ]; do
		echo "What driver?? [$drv] \c"
		read ANS
		if [ -z "$ANS" -a -z "$drv" ]; then
			echo "Driver name is required."
			continue
		fi
		drv="$ANS"
		break
	done

	# Get the instance number
	echo "What instance of $drv?? [$inst] \c"
	read ANS
	if [ -n "$ANS" ]; then
		inst="$ANS"
	fi
	ifconfig $drv$inst >/dev/null 2>&1
	if [ $? -ne 0 ]; then
		echo "!ERROR: There's no such driver/instance pair of $drv$inst."
		exit 1
	fi

	# Get the IP of the remote machine
	while [ 1 -eq 1 ]; do
		echo "Hostname / IP of remote machine?? [$rmachine] \c"
		read ANS
		if [ -z "$ANS" -a -z "$rmachine" ]; then
			echo "Remote machine address is required."
			continue
		fi
		rmachine="$ANS"
		break
	done
	
	# Get the broadcast address
	ping=`ifconfig $drv$inst 2>/dev/null |tail +2 |head -1 |awk '{print $6}'`
	echo "Broadcast address [$ping]?? \c"
	read ANS
	if [ -n "$ANS" ]; then
		ping="$ANS"
	fi

	# Get the time in seconds to run for
	echo "Time to run (seconds)? [$time_to_run] \c"
	read ANS
	if [ -n "$ANS" ]; then
		time_to_run="$ANS"
	fi
fi


##########################
# Hammer Once
##########################

delay=$DEFAULTDELAY
localhost=`cat /etc/hostname.$drv$inst`


echo "|--------------------Bringing the card Down and Up once.----------------|"
./hammer.sh $drv $inst $rmachine 1  
if [ $? -ne 0 ]
then
	echo "ERROR: Failed to bring card down and up once."
	exit 1
fi
echo "Please wait ..."
echo
echo
./delay.sh $delay

##########################
# Ping Test
##########################

echo "|-----------------------------Pinging $rmachine-------------------------|"
ping $rmachine						
if [ $? -ne 0 ]
then
	echo "ERROR: Ping test failed."
	exit 1
fi
echo "Finished."
echo "Please wait ..."
echo
./delay.sh $delay

##########################
# Hammer Test
##########################

echo "|---------------------------Hammer Test---------------------------------|"
echo

./hammer.sh $drv $inst $rmachine

./delay.sh $delay

###########################
# Broadcast Test
###########################

echo
echo 
echo "|-------------------------Broadcast Test--------------------------------|"
ping -s $ping &					## Broadcast Test
pid=$!
./delay.sh $delay

kill -2 $pid
echo "Finished Pinging."
./delay.sh $delay

###########################
# Multicast Test
###########################

echo  
echo  
echo  
echo "|-------------------------Multicast Test--------------------------------|"
if [ `uname -r` = '5.7' ]
then
	ping -i $localhost 224.0.0.1 
else
	ping -i $drv$inst 224.0.0.1
fi

############################
# Snoop Test
############################

echo  
echo  
echo  
echo "|---------------------------Snoop Test----------------------------------|"
snoop -d $drv$inst &
pid=$!
./delay.sh $delay

kill -2 $pid
echo "Finished Snooping."

############################
# Netstat
############################

echo "|-----------------------------Netstat----------------------------------------|"
netstat -i
kstat $drv:$inst

exit 0

