class LogTimer::LogTimer

Attributes

config[RW]
mailer[RW]

Public Class Methods

new(config) click to toggle source
# File lib/log_timer.rb, line 13
def initialize(config)
  @config = config
  @mailer = Mailer.new(@config[:mail])
end

Public Instance Methods

check() click to toggle source
# File lib/log_timer.rb, line 18
def check
  @config[:files].each do |_key, file_info|
    log = Log.new(file_info[:path])
    if log.older?(Time.now - ChronicDuration.parse(file_info[:limit]))
      mailer.send(mail_subject(log), mail_body(log, file_info)) if @config[:cmd_options][:mail]
      puts "#{log.path} is older than the limit #{file_info[:limit]}" unless @config[:cmd_options][:quiet]
    end
  end
end
mail_body(log, file_info) click to toggle source
# File lib/log_timer.rb, line 32
    def mail_body(log, file_info)
      tail_limit = file_info[:tail_limit] || 10
      body = <<EOD
The log file #{log.path} is got older than the specified limit (#{file_info[:limit]}). Maybe a service stopped working.
The file was last changed on #{log.modification_date}.

The last #{tail_limit} lines are:
#{log.tail(tail_limit)}

Sincerely,
The LogTimer gem
EOD
      body
    end
mail_subject(log) click to toggle source
# File lib/log_timer.rb, line 28
def mail_subject(log)
  "#{File.basename(log.path)} on #{@config[:hostname] || `hostname`} is getting old"
end