Exemple #1
0
    def from_action(self, action: str, text: str='', timeout: int=None) -> 'Alert':
        self.timeout = timeout or current_app.config['ALERT_TIMEOUT']

        status, _, previous_status = self._get_hist_info(action)

        new_severity, new_status = alarm_model.transition(
            alert=self,
            current_status=status,
            previous_status=previous_status,
            action=action
        )

        history = [History(
            id=self.id,
            event=self.event,
            severity=new_severity,
            status=new_status,
            value=self.value,
            text=text,
            change_type=action,
            update_time=datetime.utcnow()
        )]
        status_change_hook.send(self, status=new_status, text=text)

        return Alert.from_db(db.set_alert(
            id=self.id,
            severity=new_severity,
            status=new_status,
            tags=self.tags,
            attributes=self.attributes,
            timeout=timeout,
            previous_severity=self.severity if new_severity != self.severity else self.previous_severity,
            history=history)
        )
Exemple #2
0
    def update(self) -> 'Alert':
        now = datetime.utcnow()

        self.previous_severity = db.get_severity(self)
        self.trend_indication = alarm_model.trend(self.previous_severity,
                                                  self.severity)

        status, _, previous_status = self._get_hist_info()

        _, new_status = alarm_model.transition(alert=self,
                                               current_status=status,
                                               previous_status=previous_status)

        self.duplicate_count = 0
        self.repeat = False
        self.receive_time = now
        self.last_receive_id = self.id
        self.last_receive_time = now

        history = [
            History(id=self.id,
                    event=self.event,
                    severity=self.severity,
                    status=new_status,
                    value=self.value,
                    text='correlated alert',
                    change_type='severity',
                    update_time=self.create_time)
        ]

        if new_status != status:
            status_change_hook.send(self, status=new_status, text=self.text)

        self.status = new_status
        return Alert.from_db(db.correlate_alert(self, history))
Exemple #3
0
    def deduplicate(self) -> 'Alert':
        now = datetime.utcnow()

        status, previous_value, previous_status = self._get_hist_info()

        _, new_status = alarm_model.transition(
            alert=self,
            current_status=status,
            previous_status=previous_status
        )

        self.repeat = True
        self.last_receive_id = self.id
        self.last_receive_time = now

        if new_status != status:
            history = History(
                id=self.id,
                event=self.event,
                severity=self.severity,
                status=new_status,
                value=self.value,
                text='duplicate alert (with status change)',
                change_type='status',
                update_time=self.create_time,
                user=g.login,
            )  # type: Optional[History]
            self.update_time = now

            status_change_hook.send(self, status=new_status, text=self.text)

        elif current_app.config['HISTORY_ON_VALUE_CHANGE'] and self.value != previous_value:
            history = History(
                id=self.id,
                event=self.event,
                severity=self.severity,
                status=status,
                value=self.value,
                text='duplicate alert (with value change)',
                change_type='value',
                update_time=self.create_time,
                user=g.login
            )
        else:
            history = None

        self.status = new_status
        return Alert.from_db(db.dedup_alert(self, history))
Exemple #4
0
    def from_action(self,
                    action: str,
                    text: str = '',
                    timeout: int = None) -> 'Alert':
        now = datetime.utcnow()

        status, _, previous_status, previous_timeout = self._get_hist_info(
            action)

        if action in ['unack', 'unshelve', 'timeout']:
            timeout = timeout or previous_timeout

        if action in ['ack', 'unack']:
            timeout = timeout or current_app.config['ACK_TIMEOUT']
        elif action in ['shelve', 'unshelve']:
            timeout = timeout or current_app.config['SHELVE_TIMEOUT']
        else:
            timeout = timeout or self.timeout or current_app.config[
                'ALERT_TIMEOUT']

        new_severity, new_status = alarm_model.transition(
            alert=self,
            current_status=status,
            previous_status=previous_status,
            action=action)

        r = status_change_hook.send(self, status=new_status, text=text)
        _, (_, new_status, text) = r[0]

        try:
            change_type = ChangeType(action)
        except ValueError:
            change_type = ChangeType.action

        history = [
            History(id=self.id,
                    event=self.event,
                    severity=new_severity,
                    status=new_status,
                    value=self.value,
                    text=text,
                    change_type=change_type,
                    update_time=now,
                    user=g.login,
                    timeout=timeout)
        ]

        return Alert.from_db(
            db.set_alert(
                id=self.id,
                severity=new_severity,
                status=new_status,
                tags=self.tags,
                attributes=self.attributes,
                timeout=self.timeout,
                previous_severity=self.severity
                if new_severity != self.severity else self.previous_severity,
                update_time=now,
                history=history))
