Continiously monitor the log file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Continiously monitor the log file
# 1  
Old 10-27-2011
Continiously monitor the log file

Hi Friends,

I am trying to write a script which continiously monitor one specific error message from a log file. This script should continiously monitor the file for the error and send out the email when detect the error message. I tried the below command but fails. Please help me.
Code:
tail -f /var/log/messages | grep "file not found" | mail -s "error found" arumon@testing.com


Regards,
Arumon

Last edited by Franklin52; 10-27-2011 at 03:22 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 10-27-2011
run the command
Code:
tail -f /var/log/messages >>monitor.log &

put below monitor.sh in cronjob
Code:
$ cat monitor.sh

#!/bin/bash
n1=`awk '/file not found/{n++}END{print n}' monitor.log`
[ $n1 -ge 1 ] && mailx  -s "error found" arumon@testing.com
>my.log

Code:
*/5 * * * * monitor.sh

# 3  
Old 10-27-2011
Thanks rcdwayx :-)

Have some doubts on this.

1. As this new monitor.log file will have all time entries and the cron job will execute every 5 sec, I think this will send the old error messages again and again.
2. Also i need to avoid the same erros to be send frequently if it is happening continiously.

Please help me.

Regards,
Arumon
# 4  
Old 10-27-2011
check for every 5 second on evet about klogd
Code:
#./justdoit 5 klogd &

check for every 15 second on evet about klogd
Code:
#./justdoit 15 crond &

Code:
#!/bin/bash
## justdoit ##
while :; do
lstlf="$(grep "$2" /var/log/messages|sed -n '$p')"
sleep $1;lstll="$(grep "$2" /var/log/messages|sed -n '$p')"
if [[ "$lstlf" != "$lstll" ]] ; then
echo "There is a event about on '$2'"|mail -s "Changes detected!!.." arumon@testing.com ; fi;done

regards
ygemici
# 5  
Old 10-27-2011
Quote:
Originally Posted by arumon
Thanks rcdwayx :-)

Have some doubts on this.

1. As this new monitor.log file will have all time entries and the cron job will execute every 5 sec, I think this will send the old error messages again and again.
2. Also i need to avoid the same erros to be send frequently if it is happening continiously.

Please help me.

Regards,
Arumon
1. the command ">my.log" will clean the log file each time. So you should not get old error messages.

2. [ $n1 -ge 1 ] can be used to adjust with your request, and the cronjob frequency can be adjusted , for example, every 10 minutes.

Code:
[ $n1 -ge 5 ]

0,10,20,30,40,50 * * * * monitor.sh

with above changes, you will check the log every 10 minutes, and if found more than 5 errors, send mail to you. Otherwise, keep quiet.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How do I add a log file path to a vi file to monitor all the changes made to the file?

I'm curious to know how do I add an empty log file (test1.log) to an existing text file to monitor all the changes made to a.txt. Is this expression export PATH=$PATH:/home/test1.log right to be added to the text file a.txt? (5 Replies)
Discussion started by: TestKing
5 Replies

2. Shell Programming and Scripting

prepend timestamp to continiously updating log file

Hi, I have a process which outputs to a log. Below is the code snippet: process &> $LOGFILE& The log file keeps on updating whenever a transaction is processed. The log file has a time stamp added so every time I kill the process and start the process a new log file is... (4 Replies)
Discussion started by: rajkumarme_1
4 Replies

3. Shell Programming and Scripting

Monitor log file

Hi, I need to amend an existing ksh script so that it runs a process (stop weblogic) and in parallel needs to monitor a log file (startup.log) in the background for a certain string (e.g. unable to stop weblogic). If the string appears in the log i need to kill the stop weblogic process. ... (5 Replies)
Discussion started by: dholmaster
5 Replies

4. Shell Programming and Scripting

Script to monitor log file

Hi, Have written a script to monitor linux non standard log file based on line numbers, so each check store $otalinenum .. then in next check after 10 minutes it compre the current_total_line_num > last_total_line_num then it will parse the log file from last_total_line_num to... (0 Replies)
Discussion started by: Shirishlnx
0 Replies

5. Shell Programming and Scripting

How to monitor log file for a Error and generate the e-mail ( Please help)

This is my log file and this is live log. Any abnormal error other than following I need to generate the email. Log path : /DER/app/admin/ABC/bdump/erg.log Current log# 2 seq# 103046 mem# 0: /ora2/oradata/ABC/redo02a.log Current log# 2 seq# 103046 mem# 1:... (7 Replies)
Discussion started by: hishanms
7 Replies

6. HP-UX

Script to monitor /var/opt/resmon/log/event.log file

AM in need of some plugin/script that can monitor HP-UX file "/var/opt/resmon/log/event.log" . Have written a scrip in sh shell that is working fine for syslog.log and mail.log as having standard format, have interrogated that to Nagios and is working as I required . But same script failed to... (3 Replies)
Discussion started by: Shirishlnx
3 Replies

7. Shell Programming and Scripting

Monitor dynamic log file for a particular Error

Hi Folks, I need help in creating a script to monitor a continuously updating log for one particular error. If the the script finds the error it should send out an email. Thanks for all ur help (2 Replies)
Discussion started by: a12ka4
2 Replies

8. Shell Programming and Scripting

Script to monitor the pattern in the log file

hi All, how to find a pattern in the log file & display the above and below line for example in the log file, i have many lines, whenever i search for "Category" it should display the above line with only few parameter like i want only the location name & department name Thu Jul 02 11:05:23... (2 Replies)
Discussion started by: rithick256
2 Replies

9. Shell Programming and Scripting

Monitor log file and execute command

I would like to monitor a log file using a shell script and as soon as a line with a certain string in it appears I would like to run a program. I have been playing around with doing this using tail -f, but cannot get it to work. I found something similar here:... (1 Reply)
Discussion started by: danielsbrewer
1 Replies

10. Shell Programming and Scripting

Bash tail monitor log file

Hi there, I have a problem here that involves bash script since I was noob in that field. Recently, I have to monitor data involve in logs so I just run command tail -f for the monitoring. The logs was generate every hour so I need to quickly change my logs every time the new hour hits according... (2 Replies)
Discussion started by: kriezo
2 Replies
Login or Register to Ask a Question