# # Fluent # # Copyright (C) 2011 FURUHASHI Sadayuki # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # module Fluent class TextParser class RegexpParser include Configurable config_param :time_format, :string, :default => nil def initialize(regexp, conf={}) super() @regexp = regexp unless conf.empty? configure(conf) end end def call(text) m = @regexp.match(text) unless m $log.warn "pattern not match: #{text.inspect}" return nil, nil end time = nil record = {} m.names.each {|name| if value = m[name] case name when "time" if @time_format time = Time.strptime(value, @time_format).to_i else time = Time.parse(value).to_i end else record[name] = value end end } time ||= Engine.now return time, record end end class JSONParser include Configurable config_param :time_key, :string, :default => 'time' config_param :time_format, :string, :default => nil def call(text) record = Yajl.load(text) if value = record.delete(@time_key) if @time_format time = Time.strptime(value, @time_format).to_i else time = value.to_i end else time = Engine.now end return time, record rescue Yajl::ParseError $log.warn "pattern not match: #{text.inspect}: #{$!}" return nil, nil end end class ValuesParser include Configurable config_param :keys, :string config_param :time_key, :string, :default => nil config_param :time_format, :string, :default => nil def configure(conf) super @keys = @keys.split(",") if @time_key && !@keys.include?(@time_key) raise ConfigError, "time_key (#{@time_key.inspect}) is not included in keys (#{@keys.inspect})" end if @time_format && !@time_key $log.warn "time_format parameter is ignored because time_key parameter is not set" end end def values_map(values) record = Hash[keys.zip(values)] if @time_key value = record[@time_key] if @time_format time = Time.strptime(value, @time_format).to_i else time = Time.parse(value).to_i end else time = Engine.now end return time, record end end class TSVParser < ValuesParser config_param :delimiter, :string, :default => "\t" def call(text) return values_map(text.split(@delimiter)) end end class CSVParser < ValuesParser def initialize super require 'csv' end def call(text) return values_map(CSV.parse_line(text)) end end class ApacheParser include Configurable REGEXP = /^(?[^ ]*) [^ ]* (?[^ ]*) \[(?