Class: Fluent::Plugin::NotifierOutput::Definition

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(section, plugin) ⇒ Definition



313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'lib/fluent/plugin/out_notifier.rb', line 313

def initialize(section, plugin)
  @pattern = section.pattern

  @tag = section.tag || plugin.default_tag
  @tag_warn = section.tag_warn || plugin.default_tag_warn
  @tag_crit = section.tag_crit || plugin.default_tag_crit

  @target_keys = section.target_keys
  @target_key_pattern = section.target_key_pattern ? Regexp.compile(section.target_key_pattern) : nil
  @exclude_key_pattern = section.exclude_key_pattern ? Regexp.compile(section.exclude_key_pattern) : nil
  if !@target_keys and !@target_key_pattern
    raise Fluent::ConfigError, "out_notifier needs one of target_keys or target_key_pattern in <def>"
  end

  case section.check
  when :numeric_upward
    @check = :upward
    if !section.crit_threshold || !section.warn_threshold
      raise Fluent::ConfigError, "Both of crit_threshold and warn_threshold must be specified for 'check numeric_upward'"
    end
    @crit_threshold = section.crit_threshold
    @warn_threshold = section.warn_threshold
  when :numeric_downward
    @check = :downward
    if !section.crit_threshold || !section.warn_threshold
      raise Fluent::ConfigError, "Both of crit_threshold and warn_threshold must be specified for 'check numeric_downward'"
    end
    @crit_threshold = section.crit_threshold
    @warn_threshold = section.warn_threshold
  when :string_find
    @check = :find
    if !section.crit_regexp || !section.warn_regexp
      raise Fluent::ConfigError, "Both of crit_regexp and warn_regexp must be specified for 'check string_find'"
    end
    @crit_regexp = Regexp.compile(section.crit_regexp)
    @warn_regexp = Regexp.compile(section.warn_regexp)
  else
    raise "BUG: unknown check: #{section.check}"
  end

  @intervals = if section.intervals
                 section.intervals
               elsif section.interval_1st || section.interval_2nd || section.interval_3rd
                 [section.interval_1st || plugin.default_intervals[0], section.interval_2nd || plugin.default_intervals[1], section.interval_3rd || plugin.default_intervals[2]]
               else
                 plugin.default_intervals
               end
  @repetitions = if section.repetitions
                   section.repetitions
                 elsif section.repetitions_1st || section.repetitions_2nd
                   [section.repetitions_1st || plugin.default_repetitions[0], section.repetitions_2nd || plugin.default_repetitions[1]]
                 else
                   plugin.default_repetitions
                 end
end

Instance Attribute Details

#crit_regexpObject

for ‘string_find’



311
312
313
# File 'lib/fluent/plugin/out_notifier.rb', line 311

def crit_regexp
  @crit_regexp
end

#crit_thresholdObject

for ‘numeric_upward’, ‘numeric_downward’



310
311
312
# File 'lib/fluent/plugin/out_notifier.rb', line 310

def crit_threshold
  @crit_threshold
end

#exclude_key_patternObject

Returns the value of attribute exclude_key_pattern.



309
310
311
# File 'lib/fluent/plugin/out_notifier.rb', line 309

def exclude_key_pattern
  @exclude_key_pattern
end

#intervalsObject

Returns the value of attribute intervals.



308
309
310
# File 'lib/fluent/plugin/out_notifier.rb', line 308

def intervals
  @intervals
end

#patternObject

Returns the value of attribute pattern.



309
310
311
# File 'lib/fluent/plugin/out_notifier.rb', line 309

def pattern
  @pattern
end

#repetitionsObject

Returns the value of attribute repetitions.



308
309
310
# File 'lib/fluent/plugin/out_notifier.rb', line 308

def repetitions
  @repetitions
end

#tagObject

Returns the value of attribute tag.



307
308
309
# File 'lib/fluent/plugin/out_notifier.rb', line 307

def tag
  @tag
end

#tag_critObject

Returns the value of attribute tag_crit.



307
308
309
# File 'lib/fluent/plugin/out_notifier.rb', line 307

def tag_crit
  @tag_crit
end

#tag_warnObject

Returns the value of attribute tag_warn.



307
308
309
# File 'lib/fluent/plugin/out_notifier.rb', line 307

def tag_warn
  @tag_warn
end

#target_key_patternObject

Returns the value of attribute target_key_pattern.



309
310
311
# File 'lib/fluent/plugin/out_notifier.rb', line 309

def target_key_pattern
  @target_key_pattern
end

#target_keysObject

Returns the value of attribute target_keys.



309
310
311
# File 'lib/fluent/plugin/out_notifier.rb', line 309

def target_keys
  @target_keys
end

#warn_regexpObject

for ‘string_find’



311
312
313
# File 'lib/fluent/plugin/out_notifier.rb', line 311

def warn_regexp
  @warn_regexp
end

#warn_thresholdObject

for ‘numeric_upward’, ‘numeric_downward’



310
311
312
# File 'lib/fluent/plugin/out_notifier.rb', line 310

