Results 1 to 7 of 7

Thread: script to generate email alert

  1. #1
    Senior Member
    Join Date
    Jul 2006
    Posts
    190
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Rep Power
    9

    Default script to generate email alert

    Hi,

    I want to have an email alert when a greped event occur in /var/log/messages

    Any hint please

  2. #2
    Member
    Join Date
    May 2010
    Posts
    53
    Thanks
    0
    Thanked 6 Times in 6 Posts
    Rep Power
    5

    Default

    put to cron for each hour something like

    Code:
    #!/bin/sh
    
    touch /var/log/messages.old
    grep -v -f /var/log/messages.old /var/log/messages | grep PATTERN | mail -s "report" my@email.com
    cp /var/log/messages /var/log/messages.old
    for sure are many better ways, this just got first one to my mind

  3. #3
    Member billconner's Avatar
    Join Date
    Jul 2010
    Posts
    90
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Rep Power
    5

    Default

    Quote Originally Posted by asim.mcp View Post
    Hi,

    I want to have an email alert when a greped event occur in /var/log/messages

    Any hint please
    You'll need either cron or at to execute a scrip periodically.
    Tell the scrip to grep for the pattern you want:

    if [ `grep -c "string" in file` -ge 1 ]
    then
    mail me@someplace.con <<!
    The string matched
    !

    The grep -c will return a count of the number of matches in file and then test that number against 1. If true it then invokes mail with a "Here Document" to compose a message to be sent. This is a strategy that might work for you without creating temporary file or confusing pipes or complex commands.

    Bill

  4. #4
    Member
    Join Date
    May 2010
    Posts
    53
    Thanks
    0
    Thanked 6 Times in 6 Posts
    Rep Power
    5

    Default

    Bill Bill

    1. your solution will return the same errors every time is executed. My solution will inform you just about new errors. Thats why you need to use temporary file.

    Recomendation
    Code:
    if [ `grep -c "string" in file` -ge 1 ]; then
    has wrong syntax and you can replace it by
    Code:
    if grep -q "string" file; then
    to dont confuse anybody with complexity

  5. #5
    Member billconner's Avatar
    Join Date
    Jul 2010
    Posts
    90
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Rep Power
    5

    Default Temp Files

    Quote Originally Posted by cooler View Post
    Bill Bill

    1. your solution will return the same errors every time is executed. My solution will inform you just about new errors. Thats why you need to use temporary file.

    Recomendation
    Code:
    if [ `grep -c "string" in file` -ge 1 ]; then
    has wrong syntax and you can replace it by
    Code:
    if grep -q "string" file; then
    to dont confuse anybody with complexity
    Oops, the grep should have read

    if [ `grep -c "string" file` -ge 1 ]

    This returns a count of the number of times "string" appears in file. If greater than 1 we execute the "here document" to send you an email. Not very complex but you would need some error checking to complete it.

    Since you're apparently checking a log file, you may have to match the time stamp for the line having the string you want to make sure. it's newer that the last iteration. To do this save the time stamp in a variable (using awk should do it) so that you can compare it to the grep'ed value next time.

    With some tinkering, you should be able to eliminate temporary files. It's just a personal preference on my part to avoid doing stuff external to a script whenever I can.

    Bill

  6. #6
    Contributors jaysunn's Avatar
    Join Date
    Apr 2009
    Location
    NYC - USA
    Posts
    1,025
    Thanks
    164
    Thanked 153 Times in 132 Posts
    Rep Power
    24

    Default

    Have a look at SWATCH for log scrapping and emailing results. This runs in a daemon mode using regex for matching.

    Simple Log Watcher | Download Simple Log Watcher software for free at SourceForge.net


    We use for all our log scraping / watching.

    Oldie but a goodie.

    Jaysunn

  7. #7
    Member billconner's Avatar
    Join Date
    Jul 2010
    Posts
    90
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Rep Power
    5

    Default Scripting for Fun?

    Quote Originally Posted by jaysunn View Post
    Have a look at SWATCH for log scrapping and emailing results. This runs in a daemon mode using regex for matching.

    Simple Log Watcher | Download Simple Log Watcher software for free at SourceForge.net


    We use for all our log scraping / watching.

    Oldie but a goodie.

    Jaysunn
    No doubt, but where's the fun? I had a boss once who wanted formatted output for snort logs and he wanted it customized so no canned solution would work. Took a while but I managed it with a shell script. I actually enjoyed the challenge and got paid for it.

    Bill

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 4
    Last Post: 6th November 2010, 08:23 AM
  2. Replies: 2
    Last Post: 7th October 2009, 04:30 PM
  3. Smartd Send An Email Alert and Run A Shell Script
    By dipakzope in forum Linux hardware
    Replies: 3
    Last Post: 8th September 2009, 06:31 PM
  4. Shell Script To Generate License Keys
    By please in forum Shell scripting
    Replies: 2
    Last Post: 4th January 2009, 06:03 PM
  5. Disk space alert via email
    By aasif.shaikh in forum Shell scripting
    Replies: 1
    Last Post: 10th June 2008, 03:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •