#!/bin/bash          
# Setting variables
####################################################################
SCENARIO=(
ack
bye
invite
options
register
)
FREQ_CALLS=(
#100
#200
#300
#400
500
#600
#700
800
#900
1000
#1100
1200
#1300
#1400
1500
#1600
#1700
#1800
#1900
#2000
)
SCENARIOS_PATH="scenarios"
LOCAL_IP=`ifconfig|grep inet|head -1|sed 's/\:/ /'|awk '{print $3}'`
TARGET_IP="$1"
LOG="./sipp_test.log"
TEMP_LOG="/tmp/sipp_test_temp_log"
SIPP_PATH="."

####################################################################
if [ "$1" == "--help" ] || [ "$1" == "" ]; then
 echo -e "\nsipp_check.sh IP|address [[Y|N] [number]]\n"
 echo "IP|address - IP or address of tested PBX"
 echo "[[Y|N] [number]] - testing calls yes or no and registered number"
exit
fi
ulimit -c unlimited # The maximum size of core files created.
ulimit -d unlimited # The maximum size of a process's data segment.
ulimit -f unlimited # The maximum size of files created by the shell (default option)
ulimit -i unlimited # The maximum number of pending signals
ulimit -n 999999    # The maximum number of open file descriptors.
ulimit -q unlimited # The maximum POSIX message queue size
ulimit -u unlimited # The maximum number of processes available to a single user.
ulimit -v unlimited # The maximum amount of virtual memory available to the process.
ulimit -x unlimited # ???
ulimit -s 8192      # The maximum stack size
ulimit -l unlimited # The maximum size that may be locked into memory.
#ulimit -a
if [ "$2" == "Y" ]; then 
 CPU_CHECK="Y"
 rdate -s tik.cesnet.cz
else
 CPU_CHECK="N"
 FREQ_CALLS=( 100 200 300 400 500 600 700 800 900 1000 1100 1200 1300 1400 1500 1600 1700 1800 1900 2000 )
fi
rm $SCENARIOS_PATH/*.csv > /dev/null 2>&1
rm $SCENARIOS_PATH/*.log > /dev/null 2>&1
rm *.csv > /dev/null 2>&1
echo "**************************************************************************" > $TEMP_LOG
echo "`date` Starting SIPp test script on $TARGET_IP" >> $TEMP_LOG
echo "**************************************************************************" >> $TEMP_LOG
if [ "$3" != "" ]; then
 echo "Scenario;Start time;Transport;Designed attempt;Running time;Total attempt;Testing rate/s;Real rate/s;<10ms;<50ms;<100ms;<500ms;<1000ms;<5000ms;<10000ms;>=10000ms;Successful;Failed;Hang;Successfull testing calls [%]" > delay.csv
else
 echo "Scenario;Start time;Transport;Designed attempt;Running time;Total attempt;Testing rate/s;Real rate/s;<10ms;<50ms;<100ms;<500ms;<1000ms;<5000ms;<10000ms;>=10000ms;Successful;Failed;Hang" > delay.csv
fi
$SIPP_PATH/sipp -sf $SCENARIOS_PATH/options.xml -m 1 -l 1 -r 1 -i $LOCAL_IP -trace_msg -recv_timeout 1000 -timeout 1 -t u1 $TARGET_IP:5060 > /dev/null 2>&1
PBX=`cat $SCENARIOS_PATH/*.log | grep -e Agent -e Server | tail -1`
k=0			#number of open ports
echo
 if [ "$PBX" == "" ]; then 
  echo -e "Unsecured UDP transport on port 5060 \e[00;32mNOT FOUND\e[00m."
  echo "Unsecured UDP transport on port 5060 NOT FOUND." >> $TEMP_LOG
 else 
  echo -e "Unsecured UDP transport on port 5060 \e[00;31mFOUND\e[00m. Eavesdropping is possible."
  echo -e "Unsecured UDP transport on port 5060 FOUND. Eavesdropping is possible." >> $TEMP_LOG
  echo "$PBX"
  PORTS["$k"]="5060"
  TRANSPORTS["$k"]="u"
  k=`expr $k + 1`
 fi
PBX=""
rm $SCENARIOS_PATH/*.log > /dev/null 2>&1
$SIPP_PATH/sipp -sf $SCENARIOS_PATH/options.xml -m 1 -l 1 -r 1 -i $LOCAL_IP -trace_msg -recv_timeout 1000 -timeout 1 -t t1 $TARGET_IP:5060 > /dev/null 2>&1
PBX=`cat $SCENARIOS_PATH/*.log | grep -e Agent -e Server | tail -1`
 if [ "$PBX" == "" ]; then 
  echo -e "Unsecured TCP transport on port 5060 \e[00;32mNOT FOUND\e[00m."
  echo "Unsecured TCP transport on port 5060 NOT FOUND." >> $TEMP_LOG
 else 
  echo -e "Unsecured TCP transport on port 5060 \e[00;31mFOUND\e[00m. Eavesdropping is possible."
  echo -e "Unsecured UDP transport on port 5060 FOUND. Eavesdropping is possible." >> $TEMP_LOG
  echo "$PBX"
  PORTS["$k"]="5060"
  TRANSPORTS["$k"]="t"
  k=`expr $k + 1`
 fi
PBX=""
rm $SCENARIOS_PATH/*.log > /dev/null 2>&1
$SIPP_PATH/sipp -sf $SCENARIOS_PATH/options.xml -m 1 -l 1 -r 1 -i $LOCAL_IP -trace_msg -recv_timeout 1000 -timeout 1 -t l1 $TARGET_IP:5061 > /dev/null 2>&1
PBX=`cat $SCENARIOS_PATH/*.log | grep -e Agent -e Server | tail -1`
 if [ "$PBX" == "" ]; then 
  echo -e "Secured tls transport on port 5061 \e[00;31mNOT FOUND\e[00m. You may use this for security." 
  echo -e "Secured tls transport on port 5061 NOT FOUND. You may use this for security." >> $TEMP_LOG
 else 
  echo -e "Secured tls transport on port 5061 \e[00;32mFOUND\e[00m. Secure call enabled."
  echo -e "Secured tls transport on port 5061 FOUND. Secure call enabled." >> $TEMP_LOG
  echo "$PBX"
  PORTS["$k"]="5061"
  TRANSPORTS["$k"]="l"
 fi
PBX=""
rm $SCENARIOS_PATH/*.log > /dev/null 2>&1
if [ "${PORTS[0]}" == "" ]; then
 echo "No answer from PBX" 
 exit
fi
l="0"	#Max attempts position
echo -e "\nPlease wait, testing in progress, this may take a while."
 for scenario in ${SCENARIO[*]}; do	#loop for each scenario
  for (( i = 0; i <= $k ; i++)); do	#loop for each trasport 
   TRANSPORT=`echo  ${TRANSPORTS[$i]} | sed -e s/u/UDP/ -e s/t/TCP/ -e s/l/tls/` #convert to full name for log	
   MAX_ATTEMPTS[$l]="$scenario-$TRANSPORT-2000/sec."
    for freq in ${FREQ_CALLS[*]}; do	#loop for each frequency
     echo `date`" Test for $freq $scenario attempts/s, transport $TRANSPORT." >> $TEMP_LOG
     CALLS=`expr $freq \* 30`
     echo -e "Testing scenario \e[00;34m$scenario for $freq attempts/s\e[00m, transport $TRANSPORT."
     SECS=`date +%S`
     WAIT=`expr 60 - $SECS` 
     echo "Waiting $WAIT sec. for timesync" 
     sleep $WAIT
     echo "You may make a testing call to any registered number in next 30 s."
     TIME=`date +%H:%M:%S`
     if [ "$3" != "" ]; then
      $SIPP_PATH/sipp -sf $SCENARIOS_PATH/call.xml -bg -s "$3" -p 65001 -t "${TRANSPORTS[$i]}"1 -r 1 -trace_stat -send_timeout 5000 -timeout 45s -m 30 -rtp_echo -i $LOCAL_IP $TARGET_IP:"${PORTS[$i]}" > /dev/null 2>&1
     fi
     $SIPP_PATH/sipp -sf $SCENARIOS_PATH/$scenario.xml -p 65002 -t "${TRANSPORTS[$i]}"1 -l $CALLS -r $freq -trace_stat -send_timeout 5000 -recv_timeout 15000 -timeout 53s -timeout_error -m $CALLS -i $LOCAL_IP $TARGET_IP:"${PORTS[$i]}" > /dev/null 2>&1
     echo "End of test, waiting for flushing buffers."
     sleep 20
     mv $SCENARIOS_PATH/$scenario*.csv "test_res_$scenario $freq per_sec_transport $TRANSPORT.csv"
     if [ "$3" != "" ]; then	#if testing call enable, rename its log
      mv $SCENARIOS_PATH/call*.csv "test_call_$scenario $freq per_sec_transport $TRANSPORT.csv"
     fi
     sleep 2
     echo "Rate/s   <10ms  <50ms  <100ms <500ms <1s    <5s    <10s   >=10s  Succ.  Failed" | tee -a $TEMP_LOG
     printf "%-8s %-6s %-6s %-6s %-6s %-6s %-6s %-6s %-6s %-6s %-6s \n" `tail -1 "test_res_$scenario $freq per_sec_transport $TRANSPORT.csv" | awk -F ";" '{ print $8 " " $66 " " $67 " " $68 " " $69 " " $70 " " $71 " " $72 " " $73 " " $16 " " $18 }'` | tee -a $TEMP_LOG
     DATA=`tail -1 "test_res_$scenario $freq per_sec_transport $TRANSPORT.csv" | awk -F ";" '{ print $5 ";" $13 ";" $6 ";" $8 ";" $66 ";" $67 ";" $68 ";" $69 ";" $70 ";" $71 ";" $72 ";" $73 ";" $16 ";" $18 ";" $14 }'`
     FAILED=`tail -1 "test_res_$scenario $freq per_sec_transport $TRANSPORT.csv" | awk -F ";" '{ print $18 }'`
     if [ "$3" != "" ]; then	#if testing call enable, calculate its efficiency
      TEST_CALLS=`tail -1 "test_call_$scenario $freq per_sec_transport $TRANSPORT.csv" | awk -F ";" '{ print $16 }'`
      TEST_CALLS=`expr $TEST_CALLS \* 100 \* 33 / 1000 + $TEST_CALLS \* 100 % 33 / 30`
      echo "$scenario;$TIME;$TRANSPORT;$CALLS;$DATA;$TEST_CALLS">> "delay.csv"
      echo "$TEST_CALLS % successfull testing calls" | tee -a $TEMP_LOG
     else
      echo "$scenario;$TIME;$TRANSPORT;$CALLS;$DATA">> "delay.csv" 
     fi
     DELAY_ARRAY=( `tail -1 "test_res_$scenario $freq per_sec_transport $TRANSPORT.csv" | awk -F ";" '{ print $66 " " $67 " " $68 " " $69 " " $70 " " $71 " " $72 " " $73 }'` )
     if [ "$3" != "" ]; then	#if testing call enable
      if [ "$TEST_CALLS" -lt "100" ]; then	#if efficiency is lower than 100%, show it
       echo -e "\e[00;31m!!!ERROR!!!\e[00m, PBX cannot answer to all testing calls!!!"
       echo "!!!ERROR!!!, PBX cannot answer to testing calls!!!" >> $TEMP_LOG
      fi
     fi
     if [ "${DELAY_ARRAY[3]}" -gt "0" ]; then
      echo -e "\e[00;35mNOTIFICATION\e[00m, ${DELAY_ARRAY[3]} of $CALLS answer(s) is delayed more than 100ms."
      echo "NOTIFICATION, ${DELAY_ARRAY[3]} of $CALLS answer(s) is delayed more than 100ms." >> $TEMP_LOG
     fi
     if [ "${DELAY_ARRAY[4]}" -gt "0" ]; then
      echo -e "\e[00;33mALERT\e[00m, ${DELAY_ARRAY[4]} of $CALLS answer(s) is delayed more than 500ms."
      echo "ALERT, ${DELAY_ARRAY[4]} of $CALLS answer(s) is delayed more than 500ms." >> $TEMP_LOG
     fi
     if [ "${DELAY_ARRAY[5]}" -gt "0" ]; then
      echo -e "\e[00;31mWARNING\e[00m, ${DELAY_ARRAY[5]} of $CALLS answer(s) is delayed more than 1s!"
      echo "WARNING, ${DELAY_ARRAY[5]} of $CALLS answer(s) is delayed more than 1s!" >> $TEMP_LOG
     fi
     if [ "${DELAY_ARRAY[6]}" -gt "0" ]; then
      echo -e "\e[00;31mWARNING\e[00m, ${DELAY_ARRAY[6]} of $CALLS answer(s) is delayed more than 5s!"
      echo "WARNING, ${DELAY_ARRAY[6]} of $CALLS answer(s) is delayed more than 5s!" >> $TEMP_LOG
     fi
     if [ "${DELAY_ARRAY[7]}" -gt "0" ]; then
      echo -e "\e[00;31mWARNING\e[00m, ${DELAY_ARRAY[7]} of $CALLS answer(s) is delayed more than 10s (timeout)!!!"
      echo "WARNING, ${DELAY_ARRAY[7]} of $CALLS answer(s) is delayed more than 10s (timeout)!!!" >> $TEMP_LOG
     fi
     if [ "$3" != "" ]; then	#if testing call enable
      if [ "${DELAY_ARRAY[3]}" -eq "0" ] && [ "${DELAY_ARRAY[4]}" -eq "0" ] && [ "${DELAY_ARRAY[5]}" -eq "0" ] && [ "${DELAY_ARRAY[6]}" -eq "0" ] && [ "${DELAY_ARRAY[7]}" -eq "0" ] && [ "$TEST_CALLS" -eq "100" ]; then	#criteria for succesfull test, delay is lower than 100ms, testing calls 100%
       echo -e "\e[00;32mOK\e[00m, the PBX is able to serve $freq $scenario per sec."
       echo "OK, the PBX is able to serve $freq $scenario per sec." >> $TEMP_LOG
      fi
     else	#if testing call disable
      if [ "${DELAY_ARRAY[3]}" -eq "0" ] && [ "${DELAY_ARRAY[4]}" -eq "0" ] && [ "${DELAY_ARRAY[5]}" -eq "0" ] && [ "${DELAY_ARRAY[6]}" -eq "0" ] && [ "${DELAY_ARRAY[7]}" -eq "0" ]; then	#criteria for succesfull test, delay is lower than 100ms
       echo -e "\e[00;32mOK\e[00m, the PBX is able to serve $freq $scenario per sec."
       echo "OK, the PBX is able to serve $freq $scenario per sec." >> $TEMP_LOG      
      fi
     fi
      if [ "${DELAY_ARRAY[5]}" -eq "0" ] && [ "${DELAY_ARRAY[6]}" -eq "0" ] && [ "${DELAY_ARRAY[7]}" -eq "0" ] && [ "$FAILED" -gt "$freq" ]; then
       if [ "$CPU_CHECK" == "N" ]; then	#without PBX access, brake
        echo -e "\e[00;31mThe PBX is probably protected, flood attack isn´t effective.\e[00m"
        echo "The PBX is probably protected, flood attack isn´t effective." >> $TEMP_LOG
        MAX_ATTEMPTS[$l]="$scenario-$TRANSPORT-protected."  
        break
       fi
      fi
     if [ "${DELAY_ARRAY[5]}" -gt "0" ] || [ "${DELAY_ARRAY[6]}" -gt "0" ] || [ "${DELAY_ARRAY[7]}" -gt "0" ]; then
     if [ "${DELAY_ARRAY[5]}" -eq "0" ] && [ "${DELAY_ARRAY[6]}" -eq "0" ] && [ "${DELAY_ARRAY[7]}" -gt "$freq" ]; then
      if [ "$CPU_CHECK" == "N" ]; then
       echo -e "\e[00;31mThe PBX is probably protected, flood attack isn´t effective.\e[00m"
       echo "The PBX is probably protected, flood attack isn´t effective." >> $TEMP_LOG
       MAX_ATTEMPTS[$l]="$scenario-$TRANSPORT-protected."
       break
      fi
     else
      if [ "$CPU_CHECK" == "N" ]; then
       echo -e "\e[00;31mWARNING, The PBX is going to overload, maximum ($freq) attempts per sec. reached!\e[00m"
       echo "WARNING, The PBX is going to overload, maximum ($freq) attempts per sec. reached!" >> $TEMP_LOG
       MAX_ATTEMPTS[$l]="$scenario-$TRANSPORT-$freq/sec."
       break
      fi
     fi
    fi
   done
  l=`expr $l + 1`
  done
 done
cat test*.csv > cumulative_results.csv
if [ "$CPU_CHECK" == "Y" ]; then
 echo `date`" Test completed, please press Ctrl+c on server machine, copy siptop.log here and run siptop_log.sh file."  | tee -a $TEMP_LOG
else
 echo -e "\nPBX maximum rates:\n" | tee -a $TEMP_LOG
 for result in ${MAX_ATTEMPTS[*]}; do	#loop for each result  
  echo "$result" | tee -a $TEMP_LOG
 done
fi
echo "For detail info look at file delay.csv"  | tee -a $TEMP_LOG
cat $TEMP_LOG >> $LOG
rm $TEMP_LOG
exit 0