def warn_threshold
  @warn_threshold
end

Instance Method Details

#check(tag, time, record, key) ⇒ Object

'pattern' => 'http_status_errors',
'target_tag' => 'httpstatus.blog',
'target_key' => 'blog_5xx_percentage',
'check_type' => 'numeric_upward'
'level' => 'warn', # 'regexp' => '[WARN] .* MUST BE CHECKED!$'
'threshold' => 25,
'value' => 49, # 'value' => '2012/05/15 18:01:59 [WARN] wooooops, MUST BE CHECKED!'
'message_time' => Time.instance



387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
# File 'lib/fluent/plugin/out_notifier.rb', line 387

def check(tag, time, record, key)
  case @check
  when :upward
    value = record[key].to_f
    if @crit_threshold and value >= @crit_threshold
      {
        :hashkey => @pattern + "\t" + tag + "\t" + key,
        :match_def => self,
        :emit_tag => (@tag_crit || @tag),
        'pattern' => @pattern, 'target_tag' => tag, 'target_key' => key, 'check_type' => 'numeric_upward', 'level' => 'crit',
        'threshold' => @crit_threshold, 'value' => value, 'message_time' => Time.at(time).to_s
      }
    elsif @warn_threshold and value >= @warn_threshold
      {
        :hashkey => @pattern + "\t" + tag + "\t" + key,
        :match_def => self,
        :emit_tag => (@tag_warn || @tag),
        'pattern' => @pattern, 'target_tag' => tag, 'target_key' => key, 'check_type' => 'numeric_upward', 'level' => 'warn',
        'threshold' => @warn_threshold, 'value' => value, 'message_time' => Time.at(time).to_s
      }
    else
      nil
    end
  when :downward
    value = record[key].to_f
    if @crit_threshold and value <= @crit_threshold
      {
        :hashkey => @pattern + "\t" + tag + "\t" + key,
        :match_def => self,
        :emit_tag => (@tag_crit || @tag),
        'pattern' => @pattern, 'target_tag' => tag, 'target_key' => key, 'check_type' => 'numeric_downward', 'level' => 'crit',
        'threshold' => @crit_threshold, 'value' => value, 'message_time' => Time.at(time).to_s
      }
    elsif @warn_threshold and value <= @warn_threshold
      {
        :hashkey => @pattern + "\t" + tag + "\t" + key,
        :match_def => self,
        :emit_tag => (@tag_warn || @tag),
        'pattern' => @pattern, 'target_tag' => tag, 'target_key' => key, 'check_type' => 'numeric_downward', 'level' => 'warn',
        'threshold' => @warn_threshold, 'value' => value, 'message_time' => Time.at(time).to_s
      }
    else
      nil
    end
  when :find
    str = record[key].to_s
    if match(@crit_regexp, str)
      {
        :hashkey => @pattern + "\t" + tag + "\t" + key,
        :match_def => self,
        :emit_tag => (@tag_crit || @tag),
        'pattern' => @pattern, 'target_tag' => tag, 'target_key' => key, 'check_type' => 'string_find', 'level' => 'crit',
        'regexp' => @crit_regexp.inspect, 'value' => str, 'message_time' => Time.at(time).to_s
      }
    elsif match(@warn_regexp, str)
      {
        :hashkey => @pattern + "\t" + tag + "\t" + key,
        :match_def => self,
        :emit_tag => (@tag_warn || @tag),
        'pattern' => @pattern, 'target_tag' => tag, 'target_key' => key, 'check_type' => 'string_find', 'level' => 'warn',
        'regexp' => @warn_regexp.inspect, 'value' => str, 'message_time' => Time.at(time).to_s
      }
    else
      nil
    end
  else
    raise ArgumentError, "unknown check type (maybe bug): #{@check}"
  end
end

#match(regexp, string) ⇒ Object



457
458
459
460
461
462
463
# File 'lib/fluent/plugin/out_notifier.rb', line 457

def match(regexp,string)
  regexp && regexp.match(string)
rescue ArgumentError => e
  raise e unless e.message.index("invalid byte sequence in") == 0
  replaced_string = replace_invalid_byte(string)
  regexp.match(replaced_string)
end

#match?(key) ⇒ Boolean



369
370
371
372
373
374
375
# File 'lib/fluent/plugin/out_notifier.rb', line 369

def match?(key)
  if @target_keys
    @target_keys.include?(key)
  elsif @target_key_pattern
    @target_key_pattern.match(key) and not @exclude_key_pattern.match(key)
  end
end

#replace_invalid_byte(string) ⇒ Object



465
466
467
468
469
# File 'lib/fluent/plugin/out_notifier.rb', line 465

def replace_invalid_byte(string)
   replace_options = { invalid: :replace, undef: :replace, replace: '?' }
   temporal_encoding = (string.encoding == Encoding::UTF_8 ? Encoding::UTF_16BE : Encoding::UTF_8)
   string.encode(temporal_encoding, string.encoding, replace_options).encode(string.encoding)
end