Class: Fluent::RTFDashOutput

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

Constant Summary collapse

MATCH_OPERATOR_EXCLUDE =
'!'

Instance Method Summary collapse

Instance Method Details

#configure(conf) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/fluent/plugin/out_rtf_dash.rb', line 14

def configure(conf)
  super

  @rewriterules = []
  rewriterule_names = []
  @hostname = `#{@hostname_command}`.chomp

  conf.keys.select{|k| k =~ /^rewriterule(\d+)$/}.sort_by{|i| i.sub('rewriterule', '').to_i}.each do |key|
    rewritekey,regexp,rewritetag = parse_rewriterule(conf[key])
    if regexp.nil? || rewritetag.nil?
      raise Fluent::ConfigError, "failed to parse rewriterules at #{key} #{conf[key]}"
    end
    @rewriterules.push([rewritekey, /#{trim_regex_quote(regexp)}/, get_match_operator(regexp), rewritetag])
    rewriterule_names.push(rewritekey + regexp)
    $log.info "adding rewrite_tag_filter rule: #{key} #{@rewriterules.last}"
  end

  unless @rewriterules.length > 0
    raise Fluent::ConfigError, "missing rewriterules"
  end

  unless @rewriterules.length == rewriterule_names.uniq.length
    raise Fluent::ConfigError, "duplicated rewriterules found #{@rewriterules.inspect}"
  end

  unless @remove_tag_prefix.nil?
    @remove_tag_prefix = /^#{Regexp.escape(@remove_tag_prefix)}\.?/
  end

  unless @use_of_first_match_tag_regexp.nil?
    @use_of_first_match_tag_regexp = Regexp.new(@use_of_first_match_tag_regexp)
  end
end

#emit(tag, es, chain) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/fluent/plugin/out_rtf_dash.rb', line 48

def emit(tag, es, chain)
  es.each do |time,record|
    rewrited_tag = rewrite_tag(tag, record)
    next if rewrited_tag.nil? || tag == rewrited_tag
    Fluent::Engine.emit(rewrited_tag, time, record)
  end

  chain.next
end

#get_backreference_table(elements) ⇒ Object



100
101
102
103
104
105
106
# File 'lib/fluent/plugin/out_rtf_dash.rb', line 100

def get_backreference_table(elements)
  hash_table = Hash.new
  elements.each.with_index(1) do |value, index|
    hash_table["$#{index}"] = @capitalize_regex_backreference ? value.capitalize : value
  end
  return hash_table
end

#get_match_operator(regexp) ⇒ Object



95
96
97
98
# File 'lib/fluent/plugin/out_rtf_dash.rb', line 95

def get_match_operator(regexp)
  return MATCH_OPERATOR_EXCLUDE if regexp.start_with?(MATCH_OPERATOR_EXCLUDE)
  return ''
end

#get_placeholder(tag) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/fluent/plugin/out_rtf_dash.rb', line 108

def get_placeholder(tag)
  tag = tag.sub(@remove_tag_prefix, '') if @remove_tag_prefix
  tag = tag.match(@use_of_first_match_tag_regexp)[1] if @use_of_first_match_tag_regexp
  p tag
  return {
    '__HOSTNAME__' => @hostname,
    '${hostname}' => @hostname,
    '__TAG__' => tag,
    '${tag}' => tag,
  }
end

#parse_rewriterule(rule) ⇒ Object



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

def parse_rewriterule(rule)
  if rule.match(/^([^\s]+)\s+(.+?)\s+([^\s]+)$/)
    return $~.captures
  end
end

#rewrite_tag(tag, record) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/fluent/plugin/out_rtf_dash.rb', line 58

def rewrite_tag(tag, record)
  placeholder = get_placeholder(tag)
  @rewriterules.each do |rewritekey, regexp, match_operator, rewritetag|
    rewritevalue = record[rewritekey].to_s
    next if rewritevalue.empty? && match_operator != MATCH_OPERATOR_EXCLUDE
    matched = regexp && regexp.match(rewritevalue)
    case match_operator
    when MATCH_OPERATOR_EXCLUDE
      next if matched
    else
      next if !matched
      backreference_table = get_backreference_table($~.captures)
      rewritetag = rewritetag.gsub(/\$\d+/, backreference_table)
    end
    rewritetag = rewritetag.gsub(/(\${[a-z]+(\[[0-9]+\])?}|__[A-Z]+__)/, placeholder)
    return rewritetag
  end
  return nil
end

#trim_regex_quote(regexp) ⇒ Object



84
85
86
87
88
89
90
91
92
93
# File 'lib/fluent/plugin/out_rtf_dash.rb', line 84

def trim_regex_quote(regexp)
  if regexp.start_with?('"') && regexp.end_with?('"')
    $log.info "rewrite_tag_filter: [DEPRECATED] Use ^....$ pattern for partial word match instead of double-quote-delimiter. #{regexp}"
    regexp = regexp[1..-2]
  end
  if regexp.start_with?(MATCH_OPERATOR_EXCLUDE)
    regexp = regexp[1, regexp.length]
  end
  return regexp
end