Module: Sensu::Server::Filter

Included in:
Process
Defined in:
lib/sensu/server/filter.rb

Instance Method Summary collapse

Instance Method Details

#event_filter(filter_name, event) {|filtered| ... } ⇒ Object

Determine if an event is filtered by an event filter, native or extension. This method first checks for the existence of a native filter, then checks for an extension if a native filter is not defined. The provided callback is called with a single parameter, indicating if the event was filtered by a filter. If a filter does not exist for the provided name, the event is not filtered.

Parameters:

  • filter_name (String)
  • event (Hash)

Yields:

  • (filtered)

    callback/block called with a single parameter to indicate if the event was filtered.

Yield Parameters:

  • filtered (TrueClass, FalseClass)

    indicating if the event was filtered.

  • filter_name (String)

    name of the filter being evaluated



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/sensu/server/filter.rb', line 135

def event_filter(filter_name, event)
  case
  when @settings.filter_exists?(filter_name)
    native_filter(filter_name, event) do |filtered|
      yield(filtered, filter_name)
    end
  when @extensions.filter_exists?(filter_name)
    extension_filter(filter_name, event) do |filtered|
      yield(filtered, filter_name)
    end
  else
    @logger.error("unknown filter", :filter_name => filter_name)
    yield(false, filter_name)
  end
end

#event_filtered?(handler, event) {|filtered| ... } ⇒ Boolean

Determine if an event is filtered for a handler. If a handler specifies one or more filters, via ‘:filters` or `:filter`, the `event_filter()` method is called for each of them. If any of the filters return `true`, the event is filtered for the handler. If no filters are defined in the handler definition, the event is not filtered.

Parameters:

  • handler (Hash)

    definition.

  • event (Hash)

Yields:

  • (filtered)

    callback/block called with a single parameter to indicate if the event was filtered.

Yield Parameters:

  • filtered (TrueClass, FalseClass)

    indicating if the event was filtered.

Returns:

  • (Boolean)


164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/sensu/server/filter.rb', line 164

def event_filtered?(handler, event)
  if handler.has_key?(:filters) || handler.has_key?(:filter)
    filter_list = Array(handler[:filters] || handler[:filter]).dup
    filter = Proc.new do |filter_list|
      filter_name = filter_list.shift
      if filter_name.nil?
        yield(false)
      else
        event_filter(filter_name, event) do |filtered|
          filtered ? yield(true, filter_name) : EM.next_tick { filter.call(filter_list) }
        end
      end
    end
    filter.call(filter_list)
  else
    yield(false)
  end
end

#extension_filter(filter_name, event) {|filtered| ... } ⇒ Object

Determine if an event is filtered by a filter extension.

Parameters:

  • filter_name (String)
  • event (Hash)

Yields:

  • (filtered)

    callback/block called with a single parameter to indicate if the event was filtered.

Yield Parameters:

  • filtered (TrueClass, FalseClass)

    indicating if the event was filtered.

  • filter_name (String)

    name of the filter being evaluated



109
110
111
112
113
114
115
116
117
118
# File 'lib/sensu/server/filter.rb', line 109

def extension_filter(filter_name, event)
  extension = @extensions[:filters][filter_name]
  if in_filter_time_windows?(extension.definition)
    extension.safe_run(event) do |output, status|
      yield(status == 0, filter_name)
    end
  else
    yield(false, filter_name)
  end
end

#filter_event(handler, event) {|event| ... } ⇒ Object

Attempt to filter an event for a handler. This method will check to see if handling is disabled, if the event action is handled, if the event check severity is handled, if the handler is subdued, and if the event is filtered by any of the filters specified in the handler definition.

Parameters:

  • handler (Hash)

    definition.

  • event (Hash)

Yields:

  • (event)

    callback/block called if the event has not been filtered.

Yield Parameters:

  • event (Hash)


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
# File 'lib/sensu/server/filter.rb', line 194

def filter_event(handler, event)
  handler_info = case handler[:type]
    when "extension" then handler.definition
    else handler  
  end
  details = {:handler => handler_info, :event => event}
  filter_message = case
  when handling_disabled?(event)
    "event handling disabled for event"
  when !handle_action?(handler, event)
    "handler does not handle action"
  when !handle_severity?(handler, event)
    "handler does not handle event severity"
  when handler_silenced?(handler, event)
    "handler is silenced"
  end
  if filter_message
    @logger.info(filter_message, details)
    @in_progress[:events] -= 1 if @in_progress
  else
    event_filtered?(handler, event) do |filtered, filter_name|
      unless filtered
        yield(event)
      else
        details[:filter] = filter_name
        @logger.info("event was filtered", details)
        @in_progress[:events] -= 1 if @in_progress
      end
    end
  end
end

#handle_action?(handler, event) ⇒ TrueClass, FalseClass

Determine if an event with an action should be handled. An event action of ‘:flapping` indicates that the event state is flapping, and the event should not be handled unless its handler has `:handle_flapping` set to `true`.

