示例#1
0
    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_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 = {}
        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()