Last active
August 29, 2015 14:22
-
-
Save garlandkr/dff0ede6d1f5c05d910c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def filter_repeated | |
if @event['check']['name'] == 'keepalive' | |
# Keepalives are a special case because they don't emit an interval. | |
# They emit a heartbeat every 20 seconds per | |
# http://sensuapp.org/docs/0.12/keepalives | |
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'].to_i || 1 | |
initial_failing_occurrences = interval > 0 ? (alert_after / interval) : 0 | |
number_of_failed_attempts = @event['occurrences'] - initial_failing_occurrences | |
# Don't bother acting if we haven't hit the | |
# alert_after threshold | |
if number_of_failed_attempts < 1 | |
bail "Not failing long enough, only #{number_of_failed_attempts} after " \ | |
"#{initial_failing_occurrences} initial failing occurrences" | |
# If we have an interval, and this is a creation event, that means we are | |
# an active check | |
# Lets also filter based on the realert_every setting | |
elsif interval > 0 and @event['action'] == 'create' | |
# Special case of exponential backoff | |
if realert_every == -1 | |
# If our number of failed attempts is an exponent of 2 | |
if power_of_two?(number_of_failed_attempts) | |
# Then This is our MOMENT! | |
return nil | |
else | |
bail "not on a power of two: #{number_of_failed_attempts}" | |
end | |
elsif (number_of_failed_attempts - 1) % realert_every != 0 | |
# Now bail if we are not in the realert_every cycle | |
bail "only handling every #{realert_every} occurrences, and we are at" \ | |
" #{number_of_failed_attempts}" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment