Class: Sensu::Extension::Attributes

Inherits:
Filter
  • Object
show all
Defined in:
lib/sensu/extensions/filters/attributes.rb

Instance Method Summary collapse

Instance Method Details

#attributes_match?(hash_one, hash_two) ⇒ Boolean

Returns:

  • (Boolean)

43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/sensu/extensions/filters/attributes.rb', line 43

def attributes_match?(hash_one, hash_two)
  hash_one.all? do |key, value_one|
    value_two = hash_two[key]
    case
    when value_one == value_two
      true
    when value_one.is_a?(Hash) && value_two.is_a?(Hash)
      attributes_match?(value_one, value_two)
    when value_one.is_a?(String) && value_one.start_with?("eval:")
      eval_attribute_value(value_one, value_two)
    else
      false
    end
  end
end

#descriptionObject


20
21
22
# File 'lib/sensu/extensions/filters/attributes.rb', line 20

def description
  "filters events using event attributes"
end

#eval_attribute_value(raw_eval_string, value) ⇒ Object


29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/sensu/extensions/filters/attributes.rb', line 29

def eval_attribute_value(raw_eval_string, value)
  begin
    eval_string = raw_eval_string.gsub(/^eval:(\s+)?/, "")
    !!Sandbox.eval(eval_string, value)
  rescue => error
    @logger.error("filter attribute eval error", {
      :raw_eval_string => raw_eval_string,
      :value => value,
      :error => error.to_s
    })
    false
  end
end

#filter_event?(filter_name, event) ⇒ Boolean

Returns:

  • (Boolean)

59
60
61
62
63
64
65
66
67
68
# File 'lib/sensu/extensions/filters/attributes.rb', line 59

def filter_event?(filter_name, event)
  if @settings.filter_exists?(filter_name)
    filter = @settings[:filters][filter_name]
    matched = attributes_match?(filter[:attributes], event)
    filter[:negate] ? matched : !matched
  else
    @logger.error("unknown filter", :filter_name => filter_name)
    false
  end
end

#nameObject


16
17
18
# File 'lib/sensu/extensions/filters/attributes.rb', line 16

def name
  "attributes"
end

#run(event, options) {|"", status| ... } ⇒ Object

Yields:

  • ("", status)

24
25
26
27
# File 'lib/sensu/extensions/filters/attributes.rb', line 24

def run(event, options)
  status = filter_event?(options[:filter_name], event) ? 0 : 1
  yield "", status
end