diff --git a/files/base.rb b/files/base.rb index bf2396a..38e9d59 100644 --- a/files/base.rb +++ b/files/base.rb @@ -230,6 +230,19 @@ def filter_repeated initial_failing_occurrences = interval > 0 ? (alert_after / interval) : 0 number_of_failed_attempts = @event['occurrences'] - initial_failing_occurrences + # sensu 0.26+ only, else never make occurrences_watermark actionable + # https://github.com/Yelp/sensu_handlers/issues/103 + # this hack takes care of scenario + # when event has triggered at action == create + # but did not resolve the corresponding event when action == resolve. + # occurrences_watermark > initial_failing_occurrences prove enough occurrences + # of the event that triggered create and hence it is safe to filter the event to resolve handler action. + if (@event.key?('occurrences_watermark') && + @event['occurrences_watermark'] > initial_failing_occurrences && + @event['action'] == 'resolve') + return + end + # Don't bother acting if we haven't hit the # alert_after threshold if number_of_failed_attempts < 1 diff --git a/spec/functions/base_spec.rb b/spec/functions/base_spec.rb index 216855b..d420442 100644 --- a/spec/functions/base_spec.rb +++ b/spec/functions/base_spec.rb @@ -352,6 +352,43 @@ class BaseHandler expect(subject).not_to receive(:bail) expect(subject.filter_repeated).to eql(nil) end + end + context "when action == resolve" do + context "when occurrences_watermark > initial_failing_occurrences" do + it "it should fire" do + subject.event['occurrences'] = 2 + subject.event['occurrences_watermark'] = 8 + subject.event['check']['interval'] = 20 + subject.event['check']['realert_every'] = "-1" + subject.event['check']['alert_after'] = 60 + subject.event['action'] = 'resolve' + expect(subject).not_to receive(:bail) + expect(subject.filter_repeated).to eql(nil) + end + end + context "when occurrences_watermark <= initial_failing_occurrences" do + it "it should not fire" do + subject.event['occurrences'] = 3 + subject.event['occurrences_watermark'] = 3 + subject.event['check']['interval'] = 20 + subject.event['check']['realert_every'] = "-1" + subject.event['check']['alert_after'] = 60 + subject.event['action'] = 'resolve' + expect(subject).to receive(:bail) + expect(subject.filter_repeated).to eql(nil) + end + end + context "when no occurrences_watermark" do + it "it should not fire" do + subject.event['occurrences'] = 3 + subject.event['check']['interval'] = 20 + subject.event['check']['realert_every'] = "-1" + subject.event['check']['alert_after'] = 60 + subject.event['action'] = 'resolve' + expect(subject).to receive(:bail) + expect(subject.filter_repeated).to eql(nil) + end + end end end #End filter repeated