LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-04-2011, 03:49 PM   #1
Alan87i
Member
 
Registered: Oct 2005
Location: Quebec
Posts: 119

Rep: Reputation: 15
Tail the message log| grep " several words" play a sound when it's found.


I'm trying to write a script that will either tail or watch /var/log/messages for
the words
PHP Code:
signal Gone into alarm state 
from this line below.

Jul 4 16:27:52 ZM-AL-desktop zma_m5[5961]: INF [signal: 48147 - Gone into alarm state]

I would then like the script execute: mpg123 /ping.mp3
And have run at start up.
I'm stuck with grep-ping only one word
PHP Code:
tail -/var/log/messages grep "signal" 
Thanks for any help.
Allan

Last edited by Alan87i; 07-05-2011 at 11:33 AM. Reason: SOLVED
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 07-04-2011, 04:08 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by Alan87i View Post
I'm trying to write a script that will either tail or watch /var/log/messages for
the words
PHP Code:
signal Gone into alarm state 
from this line below.

Jul 4 16:27:52 ZM-AL-desktop zma_m5[5961]: INF [signal: 48147 - Gone into alarm state]

I would then like the script execute: mpg123 /ping.mp3
And have run at start up.
I'm stuck with grep-ping only one word
PHP Code:
tail -/var/log/messages grep "signal" 
All you've got to do is put the rest of the string in there...what have you tried??
Code:
tail -f /var/log/messages | grep "signal Gone into alarm state"
works fine. You could even put a "-i" for grep to make it case-insensitive on the string.
 
1 members found this post helpful.
Old 07-04-2011, 04:14 PM   #3
Alan87i
Member
 
Registered: Oct 2005
Location: Quebec
Posts: 119

Original Poster
Rep: Reputation: 15
When I tried {tail -f /var/log/messages | grep "signal Gone into alarm state" } It just sits there and doesn't print anything.
Even though I know it's in alarm .
Thanks
 
Old 07-04-2011, 04:41 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Make it look further back than 10 lines then.
 
Old 07-04-2011, 04:45 PM   #5
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by Alan87i View Post
When I tried {tail -f /var/log/messages | grep "signal Gone into alarm state" } It just sits there and doesn't print anything.
Even though I know it's in alarm .
Thanks
Your post hints at the fact you're wanting this in a program/script. Read the man page for tail...the "-f" will NEVER return, but keep looking at the log file. In a script, that won't ever come back. Try "tail -n 100" (or some number of lines), to look at the last 100 lines of the log for the pattern. Logwatch is also a good program to use, since its whole purpose is to look for patterns in log file(s).
 
Old 07-04-2011, 06:00 PM   #6
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Edit: Modified the expressions to match the given error string. Thanks, Diantre.

This snippet will keep watching /var/log/messages for future lines matching [signal:somethingGone into alarm state] (where something can contain numbers, dashes, and spaces), playing /usr/share/games/supertux2/sounds/bigjump.wav for each occurrence:
Code:
tail -n 0 -f /var/log/messages \
| grep -e '\[signal:[-0-9 ]*Gone into alarm state\]' \
| while read LINE ; do
        aplay /usr/share/games/supertux2/sounds/bigjump.wav
  done
Like TB0ne and Tinkster said, you can increase the number of existing lines tail will inspect (highlighted in red, above) before starting to watch for new lines.

Or, you can have the script stay running all the time, and have the sound play whenever the problem occurs.

If you simply want to know if there is such a string anywhere in the log file at this moment -- i.e. if this has happened recently, during this rotation of the log file --, use
Code:
grep -qe '\[signal:[-0-9 ]*Gone into alarm state\]' \
&& aplay /usr/share/games/supertux2/sounds/bigjump.wav

Last edited by Nominal Animal; 07-04-2011 at 07:39 PM.
 
2 members found this post helpful.
Old 07-04-2011, 06:41 PM   #7
Diantre
Member
 
Registered: Jun 2011
Distribution: Slackware
Posts: 515

Rep: Reputation: 234Reputation: 234Reputation: 234
A very small modification to the regex

Hi. I think that given the string the OP wants to match:

Quote:
Originally Posted by Alan87i View Post
Jul 4 16:27:52 ZM-AL-desktop zma_m5[5961]: INF [signal: 48147 - Gone into alarm state]
The regular expression passed to grep won't match.

Quote:
Originally Posted by Nominal Animal View Post
Code:
grep -qe 'signal Gone into alarm state' /var/log/messages \
&& aplay /usr/share/games/supertux2/sounds/bigjump.wav
It would if the log message contains exactly the string 'signal Gone into alarm state', but it has a few characters right after the word 'signal'.

Replacing the space after the word 'signal' for .*, will match a log entry like the example the OP posted:

Code:
grep -qe 'signal.*Gone into alarm state' /var/log/messages
 
1 members found this post helpful.
Old 07-05-2011, 04:43 AM   #8
Alan87i
Member
 
Registered: Oct 2005
Location: Quebec
Posts: 119

Original Poster
Rep: Reputation: 15
Thanks for all the replies !!

The system is Xubuntu 8.04.1 Lts . I'm running Zoneminder on it with a capture card and cctv cameras.
I need the script to play the sound when the camera called "signal" goes into alarm state so I know that someone has walked past that area being monitored.

I modified the code posted with the .* in the string and this part works for 1 entry,
Code:
allan@ZM-AL-desktop:~$ grep -qe 'signal.*Gone into alarm state' /var/log/messages \
 && aplay /ping.wav
Playing WAVE '/ping.wav' : Unsigned 8 bit, Rate 22050 Hz, Mono
When I tried it with the tail script I still get no response even though the camera is beside me and is in/out of alarm with my hand in front of it.
Code:
allan@ZM-AL-desktop:~$ tail -n 0 -f /var/log/messages \
> | grep -e 'signal.*Gone into alarm state' \
> | while read LINE ; do
>         aplay /ping.wav
>   done
I have to close the terminal window to stop it. I also tried with numbers other than 0 and get the same result.

Last edited by Alan87i; 07-05-2011 at 04:59 AM.
 
Old 07-05-2011, 05:05 AM   #9
Alan87i
Member
 
Registered: Oct 2005
Location: Quebec
Posts: 119

Original Poster
Rep: Reputation: 15
Just tried only part of the script and this much seems to work
Quote:
allan@ZM-AL-desktop:~$ tail -n 2 -f /var/log/messages \
> | grep -e 'signal.*Gone into alarm state' \
>
Jul 5 06:03:33 ZM-AL-desktop zma_m5[5034]: INF [signal: 22766 - Gone into alarm state]
Jul 5 06:03:48 ZM-AL-desktop zma_m5[5034]: INF [signal: 22839 - Gone into alarm state]
Jul 5 06:04:02 ZM-AL-desktop zma_m5[5034]: INF [signal: 22911 - Gone into alarm state]
So I put this into a file called it playalarm.sh made it executable and executed it.
Quote:
tail -n 2 -f /var/log/messages \
| grep -e 'signal.*Gone into alarm state' \
| while read LINE ; do
aplay /ping.wav
done
Went to make a coffee and when I walked back in I heard the ping.wav playing over and over and over then 10 seconds in it stopped. a few minutes later it started up again ran 5 or 6 times and stopped. I'll leave it for a while and try to time the intervals .

Edit
It just played again 8 or more times. It's been about 20 minutes or so. 1 minute later it played 11 more times. Weird <<< I wonder if I executed the script twice. But it does not do it when the camera is actually in alarm. Will restart the box.

Last edited by Alan87i; 07-05-2011 at 05:35 AM.
 
Old 07-05-2011, 11:09 AM   #10
Alan87i
Member
 
Registered: Oct 2005
Location: Quebec
Posts: 119

Original Poster
Rep: Reputation: 15
Quote:
allan@ZM-AL-desktop:~$ tail -n 0 -f /var/log/messages | grep -qe 'signal.*Gone into alarm state' && aplay /ping.wav
Playing WAVE '/ping.wav' : Unsigned 8 bit, Rate 22050 Hz, Mono
allan@ZM-AL-desktop:~$
Now I've been playing around the above works but 1 time only.
How can I make it loop?
 
Old 07-05-2011, 11:18 AM   #11
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Use the --line-buffered option of grep to correctly stream the I/O through the pipe chain:
Code:
tail -f /var/log/messages | grep --line-buffered 'signal.*Gone into alarm state' | while read line; do aplay /ping.wav; done
 
1 members found this post helpful.
Old 07-05-2011, 11:33 AM   #12
Alan87i
Member
 
Registered: Oct 2005
Location: Quebec
Posts: 119

Original Poster
Rep: Reputation: 15
SOLVED !!Tail the message log| grep " several words" play a sound when it's found

Thanks colucix that seems to do the trick , Excellent I added the script file to start up applications rebooted and BAM I hear the sound each time the camera goes into alarm.

Thanks again to all who helped!
Allan
 
Old 07-06-2011, 10:17 AM   #13
Alan87i
Member
 
Registered: Oct 2005
Location: Quebec
Posts: 119

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by colucix View Post
Use the --line-buffered option of grep to correctly stream the I/O through the pipe chain:
Code:
tail -f /var/log/messages | grep --line-buffered 'signal.*Gone into alarm state' | while read line; do aplay /ping.wav; done
Another question I made a second script but changed out the word (signal) for the name of a different monitor. I also told this new script to aplay /dog1.wav instead of ping.wav.
My plan was to have 2 cameras each play there own sound on alarm. Problem is both play the same sound dog1.wav

Is there a way to run both lines in the same script?
Thanks
Allan
 
Old 07-06-2011, 10:36 AM   #14
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You can try to put the processes in background:
Code:
tail -f /var/log/messages | grep --line-buffered 'signal.*Gone into alarm state' | while read line; do aplay /ping.wav; done &
tail -f /var/log/messages | grep --line-buffered 'something.*Gone into alarm state' | while read line; do aplay /dog1.wav; done &
Take in mind that in this case the script terminates almost immediately but leaves the two tail processes in background. If you want to terminate them, you have to kill them manually, e.g.
Code:
pkill -9 tail
 
1 members found this post helpful.
Old 07-07-2011, 08:02 AM   #15
Alan87i
Member
 
Registered: Oct 2005
Location: Quebec
Posts: 119

Original Poster
Rep: Reputation: 15
I managed to get it working with two scripts , I changed one monitor name and modified 1 script to match. Things work fine now.

