#!/bin/bash
#  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
#
#  Author : Juan Antonio Alvarez <jualvarez@gmail.com>
#           Ignacio Martin
#

# Requirements:
# - bash
# - awk
# - netcat
# - net-snmp

#[general]
test_name=asterisk_test7 # just for reference. A directory will be created with this name to dump data
sut=192.168.101.247 # Server Under Test
sipp_path=/home/juan/src/sipp.2006-11-30
sipp_executable=sipp
test_duration=300   # Test duration in minutes.
log_frequency=1   # Set the statistics dump log report frequency [mins]
increase=10   # Increase n calls
increase_every=6 # Increase calls every n mins.

#[sipp_basic]
service_name=s   # Set the username part of the resquest URI.
duration=default   # Duration of the call
call_rate=10   # Call rate
rate_period=default   # rate period for call rate
max_sockets=default   # Set the max number of sockets to open simultaneously.
scenario_file=default   # Loads an alternate xml scenario file.
scenario_name=default   # Use a default scenario.
transport_mode=default   # [u1|un|ui|t1|tn|l1|ln] : Set the transport mode
rtp_echo=default   # Enable RTP echo.
media_port=default   # Set the local RTP echo port number.
inf=default   # csv to inject values from
calls_limit=default   # Set the maximum number of simultaneous calls.
max_calls=default   # Stop the test and exit when 'calls' calls are processed.

#[sipp_logs]
trace_msg=default   # Displays sent and received SIP messages in <scenario file name>_<pid>_messages.log
trace_screen=true   # Dump statistic screens in the  <scenario_name>_<pid>_screens.log
trace_timeout=default   # Displays call ids for calls with timeouts in <scenario file name>_<pid>_timeout.log
# trace_stat=default   # Dumps all statistics in <scenario_name>_<pid>.csv !!!OBSOLETE ALLWAYS ON!!!
stats_filename=sipp_stats   # Set the file name to use to dump statistics
trace_err=true   # Trace all unexpected messages in <scenario file name>_<pid>_errors.log
trace_logs=default   # Allow tracing of <log> actions in <scenario file name>_<pid>_logs.log
trace_rtt=default   # Allow tracing of all response times in <scenario file name>_<pid>_rtt.csv
rtt_freq=default   #  Dump response times every freq calls in the log file defined by -trace_rtt
# stat_frequency=default   # Set the statistics report frequency on screen [secs] OBSOLETE

#[sipp_advanced]  !!!!! NOT IMPLEMENTED !!!!!

local_port=default   # set the local port number
local_ip=default   # local IP address for several headers
bind_local=default   # the local IP address is used as the source IP address.
timer_resol=default   # Set the timer resolution in milliseconds.
max_recv_loops=default   # maximum number of messages received read per cycle.
up_nb=default   # number of updates of the internal clock during the reading of received messages.
base_cseq=default   # Start value of [cseq] for each call.
auth_uri=default   # Force the value of the URI for authentication.
ip_field=default   # Set which field from the injection file contains the IP address.
rtt_freq=default   # Dump response times every freq calls in the log file 
auth_password=default   # Set the password for authentication challenges.
tls_cert=default   # Set the name for TLS Certificate file.
tls_key=default   # Set the name for TLS Private Key file.
tls_crl=defailt   # Set the name for Certificate Revocation List file.
local_rtp_ip=default   # Set the local media IP address.
rtp_buf_size=default   # Set the RTP echo buffer size (default: 2048).
third_pcc=default   # ip:port. Launch the tool in 3pcc mode
udp_retrans_off=default   # Disable retransmission in UDP mode.
max_udp_retrans=default   # Maximum number of UDP retransmissions before call ends on timeout.
no_default=default   # No Default. Disable all default behavior of SIPp
rsa=default   # Set the remote sending address to host:port.
max_reconnect=default   # Set the the maximum number of reconnection.
aa=default   # Enable the automatic answer for the INFO msgs.

#[fixed]
bg=1         # background mode

#[snmp]
version="2c"
community="sipptest"
sys_descr="SNMPv2-MIB::sysDescr.0"

total_tables=0 # Number of tables retrieved

total_walks=7
walk[1]=".1.3.6.1.4.1.2021" # UC Davis MIB
#walk[2]=".1.3.6.1.2.1.25.4.2" # process list
#walk[3]=".1.3.6.1.2.1.25.5.1" # CPU and mem
walk[2]=".1.3.6.1.2.1.25.2.3" # Storage
walk[3]=".1.3.6.1.2.1.2.2" # Interfaces
walk[4]=".1.3.6.1.2.1.1" # System
walk[5]=".1.3.6.1.2.1.4" # ip
walk[6]=".1.3.6.1.2.1.5" # icmp
#walk[7]=".1.3.6.1.2.1.6" # tcp
#walk[8]=".1.3.6.1.2.1.7" # udp
#host
walk[7]=".1.3.6.1.2.1.25.1" # hrSystem

##### Test start time #####
b_time=`date +%s`
echo Starting the test on time: $b_time

##### Get System info #####

system=`snmpget -v $version -c $community $sut $sys_descr`

if [ $? -ne 0 ]
then
	echo Error: Could not establish SNMP connection!
	exit 33
fi
echo Testing: $system


mkdir $test_name
cd $test_name



