Skip to content

Instantly share code, notes, and snippets.

@jasonrclark
Created November 16, 2012 16:53
Show Gist options
  • Save jasonrclark/4088933 to your computer and use it in GitHub Desktop.
Save jasonrclark/4088933 to your computer and use it in GitHub Desktop.
watched-log
.error-notifier.rb.swp
require './watched_log.rb'
run([
['rpm_test_app/log/newrelic_agent.log', RUBY_ERRORS],
['rpm_site/java_collector/log/collector.8081.log', JAVA_ERRORS],
['rpm_site/beacon/log/server.9000.log', JAVA_ERRORS],])
source 'https://rubygems.org'
gem 'terminal-notifier'
gem 'file-tail'
GEM
remote: https://rubygems.org/
specs:
file-tail (1.0.12)
tins (~> 0.5)
terminal-notifier (1.4.2)
tins (0.6.0)
PLATFORMS
ruby
DEPENDENCIES
file-tail
terminal-notifier
require 'terminal-notifier'
require 'file-tail'
$newrelic_root = ARGV[0] || Dir.pwd
RUBY_ERRORS = [
/(ERROR \: .*)/,
]
JAVA_ERRORS = [
/( FATAL .*)/,
/(.*Exception\:.*)/,
/(.*Exception$)/,
]
def tail_file(path, patterns)
Thread.new do
full_path = File.absolute_path(File.join($newrelic_root, path))
File.open(full_path) do |log|
log.extend(File::Tail)
log.interval = 10
log.backward(10)
log.tail do |line|
match = patterns.map { |r| r.match(line) }.compact.first
if !match.nil?
TerminalNotifier.notify(normalize_error(match[0]),
:title => path,
:open => "file://#{full_path}")
end
end
end
end
end
def normalize_error(original)
original.gsub(/[\[\]\(\)]/, '_')
end
def run(logs_to_watch)
threads = []
logs_to_watch.each do |log_pair|
threads << tail_file(log_pair[0], log_pair[1])
end
threads.each { |t| t.join }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment