class Consumer
Public Class Methods
new(path, startpos=-1, params = {})
click to toggle source
Calls superclass method
# File lib/logstash_sqs/consumer.rb, line 3 def initialize(path, startpos=-1, params = {}) super(path, startpos) @buffer = BufferedTokenizer.new @aws_sqs_client = AWSSQS.new(params['buffer_size']) @format = params['format'] || 'json' end
Public Instance Methods
receive_data(data)
click to toggle source
# File lib/logstash_sqs/consumer.rb, line 10 def receive_data(data) @buffer.extract(data).each do |message| @aws_sqs_client.add_message(marshall(message)) end end
Private Instance Methods
format_apache(entry)
click to toggle source
# File lib/logstash_sqs/consumer.rb, line 40 def format_apache(entry) pattern = /^(\S*) (\S*) (\S*) (\S*) (\[[^\]]+\]) (\S*) (\S*) (\S*) (\S*) (\S*) (\S*) (.*)$/ if (matcher = pattern.match(entry)) return "{\"@virtual_host\": \"#{matcher[1]}\",\"@remote_host\": \"#{matcher[2]}\",\"@i\": \"#{matcher[3]}\",\"@userid\": \"#{matcher[4]}\",\"@time\": \"#{matcher[5]}\",\"@method\": \"#{matcher[6].gsub('"','')}\",\"@req_path\": \"#{matcher[7]}\",\"@http_version\": \"#{matcher[8].gsub('"','')}\",\"@status\": \"#{matcher[9]}\",\"@size\": \"#{matcher[10]}\",\"@referer\": \"#{matcher[11].gsub('"','')}\",\"@user_agent\": \"#{matcher[12].gsub('"','')}\"}" end end
format_syslog(entry)
click to toggle source
# File lib/logstash_sqs/consumer.rb, line 28 def format_syslog(entry) #TODO - rewrite this function! month_map = {'Jan' => 1, 'Fev' => 2, 'Mar' => 3, 'Apr' => 4, 'May' => 5, 'Jun' => 6, 'Jul' => 7, 'Aug' => 8, 'Sep' => 9, 'Oct' => 10, 'Nov' => '11', 'Dec' => 12} month_name, day, hhmmss, host, *message = entry.split year = Time.now.year month = month_map[month_name] hours, minutes, seconds = hhmmss.split(':') timestamp = Time.local(year,month,day,hours,minutes,seconds).utc.iso8601 "{\"@timestamp\": \"#{timestamp}\",\"@type\":\"syslog\", \"@message\":\"#{message.join(' ')}\",\"@host\":\"#{host}\"}" end
marshall(message)
click to toggle source
# File lib/logstash_sqs/consumer.rb, line 18 def marshall(message) case @format when 'json' then message when 'syslog' then format_syslog(message) when 'apache' then format_apache(message) else message end end