help with a shell script that greps an error from the logs


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting help with a shell script that greps an error from the logs
# 1  
Old 10-05-2010
help with a shell script that greps an error from the logs

Hello everyone.
I wrote the following script but the second part is not excecuting. It is not sending the notification by email if the error occurs.
the send mail is working so i think the errorr should be in the if statement
Code:
LOGDIR=/logs/out
LOG=`date "+%Y%m%d"`.LOG-FILE.out #the log file 
TEMPDIR=/logs/out/test
DATE=`date +"%d-%m-%Y"`
TIME=`date +"%H:%M:%S"`
CUR_SIZE=`wc -l $LOGDIR/$LOG | awk '{print $1}'`
PREV_SIZE=`cat $TEMPDIR/log.size`
if [ $CUR_SIZE -eq $PREV_SIZE ]
then
echo " Nothing to do..."
> $TEMPDIR/log.diff
fi
if [ $CUR_SIZE -gt $PREV_SIZE ]
then
let STSIZE=$PREV_SIZE+1
sed -n "$STSIZE,$CUR_SIZE p" $LOGDIR/$LOG > $TEMPDIR/log.diff
grep 'the message i want to grep from logs' $TEMPDIR/log.diff> $TEMPDIR/log.alarm
if [ `cat $TEMPDIR/log.alarm |wc -l` -gt 0 ]
then
/usr/sbin/sendmail -f ... -t my email address < $TEMPDIR/log.alarm
fi
fi
echo $CUR_SIZE > $TEMPDIR/log.size


Last edited by vbe; 10-05-2010 at 06:56 AM.. Reason: code tags please
# 2  
Old 10-05-2010
Is this how we should read the code? ( I just done a bit of formatting to ease the reading, and be sure we agree on what is done...)
Code:
LOGDIR=/logs/out
LOG=`date "+%Y%m%d"`.LOG-FILE.out #the log file 
TEMPDIR=/logs/out/test
DATE=`date +"%d-%m-%Y"`
TIME=`date +"%H:%M:%S"`
CUR_SIZE=`wc -l $LOGDIR/$LOG | awk '{print $1}'`
PREV_SIZE=`cat $TEMPDIR/log.size`
if [ $CUR_SIZE -eq $PREV_SIZE ]
then
   echo " Nothing to do..." > $TEMPDIR/log.diff
else
if [ $CUR_SIZE -gt $PREV_SIZE ]
then
   let STSIZE=$PREV_SIZE+1
   sed -n "$STSIZE,$CUR_SIZE p" $LOGDIR/$LOG > $TEMPDIR/log.diff
   grep 'the message i want to grep from logs' $TEMPDIR/log.diff> $TEMPDIR/log.alarm
   if [ `cat $TEMPDIR/log.alarm |wc -l` -gt 0 ]
   then
      /usr/sbin/sendmail -f ... -t my email address < $TEMPDIR/log.alarm
   fi
fi
echo $CUR_SIZE > $TEMPDIR/log.size

# 3  
Old 10-05-2010
Are you getting any error? or just not receiving mail?
Are you sure the execution is going inside if to call sendmail?
try putting a msg inside if. and echo the count of "$TEMPDIR/log.alarm".

there is no need of cat

use
Code:
wc -l < $TEMPDIR/log.alarm

# 4  
Old 10-05-2010
does the script throw any error msg....?
do $TEMPDIR/log.diff and $TEMPDIR/log.alarm get created...?
# 5  
Old 10-05-2010
Try to run the script in debug mode to see what happens, add the following line:
Code:
set -x

below the shebang #!/bin/ksh
# 6  
Old 10-05-2010
Where does the message :
Quote:
'the message i want to grep from logs'
gets into your log.diff so you can grep it? (I see no where...) and
Code:
echo " Nothing to do..."
> $TEMPDIR/log.diff

