Beispiel #1
0
def action_alert(alert_id):
    action = request.json.get('action', None)
    text = request.json.get('text', '%s operator action' % action)
    timeout = request.json.get('timeout', None)

    if not action:
        raise ApiError("must supply 'action' as json data", 400)

    customers = g.get('customers', None)
    alert = Alert.find_by_id(alert_id, customers)

    if not alert:
        raise ApiError('not found', 404)

    try:
        alert, action, text, timeout = process_action(alert, action, text, timeout)
        alert = alert.from_action(action, text, timeout)
    except RejectException as e:
        write_audit_trail.send(current_app._get_current_object(), event='alert-action-rejected', message=alert.text,
                               user=g.login, customers=g.customers, scopes=g.scopes, resource_id=alert.id, type='alert',
                               request=request)
        raise ApiError(str(e), 400)
    except InvalidAction as e:
        raise ApiError(str(e), 409)
    except Exception as e:
        raise ApiError(str(e), 500)

    write_audit_trail.send(current_app._get_current_object(), event='alert-actioned', message=text, user=g.login,
                           customers=g.customers, scopes=g.scopes, resource_id=alert.id, type='alert', request=request)

    if alert:
        return jsonify(status='ok')
    else:
        raise ApiError('failed to action alert', 500)
Beispiel #2
0
def action_alerts(alerts: List[str], action: str, text: str,
                  timeout: Optional[int]) -> None:
    updated = []
    errors = []
    for alert_id in alerts:
        alert = Alert.find_by_id(alert_id)

        try:
            previous_status = alert.status
            alert, action, text, timeout = process_action(
                alert, action, text, timeout)
            alert = alert.from_action(action, text, timeout)
        except RejectException as e:
            errors.append(str(e))
            continue
        except InvalidAction as e:
            errors.append(str(e))
            continue
        except Exception as e:
            errors.append(str(e))
            continue

        if previous_status != alert.status:
            try:
                alert, status, text = process_status(alert, alert.status, text)
                alert = alert.from_status(status, text, timeout)
            except RejectException as e:
                errors.append(str(e))
                continue
            except Exception as e:
                errors.append(str(e))
                continue

        updated.append(alert.id)
Beispiel #3
0
def action_alert(alert_id):
    action = request.json.get('action', None)
    text = request.json.get('text', '%s operator action' % action)
    timeout = request.json.get('timeout', None)

    if not action:
        raise ApiError("must supply 'action' as json data")

    customers = g.get('customers', None)
    alert = Alert.find_by_id(alert_id, customers)

    if not alert:
        raise ApiError("not found", 404)

    try:
        alert = process_action(alert, action, text, timeout)
    except RejectException as e:
        raise ApiError(str(e), 400)
    except Exception as e:
        raise ApiError(str(e), 500)

    if alert:
        return jsonify(status="ok")
    else:
        raise ApiError("failed to perform action", 500)
Beispiel #4
0
def action_alert(alert_id):
    action = request.json.get('action', None)
    text = request.json.get('text', '%s operator action' % action)
    timeout = request.json.get('timeout', None)

    if not action:
        raise ApiError("must supply 'action' as json data", 400)

    customers = g.get('customers', None)
    alert = Alert.find_by_id(alert_id, customers)

    if not alert:
        raise ApiError('not found', 404)

    try:
        previous_status = alert.status
        alert, action, text = process_action(alert, action, text)
        alert = alert.from_action(action, text, timeout)
    except RejectException as e:
        raise ApiError(str(e), 400)
    except Exception as e:
        raise ApiError(str(e), 500)

    if previous_status != alert.status:
        try:
            alert, status, text = process_status(alert, alert.status, text)
        except RejectException as e:
            raise ApiError(str(e), 400)
        except Exception as e:
            raise ApiError(str(e), 500)

    if alert:
        return jsonify(status='ok')
    else:
        raise ApiError('failed to action alert', 500)
Beispiel #5
0
def action_alerts(alerts: List[str], action: str, text: str, timeout: int) -> None:
    updated = []
    errors = []
    for alert_id in alerts:
        alert = Alert.find_by_id(alert_id)

        try:
            previous_status = alert.status
            alert, action, text = process_action(alert, action, text)
            alert = alert.from_action(action, text, timeout)
        except RejectException as e:
            errors.append(str(e))
            continue
        except InvalidAction as e:
            errors.append(str(e))
            continue
        except Exception as e:
            errors.append(str(e))
            continue

        if previous_status != alert.status:
            try:
                alert, status, text = process_status(alert, alert.status, text)
                alert = alert.from_status(status, text, timeout)
            except RejectException as e:
                errors.append(str(e))
                continue
            except Exception as e:
                errors.append(str(e))
                continue

        updated.append(alert.id)
