2011-01-13 6 views
0

Ich bin mir sicher, dass mir etwas fehlt. Grundlegend möchte ich eine logs IO überwachen und wenn ein FATAL ERROR protokolliert wird, um eine E-Mail mit dem Fehler zu senden.ruby ​​sendmail nach dem Muster gefunden in IO

#!/usr/bin/ruby -w 

require 'rubygems' 

def mailer(line) 

    date = `date +%D-%T` 
    f = File.open("/root/error.mail", "w") 
    f.puts("Subject: Fatal Error on SERVER #{date}\n\n#{line}") 
    f.close 
    system("sendmail [email protected] < /root/error.mail") 
end 

def fatal_check(file, pattern) 

    f = File.open(file, "r") 
    f.seek(0,IO::SEEK_END) 
    while true do 
    select([f]) 
    line = f.gets 
    mailer("#{line}") if line=~pattern 
    #system("./mailer.rb #{line}") if line=~pattern 
    end 
end 

fatal_check("/root/test.log", /FATAL ERROR/) 
+0

Und warum ist Ihr Code versagt? Bearbeiten Sie Ihre Frage oben und erklären Sie das Problem. –

Antwort

1

Wie wäre es damit. Sie werden ein paar Edelsteine ​​brauchen:

gem install file-tail 
gem install pony 

Und dann Ihr Skript:

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 => '[email protected]', :from => '[email protected]', :subject => "There was a nasty error on #{date}", :body => line) 
    end 
    end 
end 

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