#!/bin/bash

PBX_BIN=`tail -2 siptop.log | head -1 | awk '{ print $(NF) }'`
#PBX_BIN=`tail -1 siptop.log | awk '{ print $(NF) }'`
cat siptop.log | grep -e "load average" -e $PBX_BIN | sed '$!N;s/\n/ /' | awk -F " " '{ print $3 ";" $(NF-6) ";" $(NF-3) }' > siptop.csv
k=2
LINES_METHODS=`wc -l delay.csv | awk -F " " '{ print $1}'`	#lines of delay.csv
METHOD=`tail -1 delay.csv | awk -F ";" '{ print $1}'`		#last method from delay.csv
ROUNDS_METHODS=`cat delay.csv |grep $METHOD |wc -l`		#number of rounds
TEMP=`expr "$LINES_METHODS" - "1"`
NUM_METHODS=`expr $TEMP / "$ROUNDS_METHODS"`			#number of methods
STARTTIME=`sed -n '2,2p' delay.csv | awk -F ";" '{ print $2}'`	#start of test time
ENDTIME=`tail -1 delay.csv | awk -F ";" '{ print $2}'`		#end of test time
STARTLINE=`sed -n -e '/'$STARTTIME'/{=}' -e h siptop.csv`
TEMP=`sed -n -e '/'$ENDTIME'/{=}' -e h siptop.csv`
ENDLINE=`expr 30 + $TEMP`
LINES=`expr $ENDLINE - $STARTLINE`
HIGH_CPU="80"							#define % of high CPU
rm -f CPUTEMP.csv
rm -f MEMTEMP.csv
for (( i = 2; i <= $LINES_METHODS ; i = $i+$ROUNDS_METHODS))
 do
  TEMP=`sed -n ''$i','$i'p' delay.csv | awk -F ";" '{ print $1}'`		
  echo -n ";;;;;;;;;;;;;;;;;;;;;$TEMP;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;" >>"CPUTEMP.csv"
  echo -n ";;;;;;;;;;;;;;;;;;;;;$TEMP;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;" >>"MEMTEMP.csv"
 done

for (( k = 0; k <= $ROUNDS_METHODS-1 ; k++ ))
do

    echo "">>"CPUTEMP.csv"
    echo "">>"MEMTEMP.csv"
l=`expr 2 + $k`
  TRANSPORT=`sed -n ''$l','$l'p' delay.csv | awk -F ";" '{ print $3}'`
  RATE=`sed -n ''$l','$l'p' delay.csv | awk -F ";" '{ print $7}'`


    echo -n "$RATE/s $TRANSPORT">> "CPUTEMP.csv"
    echo -n "$RATE/s $TRANSPORT">> "MEMTEMP.csv"


for (( i = 2+$k; i <= $LINES_METHODS ; i = $i+$ROUNDS_METHODS ))
 do
  STARTTIME=`sed -n ''$i','$i'p' delay.csv | awk -F ";" '{ print $2}'`
  STARTLINE=`sed -n -e '/'$STARTTIME'/{=}' -e h siptop.csv`		
  METHOD=`sed -n ''$i','$i'p' delay.csv | awk -F ";" '{ print $1}'`
  TRANSPORT=`sed -n ''$i','$i'p' delay.csv | awk -F ";" '{ print $3}'`
  RATE=`sed -n ''$i','$i'p' delay.csv | awk -F ";" '{ print $7}'`
  high=0	#secs of high CPU usage
  AVERAGE_CPU=0
   for (( j = `expr $STARTLINE - 5`; j <= `expr $STARTLINE + 45` ; j++ ))
    do
     CPU=`sed -n ''$j','$j'p' siptop.csv | awk -F ";" '{ print $3}'`
     MEM=`sed -n ''$j','$j'p' siptop.csv | awk -F ";" '{ print $2}'`
     if [ "${CPU/.*}" -gt "$HIGH_CPU" ]; then
      high=`expr $high + 1`
     fi
     if [ "$j" -ge "$STARTLINE" ] && [ "$j" -le "`expr $STARTLINE + 30`" ]; then
      AVERAGE_CPU=$(echo $AVERAGE_CPU + $CPU | bc)
     fi
    echo -n ";$CPU">> "CPUTEMP.csv"
    echo -n ";$MEM">> "MEMTEMP.csv"
   done
  AVERAGE_CPU=$(echo $AVERAGE_CPU / 30 | bc)
  echo -en "\nAverage CPU: $AVERAGE_CPU%, flood: $RATE $METHOD/s on $TRANSPORT"
   if [ "$high" -gt "5" ]; then
    echo -en ", \e[00;31m$high sec. of 50 is greather than $HIGH_CPU%\e[00m"
   fi
done

done
echo
cat CPUTEMP.csv | tr "." "," >CPU.csv
cat MEMTEMP.csv | tr "." "," >MEM.csv
rm -f CPUTEMP.csv
rm -f MEMTEMP.csv
exit 0
