Class: Fluent::WootheeOutput

Inherits:
Output
  • Object
show all
Defined in:
lib/fluent/plugin/out_woothee.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeWootheeOutput

Returns a new instance of WootheeOutput.


29
30
31
32
# File 'lib/fluent/plugin/out_woothee.rb', line 29

def initialize
  super
  require 'woothee'
end

Instance Attribute Details

#modeObject

Returns the value of attribute mode.


19
20
21
# File 'lib/fluent/plugin/out_woothee.rb', line 19

def mode
  @mode
end

Instance Method Details

#configure(conf) ⇒ Object


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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/fluent/plugin/out_woothee.rb', line 34

def configure(conf)
  super

  # tag ->
  if not @tag and not @remove_prefix and not @add_prefix
    raise Fluent::ConfigError, "missing both of remove_prefix and add_prefix"
  end
  if @tag and (@remove_prefix or @add_prefix)
    raise Fluent::ConfigError, "both of tag and remove_prefix/add_prefix must not be specified"
  end
  if @remove_prefix
    @removed_prefix_string = @remove_prefix + '.'
    @removed_length = @removed_prefix_string.length
  end
  if @add_prefix
    @added_prefix_string = @add_prefix + '.'
  end
  # <- tag

  if conf['type'] == 'woothee_fast_crawler_filter' or @fast_crawler_filter_mode
    @fast_crawler_filter_mode = true

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

  if @filter_categories.size > 0 and @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 and not @merge_agent_info
    raise Fluent::ConfigError, "configured not to do nothing (not to do either filter/drop nor addition of parser result)"
  end
end

#emit(tag, es, chain) ⇒ Object


133
134
135
136
137
138
139
140
141
142
143
# File 'lib/fluent/plugin/out_woothee.rb', line 133

def emit(tag, es, chain)
  tag = tag_mangle(tag)

  if @fast_crawler_filter_mode
    fast_crawler_filter_emit(tag, es)
  else
    normal_emit(tag, es)
  end

  chain.next
end

#fast_crawler_filter_emit(tag, es) ⇒ Object


103
104
105
106
107
108
109
# File 'lib/fluent/plugin/out_woothee.rb', line 103

def fast_crawler_filter_emit(tag, es)
  es.each do |time,record|
    unless Woothee.is_crawler(record[@key_name] || '')
      Fluent::Engine.emit(tag, time, record)
    end
  end
end

#normal_emit(tag, es) ⇒ Object


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/fluent/plugin/out_woothee.rb', line 111

def normal_emit(tag, es)
  es.each do |time,record|
    parsed = Woothee.parse(record[@key_name] || '')

    category = parsed[Woothee::ATTRIBUTE_CATEGORY]
    next if @mode == :filter and not @filter_categories.include?(category)
    next if @mode == :drop and @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
    Fluent::Engine.emit(tag, time, record)
  end
end

#tag_mangle(tag) ⇒ Object


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

def tag_mangle(tag)
  if @tag
    @tag
  else
    if @remove_prefix and
        ( (tag.start_with?(@removed_prefix_string) and tag.length > @removed_length) or tag == @remove_prefix)
      tag = tag[@removed_length..-1]
    end 
    if @add_prefix
      tag = if tag and tag.length > 0
              @added_prefix_string + tag
            else
              @add_prefix
            end
    end
    tag
  end
end