Exemple #5
0
    def from_action(self,
                    action: str,
                    text: str = '',
                    timeout: int = None) -> 'Alert':
        self.timeout = timeout or current_app.config['ALERT_TIMEOUT']
        previous_status = db.get_status(self)

        new_severity, new_status = alarm_model.transition(
            previous_severity=self.previous_severity,
            current_severity=self.severity,
            previous_status=previous_status,
            current_status=self.status,
            action=action)

        history = []
        if new_severity != self.previous_severity:
            history.append(
                History(id=self.id,
                        event=self.event,
                        severity=new_severity,
                        text=text,
                        change_type='action',
                        update_time=datetime.utcnow()))

        if new_status != previous_status:
            history_text = 'alert status change from action'
            history.append(
                History(id=self.id,
                        event=self.event,
                        status=new_status,
                        text=history_text,
                        change_type='action',
                        update_time=datetime.utcnow()))
            status_change_hook.send(self, status=new_status, text=history_text)

        return Alert.from_db(
            db.set_alert(self.id, new_severity, new_status, self.tags,
                         self.attributes, timeout, history))
Exemple #6
0
    def deduplicate(self, duplicate_of) -> 'Alert':
        now = datetime.utcnow()

        status, previous_value, previous_status = self._get_hist_info()

        _, new_status = alarm_model.transition(
            alert=self,
            current_status=status,
            previous_status=previous_status
        )

        self.repeat = True
        self.last_receive_id = self.id
        self.last_receive_time = now

        if new_status != status:
            text = 'duplicate alert (with status change)'
            r = status_change_hook.send(duplicate_of, status=new_status, text=text)
            _, (_, new_status, text) = r[0]
            self.update_time = now

            history = History(
                id=self.id,
                event=self.event,
                severity=self.severity,
                status=new_status,
                value=self.value,
                text=text,
                change_type='status',
                update_time=self.create_time,
                user=g.login,
            )  # type: Optional[History]

        elif current_app.config['HISTORY_ON_VALUE_CHANGE'] and self.value != previous_value:
            history = History(
                id=self.id,
                event=self.event,
                severity=self.severity,
                status=status,
                value=self.value,
                text='duplicate alert (with value change)',
                change_type='value',
                update_time=self.create_time,
                user=g.login
            )
        else:
            history = None

        self.status = new_status
        return Alert.from_db(db.dedup_alert(self, history))
Exemple #7
0
    def update(self, correlate_with) -> 'Alert':
        now = datetime.utcnow()

        self.previous_severity = db.get_severity(self)
        self.trend_indication = alarm_model.trend(self.previous_severity,
                                                  self.severity)

        status, _, previous_status, _ = self._get_hist_info()

        _, new_status = alarm_model.transition(alert=self,
                                               current_status=status,
                                               previous_status=previous_status)

        self.duplicate_count = 0
        self.repeat = False
        self.receive_time = now
        self.last_receive_id = self.id
        self.last_receive_time = now

        if new_status != status:
            r = status_change_hook.send(correlate_with,
                                        status=new_status,
                                        text=self.text)
            _, (_, new_status, text) = r[0]
            self.update_time = now
        else:
            text = self.text

        history = [
            History(id=self.id,
                    event=self.event,
                    severity=self.severity,
                    status=new_status,
                    value=self.value,
                    text=text,
                    change_type=ChangeType.severity,
                    update_time=self.create_time,
                    user=g.login,
                    timeout=self.timeout)
        ]

        self.status = new_status
        return Alert.from_db(db.correlate_alert(self, history))
Exemple #8
0
    def from_action(self, action: str, text: str='', timeout: int=None) -> 'Alert':
        now = datetime.utcnow()

        self.timeout = timeout or current_app.config['ALERT_TIMEOUT']

        status, _, previous_status = self._get_hist_info(action)

        new_severity, new_status = alarm_model.transition(
            alert=self,
            current_status=status,
            previous_status=previous_status,
            action=action
        )

        r = status_change_hook.send(self, status=new_status, text=text)
        _, (_, new_status, text) = r[0]

        history = [History(
            id=self.id,
            event=self.event,
            severity=new_severity,
            status=new_status,
            value=self.value,
            text=text,
            change_type=action,
            update_time=now,
            user=g.login
        )]

        return Alert.from_db(db.set_alert(
            id=self.id,
            severity=new_severity,
            status=new_status,
            tags=self.tags,
            attributes=self.attributes,
            timeout=timeout,
            previous_severity=self.severity if new_severity != self.severity else self.previous_severity,
            update_time=now,
            history=history)
        )
Exemple #9
0
    def update(self, correlate_with) -> 'Alert':
        now = datetime.utcnow()

        self.previous_severity = db.get_severity(self)
        self.trend_indication = alarm_model.trend(self.previous_severity, self.severity)

        status, _, previous_status = self._get_hist_info()

        _, new_status = alarm_model.transition(
            alert=self,
            current_status=status,
            previous_status=previous_status
        )

        self.duplicate_count = 0
        self.repeat = False
        self.receive_time = now
        self.last_receive_id = self.id
        self.last_receive_time = now
        text = 'correlated alert'

        if new_status != status:
            r = status_change_hook.send(correlate_with, status=new_status, text=text)
            _, (_, new_status, text) = r[0]
            self.update_time = now

        history = [History(
            id=self.id,
            event=self.event,
            severity=self.severity,
            status=new_status,
            value=self.value,
            text=text,
            change_type='severity',
            update_time=self.create_time,
            user=g.login
        )]

        self.status = new_status
        return Alert.from_db(db.correlate_alert(self, history))