Skip to content

Instantly share code, notes, and snippets.

@cabecada
Created April 7, 2017 11:56
Show Gist options
  • Save cabecada/3bd548a3cb5e3415a8fed30b276d2409 to your computer and use it in GitHub Desktop.
Save cabecada/3bd548a3cb5e3415a8fed30b276d2409 to your computer and use it in GitHub Desktop.
extension simulating yelp/filter_repeated
module Sensu
module Extension
class Yelp < Filter
def name
"yelp"
end
def description
"filter events using event occurrences"
end
def power_of_two?(x)
while ( x % 2) == 0 and x > 1
x /= 2
end
x==1
end
def event_filtered?(event)
@event = event
if @event[:check][:name] == 'keepalive'
interval = 20
else
interval = @event[:check][:interval].to_i || 0
end
alert_after = @event[:check][:alert_after].to_i || 0
realert_every = ( @event[:check][:realert_every] || 1 ).to_i
initial_failing_occurrences = interval > 0 ? (alert_after / interval) : 0
number_of_failed_attempts = @event[:occurrences] - initial_failing_occurrences
puts "#{@event}"
puts "interval=#{interval}, alert_after=#{alert_after}, alert_after=#{alert_after}, initial_failing_occurrences=#{initial_failing_occurrences}, number_of_failed_attempts=#{number_of_failed_attempts}"
if (@event.key?(:occurrences_watermark) &&
@event[:occurrences_watermark] > initial_failing_occurrences &&
@event[:action] == :resolve)
return ["enough occurrences", 1]
end
if number_of_failed_attempts < 1
return ["not enough occurrences", 0]
elsif interval > 0 and @event[:action] == :create
if realert_every == -1
if power_of_two?(number_of_failed_attempts)
return ["enough occurrences", 1]
else
return ["not enough occurrences", 0]
end
elsif (number_of_failed_attempts - 1) % realert_every != 0
return ["not enough occurrences", 0]
end
end
return ["not enough occurrences", 0]
end
def run(event)
yield event_filtered?(event)
end
end
end
end
@cabecada
Copy link
Author

cabecada commented Apr 7, 2017

test:

{
  "checks": {
    "pls_ignore": {
      "command": "ls /tmp/foo",
      "dependencies": [
      ],
      "handlers": [
        "hipchat"
      ],
      "occurrences": 4,
      "refresh": 300,
      "interval": 10,
      "runbook": "https://example.com/",
      "team": "india_ops",
      "alert_after": 40,
      "realert_every": "-1",
      "page": true,
      "ticket": false,
      "region": "vagrant-vagrant",
      "standalone": true,
      "timeout": 10
    }
  }
}

expectation:
handler should trigger when
action == create and

  • number_of_failed_attempts > 1
  • number_of_failed_attempts is a power of two

handler should trigger when

  • action == resolve.

yelp/filter_repeated should not be called at all.

observation:
works as expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment