Exemple #1
0
 def sync_alarms(self):
     """
     Raise/close related alarms
     """
     # Check errors are exists
     n_errors = sum(1 for e in self.iter_errors())
     alarm = ActiveAlarm.objects.filter(
         alarm_class=self.AC_POLICY_VIOLATION.id,
         managed_object=self.object.id).first()
     if n_errors:
         if not alarm:
             self.logger.info("Raise alarm")
             # Raise alarm
             alarm = ActiveAlarm(
                 timestamp=datetime.datetime.now(),
                 managed_object=self.object,
                 alarm_class=self.AC_POLICY_VIOLATION,
                 severity=2000,  # WARNING
             )
         # Alarm is already exists
         alarm.log_message("%d errors has been found" % n_errors)
     elif alarm:
         # Clear alarm
         self.logger.info("Clear alarm")
         alarm.clear_alarm("No errors has been registered")
Exemple #2
0
    def update_umbrella(self, umbrella_cls, details):
        """
        Update umbrella alarm status for managed object

        :param umbrella_cls: Umbrella alarm class (AlarmClass instance)
        :param details: List of dicts, containing
            * alarm_class - Detail alarm class
            * path - Additional path
            * severity - Alarm severity
            * vars - dict of alarm vars
        :return:
        """
        from noc.fm.models.activealarm import ActiveAlarm

        now = datetime.datetime.now()
        umbrella = ActiveAlarm.objects.filter(
            alarm_class=umbrella_cls.id,
            managed_object=self.object.id).first()
        u_sev = sum(d.get("severity", 0) for d in details)
        if not umbrella and not details:
            # No money, no honey
            return
        elif not umbrella and details:
            # Open new umbrella
            umbrella = ActiveAlarm(timestamp=now,
                                   managed_object=self.object.id,
                                   alarm_class=umbrella_cls.id,
                                   severity=u_sev)
            umbrella.save()
            self.logger.info("Opening umbrella alarm %s (%s)", umbrella.id,
                             umbrella_cls.name)
        elif umbrella and not details:
            # Close existing umbrella
            self.logger.info("Clearing umbrella alarm %s (%s)", umbrella.id,
                             umbrella_cls.name)
            umbrella.clear_alarm("Closing umbrella")
        elif umbrella and details and u_sev != umbrella.severity:
            self.logger.info("Change umbrella alarm %s severity %s -> %s (%s)",
                             umbrella.id, umbrella.severity, u_sev,
                             umbrella_cls.name)
            umbrella.change_severity(severity=u_sev)
        # Get existing details for umbrella
        active_details = {}  # (alarm class, path) -> alarm
        if umbrella:
            for da in ActiveAlarm.objects.filter(root=umbrella.id):
                d_path = da.vars.get("path", "")
                active_details[da.alarm_class, d_path] = da
        # Synchronize details
        self.logger.info("Active details: %s" % active_details)
        seen = set()
        for d in details:
            d_path = d.get("path", "")
            d_key = (d["alarm_class"], d_path)
            d_sev = d.get("severity", 0)
            # Key for seen details
            seen.add(d_key)
            if d_key in active_details and active_details[
                    d_key].severity != d_sev:
                # Change severity
                self.logger.info("Change detail alarm %s severity %s -> %s",
                                 active_details[d_key].id,
                                 active_details[d_key].severity, d_sev)
                active_details[d_key].change_severity(severity=d_sev)
            elif d_key not in active_details:
                # Create alarm
                self.logger.info("Create detail alarm to path %s", d_key)
                v = d.get("vars", {})
                v["path"] = d_path
                da = ActiveAlarm(timestamp=now,
                                 managed_object=self.object.id,
                                 alarm_class=d["alarm_class"],
                                 severity=d_sev,
                                 vars=v,
                                 root=umbrella.id)
                da.save()
                self.logger.info("Opening detail alarm %s %s (%s)", da.id,
                                 d_path, da.alarm_class.name)
        # Close details when necessary
        for d in set(active_details) - seen:
            self.logger.info("Clearing detail alarm %s", active_details[d].id)
            active_details[d].clear_alarm("Closing")