#!/bin/sh

################################################################################
##                                                                            ##
## Copyright (c) International Business Machines  Corp., 2005                 ##
##                                                                            ##
## This program is free software;  you can redistribute it and#or modify      ##
## it under the terms of the GNU General Public License as published by       ##
## the Free Software Foundation; either version 2 of the License, or          ##
## (at your option) any later version.                                        ##
##                                                                            ##
## This program is distributed in the hope that it will be useful, but        ##
## WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY ##
## or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License   ##
## for more details.                                                          ##
##                                                                            ##
## You should have received a copy of the GNU General Public License          ##
## along with this program;  if not, write to the Free Software               ##
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA    ##
##                                                                            ##
##                                                                            ##
################################################################################
#
# File:
#   dns-stress01-rmt
#
# Description:
#   This is the remote script for dns-ipv${IP_VER}-stress01
#
# Author:
#   Mitsuru Chinen <mitch@jp.ibm.com>
#
# Arguments:
#
# Exit Value:
#    0: Success
#   >0: Fail
#
# History:
#	Oct 19 2005 - Created (Mitsuru Chinen)
#
#-----------------------------------------------------------------------
# Uncomment line below for debug output.
#trace_logic=${trace_logic:-"set -x"}
$trace_logic

# Check the arguments
if [ $# -ne 7 ]; then
    echo "Usage: $0 IP_version server_ipaddr port domain minimum_id maximum_id connect_quantity"
    exit 1
fi
ip_ver="$1"
server_ipaddr="$2"
port="$3"
domain="$4"
minimum_id="$5"
maximum_id="$6"
connect_quontity="$7"

# Specify the record of dns accoring to the version of IP
case $ip_ver in
    4)
    record="A"
    ;;
    6)
    record="AAAA"
    ;;
    *)
    echo "$ver_opt is unknown IP version"
    exit 1
    ;;
esac

# Check the connectivity first
dig @$server_ipaddr -p $port node${minimum_id}.${domain} $record >/dev/null 2>&1
if [ $? -ne 0 ]; then
    echo "Failed to connect $server_ipaddr"
    exit 1
fi

#
# Loop for a large number of name lookup querys
#
num=0
id=$minimum_id
while [ $num -lt $connect_quontity ]; do
    dig @$server_ipaddr -p $port node${id}.${domain} $record >/dev/null 2>&1
    id=`expr $id + 1`
    if [ $id -gt $maximum_id ]; then
	id=$minimum_id
    fi
    num=`expr $num + 1`
done

# Check the connectivity again
dig @$server_ipaddr -p $port node${minimum_id}.${domain} $record >/dev/null 2>&1
if [ $? -ne 0 ]; then
    echo "Failed to connect $server_ipaddr. dns server seems down."
    exit 1
fi

exit 0
