class LogMonitor::Alerter

Public Class Methods

new() click to toggle source
# File lib/log_monitor/alerter.rb, line 3
def initialize
  clear_alert
end

Public Instance Methods

alert() click to toggle source
# File lib/log_monitor/alerter.rb, line 45
def alert
  begin
    $stdout.puts @alert_body
  rescue => e
    $stderr.puts "LogMonitor error"
    $stderr.puts e.message
    2.times $stderr.puts
  end
  clear_alert
end
check_words() click to toggle source
# File lib/log_monitor/alerter.rb, line 26
def check_words
  is_alert = false
  @words.each do | word |
    if @alert_body.match(/#{word}/)
      is_alert = true
      break
    end
  end
  if is_alert
    alert
  end
  clear_alert
end
clear_alert() click to toggle source
# File lib/log_monitor/alerter.rb, line 40
def clear_alert
  @alert_body = ''
  @blank_line_count = 0
end
monitor() click to toggle source
# File lib/log_monitor/alerter.rb, line 17
def monitor
  @in.seek(0, IO::SEEK_END)
  begin
      revival_monitor
  ensure
    @in.close
  end
end
set_in(io_in) click to toggle source
# File lib/log_monitor/alerter.rb, line 7
def set_in(io_in)
  @io_in = io_in
  FileUtils.touch(io_in) unless File.exists?(io_in)
  @in = File.open(io_in, 'r')
end
set_words(words) click to toggle source
# File lib/log_monitor/alerter.rb, line 13
def set_words(words)
  @words = words
end

Protected Instance Methods

revival_monitor() click to toggle source
# File lib/log_monitor/alerter.rb, line 58
def revival_monitor
  return if @in.nil?
  while true
    sleep(1) if @in.eof
    line = @in.gets
    @alert_body += "#{line}"
    if line.blank?
      @blank_line_count += 1
    else
      @blank_line_count = 0
    end
    check_words if @blank_line_count >= 2
  end
end