Skip to content

Commit

Permalink
Merge pull request #442 from arachnys/trigger-alert-severity
Browse files Browse the repository at this point in the history
Make alerts trigger if they are more severe than previous alert
  • Loading branch information
frankh authored Mar 2, 2017
2 parents a43efe7 + 8c72484 commit 1908f74
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
master
------

* More severe alerts should trigger even if a less severe alert was recently sent
* Update production.env.example email settings
* Convert environment vars to boolean nicely

Expand Down
4 changes: 3 additions & 1 deletion cabot/cabotapp/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,9 @@ def alert(self):
timezone.now() - timedelta(minutes=settings.NOTIFICATION_INTERVAL)) < self.last_alert_sent:
return
elif self.overall_status in (self.CRITICAL_STATUS, self.ERROR_STATUS):
if self.last_alert_sent and (
more_important = self.old_overall_status == self.WARNING_STATUS or \
(self.old_overall_status == self.ERROR_STATUS and self.overall_status == self.CRITICAL_STATUS)
if not more_important and self.last_alert_sent and (
timezone.now() - timedelta(minutes=settings.ALERT_INTERVAL)) < self.last_alert_sent:
return
self.last_alert_sent = timezone.now()
Expand Down
67 changes: 67 additions & 0 deletions cabot/cabotapp/tests/tests_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,27 @@ class TestAlerts(LocalTestCase):
def setUp(self):
super(TestAlerts, self).setUp()

self.warning_http_check = HttpStatusCheck.objects.create(
name='Http Check',
created_by=self.user,
importance=Service.WARNING_STATUS,
endpoint='http://arachnys.com',
timeout=10,
status_code='200',
text_match=None,
)
self.error_http_check = HttpStatusCheck.objects.create(
name='Http Check',
created_by=self.user,
importance=Service.ERROR_STATUS,
endpoint='http://arachnys.com',
timeout=10,
status_code='200',
text_match=None,
)
self.service.status_checks.add(self.warning_http_check, self.error_http_check)
self.critical_http_check = self.http_check

self.user.profile.hipchat_alias = "test_user_hipchat_alias"
self.user.profile.save()

Expand All @@ -935,6 +956,52 @@ def test_alert(self, fake_send_alert):
self.assertEqual(fake_send_alert.call_count, 1)
fake_send_alert.assert_called_with(self.service, duty_officers=[])

def trigger_failing_check(self, check):
StatusCheckResult(
status_check=check,
time=timezone.now() - timedelta(seconds=60),
time_complete=timezone.now() - timedelta(seconds=59),
succeeded=False
).save()
check.last_run = timezone.now()
check.save()

@patch('cabot.cabotapp.models.send_alert')
def test_alert_increasing_severity(self, fake_send_alert):
self.trigger_failing_check(self.warning_http_check)
self.assertEqual(fake_send_alert.call_count, 1)

self.trigger_failing_check(self.error_http_check)
self.assertEqual(fake_send_alert.call_count, 2)

self.trigger_failing_check(self.critical_http_check)
self.assertEqual(fake_send_alert.call_count, 3)

@patch('cabot.cabotapp.models.send_alert')
def test_alert_decreasing_severity(self, fake_send_alert):
self.trigger_failing_check(self.critical_http_check)
self.assertEqual(fake_send_alert.call_count, 1)

self.trigger_failing_check(self.error_http_check)
self.assertEqual(fake_send_alert.call_count, 1)

self.trigger_failing_check(self.warning_http_check)
self.assertEqual(fake_send_alert.call_count, 1)

@patch('cabot.cabotapp.models.send_alert')
def test_alert_alternating_severity(self, fake_send_alert):
self.trigger_failing_check(self.error_http_check)
self.assertEqual(fake_send_alert.call_count, 1)

self.trigger_failing_check(self.warning_http_check)
self.assertEqual(fake_send_alert.call_count, 1)

self.trigger_failing_check(self.error_http_check)
self.assertEqual(fake_send_alert.call_count, 1)

self.trigger_failing_check(self.critical_http_check)
self.assertEqual(fake_send_alert.call_count, 2)

def test_update_plugins(self):
# Test that disabling a plugin is detected by update_alert_plugins
plugins = update_alert_plugins()
Expand Down

0 comments on commit 1908f74

Please sign in to comment.