Skip to content

Instantly share code, notes, and snippets.

@ymek
Created April 7, 2011 17:20
Show Gist options
  • Select an option

  • Save ymek/908243 to your computer and use it in GitHub Desktop.

Select an option

Save ymek/908243 to your computer and use it in GitHub Desktop.
$LOAD_PATH.unshift File.dirname(__FILE__)
#
# Debugging method and flag
#DEBUG = false
DEBUG = true
def debug(msg)
DEBUG or return
puts "DEBUG:: #{msg}"
end
#
# Monkey-patch array for use with metaclasses
class Array
def errors_by_type(error_type)
#drop the metaclass
self.reject! { |item| item.to_s.eql? 'Error' }
self.reject! { |item| (LogMailer::Errors.const_get(item).new).type != error_type }
return self
end
end
module LogMailer
VERSION = '1.1.12'
require 'fssm'
autoload :Utils, 'logmailer/utils'
autoload :Errors, 'logmailer/errors'
autoload :Summary, 'logmailer/summary'
autoload :Log, 'logmailer/log'
autoload :Mailer, 'logmailer/mailer'
autoload :Zabbix, 'logmailer/zabbix'
@@summary = Summary.new
@@log_objs = { }
@@watches = { '/home/ymek/tmp/var/log/php/' => '*_log',
'/home/ymek/tmp/var/log/nginx/' => 'error\.log' }
def self.init()
LogMailer.watches.each do |path, glob|
files = Dir.glob("#{path}#{glob}").sort
files.each do |file|
debug("New Log Object: #{file}")
@@log_objs[file.to_sym] = Log.new file
end
end
#log_objs
end
def self.summary
@@summary
end
def self.watches
@@watches
end
def self.create(base, relative, type)
debug("create: #{base}/#{relative} #{type}")
path_and_file = "#{base}/#{relative}"
if @@log_objs[relative.to_sym].nil?
debug("update log object: #{relative}")
@@log_objs[path_and_file.to_sym] = Log.new path_and_file
end
@@log_objs[path_and_file.to_sym].create(base, relative, type)
end
def self.update(base, relative, type)
debug("update: #{base}/#{relative} #{type}")
path_and_file = "#{base}/#{relative}"
if @@log_objs[relative.to_sym].nil?
debug("update log object: #{relative}")
@@log_objs[path_and_file.to_sym] = Log.new path_and_file
end
@@log_objs[path_and_file.to_sym].update(base, relative, type)
end
def self.delete(base, relative, type)
debug "delete: #{base}/#{relative} #{type}"
path_and_file = "#{base}/#{relative}"
if @@log_objs.key? path_and_file.to_sym
@@log_objs.delete path_and_file.to_sym
end
end
end # module LogMailer
begin
LogMailer.init
FSSM.monitor do |monitor|
LogMailer.watches.each do |dir, pattern|
monitor.path dir do |path|
path.glob pattern
path.update { |base, relative, type| LogMailer.update(base, relative, type) }
path.create { |base, relative, type| LogMailer.create(base, relative, type) }
path.delete { |base, relative, type| LogMailer.delete(base, relative, type) }
end
end # LogMailer.watches.each
end # FSSM
rescue
Mailer::error($!)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment