Ruby Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesRuby Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 2nd, 2012, 08:44 AM
letstestthis letstestthis is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 5 letstestthis User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 15 m 36 sec
Reputation 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

Reply With Quote
  #2  
Old October 2nd, 2012, 09:38 AM
Jacques1's Avatar
Jacques1 Jacques1 is online now
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,875 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 3 h 45 m 24 sec
Reputation Power: 813
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.

Reply With Quote
  #3  
Old October 2nd, 2012, 09:59 AM
letstestthis letstestthis is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 5 letstestthis User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 15 m 36 sec
Reputation Power: 0
Thank you! I was not surprised but I thought I was missing something. This makes sense. Thanks for the help!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesRuby Programming > Trying to use File:Tail with Ruby

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap