Alternative to tail -n -0 -F for monitoring live log file


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Alternative to tail -n -0 -F for monitoring live log file
# 1  
Old 08-18-2015
Linux Alternative to tail -n -0 -F for monitoring live log file

Hello,

I have been working on script which need to generate an alert based upon live logs. If string is found then an alert mail must triggered.

tail -n -0 -F works fine to redirect the each latest line from live logs file to grep a pattern for matching but it seems to be not working on SunOS 5.10 Generic_147441-19 i86pc i386 i86pc

Can anyone have a good alternative or any suggestion on this?Smilie

Code:
tail -n -0 -F $LOG_DIR"/COMMON-ERROR.log" | while read myline; do
ERROR_CODE=$(echo $myline | awk -F ' ' '{for (i=1;i<=NF;i++){if($i ~ "^[1-1]" && length($i)==6){print $i}}}')
:
:
done

Thanks,
Ketan R

Last edited by rbatte1; 08-18-2015 at 12:44 PM.. Reason: Added ICODE tags for clarity
# 2  
Old 08-18-2015
Why would you tail only the last line, since you are analyzing line by line anyway in a while read loop? Try using tail -f instead. Since the -F option is not available on Solaris, you need to check if files are rotated yourself.

What does your awk do ? It print every field that starts with the digit 1 and length is exactly 6? Why do you use ^[1-1] instead of ^1 ??

Last edited by Scrutinizer; 08-18-2015 at 06:01 AM..
# 3  
Old 08-18-2015
In addition to what Scrutinizer already said, on Solaris systems, you need to use /usr/xpg4/bin/awk or nawk instead of awk.
# 4  
Old 08-18-2015
Are you sure you need an immediate mail based on "live log" analysis? What be the delivery and reaction times for this mail? Depending on it going over the internet delivery times can be tens of minutes or even more. An e-mail client would pop it from the server every interval minutes only. What if the recipent is not perched on his or her keyboard?
You should reconsider the timing of the entire alert response chain and discuss it with your clients, and either relax on the timing or consider a different alert mechanism.
# 5  
Old 08-18-2015
well this is the script i have modified.
when i am running
Code:
tail -f opt/All/Meta/CableData/logs/meta_all/Server.log" |tail +10| egrep -i "Rebuilding free list took"

on command line and appending lines in Server.log its gives the perfect out put.
but by using below script tail -f wont redirect its out put to while loop.

Code:
#!/bin/bash
o=$IFS
IFS=$(echo -en "\n\b")
LOG_DIR="/opt/All/Meta/CableData/logs/meta_all"
while read myline; do
BIND_VAL=$(echo $myline | awk -F' ' '/Rebuilding/ {print $7}')
if [ "$(echo $myline| awk -F' ' '/Rebuilding/ {print $7}' | bc -l)" -gt 5 ];then
echo $myline | mailx -s "Alert from server for Rebuilding free list " 'KetanR@abc.com'
fi
done <$(tail -f $LOG_DIR"/Server.log" |tail +10| egrep -i "Rebuilding free list took" )
IFS=$o

Executing script in debug mode:
Code:
while read myline; do

BIND_VAL=$(echo $myline | awk -F' ' '/Rebuilding/ {print $7}')
if [ "$(echo $myline| awk -F' ' '/Rebuilding/ {print $7}' | bc -l)" -gt 5 ];then
echo $myline | mailx -s "Alert from server: $SERVER_IP for Rebuilding free list " 'Ketan.Raut@sigma-systems.com'
fi
done <$(tail -f $LOG_DIR"/DHCPServer.log" |tail +10| egrep -i "Rebuilding free list took" )
tail -f $LOG_DIR"/DHCPServer.log" |tail +10| egrep -i "Rebuilding free list took"
++ tail -f /opt/Alopa/MetaServ/CableProv/logs/metaprov_new/DHCPServer.log
++ tail +10
++ egrep -i 'Rebuilding free list took'

Any Suggestion?

Last edited by Corona688; 08-18-2015 at 12:17 PM.. Reason: Use [code], not [icode]
# 6  
Old 08-18-2015
Don't do $() on an endless stream. That attempts to read all the text at once and hangs waiting. Use command | while read ...
# 7  
Old 08-19-2015
seems to be same issue ....Smilie
Code:
#!/bin/bash
o=$IFS
IFS=$(echo -en "\n\b")
LOG_DIR="/opt/All/Meta/CableData/logs/meta_all"
tail -f $LOG_DIR"/Server.log" |tail +10| egrep -i "Rebuilding free list took" | while read myline; do
BIND_VAL=$(echo $myline | awk -F' ' '/Rebuilding/ {print $7}')
if [ "$(echo $myline| awk -F' ' '/Rebuilding/ {print $7}' | bc -l)" -gt 5 ];then
echo $myline | mailx -s "Alert from server for Rebuilding free list " 'KetanR@abc.com'
fi
done 
IFS=$o


Last edited by Don Cragun; 08-19-2015 at 05:46 AM.. Reason: Change ICODE tags to CODE tags again.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Monitoring script for Log file

Hi, Iam new to unix , plz help me to write below script. I need to write a script for Monitoring log file when any error occurs it has to send a mail to specified users and it should be always pick latest error not the existing one and the script should be able to send mail all errors (more... (1 Reply)
Discussion started by: vij05
1 Replies

2. Shell Programming and Scripting

Monitoring script for a log file

Hi, I need to get a script working to monitor a log file and throw an alert via mailx as soon as a particular error is encountered. I do not want repeatative email notifications of same error so simply cat logfile and grepping the error would not work. Here is what i planned but it seems... (2 Replies)
Discussion started by: roshan.171188
2 Replies

3. Shell Programming and Scripting

How to process only new line of tail -f command from live log file?

Hi, I want to read a live log file line by line and considering those line which are newly added to file Below code I am using, which read line but as soon as it read new line from log file its starts processing from very first line of file. tail -F /logs/COMMON-ERROR.log | while read... (11 Replies)
Discussion started by: ketanraut
11 Replies

4. Shell Programming and Scripting

How to tail -f logfile. if log file is generate every 1 HR.?

Hello, How to tail -f logfile. if log file is gennerate every 1 HR. I want it works automatically all the time. never changes it by manual. Thank ls -trl CybertonTransaction.* -rw-r--r-- 1 autobot robot 617071 Jul 9 00:02 CybertonTransaction.20130709-00.log -rw-r--r-- 1 autobot ... (12 Replies)
Discussion started by: ooilinlove
12 Replies

5. Shell Programming and Scripting

Piping tail to awk to parse a log file

Hello all, I've got what I'm pretty sure is a simple problem, but I just can't seem to work past it. I'm trying to use awk to pretty up a log file, and calculate a percentage. The log file looks like this: # tail strtovrUsage 20090531-18:15:45 RSreq - 24, RSsuc - 24, RSrun - 78, RSerr -... (4 Replies)
Discussion started by: DeCoTwc
4 Replies

6. Shell Programming and Scripting

Monitoring log file

Hi, I ned to monitor the tomcat log file called "catalina.out" for "Out of memory" error. the script should monitor this file and send us the mail as soon as it finds the string "Out of memory" in the file. can ypu suggest me which is the best way to do this? (4 Replies)
Discussion started by: shivanete
4 Replies

7. Shell Programming and Scripting

how to tail a log file..

Hi All.. I have a log file in which all the backup information is stored. Now i have written a script which get the last line in the backup log file.. ssh -l ora${sid} ${primaryhost} "tail -1 /oracle/$ORACLE_SID/sapbackup/back$ORACLE_SID.log" However i would like to tail the line last... (4 Replies)
Discussion started by: suri.tyson
4 Replies

8. Shell Programming and Scripting

tail -f monitoring

Is there any file on UNIX that changes periodically so that I could use tail -f command to watch changes? I was searching a long time and I didn't find nothing (I'm newbie to UNIX so it's not a surprise to me though) Thanks for help :) (6 Replies)
Discussion started by: MartyIX
6 Replies

9. 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

10. Shell Programming and Scripting

tail -f a log file redirected into a new window?

Is this possible? I am attempting to display a new xterm window and tail -f the log file within that new window. I am currently working on a solaris 8 machine if that has any different meaning than the other platforms. As you can see, I am a newbie to this forum and to UNIX. Any help would be... (2 Replies)
Discussion started by: douknownam
2 Replies
Login or Register to Ask a Question