def test_sct_severity_wrong(self): event = PrometheusAlertManagerEvent(raw_alert=dict(labels=dict(sct_severity="WRONG"))) event.publish_event = False event.end_event() self.assertEqual(event.severity, Severity.NORMAL) self.assertTrue(str(event).startswith("(PrometheusAlertManagerEvent Severity.NORMAL)")) self.assertEqual(event, pickle.loads(pickle.dumps(event)))
def _publish_end_of_alerts(self, alerts: dict): all_alerts = self._get_alerts() updated_dict = {} if all_alerts: for alert in all_alerts: fingerprint = alert.get('fingerprint', None) if not fingerprint: continue updated_dict[fingerprint] = alert for alert in alerts.values(): if not alert.get('endsAt', None): alert['endsAt'] = time.strftime("%Y-%m-%dT%H:%M:%S.0Z", time.gmtime()) alert = updated_dict.get(alert['fingerprint'], alert) PrometheusAlertManagerEvent.end(raw_alert=alert).publish()
def test_sct_severity_wrong(self): event = PrometheusAlertManagerEvent.end(raw_alert=dict(labels=dict( sct_severity="WRONG"))) self.assertEqual(event.severity, Severity.WARNING) self.assertTrue( str(event).startswith( "(PrometheusAlertManagerEvent Severity.WARNING)")) self.assertEqual(event, pickle.loads(pickle.dumps(event)))
def test_sct_severity(self): event = PrometheusAlertManagerEvent.end(raw_alert=dict(labels=dict( sct_severity="CRITICAL"))) self.assertEqual(event.severity, Severity.CRITICAL) self.assertTrue( str(event).startswith( "(PrometheusAlertManagerEvent Severity.CRITICAL)")) self.assertEqual(event, pickle.loads(pickle.dumps(event)))
def test_msgfmt(self): event = PrometheusAlertManagerEvent.start(raw_alert=RAW_ALERT) self.assertEqual( str(event), "(PrometheusAlertManagerEvent Severity.WARNING): alert_name=InstanceDown type=start" " start=2019-12-24T17:00:09.591Z end=2019-12-26T06:21:09.591Z" " description=[10.0.201.178] has been down for more than 30 seconds. updated=2019-12-26T06:18:09.593Z" " state= fingerprint=None labels={'alertname': 'InstanceDown', 'instance': '[10.0.201.178]'," " 'job': 'scylla', 'monitor': 'scylla-monitor', 'severity': '2'}") self.assertEqual(event, pickle.loads(pickle.dumps(event)))
def _publish_end_of_alerts(self, alerts: dict): all_alerts = self._get_alerts() updated_dict = {} event_filter = self.event_registry.get_registry_filter() if all_alerts: for alert in all_alerts: fingerprint = alert.get('fingerprint', None) if not fingerprint: continue updated_dict[fingerprint] = alert for alert in alerts.values(): if not alert.get('endsAt', None): alert['endsAt'] = time.strftime("%Y-%m-%dT%H:%M:%S.0Z", time.gmtime()) alert = updated_dict.get(alert['fingerprint'], alert) labels = alert.get("labels") or {} alert_name = labels.get("alertname", "") node = labels.get("instance", "N/A") event_filter.filter_by_attr(base="PrometheusAlertManagerEvent", node=node, starts_at=alert.get("startsAt"), alert_name=alert_name, period_type=EventPeriod.BEGIN.value) begun_events = event_filter.get_filtered() if not begun_events: new_event = PrometheusAlertManagerEvent(raw_alert=alert) new_event.period_type = EventPeriod.INFORMATIONAL.value new_event.end_event() return if len(begun_events) > 1: LOGGER.debug( "Found %s events of type '%s' started at %s with period %s. " "Will apply the function to most recent event by default.", len(begun_events), alert_name, alert.get("startsAt"), EventPeriod.BEGIN.value) event = begun_events[-1] event.end_event()
def _publish_end_of_alerts(self, alerts: dict): all_alerts = self._get_alerts() updated_dict = {} if all_alerts: for alert in all_alerts: fingerprint = alert.get('fingerprint', None) if not fingerprint: continue updated_dict[fingerprint] = alert for alert in alerts.values(): if not alert.get('endsAt', None): alert['endsAt'] = time.strftime("%Y-%m-%dT%H:%M:%S.0Z", time.gmtime()) alert = updated_dict.get(alert['fingerprint'], alert) labels = alert.get("labels") or {} alert_name = labels.get("alertname", "") node = labels.get("instance", "N/A") continuous_hash = PrometheusAlertManagerEvent.get_continuous_hash_from_dict( { 'node': node, 'starts_at': alert.get("startsAt"), 'alert_name': alert_name }) if begin_event := self.event_registry.find_continuous_events_by_hash( continuous_hash): begin_event[-1].end_event() continue new_event = PrometheusAlertManagerEvent(raw_alert=alert) new_event.period_type = EventPeriod.INFORMATIONAL.value new_event.end_event()
def test_msgfmt(self): event = PrometheusAlertManagerEvent(raw_alert=RAW_ALERT) event.event_id = "536eaf22-3d8f-418a-9381-fe0bcdce7ad9" event.publish_event = False event.begin_event() self.assertEqual( str(event), "(PrometheusAlertManagerEvent Severity.NORMAL) period_type=begin " "event_id=536eaf22-3d8f-418a-9381-fe0bcdce7ad9: alert_name=InstanceDown node=[10.0.201.178] " "start=2019-12-24T17:00:09.591Z end=2019-12-26T06:21:09.591Z " "description=[10.0.201.178] has been down for more than 30 seconds. updated=2019-12-26T06:18:09.593Z " "state= fingerprint=None labels={'alertname': 'InstanceDown', 'instance': '[10.0.201.178]', 'job': 'scylla'" ", 'monitor': 'scylla-monitor', 'severity': '2'}") self.assertEqual(event, pickle.loads(pickle.dumps(event)))
def _publish_new_alerts(self, alerts: dict): # pylint: disable=no-self-use for alert in alerts.values(): PrometheusAlertManagerEvent(raw_alert=alert).begin_event()