Skip to content

Instantly share code, notes, and snippets.

@marvin
Created January 22, 2012 11:00
Show Gist options
  • Save marvin/e210c4189e1c1bbc9c31 to your computer and use it in GitHub Desktop.
Save marvin/e210c4189e1c1bbc9c31 to your computer and use it in GitHub Desktop.
watches a logfile and drops the new lines into mongodb
def watch_for(file)
f = File.open(file,"r")
f.seek(0,IO::SEEK_END)
while true do
select([f])
line = f.gets
if line != nil
if file == "/var/log/syslog"
line.gsub!(/^(\w+.\d+.\d+:\d+:\d+)/u,"")
print line
else
line = f.gets
end
logentry = Log.create(
:jid => "0",
:logdate => Time.now,
:logfile => file,
:logentry => line
)
if logentry.errors
logentry.errors.each do |e|
puts e
end
end
end
end
end
Thread.new do
watch_for("/var/log/syslog")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment