#!/bin/sh

########################################################################
#  Cron job called every 30minutes to run ipaudit and deal with files
#  and reports
########################################################################

#  Make sure we start on the half hour (sometime crons gets it wrong?)
minute=`date +%M`
if [ $minute -eq 29  -o  $minute -eq 59 ] 
then
   sleep 30
fi

#
#  Constants
#
CURDATE_DEF=`date`
CURDATE=`date "+%Y-%m-%d-%H:%M" -d "$CURDATE_DEF"`
IP_DIR=/usr2/WatchDog/ipaudit


#
#  Signal previous instance to finish up
#  (won't actually die until work is done)
#
oldpid=`cat $IP_DIR/run/ipaudit.pid`
kill -2 $oldpid
if [ $? -ne 0 ]
then
   echo '$IP_DIR/ipaudit died prematurely'
fi


#
#  Start new sequence of programs (enclose in parenthesis) beginning with
#  ipaudit.  This instance of ipaudit will be 'kill -2' at the start of 
#  next time period.
#    Upon receiving this signal ipaudit will stop gathering data and print 
#  a summary, and stop execution.  The subequent programs will then be 
#  executed in turn

(

   #  Gather IP data an summarize (when kill -2 signal received)
   $IP_DIR/ipaudit -i $IP_DIR/run/ipaudit.pid  -p'6,21,23,111,513,1524,6667' -w $IP_DIR/dump/$CURDATE.dump -o $IP_DIR/data/$CURDATE.txt eth1  
   
   #  Generate report from summary data
   REP_DIR=$IP_DIR/reports/30min
   cat $IP_DIR/data/$CURDATE.txt | $REP_DIR/MakeReport30 $REP_DIR/html/$CURDATE.html
   
   #  Zip newly created files
   gzip $IP_DIR/dump/$CURDATE.dump
   gzip $IP_DIR/data/$CURDATE.txt
) &


#
#  Erase old dump files
#
OLDDATE=`date "+%Y-%m-%d" -d "$CURDATE_DEF 4 days ago"`
rm "$IP_DIR/dump/$OLDDATE*dump" 2> /dev/null