Parameters:

  • handler (Hash)

    definition.

  • event (Hash)

Returns:

  • (TrueClass, FalseClass)


31
32
33
34
# File 'lib/sensu/server/filter.rb', line 31

def handle_action?(handler, event)
  event[:action] != :flapping ||
    (event[:action] == :flapping && !!handler[:handle_flapping])
end

#handle_severity?(handler, event) ⇒ TrueClass, FalseClass

Determine if an event with a check severity will be handled. Event handlers can specify the check severities they will handle, using the definition attribute ‘:severities`. The possible severities are “ok”, “warning”, “critical”, and “unknown”. Handler severity filtering is bypassed when the event `:action` is `:resolve` and a previous check history status identifies a severity specified in the handler definition. It’s possible for a check history status of 0 to have had the flapping action, so we are unable to consider every past 0 to indicate a resolution.

Parameters:

  • handler (Hash)

    definition.

  • event (Hash)

Returns:

  • (TrueClass, FalseClass)


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/sensu/server/filter.rb', line 50

def handle_severity?(handler, event)
  if handler.has_key?(:severities)
    case event[:action]
    when :resolve
      event[:check][:history].any? do |status|
        severity = SEVERITIES[status.to_i] || "unknown"
        handler[:severities].include?(severity)
      end
    else
      severity = SEVERITIES[event[:check][:status]] || "unknown"
      handler[:severities].include?(severity)
    end
  else
    true
  end
end

#handler_silenced?(handler, event) ⇒ TrueClass, FalseClass

Determine if an event handler is silenced.

Parameters:

  • handler (Hash)

    definition.

  • event (Hash)

Returns:

  • (TrueClass, FalseClass)


9
10
11
# File 'lib/sensu/server/filter.rb', line 9

def handler_silenced?(handler, event)
  event[:silenced] && !handler[:handle_silenced]
end

#handling_disabled?(event) ⇒ TrueClass, FalseClass

Determine if handling is disabled for an event. Check definitions can disable event handling with an attribute, ‘:handle`, by setting it to `false`.

Parameters:

  • event (Hash)

Returns:

  • (TrueClass, FalseClass)


19
20
21
# File 'lib/sensu/server/filter.rb', line 19

def handling_disabled?(event)
  event[:check][:handle] == false
end

#in_filter_time_windows?(filter) ⇒ TrueClass, FalseClass

Determine if a filter is to be evoked for the current time. A filter can be configured with a time window defining when it is to be evoked, e.g. Monday through Friday, 9-5.

Parameters:

  • filter (Hash)

    definition.

Returns:

  • (TrueClass, FalseClass)


73
74
75
76
77
78
79
# File 'lib/sensu/server/filter.rb', line 73

def in_filter_time_windows?(filter)
  if filter[:when]
    in_time_windows?(filter[:when])
  else
    true
  end
end

#native_filter(filter_name, event) {|filtered| ... } ⇒ Object

Determine if an event is filtered by a native filter.

Parameters:

  • filter_name (String)
  • event (Hash)

Yields:

  • (filtered)

    callback/block called with a single parameter to indicate if the event was filtered.

Yield Parameters:

  • filtered (TrueClass, FalseClass)

    indicating if the event was filtered.

  • filter_name (String)

    name of the filter being evaluated



90
91
92
93
94
95
96
97
98
# File 'lib/sensu/server/filter.rb', line 90

def native_filter(filter_name, event)
  filter = @settings[:filters][filter_name]
  if in_filter_time_windows?(filter)
    matched = attributes_match?(event, filter[:attributes])
    yield(filter[:negate] ? matched : !matched, filter_name)
  else
    yield(false, filter_name)
  end
end