Created
November 16, 2012 16:53
-
-
Save jasonrclark/4088933 to your computer and use it in GitHub Desktop.
watched-log
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.error-notifier.rb.swp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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],]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source 'https://rubygems.org' | |
gem 'terminal-notifier' | |
gem 'file-tail' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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