#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    5
    Rep Power
    0

    Trying to use File:Tail with Ruby


    Hi

    I am a newbie and I am dabbling with scripting and using Ruby to monitor some logs and ran into a script that I thought would do the job. This is the script I found -

    -----------------------------------------------------------
    require 'rubygems'
    require 'pony'
    require 'file/tail'

    def fatal_check(file, pattern)
    File::Tail::Logfile.open(file, :backward => 0) do |log|
    log.tail do |line|
    date = `date +%D-%T`
    Pony.mail(:to => 'you@example.com', :from => 'me@example.com', :subject => "There was a nasty error on #{date}", :body => line)
    end
    end
    end

    fatal_check(File.dirname(__FILE__) + "/test.log", /FATAL/)

    -----------------------------------------------------------
    I don't understand how the method is monitoring the pattern as I dont see it being called in the method. From trying it out, it appears that any new lines causes an email to send.

    Any help in the right direction would be helpful
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    I guess you got the code from this thread.

    Yes, you're right that the pattern isn't used. But are you really surprised that a script somebody posted in some forum (obviously without testing it) doesn't work?

    But it should work when you add an "if" statement:
    Code:
    def fatal_check(file, pattern)
      File::Tail::Logfile.open(file, :backward => 0) do |log|
        log.tail do |line|
          if line =~ pattern
            date = `date +%D-%T`
            Pony.mail(:to => 'you@example.com', :from => 'me@example.com', :subject => "There was a nasty error on #{date}", :body => line)
          end
        end
      end
    end
    If it doesn't work, read through the documentation of File::Tail and write the script yourself. That's always better than copying and pasting code from the internet.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    5
    Rep Power
    0
    Thank you! I was not surprised but I thought I was missing something. This makes sense. Thanks for the help!

IMN logo majestic logo threadwatch logo seochat tools logo