Created
December 9, 2010 17:16
-
-
Save asim/734999 to your computer and use it in GitHub Desktop.
ugly code
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
#!/usr/local/ruby/bin/ruby | |
require "rubygems" | |
require "redis" | |
require "eventmachine" | |
require "eventmachine-tail" | |
LOG = "/tmp/foo" | |
R = Redis.new(:host => "10.1.10.2") | |
class Reader < EventMachine::FileTail | |
def initialize(path, startpos=-1) | |
super(path, startpos) | |
# puts "Tailing #{path}" | |
@buffer = BufferedTokenizer.new | |
@stack = { :m => 0 } | |
end | |
def self.parse(line) | |
{ :time => line[2], :hostname => line[3], :postfix => line[4][/^([^\/]+)\/smtp.*/,1], :message_id => line[5], :message => line[6..-1] } | |
end | |
def receive_data(data) | |
@buffer.extract(data).each do |line| | |
if line.match(/ postfix-10\.1\.5\.[0-9]{1,3}\/smtp\[/) | |
newline = Reader.parse(line.split(" ")) | |
# if newline[:message].match(/ status=sent /) | |
if line.match(/ status=sent /) | |
@stack.has_key?("#{newline[:postfix]}-sent") ? @stack["#{newline[:postfix]}-sent"] += 1 : @stack["#{newline[:postfix]}-sent"] = 1 | |
end | |
this_m = newline[:time].split(":")[1].to_i | |
if this_m > @stack[:m] | |
@stack.each do |k,v| | |
v += R[k].to_i | |
R.set(k, v) | |
@stack[k] = 0 | |
end | |
@stack[:m] = this_m | |
end | |
end | |
end | |
end | |
end | |
EventMachine.run do | |
EventMachine::file_tail(LOG, Reader) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment