예제 #1
0
def process_alert(alert: Alert) -> Alert:

    skip_plugins = False
    for plugin in plugins.routing(alert):
        if alert.is_suppressed:
            skip_plugins = True
            break
        try:
            alert = plugin.pre_receive(alert)
        except (RejectException, BlackoutPeriod, RateLimit):
            raise
        except Exception as e:
            if current_app.config['PLUGINS_RAISE_ON_ERROR']:
                raise RuntimeError(
                    "Error while running pre-receive plug-in '{}': {}".format(
                        plugin.name, str(e)))
            else:
                logging.error(
                    "Error while running pre-receive plug-in '{}': {}".format(
                        plugin.name, str(e)))
        if not alert:
            raise SyntaxError(
                "Plug-in '%s' pre-receive hook did not return modified alert" %
                plugin.name)

    try:
        if alert.is_duplicate():
            alert = alert.deduplicate()
        elif alert.is_correlated():
            alert = alert.update()
        else:
            alert = alert.create()
    except Exception as e:
        raise ApiError(str(e))

    updated = None
    for plugin in plugins.routing(alert):
        if skip_plugins:
            break
        try:
            updated = plugin.post_receive(alert)
        except Exception as e:
            if current_app.config['PLUGINS_RAISE_ON_ERROR']:
                raise ApiError(
                    "Error while running post-receive plug-in '{}': {}".format(
                        plugin.name, str(e)))
            else:
                logging.error(
                    "Error while running post-receive plug-in '{}': {}".format(
                        plugin.name, str(e)))
        if updated:
            alert = updated

    if updated:
        alert.tag(alert.tags)
        alert.update_attributes(alert.attributes)

    return alert
예제 #2
0
def process_alert(alert: Alert) -> Alert:

    wanted_plugins, wanted_config = plugins.routing(alert)

    skip_plugins = False
    for plugin in wanted_plugins:
        if alert.is_suppressed:
            skip_plugins = True
            break
        try:
            alert = plugin.pre_receive(alert, config=wanted_config)
        except TypeError:
            alert = plugin.pre_receive(alert)  # for backward compatibility
        except (RejectException, HeartbeatReceived, BlackoutPeriod, RateLimit, ForwardingLoop):
            raise
        except Exception as e:
            if current_app.config['PLUGINS_RAISE_ON_ERROR']:
                raise RuntimeError("Error while running pre-receive plugin '{}': {}".format(plugin.name, str(e)))
            else:
                logging.error("Error while running pre-receive plugin '{}': {}".format(plugin.name, str(e)))
        if not alert:
            raise SyntaxError("Plugin '%s' pre-receive hook did not return modified alert" % plugin.name)

    try:
        is_duplicate = alert.is_duplicate()
        if is_duplicate:
            alert = alert.deduplicate(is_duplicate)
        else:
            is_correlated = alert.is_correlated()
            if is_correlated:
                alert = alert.update(is_correlated)
            else:
                alert = alert.create()
    except Exception as e:
        raise ApiError(str(e))

    updated = None
    for plugin in wanted_plugins:
        if skip_plugins:
            break
        try:
            updated = plugin.post_receive(alert, config=wanted_config)
        except TypeError:
            updated = plugin.post_receive(alert)  # for backward compatibility
        except Exception as e:
            if current_app.config['PLUGINS_RAISE_ON_ERROR']:
                raise ApiError("Error while running post-receive plugin '{}': {}".format(plugin.name, str(e)))
            else:
                logging.error("Error while running post-receive plugin '{}': {}".format(plugin.name, str(e)))
        if updated:
            alert = updated

    if updated:
        alert.tag(alert.tags)
        alert.update_attributes(alert.attributes)

    return alert
예제 #3
0
파일: api.py 프로젝트: guardian/alerta
def process_alert(alert: Alert) -> Alert:

    wanted_plugins, wanted_config = plugins.routing(alert)

    skip_plugins = False
    for plugin in wanted_plugins:
        if alert.is_suppressed:
            skip_plugins = True
            break
        try:
            alert = plugin.pre_receive(alert, config=wanted_config)
        except TypeError:
            alert = plugin.pre_receive(alert)  # for backward compatibility
        except (RejectException, HeartbeatReceived, BlackoutPeriod, RateLimit):
            raise
        except Exception as e:
            if current_app.config['PLUGINS_RAISE_ON_ERROR']:
                raise RuntimeError("Error while running pre-receive plugin '{}': {}".format(plugin.name, str(e)))
            else:
                logging.error("Error while running pre-receive plugin '{}': {}".format(plugin.name, str(e)))
        if not alert:
            raise SyntaxError("Plugin '%s' pre-receive hook did not return modified alert" % plugin.name)

    try:
        is_duplicate = alert.is_duplicate()
        if is_duplicate:
            alert = alert.deduplicate(is_duplicate)
        else:
            is_correlated = alert.is_correlated()
            if is_correlated:
                alert = alert.update(is_correlated)
            else:
                alert = alert.create()
    except Exception as e:
        raise ApiError(str(e))

    updated = None
    for plugin in wanted_plugins:
        if skip_plugins:
            break
        try:
            updated = plugin.post_receive(alert, config=wanted_config)
        except TypeError:
            updated = plugin.post_receive(alert)  # for backward compatibility
        except Exception as e:
            if current_app.config['PLUGINS_RAISE_ON_ERROR']:
                raise ApiError("Error while running post-receive plugin '{}': {}".format(plugin.name, str(e)))
            else:
                logging.error("Error while running post-receive plugin '{}': {}".format(plugin.name, str(e)))
        if updated:
            alert = updated

    if updated:
        alert.tag(alert.tags)
        alert.update_attributes(alert.attributes)

    return alert