0

I have written a script, which polls a log file entries last half an hour only and send an email, if error is found.I have scheduled this script to run in crontab for every half an hr.

Below is the script. But this script is not working as I wanted .For ex. if it runs at 11:30 AM, it should scan for the log for duration between 11:00:00 AM to 11:30:00 AM. Instead, it is scanning the file for "00:00" or "30:00" also. I guess, I have made some mistake in applying regular expressions, could anyone help?

blogs=/opt/docs/datapower/prod/business.log
slogs=/opt/docs/datapower/prod/system.log


starttime=$(date +'%H')
currmin=$(date +'%M')
curdate=`date|cut -d' ' -f5`
echo $(date)

if [ $currmin -le 29 ] && [ $starttime -ne 00 ] ; then
starttime1=`echo "$(date +'%H') - 1" | bc`
logtime="$starttime1"
logtime="$logtime:[3-5][0-9]"
echo $logtime



elif [ $currmin -le 29 ] && [ $starttime -eq 00 ] ; then
logtime="23:[3-5][0-9]"
echo $logtime



else
logtime="$starttime"
logtime="$logtime:[0-2][0-9]"
echo $logtime

fi

if ( grep "$logtime" $slogs | egrep "AAA Authentication Failure|AAA Authorization Failure") > dptest 2>&1;then

       Do something

fi 

Below is the example log entry

Nov 20 06:06:58 business-log-sta [DP-Domain-STAGING][0x80000001][business-log][info] mpgw(GenServiceMPG): trans(31513092)[request]: AAA Authentication failure/>
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
user2607367
  • 225
  • 4
  • 25
  • possible duplicate of [Scanning the log files for last 30 minutes of data](http://stackoverflow.com/questions/27037369/scanning-the-log-files-for-last-30-minutes-of-data) – arco444 Nov 20 '14 at 15:16

1 Answers1

0

Your code works on my system, the only problem I was having is that "AAA Authentication Failure" is written with a capital 'F' in your code and a lower case 'f' in your log file.

You can reduce your code too by changing lines like

logtime="$starttime1"
logtime="$logtime:[3-5][0-9]"

to logtime="$starttime1:[3-5][0-9]".

Edit: If you want I can supply sample outputs of the script working on my system.

ShellFish
  • 4,351
  • 1
  • 20
  • 33
  • Here we are searching for pattern `hh:mm`, can we search for pattern `hh:mm:ss`and how? – user2607367 Nov 21 '14 at 11:28
  • Why would you want to do that, you're getting all files within the desired interval are you not? You could add `:[0-5][0-9]` to the variable but this will result in the same output. I guess I don't really understand your question. – ShellFish Nov 21 '14 at 11:43