Class: LogStash::Filters::Grokdiscovery

Inherits:
Base
  • Object
show all
Defined in:
lib/logstash/filters/grokdiscovery.rb

Overview

TODO(sissel): This is not supported yet. There is a bug in grok discovery that causes segfaults in libgrok.

Constant Summary

Constants inherited from Base

Base::RESERVED

Constants included from Config::Mixin

Config::Mixin::CONFIGSORT

Instance Attribute Summary

Attributes included from Config::Mixin

#config, #original_params

Attributes inherited from Plugin

#logger, #params

Instance Method Summary collapse

Methods inherited from Base

#execute, #threadsafe?

Methods included from Config::Mixin

#config_init, included

Methods inherited from Plugin

#eql?, #finished, #finished?, #hash, #inspect, lookup, #reload, #running?, #shutdown, #teardown, #terminating?, #to_s

Constructor Details

#initialize(config = {}) ⇒ Grokdiscovery

Returns a new instance of Grokdiscovery.



13
14
15
16
17
# File 'lib/logstash/filters/grokdiscovery.rb', line 13

def initialize(config = {})
  super

  @discover_fields = {}
end

Instance Method Details

#filter(event) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/logstash/filters/grokdiscovery.rb', line 38

def filter(event)
  return unless filter?(event)

  # parse it with grok
  message = event["message"]
  match = false

  if event.type and @discover_fields.include?(event.type)
    discover = @discover_fields[event.type] & event.to_hash.keys
    discover.each do |field|
      value = event[field]
      value = [value] if value.is_a?(String)

      value.each do |v| 
        pattern = @grok.discover(v)
        @logger.warn("Trying #{v} => #{pattern}")
        @grok.compile(pattern)
        match = @grok.match(v)
        if match
          @logger.warn(["Match", match.captures])
          event.to_hash.merge!(match.captures) do |key, oldval, newval|
            @logger.warn(["Merging #{key}", oldval, newval])
            oldval + newval # should both be arrays...
          end
        else
          @logger.warn(["Discovery produced something not matchable?", { :input => v }])
        end
      end # value.each
    end # discover.each
  else
    @logger.info("Unknown type for #{event.source} (type: #{event.type})")
    @logger.debug(event.to_hash)
  end
  @logger.debug(["Event now: ", event.to_hash])

  filter_matched(event) if !event.cancelled?
end

#registerObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/logstash/filters/grokdiscovery.rb', line 20

def register
  gem "jls-grok", ">=0.4.3"
  require "grok" # rubygem 'jls-grok'

  # TODO(sissel): Make patterns files come from the config
  @config.each do |type, typeconfig|
    @logger.debug("Registering type with grok: #{type}")
    @grok = Grok.new
    Dir.glob("patterns/*").each do |path|
      @grok.add_patterns_from_file(path)
    end
    @discover_fields[type] = typeconfig
    @logger.debug(["Enabling discovery", { :type => type, :fields => typeconfig }])
    @logger.warn(@discover_fields)
  end # @config.each
end