#!/bin/sh
#
# Run net_test to find data throughput through network
# Originally written by Ray Pasetes
# Modified by Troy Dawson on June 29, 2000

DEBUG=0
#
# CONFIG VARIABLES
#
SOURCEDIR=.
BINDIR=/root/bin
DATADIR=/root/test/nettest
MAILFILE=/tmp/nettest_failures
TCP_RATE=6144

############################################

################# Send a 2GB file ##########################
CHUNK=262144			# Bytes/Operation
COUNT=800			# Number of Chunks
#COUNT=20


testnet () {
	if [ $DEBUG -eq 1 ];
	then
		echo $cpu
	fi
	
	if [ ! -d $DATADIR ]
	then
		mkdir -p $DATADIR
	fi

	DATE="`date`"
	${BINDIR}/nettest -f $cpu $COUNT $CHUNK > /tmp/net.$$
	if [ $? -eq 0 ]
	then
		cat /tmp/net.$$ | head -6 >> $DATADIR/net_history.$cpu
		echo ""         >> $DATADIR/net_history.$cpu
		tcp_rate="`grep write /tmp/net.$$ | awk -F')' '{print $3}' | cut -d'.' -f1 | tr -d ' '`"
		rm /tmp/net.$$
	else
		tcp_rate=-1
	fi

    echo "$DATE --> $tcp_rate" >> $DATADIR/net_history.$cpu

	if [ $DEBUG -eq 1 ];
	then
		echo "$cpu TCP_RATE IS $tcp_rate"
		echo ""
	fi

	if [ $tcp_rate -lt $TCP_RATE ]; then
		cat <<-EOF >> $MAILFILE
		$cpu failed: Expected > $TCP_RATE KB/s, got $tcp_rate KB/s.
		EOF
		echo "$cpu" >> /tmp/BADLIST
	fi

}


##################################################
##################################################
if [ "$1" = "" ];
then
	echo "Usage: $0 [ipaddress | nodename]"
	exit 1
fi

cpu=$1
i=1
#
# There will be three attempts to complete a successful
# run to failed nodes.
#
while [ $i -lt 4 ]
do
	testnet
	i=`expr $i + 1`
	if [ -f /tmp/BADLIST ] ;
	then
		rm -f /tmp/BADLIST
	else
		i=4
	fi
done