I have been messing with grep trying to figure out how to grep between 06:00 and 21:30 ignoring the date So when a critter turns on the motion light and makes my camera go into alarm I don't get smacked by the Wife LOL at 2 AM .
I can't seem to 3get the syntax correct. The cat is printed below is missing 15 or more alarms from July 6 in the 17's 18's up to 21:45
Code:
allan@allan-cameras:~$ cat /var/log/messages | grep -e 'frontdoor.*Gone into alarm state' | grep -E '(0[0-9]:[0-9][0-9]|21:30)'
Jul 6 10:05:59 allan-cameras zma_m11[19842]: INF [frontdoor: 038 - Gone into alarm state]
Jul 6 11:05:32 allan-cameras zma_m11[25239]: INF [frontdoor: 2234 - Gone into alarm state]
Jul 6 11:05:44 allan-cameras zma_m11[25239]: INF [frontdoor: 2292 - Gone into alarm state]
Jul 6 14:02:20 allan-cameras zma_m11[5238]: INF [frontdoor: 46279 - Gone into alarm state]
Jul 6 14:02:34 allan-cameras zma_m11[5238]: INF [frontdoor: 46351 - Gone into alarm state]
Jul 6 14:06:38 allan-cameras zma_m11[5238]: INF [frontdoor: 47567 - Gone into alarm state]
Jul 6 14:08:58 allan-cameras zma_m11[5238]: INF [frontdoor: 48267 - Gone into alarm state]
Jul 6 14:09:54 allan-cameras zma_m11[5238]: INF [frontdoor: 48544 - Gone into alarm state]
Jul 6 15:04:44 allan-cameras zma_m11[23015]: INF [frontdoor: 14230 - Gone into alarm state]
Jul 6 15:08:06 allan-cameras zma_m11[23015]: INF [frontdoor: 15238 - Gone into alarm state]
Jul 6 15:08:47 allan-cameras zma_m11[23015]: INF [frontdoor: 15444 - Gone into alarm state]
Jul 6 16:01:03 allan-cameras zma_m11[23015]: INF [frontdoor: 31088 - Gone into alarm state]
Jul 6 16:03:02 allan-cameras zma_m11[23015]: INF [frontdoor: 31678 - Gone into alarm state]
Jul 6 16:04:35 allan-cameras zma_m11[23015]: INF [frontdoor: 32146 - Gone into alarm state]
Jul 6 16:05:02 allan-cameras zma_m11[23015]: INF [frontdoor: 32280 - Gone into alarm state]
Jul 6 17:04:53 allan-cameras zma_m11[23015]: INF [frontdoor: 50205 - Gone into alarm state]
Jul 7 06:37:45 allan-cameras zma_m11[22629]: INF [frontdoor: 14290 - Gone into alarm state]
Jul 7 06:59:13 allan-cameras zma_m11[22629]: INF [frontdoor: 20711 - Gone into alarm state]
Jul 7 07:00:21 allan-cameras zma_m11[22629]: INF [frontdoor: 21048 - Gone into alarm state]
Jul 7 07:06:10 allan-cameras zma_m11[22629]: INF [frontdoor: 22793 - Gone into alarm state]
Jul 7 07:06:58 allan-cameras zma_m11[22629]: INF [frontdoor: 23029 - Gone into alarm state]
Jul 7 07:13:26 allan-cameras zma_m11[22629]: INF [frontdoor: 24965 - Gone into alarm state]
Jul 7 07:15:08 allan-cameras zma_m11[22629]: INF [frontdoor: 25473 - Gone into alarm state]
Jul 7 07:17:03 allan-cameras zma_m11[22629]: INF [frontdoor: 26047 - Gone into alarm state]
Jul 7 07:17:29 allan-cameras zma_m11[22629]: INF [frontdoor: 26176 - Gone into alarm state]
Jul 7 07:23:11 allan-cameras zma_m11[22629]: INF [frontdoor: 27883 - Gone into alarm state]
Jul 7 07:24:31 allan-cameras zma_m11[22629]: INF [frontdoor: 28282 - Gone into alarm state]
Jul 7 07:29:40 allan-cameras zma_m11[22629]: INF [frontdoor: 29823 - Gone into alarm state]
Jul 7 07:32:12 allan-cameras zma_m11[22629]: INF [frontdoor: 30581 - Gone into alarm state]
Jul 7 07:43:04 allan-cameras zma_m11[22629]: INF [frontdoor: 33831 - Gone into alarm state]
Jul 7 07:44:22 allan-cameras zma_m11[22629]: INF [frontdoor: 34221 - Gone into alarm state]
Jul 7 07:44:32 allan-cameras zma_m11[22629]: INF [frontdoor: 34268 - Gone into alarm state]
Jul 7 07:51:30 allan-cameras zma_m11[22629]: INF [frontdoor: 36355 - Gone into alarm state]
Jul 7 07:51:50 allan-cameras zma_m11[22629]: INF [frontdoor: 36452 - Gone into alarm state]
Jul 7 08:13:58 allan-cameras zma_m11[22629]: INF [frontdoor: 43074 - Gone into alarm state]
Jul 7 08:14:30 allan-cameras zma_m11[22629]: INF [frontdoor: 43234 - Gone into alarm state]
Jul 7 08:14:40 allan-cameras zma_m11[22629]: INF [frontdoor: 43284 - Gone into alarm state]
Jul 7 08:15:51 allan-cameras zma_m11[22629]: INF [frontdoor: 43640 - Gone into alarm state]
Jul 7 08:17:09 allan-cameras zma_m11[22629]: INF [frontdoor: 44030 - Gone into alarm state]
Jul 7 08:26:40 allan-cameras zma_m11[7332]: INF [frontdoor: 2485 - Gone into alarm state]
Jul 7 08:38:44 allan-cameras zma_m11[7332]: INF [frontdoor: 6096 - Gone into alarm state]
Jul 7 08:51:23 allan-cameras zma_m11[7332]: INF [frontdoor: 9881 - Gone into alarm state]
 
  


Reply



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
how can I "cat" or "grep" a file to ignore lines starting with "#" ??? callagga Linux - Newbie 7 08-16-2013 06:58 AM
How do I extract "words" from a log in linux ? ytd Linux - Newbie 12 04-28-2009 09:54 AM
How to make newer "tail" behave like older "tail" rylan76 Linux - Software 4 12-07-2007 04:27 AM
From BASH, "Play <filename.wav" gives obsolete oss interface message ericcarlson Fedora 3 09-14-2004 06:20 AM
"Undeleting" data using grep, but get "grep: memory exhausted" error SammyK Linux - Software 2 03-13-2004 03:11 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:04 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