Class: Fluent::DataCalculatorOutput

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#_finalizerObject

Returns the value of attribute _finalizer.



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

def _finalizer
  @_finalizer
end

#_formulasObject

Returns the value of attribute _formulas.



18
19
20
# File 'lib/fluent/plugin/out_datacalculator.rb', line 18

def _formulas
  @_formulas
end

#aggregate_keysObject

Returns the value of attribute aggregate_keys.



17
18
19
# File 'lib/fluent/plugin/out_datacalculator.rb', line 17

def aggregate_keys
  @aggregate_keys
end

#countsObject

Returns the value of attribute counts.



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

def counts
  @counts
end

#last_checkedObject

Returns the value of attribute last_checked.



16
17
18
# File 'lib/fluent/plugin/out_datacalculator.rb', line 16

def last_checked
  @last_checked
end

#tickObject

Returns the value of attribute tick.



14
15
16
# File 'lib/fluent/plugin/out_datacalculator.rb', line 14

def tick
  @tick
end

Instance Method Details

#checkArgs(obj, inkeys) ⇒ Object



274
275
276
277
278
279
280
281
# File 'lib/fluent/plugin/out_datacalculator.rb', line 274

def checkArgs (obj, inkeys)
  inkeys.each{ |key|
    if not obj.has_key?(key)
      return false
    end
  }
  return true
end

#configure(conf) ⇒ Object



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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/fluent/plugin/out_datacalculator.rb', line 21

def configure(conf)
  super

  if @count_interval
    @tick = @count_interval.to_i
  else
    if @unit.index('sec') == 0
      @tick = 1
    elsif @unit.index('sec') != nil
      @tick = @unit[0, @unit.index('sec')].to_i
    elsif @unit.index('minute') == 0
      @tick = 60
    elsif @unit.index('minute') != nil
      @tick = @unit[0, @unit.index('minute')].to_i * 60
    elsif @unit.index('hour') == 0
      @tick = 3600
    elsif @unit.index('hour') != nil
      @tick = @unit[0, @unit.index('hour')].to_i * 3600
    elsif @unit.index('day') == 0
      @tick = 86400
    elsif @unit.index('day') != nil
      @tick = @unit[0, @unit.index('day')].to_i * 86400
    else
      raise RuntimeError, "@unit must be one of Xsec[onds]/Xminute[s]/Xhour[s]/Xday[s]"
    end
  end


  conf.elements.each do |element|
    element.keys.each do |k|
      element[k]
    end

    case element.name
    when 'unmatched'
      @unmatched = element
    end
  end
  # TODO: unmatchedの時に別のタグを付けて、ふってあげないと行けない気がする
  # unmatchedの定義
  # 1. aggregate_keys を持たないレコードが入ってきた時
  # 2. fomulaで必要な要素がなかったレコードが入ってきた時
  # 3. fomulaで集計可能な数値でない場合(文字列や真偽値、正規表現、ハッシュ、配列など)

  @aggregate_keys = []
  @aggregate = case @aggregate
               when 'tag' then :tag
               when 'all' then :all
               else
                 if @aggregate.index('keys') == 0
                   @aggregate_keys = @aggregate.split(/\s/, 2)[1]
                   unless @aggregate_keys
                     raise Fluent::ConfigError, "aggregate_keys require in keys"
                   end
                   @aggregate_keys = @aggregate_keys.split(/\s*,\s*/)
                   @aggregate = 'keys'
                 else
                   raise Fluent::ConfigError, "flowcounter aggregate allows tag/all"
                 end
               end

  def createFunc (cnt, str)
    str.strip!
    left, right = str.split(/\s*=\s*/, 2)
    # Fluent moduleだけはOK
    rights = right.scan(/[a-zA-Z][\w\d_\.\$\:\@]*/).uniq.select{|x| x.index('Fluent') != 0}

    begin
      f = eval('lambda {|'+rights.join(',')+'|  '+right + '}')
    rescue SyntaxError
      raise Fluent::ConfigError, "'" + str + "' is not valid"
    end

    [cnt, left, rights, f]
  end

  def execFunc (tag, obj, argv, formula)
    if tag != nil
      tag = stripped_tag (tag)
    end
    _argv = []

    argv.each {|arg|
      if tag != nil and tag != 'all'
        arg = tag + '_' + arg
      end
      _argv.push obj[arg].to_f
    }
    formula.call(*_argv)
  end

  @_formulas = []
  if conf.has_key?('formulas')
    fs = conf['formulas'].split(/\s*,\s*/)
    fs.each_with_index { |str,i |
      @_formulas.push( createFunc(i, str) )
    }
  end

  if conf.has_key?('finalizer')
    @_finalizer = createFunc(0, conf['finalizer'])

    # check finalizer field
    cnt = @_finalizer[2].length
    @_finalizer[2].each { |key|
      @_formulas.each { |formula|
        next if formula[2] == nil
        cnt -= 1 if formula[1] == key
      }
    }
    if cnt != 0
      raise Fluent::ConfigError, 'keys in finalizer is not satisfied'
    end
  end

  if @input_tag_remove_prefix
    @removed_prefix_string = @input_tag_remove_prefix + '.'
    @removed_length = @removed_prefix_string.length
  end

  @counts = count_initialized
  @mutex = Mutex.new
end

#count_initialized(keys = nil) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/fluent/plugin/out_datacalculator.rb', line 156

def count_initialized(keys=nil)
  # counts['tag'][num] = count
  if @aggregate == :all
    {'all' => ([0] * @_formulas.length)}
  elsif keys
    values = Array.new(keys.length) {|i|
      Array.new(@_formulas.length){|j| 0 }
    }
    Hash[[keys, values].transpose]
  else
    {}
  end
end

#countups(tag, counts) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/fluent/plugin/out_datacalculator.rb', line 170

def countups(tag, counts)
  if @aggregate == :all
    tag = 'all'
  end

  @mutex.synchronize {
    @counts[tag] ||= [0] * @_formulas.length
    counts.each_with_index do |count, i|
      @counts[tag][i] += count
    end
  }
end

#createFunc(cnt, str) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/fluent/plugin/out_datacalculator.rb', line 82

def createFunc (cnt, str)
  str.strip!
  left, right = str.split(/\s*=\s*/, 2)
  # Fluent moduleだけはOK
  rights = right.scan(/[a-zA-Z][\w\d_\.\$\:\@]*/).uniq.select{|x| x.index('Fluent') != 0}

  begin
    f = eval('lambda {|'+rights.join(',')+'|  '+right + '}')
  rescue SyntaxError
    raise Fluent::ConfigError, "'" + str + "' is not valid"
  end

  [cnt, left, rights, f]
end

#emit(tag, es, chain) ⇒ Object



283
284
285
286
287
288
289
290
# File 'lib/fluent/plugin/out_datacalculator.rb', line 283

def emit (tag, es, chain)

  if @aggregate == 'keys'
    emit_aggregate_keys(tag, es, chain)
  else
    emit_single_tag(tag, es, chain)
  end
end

#emit_aggregate_keys(tag, es, chain) ⇒ Object



292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/fluent/plugin/out_datacalculator.rb', line 292

def emit_aggregate_keys (tag, es, chain)
  cs = {}
  es.each do |time, record|
    matched = false
    pat = @aggregate_keys.map{ |key| record[key] }.join(@aggregate_delimiter)
    cs[pat] = [0] * @_formulas.length unless cs.has_key?(pat)

    if @_formulas.length > 0
      @_formulas.each do | index, outkey, inkeys, formula|
        next unless formula and checkArgs(record, inkeys)

        cs[pat][index] += execFunc('all', record, inkeys, formula)
        matched = true
      end
    else
      $log.warn index
    end
    cs[pat][0] += 1 unless matched
  end

  cs.keys.each do |pat|
    countups(pat, cs[pat])
  end

  chain.next
end

#emit_single_tag(tag, es, chain) ⇒ Object



319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/fluent/plugin/out_datacalculator.rb', line 319