##### SIPP COMMAND PREP #####

sipp_comm_line=$sipp_path/$sipp_executable

## FIXED ##
if [[ $bg == 1 ]]
then
	sipp_comm_line="${sipp_comm_line} -bg"
fi

## BASIC ##

if [ $service_name != default ]
then
	sipp_comm_line="${sipp_comm_line} -s  ${service_name}"
fi
if [ $duration != default ]
then
	sipp_comm_line="${sipp_comm_line} -d ${duration}"
fi
if [ $call_rate != default ]
then
	sipp_comm_line="${sipp_comm_line} -r ${call_rate}"
fi
if [ $rate_period != default ]
then
	sipp_comm_line="${sipp_comm_line} -rp ${rate_period}"
fi
if [ $max_sockets != default ]
then
	sipp_comm_line="${sipp_comm_line} -max_sockets ${max_sockets}"
fi
if [ $scenario_file != default ]
then
	sipp_comm_line="${sipp_comm_line} -sf ${scenario_file}"
fi
if [ $scenario_name != default ]
then
	sipp_comm_line="${sipp_comm_line} -sn ${scenario_name}"
fi
if [ $transport_mode != default ]
then
	sipp_comm_line="${sipp_comm_line} -t ${transport_mode}"
fi
if [ $rtp_echo != default ]
then
	sipp_comm_line="${sipp_comm_line} -rtp_echo ${rtp_echo}"
fi
if [ $media_port != default ]
then
	sipp_comm_line="${sipp_comm_line} -mp ${media_port}"
fi
if [ $inf != default ]
then
	sipp_comm_line="${sipp_comm_line} -inf ${inf}"
fi
if [ $calls_limit != default ]
then
	sipp_comm_line="${sipp_comm_line} -l ${calls_limit}"
fi
if [ $max_calls != default ]
then
	sipp_comm_line="${sipp_comm_line} -m ${max_calls}"
fi

### STATS ###
if [ $stats_filename != default ]
then
	sipp_comm_line="${sipp_comm_line} -stf ${stats_filename}_${b_time}"
fi
if [ $trace_msg != default ]
then
	sipp_comm_line="${sipp_comm_line} -trace_msg"
fi
if [ $trace_screen != default ]
then
	sipp_comm_line="${sipp_comm_line} -trace_screen"
fi
if [ $trace_timeout != default ]
then
	sipp_comm_line="${sipp_comm_line} -trace_timeout"
fi
if [ $trace_err != default ]
then
	sipp_comm_line="${sipp_comm_line} -trace_err"
fi
if [ $trace_logs != default ]
then
	sipp_comm_line="${sipp_comm_line} -trace_logs"
fi
if [ $trace_rtt != default ]
then
	sipp_comm_line="${sipp_comm_line} -trace_rtt"
fi
if [ $rtt_freq != default ]
then
	sipp_comm_line="${sipp_comm_line} -rtt_freq ${rtt_freq}"
fi



## GENERAL ##

let "fd = $log_frequency * 60"
sipp_comm_line="${sipp_comm_line} -trace_stat -fd ${fd}"
sipp_comm_line="${sipp_comm_line} ${sut}"

# Buscamos el puerto en que va a estar escuchando esta
# instancia de sipp
sipp_instances=`pidof sipp | awk '{print NF}'`
let "sipp_port = 8888 + sipp_instances"
$sipp_comm_line
echo SIPp was run using: $sipp_comm_line

# Ciclos de ejecución

let "last_l_time = -1" # Para tomar datos en el momento 0
let "elapsed = 0" #( $t_time - $b_time ) / 5"

while (( elapsed <= test_duration ))
do

	# Verificar si hay que incrementar
	if (( (elapsed != last_i_time) && ((elapsed % increase_every) == 0) ))
	then
		echo "=====>    increasing calls...    <====="
		i=$increase
		
		# SIPp no permite llevar el numero de llamadas a "n"
		# entonces es necesario hacer lo siguiente para incrementar
		# las llamadas
		while (( (i % 10) != 0 ))
		do
			echo "+" | nc -u 127.0.0.1 $sipp_port -q 0
			let "i = i - 1"
		done
		while (( i > 0 ))
		do
			echo "*" | nc -u 127.0.0.1 $sipp_port -q 0
			let "i = i - 10"
		done
		last_i_time=$elapsed
	fi

	# Verificar si hay que loguear
	if (( (elapsed != last_l_time) && ((elapsed % log_frequency) == 0) ))
	then
		echo "<<<<<    taking data...    >>>>>"
		i=$total_tables
		while (( $i > 0 ))
		do
			snmptable -v $version -c $community $sut ${table[$i]} >> snmptable${table[$i]}_$b_time
			let "i = i - 1"
		done
		i=$total_walks
		while (( $i > 0 ))
		do
			snmpwalk -v $version -c $community $sut ${walk[$i]} >> snmpwalk${walk[$i]}_$b_time
			let "i = i - 1"
		done
		last_l_time=$elapsed
	fi

	sleep 1
	t_time=`date +%s`
	let "elapsed = ( $t_time - $b_time ) / 60"
done

echo "q" | nc -u 127.0.0.1 $sipp_port -q 0

echo Test finalized on time: $t_time

exit 0



syntax highlighted by Code2HTML, v. 0.9.1