Class: Fluent::Plugin::WootheeFilter

Inherits:
Filter
  • Object
show all
Defined in:
lib/fluent/plugin/filter_woothee.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#modeObject

Returns the value of attribute mode.



15
16
17
# File 'lib/fluent/plugin/filter_woothee.rb', line 15

def mode
  @mode
end

Instance Method Details

#configure(conf) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
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
# File 'lib/fluent/plugin/filter_woothee.rb', line 25

def configure(conf)
  specified_type_name = conf['@type']

  super

  @filter_categories = @filter_categories.map(&:to_sym)
  @drop_categories = @drop_categories.map(&:to_sym)

  if specified_type_name == 'woothee_fast_crawler_filter' || @fast_crawler_filter_mode
    @fast_crawler_filter_mode = true

    if @filter_categories.size > 0 || @drop_categories.size > 0 || @merge_agent_info
      raise Fluent::ConfigError, "fast_crawler_filter cannot be specified with filter/drop/merge options"
    end

    define_singleton_method(:filter, method(:filter_fast_crawler))
    return
  end

  if @filter_categories.size > 0 && @drop_categories.size > 0
    raise Fluent::ConfigError, "both of 'filter' and 'drop' categories specified"
  elsif @filter_categories.size > 0
    unless @filter_categories.reduce(true){|r,i| r and Woothee::CATEGORY_LIST.include?(i)}
      raise Fluent::ConfigError, "filter_categories has invalid category name"
    end
    @mode = :filter
  elsif @drop_categories.size > 0
    unless @drop_categories.reduce(true){|r,i| r and Woothee::CATEGORY_LIST.include?(i)}
      raise Fluent::ConfigError, "drop_categories has invalid category name"
    end
    @mode = :drop
  else
    @mode = :through
  end

  if @mode == :through && ! @merge_agent_info
    raise Fluent::ConfigError, "configured not to do nothing (not to do either filter/drop nor addition of parser result)"
  end

  define_singleton_method(:filter, method(:filter_standard))
end

#filter(tag, time, record) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/fluent/plugin/filter_woothee.rb', line 67

def filter(tag, time, record)
  # dynamically overwritten by #configure
  if @fast_crawler_filter_mode
    filter_fast_crawler(tag, time, record)
  else
    filter_standard(tag, time, record)
  end
end

#filter_fast_crawler(tag, time, record) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/fluent/plugin/filter_woothee.rb', line 76

def filter_fast_crawler(tag, time, record)
  if Woothee.is_crawler(record[@key_name] || '')
    nil
  else
    record
  end
end

#filter_standard(tag, time, record) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/fluent/plugin/filter_woothee.rb', line 84

def filter_standard(tag, time, record)
  parsed = Woothee.parse(record[@key_name] || '')

  category = parsed[Woothee::ATTRIBUTE_CATEGORY]
  return nil if @mode == :filter && !@filter_categories.include?(category)
  return nil if @mode == :drop && @drop_categories.include?(category)

  if @merge_agent_info
    record = record.merge({
        @out_key_name => parsed[Woothee::ATTRIBUTE_NAME],
        @out_key_category => parsed[Woothee::ATTRIBUTE_CATEGORY].to_s,
        @out_key_os => parsed[Woothee::ATTRIBUTE_OS]
    })
    record[@out_key_os_version] = parsed[Woothee::ATTRIBUTE_OS_VERSION] if @out_key_os_version
    record[@out_key_version] = parsed[Woothee::ATTRIBUTE_VERSION] if @out_key_version
    record[@out_key_vendor] = parsed[Woothee::ATTRIBUTE_VENDOR] if @out_key_vendor
  end
  record
end