class Fluent::Plugin::NotifierOutput::Definition

Attributes

crit_regexp[RW]
crit_threshold[RW]
exclude_key_pattern[RW]
intervals[RW]
pattern[RW]
repetitions[RW]
tag[RW]
tag_crit[RW]
tag_warn[RW]
target_key_pattern[RW]
target_keys[RW]
warn_regexp[RW]
warn_threshold[RW]

Public Class Methods

new(section, plugin) click to toggle source
# 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

Public Instance Methods

check(tag, time, record, key) click to toggle source

{

'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

}

# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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