LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 01-22-2013, 09:32 PM   #1
cmartz
LQ Newbie
 
Registered: Apr 2011
Posts: 18

Rep: Reputation: 0
in need of a script to grep a string in logs - then email


. . . . but after just a handfull of lines, say two or three, i would want it to stop emailing. I'll cron it every minute but then I don't want it to keep sending me alerts after 3 or 4 events since i'll know already.

i know that find can do this:

find <some path> -type f -name <some log name> -mmin -300 -exec grep <some string> {} \;

the -mmin parameter would be so after some minutes I wouldn't get kept being emailed.

. . . . can grep do this without find? so after greping so many instances of the string, i wouldn't want it to keep emailing me since i'll be doing thing cron every minute. again, an email a minute would be too much. and i'd want to do it every minute since i'd like to know soonest.

chris
 
Old 01-23-2013, 01:18 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,786

Rep: Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304
that looks strange for me. the log files will contain the info regardless of the find command. grep will also find those lines. So you need to restrict grep to find only the latest lines. We would need some info about those lines to help you further
 
Old 01-23-2013, 08:27 AM   #3
cmartz
LQ Newbie
 
Registered: Apr 2011
Posts: 18

Original Poster
Rep: Reputation: 0
. . . . . well if the event has not happened yet, grep won't find it though. I ack your find comment though.
 
Old 01-23-2013, 06:48 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,349

Rep: Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750
If the log recs are timestamped, you'd keep track of that, otherwise you'd have to count the lines in the file and allow for logfile reset ie start a new shorter file.


Don't keep firing up a new process env every minute, its a load on the system and unnecessary. Write a daemon and have it loop every minute. This also makes tracking easier, because you can keep the info in memory.
You might want to keep latest 'mark' in a small file in case you need to restart the daemon.
 
Old 01-23-2013, 07:15 PM   #5
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by chrism01 View Post
If the log recs are timestamped, you'd keep track of that, otherwise you'd have to count the lines in the file and allow for logfile reset ie start a new shorter file.
There is a tool called logtail that does that. It may work similar to the "logtail" from the (way) older logcheck package.

Apart from that I'd urge the OP to curb his wheel re-invention urges as it's not like tools incorporating functionality like that don't exist already...
 
Old 01-23-2013, 08:55 PM   #6
cmartz
LQ Newbie
 
Registered: Apr 2011
Posts: 18

Original Poster
Rep: Reputation: 0
what about something like this :

grep -q somestring some.log && mail -s 'found alert' joeblow@abc.com </dev/null

then i'd cron job it every few minutes.

this would only email when finding the string but it would keep emailing until string was not in the log anymore.

chris

Last edited by cmartz; 01-23-2013 at 08:57 PM.
 
Old 01-23-2013, 10:34 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,349

Rep: Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750
Actually, when I need this ability, I use http://search.cpan.org/~mgrabnar/Fil...0.99.3/Tail.pm which does the log file tail and you can add the emailing etc.
Needs basic Perl knowledge.

My previous answer was more general about the issues involved.
 
Old 01-24-2013, 05:50 AM   #8
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
I know, I just grafted my reply onto your text as it was the latest reply then. Nothing else intended or implicated.
 
Old 01-24-2013, 06:18 AM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,349

Rep: Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750
Sure; no worries
 
  


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
[SOLVED] grep fails in shell script when string is not found coolnfunky Linux - Newbie 4 05-04-2011 10:02 AM
Grep string from logs of last 1 hour on files of 2 different servers+calculate count saurabhmehan Linux - Newbie 6 09-06-2010 12:22 AM
Script to find, grep and email basisvasis Programming 3 09-13-2008 12:23 PM
shell script: grep using string with specail charactors? Chowroc Programming 6 11-14-2005 08:47 PM
Grep Syslog - email shell script voodoofxz Linux - Newbie 1 09-06-2005 04:27 PM

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

All times are GMT -5. The time now is 05:13 PM.

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