LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 11-23-2017, 05:35 PM   #1
vivekn1980
LQ Newbie
 
Registered: Nov 2017
Posts: 10

Rep: Reputation: Disabled
Grep a pattern & Email from latest logs


MyLOG:

2017/11/12 17:01:54.600 : Error: LPID: 3104680848 WRONG CRITERIA FOUND. tRealBuilder::Generate

Logs are in multipart

Output Required:

If Key word "WRONG CRITERIA FOUND" in latest log ( logs are regularly generating - real time) mail to us
once mailed wait for 2 hours for second mail.

mail subject like " WRONG CRITERIA FOUND" in Logs.

is it possible through script or any monitoring tool we can use for real time log monitoring.

OS : Red Hat Enterprise Linux Server release 6.8 (Santiago)

i tried script given below:

------------------------

tail -fn0 logfile | \
while read line ; do
echo "$line" | grep "pattern"
if [ $? = 0 ]
then
... do something ...
fi
done
----------------------------

but it doesnot fullfill my requirement.
 
Old 11-24-2017, 02:11 AM   #2
!!!
Member
 
Registered: Jan 2017
Location: Fremont, CA, USA
Distribution: Trying any&ALL on old/minimal
Posts: 708

Rep: Reputation: 302Reputation: 302Reputation: 302Reputation: 302
Hello. Welcome to LQ. Interesting project!!!
What does your script produce when you run it?

I tried this: [sudo] tail -fn0 /var/log/syslog | egrep 'en|eth'
and unplugged then re-plugged-in my (VBox) ethernet 'Vcable'
and it printed the Down and Up msgs ok.

What I might add next, is a sleep 7200 (after mail'ing)
But, I'm not sure about restarting the tail (wouldn't want to encounter
output occuring during the sleep). There's probably many ways;
I found something interesting (but complex) from these web-search keywords:
tail follow restart sleep hours after email
https://superuser.com/questions/2705...tring-is-found

Maybe a more-experienced LQ'er can advise on better keywords/concepts. Thanks!!!
 
Old 11-24-2017, 05:45 AM   #3
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 5,444

Rep: Reputation: 2030Reputation: 2030Reputation: 2030Reputation: 2030Reputation: 2030Reputation: 2030Reputation: 2030Reputation: 2030Reputation: 2030Reputation: 2030Reputation: 2030
Rather than messing with tail, I would just read the log file in reverse. Put this in a script called from cron every two hours.
Code:
if tac logfile | grep -q -m1 "WRONG CRITERIA FOUND"; then
  sendmail ...
fi
Depending on how often the logfile is rotated, you may need to look at processing the datetime stamp.

Last edited by allend; 11-24-2017 at 05:53 AM.
 
1 members found this post helpful.
Old 12-05-2017, 03:35 PM   #4
vivekn1980
LQ Newbie
 
Registered: Nov 2017
Posts: 10

Original Poster
Rep: Reputation: Disabled
Hi, but my file name is not static its dynamic, new files created with different name in one hour.

example ntmg_20171205_01.log
ntmg_20171205_02.log
ntmg_20171205_03.log
ntmg_20171205_04.log

please help
 
Old 12-05-2017, 11:13 PM   #5
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 5,444

Rep: Reputation: 2030Reputation: 2030Reputation: 2030Reputation: 2030Reputation: 2030Reputation: 2030Reputation: 2030Reputation: 2030Reputation: 2030Reputation: 2030Reputation: 2030
Perhaps something like
Code:
newfile="$(ls -1t ntmg*.log | head -1)"
 
1 members found this post helpful.
Old 12-06-2017, 02:44 PM   #6
vivekn1980
LQ Newbie
 
Registered: Nov 2017
Posts: 10

Original Poster
Rep: Reputation: Disabled
resolved

thanks is resolved
 
Old 12-09-2017, 02:24 AM   #7
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 15,302
Blog Entries: 9

Rep: Reputation: 4377Reputation: 4377Reputation: 4377Reputation: 4377Reputation: 4377Reputation: 4377Reputation: 4377Reputation: 4377Reputation: 4377Reputation: 4377Reputation: 4377
please tell us how.
others will benefit.
leife's a two way street.

also please mark this thread solved.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Grep pattern help!!! peacepanda Linux - Newbie 4 02-20-2014 03:30 AM
in need of a script to grep a string in logs - then email cmartz Linux - Server 8 01-24-2013 06:18 AM
[SOLVED] grep two pattern from a file Sattee Linux - General 18 06-13-2012 04:13 AM
[SOLVED] grep with pattern danielbmartin Linux - Newbie 3 12-05-2011 10:16 PM
using grep when the pattern contains a ! farmerjoe Programming 9 03-15-2005 11:04 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 04:28 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration