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.



39
40
41
42
# File 'lib/fluent/plugin/out_woothee.rb', line 39

def initialize
  super
  require 'woothee'
end

Instance Attribute Details

#modeObject

Returns the value of attribute mode.



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

def mode
  @mode
end

Instance Method Details

#configure(conf) ⇒ Object



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
83
84
85
86
87
88
89
90
91
92
# File 'lib/fluent/plugin/out_woothee.rb', line 44

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



143
144
145
146
147
148
149
150
151
152
153
# File 'lib/fluent/plugin/out_woothee.rb', line 143

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



113
114
115
116
117
118
119
# File 'lib/fluent/plugin/out_woothee.rb', line 113

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

#normal_emit(tag, es) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/fluent/plugin/out_woothee.rb', line 121

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
    router.emit(tag, time, record)
  end
end

#tag_mangle(tag) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/fluent/plugin/out_woothee.rb', line 94

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