At line 11, you just zeroed the log instead of outputing to it (but maybe thats what you want.. but then its difficult to judge on content...

Last edited by vbe; 10-05-2010 at 07:33 AM.. Reason: addendum
# 7  
Old 10-05-2010
the files are being created but they are empty. no data in log.alarm and log.diff.
but in fact there should be data in them
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

If I ran perl script again,old logs should move with today date and new logs should generate.

Appreciate help for the below issue. Im using below code.....I dont want to attach the logs when I ran the perl twice...I just want to take backup with today date and generate new logs...What I need to do for the below scirpt.............. 1)if logs exist it should move the logs with extention... (1 Reply)
Discussion started by: Sanjeev G
1 Replies

2. Shell Programming and Scripting

Shell script for capturing FTP logs

I have a script #!/bin/bash HOST=ftp.example.com USER=ftpuser PASSWORD=P@ssw0rd ftp -inv $HOST <<EOF user $USER $PASSWORD cd /path/to/file mput *.html bye EOF the script executes sucessfully I need to capture the FTP logs to a logfile should contain FTP Login successful ... (1 Reply)
Discussion started by: rajeshas83
1 Replies

3. Shell Programming and Scripting

Help with Shell Script to View Logs

Hi I'm very new to unix shell scripting. Im also new here in this forum. I'm a SQL Server DBA but I'm slowly learning Oracle and Sybase DB. Our Oracle and Sybase are on Unix platforms. Im slowly learning Linux Admin and Shell Scripting to automate tasks. I'm writing a script to view DB error... (4 Replies)
Discussion started by: Ricky777
4 Replies

4. Shell Programming and Scripting

Shell script - Asterisk logs report

Dear all, I start to build script(s) for few tasks, and I'll use log files to complete the following: 1) when ringnoanswer for a particular operator hits count 10 for waittime > 14000 send mail alert with summary of calls 2) per queue - exitwithtimout > 1 in any hour, then send mail... (12 Replies)
Discussion started by: bigbrobg
12 Replies

5. Shell Programming and Scripting

Managing logs in shell script

Hi, I need write a shell script which should be executed from the crontab every day. This shell script is running several other shell scripts , and each one of them is writing to its log file. Few of the the shell script are also connecting using ssh to some other users on remote machine , do... (1 Reply)
Discussion started by: Yoav
1 Replies

6. Shell Programming and Scripting

Help with extract application logs through shell script in performance testing

Hi Experts, I am new to shell.How to extract logs (Web,APP,Database) using shell in performance testing? Need for webserver logs,app server logs and d/b logs code. Thanks in advance Sree (3 Replies)
Discussion started by: sree vasu
3 Replies

7. Shell Programming and Scripting

Shell Script for GC Logs

Hi, I have a strange situation here, I want to archive gc.logs file, generated by a java application, the strange thing about gc.log file is is doesn't have any time/date stamp appended to it unlike other logs (catalina/access/error) and one more strange thing is when ever the application is... (6 Replies)
Discussion started by: Neeryan
6 Replies

8. Shell Programming and Scripting

Script Optimization - large delimited file, for loop with many greps

Since there are approximately 75K gsfiles and hundreds of stfiles per gsfile, this script can take hours. How can I rewrite this script, so that it's much faster? I'm not as familiar with perl but I'm open to all suggestions. ls file.list>$split for gsfile in `cat $split`; do csplit... (17 Replies)
Discussion started by: verge
17 Replies

9. Shell Programming and Scripting

shell script to grep 500 error messages from access logs

Hello Team, I need help to improve my script which is used to grep 500 error messages in the logs. I am using following logic in the script to grep 500 error messages in the logs. var1=`awk '$9 == "500"' access_log | tail -1` The above logic is not useful if logs are not getting... (1 Reply)
Discussion started by: coolguyamy
1 Replies

10. Shell Programming and Scripting

Shell script to view logs of a server

Please share a shell script to collect logs of a server (like cpu utilization, memory etc) for a perticular time interval by giving date, time and server name as input. (1 Reply)
Discussion started by: abhishek27
1 Replies
Login or Register to Ask a Question