Class: Fluent::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(element, defaults) ⇒ Definition

Returns a new instance of Definition.



287
288
289
290
291
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
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
# File 'lib/fluent/plugin/out_notifier.rb', line 287

def initialize(element, defaults)
  element.keys.each do |k|
    case k
    when 'pattern'
      @pattern = element[k]
    when 'check'
      case element[k]
      when 'numeric_upward'
        @check = :upward
        unless element.has_key?('crit_threshold') and element.has_key?('warn_threshold')
          raise Fluent::ConfigError, "Both of crit_threshold and warn_threshold must be specified for 'check numeric_upward'"
        end
        @crit_threshold = element['crit_threshold'].to_f
        @warn_threshold = element['warn_threshold'].to_f
      when 'numeric_downward'
        @check = :downward
        unless element.has_key?('crit_threshold') and element.has_key?('warn_threshold')
          raise Fluent::ConfigError, "Both of crit_threshold and warn_threshold must be specified for 'check_numeric_downward'"
        end
        @crit_threshold = element['crit_threshold'].to_f
        @warn_threshold = element['warn_threshold'].to_f
      when 'string_find'
        @check = :find
        unless element.has_key?('crit_regexp') and element.has_key?('warn_regexp')
          raise Fluent::ConfigError, "Both of crit_regexp and warn_regexp must be specified for 'check string_find'"
        end
        @crit_regexp = Regexp.compile(element['crit_regexp'].to_s)
        @warn_regexp = Regexp.compile(element['warn_regexp'].to_s)
      else
        raise Fluent::ConfigError, "invalid check type: #{element[k]}"
      end
    when 'target_keys'
      @target_keys = element['target_keys'].split(',')
    when 'target_key_pattern'
      @target_key_pattern = Regexp.compile(element['target_key_pattern'])
      @exclude_key_pattern = Regexp.compile(element['exclude_key_pattern'] || '^$')
    end
  end
  if @pattern.nil? or @pattern.length < 1
    raise Fluent::ConfigError, "pattern must be set"
  end
  if @target_keys.nil? and @target_key_pattern.nil?
    raise Fluent::ConfigError, "out_notifier needs one of target_keys or target_key_pattern"
  end
  @tag = element['tag'] || defaults[:tag]
  @tag_warn = element['tag_warn'] || defaults[:tag_warn]
  @tag_crit = element['tag_crit'] || defaults[:tag_crit]
  @intervals = [
                (element['interval_1st'] || defaults[:interval_1st]).to_f,
                (element['interval_2nd'] || defaults[:interval_2nd]).to_f,
                (element['interval_3rd'] || defaults[:interval_3rd]).to_f
               ]
  @repetitions = [
                  (element['repetitions_1st'] || defaults[:repetitions_1st]).to_f,
                  (element['repetitions_2nd'] || defaults[:repetitions_2nd]).to_f
                 ]
end

Instance Attribute 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



363
364
365
# File 'lib/fluent/plugin/out_notifier.rb', line 363

def check
  @check
end

#crit_regexpObject

for ‘string_find’



285
286
287
# File 'lib/fluent/plugin/out_notifier.rb', line 285

def crit_regexp
  @crit_regexp
end

#crit_thresholdObject

for ‘numeric_upward’, ‘numeric_downward’



284
285
286
# File 'lib/fluent/plugin/out_notifier.rb', line 284

def crit_threshold
  @crit_threshold
end

#exclude_key_patternObject

Returns the value of attribute exclude_key_pattern.



283
284
285
# File 'lib/fluent/plugin/out_notifier.rb', line 283

def exclude_key_pattern
  @exclude_key_pattern
end

#intervalsObject

Returns the value of attribute intervals.



282
283
284
# File 'lib/fluent/plugin/out_notifier.rb', line 282

def intervals
  @intervals
end

#patternObject

Returns the value of attribute pattern.



283
284
285
# File 'lib/fluent/plugin/out_notifier.rb', line 283

def pattern
  @pattern
end

#repetitionsObject

Returns the value of attribute repetitions.



282
283
284
# File 'lib/fluent/plugin/out_notifier.rb', line 282

def repetitions
  @repetitions
end

#tagObject

Returns the value of attribute tag.



281
282
283
# File 'lib/fluent/plugin/out_notifier.rb', line 281

def tag
  @tag
end

#tag_critObject

Returns the value of attribute tag_crit.



281
282
283
# File 'lib/fluent/plugin/out_notifier.rb', line 281

def tag_crit
  @tag_crit
end

#tag_warnObject

Returns the value of attribute tag_warn.



281
282
283
# File 'lib/fluent/plugin/out_notifier.rb', line 281

def tag_warn
  @tag_warn
end

#target_key_patternObject

Returns the value of attribute target_key_pattern.



283
284
285
# File 'lib/fluent/plugin/out_notifier.rb', line 283

def target_key_pattern
  @target_key_pattern
end

#target_keysObject

Returns the value of attribute target_keys.



283
284
285
# File 'lib/fluent/plugin/out_notifier.rb', line 283

def target_keys
  @target_keys
end

#warn_regexpObject

for ‘string_find’



285
286
287
# File 'lib/fluent/plugin/out_notifier.rb', line 285

def warn_regexp
  @warn_regexp
end

#warn_thresholdObject

for ‘numeric_upward’, ‘numeric_downward’



284
285
286
# File 'lib/fluent/plugin/out_notifier.rb', line 284

def warn_threshold
  @warn_threshold
end

Instance Method Details

#match(regexp, string) ⇒ Object



433
434
435
436
437
438
439
# File 'lib/fluent/plugin/out_notifier.rb', line 433

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

Returns:

  • (Boolean)


345
346
347
348
349
350
351
# File 'lib/fluent/plugin/out_notifier.rb', line 345

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



441
442
443
444
445
# File 'lib/fluent/plugin/out_notifier.rb', line 441

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