def emit_single_tag (tag, es, chain)
  c = [0] * @_formulas.length

  es.each do |time,record|
    matched = false
    if @_formulas.length > 0
      @_formulas.each do |index, outkey, inkeys, formula|
        next unless formula and checkArgs(record, inkeys)
        c[index] += execFunc(nil, record, inkeys, formula)
        matched = true
      end
    else
      $log.warn index
    end
    c[0] += 1 unless matched
  end

  countups(tag, c)

  chain.next
end

#execFunc(tag, obj, argv, formula) ⇒ Object



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

def execFunc (tag, obj, argv, formula)
  if tag != nil
    tag = stripped_tag (tag)
  end
  _argv = []

  argv.each {|arg|
    if tag != nil and tag != 'all'
      arg = tag + '_' + arg
    end
    _argv.push obj[arg].to_f
  }
  formula.call(*_argv)
end

#flush(step) ⇒ Object



244
245
246
247
# File 'lib/fluent/plugin/out_datacalculator.rb', line 244

def flush(step)
  flushed, @counts = @counts,count_initialized(@counts.keys.dup)
  generate_output(flushed, step)
end

#flush_emit(step) ⇒ Object



249
250
251
252
253
254
# File 'lib/fluent/plugin/out_datacalculator.rb', line 249

def flush_emit(step)
  data = flush(step)
  data.each do |dat|
    Fluent::Engine.emit(@tag, Fluent::Engine.now, dat)
  end
end

#generate_output(counts, step) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/fluent/plugin/out_datacalculator.rb', line 190

def generate_output(counts, step)
  if @aggregate == :all
    output = {}
    counts['all'].each_with_index do |count,i|
      name = @_formulas[i][1]
      output[name] = count
    end

    if @_finalizer
      output[@_finalizer[1]] = execFunc('all', output, @_finalizer[2], @_finalizer[3])
    end

    return [output]
  end

  if @aggregate == 'keys'
    outputs = []

    counts.keys.each do |pat|
      output = {}
      pat_val = pat.split(@aggregate_delimiter).map{|x| x.to_s }
      counts[pat].each_with_index do |count, i|
        name = @_formulas[i][1]
        output[name] = count
      end

      @aggregate_keys.each_with_index do |key, i|
        output[@aggregate_keys[i]] = pat_val[i]
      end

      if @_finalizer
        output[@_finalizer[1]] = execFunc('all', output, @_finalizer[2], @_finalizer[3])
      end

      outputs.push(output)
    end

    return outputs
  end

  output = {}
  counts.keys.each do |tag|
    t = stripped_tag(tag)
    counts[tag].each_with_index do |count,i|
      name = @_formulas[i][1]
      output[t + '_' + name] = count
    end
    if @_finalizer
      output[t + '_' + @_finalizer[1]] = execFunc(tag, output, @_finalizer[2], @_finalizer[3])
    end
  end
  [output]
end

#shutdownObject



150
151
152
153
154
# File 'lib/fluent/plugin/out_datacalculator.rb', line 150

def shutdown
  super
  @watcher.terminate
  @watcher.join
end

#startObject



145
146
147
148
# File 'lib/fluent/plugin/out_datacalculator.rb', line 145

def start
  super
  start_watch
end

#start_watchObject



256
257
258
259
# File 'lib/fluent/plugin/out_datacalculator.rb', line 256

def start_watch
  # for internal, or tests only
  @watcher = Thread.new(&method(:watch))
end

#stripped_tag(tag) ⇒ Object



183
184
185
186
187
188
# File 'lib/fluent/plugin/out_datacalculator.rb', line 183

def stripped_tag(tag)
  return tag unless @input_tag_remove_prefix
  return tag[@removed_length..-1] if tag.start_with?(@removed_prefix_string) and tag.length > @removed_length
  return tag[@removed_length..-1] if tag == @input_tag_remove_prefix
  tag
end

#watchObject



261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/fluent/plugin/out_datacalculator.rb', line 261

def watch
  # instance variable, and public accessable, for test
  @last_checked = Fluent::Engine.now
  while true
    sleep 0.5
    if Fluent::Engine.now - @last_checked >= @tick
      now = Fluent::Engine.now
      flush_emit(now - @last_checked)
      @last_checked = now
    end
  end
end