Beispiel #6
0
def action_alert(alert_id):
    action = request.json.get('action', None)
    text = request.json.get('text', '%s operator action' % action)
    timeout = request.json.get('timeout', None)

    if not action:
        raise ApiError("must supply 'action' as json data", 400)

    customers = g.get('customers', None)
    alert = Alert.find_by_id(alert_id, customers)

    if not alert:
        raise ApiError('not found', 404)

    previous_status = alert.status
    try:
        alert, action, text = process_action(alert, action, text)
        alert = alert.from_action(action, text, timeout)
    except RejectException as e:
        raise ApiError(str(e), 400)
    except Exception as e:
        raise ApiError(str(e), 500)

    # trigger status_change() hook only if status has changed
    new_status = alert.status
    if previous_status != new_status:
        alert.status = previous_status
        try:
            alert, status, text = process_status(alert, new_status, text)
        except RejectException as e:
            raise ApiError(str(e), 400)
        except Exception as e:
            raise ApiError(str(e), 500)

    write_audit_trail.send(current_app._get_current_object(), event='alert-actioned', message=text, user=g.user,
                           customers=g.customers, scopes=g.scopes, resource_id=alert.id, type='alert', request=request)

    if alert:
        return jsonify(status='ok')
    else:
        raise ApiError('failed to action alert', 500)
Beispiel #7
0
def housekeeping():
    expired_threshold = request.args.get(
        'expired',
        default=current_app.config['DEFAULT_EXPIRED_DELETE_HRS'],
        type=int)
    info_threshold = request.args.get(
        'info',
        default=current_app.config['DEFAULT_INFO_DELETE_HRS'],
        type=int)

    has_expired, shelve_timeout, ack_timeout = Alert.housekeeping(
        expired_threshold, info_threshold)

    errors = []
    for alert in has_expired:
        try:
            alert, _, text, timeout = process_action(alert,
                                                     action='expired',
                                                     text='',
                                                     timeout=None)
            alert = alert.from_expired(text, timeout)
        except RejectException as e:
            write_audit_trail.send(current_app._get_current_object(),
                                   event='alert-expire-rejected',
                                   message=alert.text,
                                   user=g.login,
                                   customers=g.customers,
                                   scopes=g.scopes,
                                   resource_id=alert.id,
                                   type='alert',
                                   request=request)
            errors.append(str(e))
            continue
        except Exception as e:
            raise ApiError(str(e), 500)

        write_audit_trail.send(current_app._get_current_object(),
                               event='alert-expired',
                               message=text,
                               user=g.login,
                               customers=g.customers,
                               scopes=g.scopes,
                               resource_id=alert.id,
                               type='alert',
                               request=request)

    for alert in shelve_timeout + ack_timeout:
        try:
            alert, _, text, timeout = process_action(alert,
                                                     action='timeout',
                                                     text='',
                                                     timeout=None)
            alert = alert.from_timeout(text, timeout)
        except RejectException as e:
            write_audit_trail.send(current_app._get_current_object(),
                                   event='alert-timeout-rejected',
                                   message=alert.text,
                                   user=g.login,
                                   customers=g.customers,
                                   scopes=g.scopes,
                                   resource_id=alert.id,
                                   type='alert',
                                   request=request)
            errors.append(str(e))
            continue
        except Exception as e:
            raise ApiError(str(e), 500)

        write_audit_trail.send(current_app._get_current_object(),
                               event='alert-timeout',
                               message=text,
                               user=g.login,
                               customers=g.customers,
                               scopes=g.scopes,
                               resource_id=alert.id,
                               type='alert',
                               request=request)

    if errors:
        raise ApiError('housekeeping failed', 500, errors=errors)
    else:
        return jsonify(status='ok',
                       expired=[a.id for a in has_expired],
                       unshelve=[a.id for a in shelve_timeout],
                       unack=[a.id for a in ack_timeout],
                       count=len(has_expired) + len(shelve_timeout) +
                       len(ack_timeout))