Please help me to finish this scripts

Hi All,

Can anyone help me to finish the scripts. this scripts is
this scripts will run on crontab in every 4 minutes to get the newest router interface status.

I would like to add a function to count the router interface flapping, if more than 2 times in 20 minutes (scripts run 5 times) then will sent out an email. But I know my programme knowledge is week. Can anyone help me to finish this. thanks.

dir=/testing
capturelog=$dir/capture.log
flapcountlog=$dir/flapcount.log
ovcmd=/opt/OV/bin/ovtopodump
$ovcmd | grep "Critical" > $capturelog
if [ -f $capturelog ]; then
        while read line
        do
        routername=`echo $line | awk '{print $3}'`
        interface=`echo $line | awk '{print $5}'`
        status=`echo $line | awk '{print $4}'`
        count=11111
 counter=`echo $count | cut -b 2-`
        
 if [ "$status" ]; then
  if [ -f $capturelog ]; then
   while read line
          do
   flaprouter=`echo $line | awk '{print $1}'`
          flapinterface=`echo $line | awk '{print $2}'`
   flapcount=`echo $line | awk '{print $4}'`
   if [ $routername -eq $flaprouter ]; then
    if [ $interface -eq $flapinterface ]; then
     $count=$flapcount
    else
     $count=$count
    fi
   else
    $count=$count
   fi
   done < $flapcountlog
  $count="$count0"
 else
  $count="$count1"
                fi
        echo $count
        echo $counter
 echo "$routername\t$interface\t$status\t$counter\n" > $flapcountlog
 
        done < $capturelog
else
        echo "missing"
fi
cat $flapcountlog
if [ -f $flapcountlog ]; then
        while read line
        do
 $count
 flapcount=`echo $count | awk '{ x=gsub("0",""); print x }'`
        if [ flapcount >= 2 ]; then
  echo "flapping"
  echo $flapcount
  mailx -s "$subject" "testing@email.com"
 else
  echo "normal"
  echo $flapcount
  mailx -s "$subject" "testing@email.com"
 fi
        done < $flapcountlog
#####################################
#XXXX IP ROUTERA.XXXX.com  Critical    192.168.1.1
#XXXX IP ROUTERB.XXXX.com Critical    192.168.1.2
#XXXX IP ROUTERC.XXXX.com Critical    192.168.1.3
#XXXX IP ROUTERD.XXXX.com  Critical    192.168.1.4
#XXXX IP ROUTERE.XXXX.com   Critical    192.168.1.5
#####################################

If there's no timestamps in the log you can't count how many per period of time without watching. How about a script which runs continuously?