Esempio n. 1
0
 def find_all(query: Query = None,
              page: int = 1,
              page_size: int = 1000) -> List['Alert']:
     return [
         Alert.from_db(alert)
         for alert in db.get_alerts(query, page, page_size)
     ]
Esempio n. 2
0
 def find_all(query: Query = None,
              raw_data: bool = False,
              history: bool = False,
              page: int = 1,
              page_size: int = 1000) -> List['Alert']:
     return [
         Alert.from_db(alert) for alert in db.get_alerts(
             query, raw_data, history, page, page_size)
     ]
Esempio n. 3
0
def parse_pagerduty(message):

    incident_key = message['data']['incident']['incident_key']
    incident_number = message['data']['incident']['incident_number']
    html_url = message['data']['incident']['html_url']
    incident_url = '<a href="%s">#%s</a>' % (html_url, incident_number)

    try:
        alert = db.get_alerts(query={'attributes.incidentKey': incident_key},
                              limit=1)[0]
    except IndexError:
        raise

    from alerta.app import status_code

    if message['type'] == 'incident.trigger':
        status = status_code.OPEN
        user = message['data']['incident']['assigned_to_user']['name']
        text = 'Incident %s assigned to %s' % (incident_url, user)
    elif message['type'] == 'incident.acknowledge':
        status = status_code.ACK
        user = message['data']['incident']['assigned_to_user']['name']
        text = 'Incident %s acknowledged by %s' % (incident_url, user)
    elif message['type'] == 'incident.unacknowledge':
        status = status_code.OPEN
        text = 'Incident %s unacknowledged due to timeout' % incident_url
    elif message['type'] == 'incident.resolve':
        status = status_code.CLOSED
        if message['data']['incident']['resolved_by_user']:
            user = message['data']['incident']['resolved_by_user']['name']
        else:
            user = '******'
        text = 'Incident %s resolved by %s' % (incident_url, user)
    elif message['type'] == 'incident.assign':
        status = status_code.ASSIGN
        user = message['data']['incident']['assigned_to_user']['name']
        text = 'Incident %s manually assigned to %s' % (incident_url, user)
    elif message['type'] == 'incident.escalate':
        status = status_code.OPEN
        user = message['data']['incident']['assigned_to_user']['name']
        text = 'Incident %s escalated to %s' % (incident_url, user)
    elif message['type'] == 'incident.delegate':
        status = status_code.OPEN
        user = message['data']['incident']['assigned_to_user']['name']
        text = 'Incident %s reassigned due to escalation to %s' % (
            incident_url, user)
    else:
        status = status_code.UNKNOWN
        text = message['type']

    return alert.id, status, text
Esempio n. 4
0
def parse_stackdriver(notification):

    notification = json.loads(notification)
    incident = notification['incident']
    state = incident['state']

    if state == 'acknowledged':
        try:
            alert = db.get_alerts(
                query={'attributes.incidentId': incident['incident_id']},
                limit=1)[0]
        except IndexError:
            raise ValueError('unknown Stackdriver Incident ID: %s' %
                             incident['incident_id'])
        return state, alert

    else:
        if state == 'open':
            severity = 'critical'
            create_time = datetime.datetime.fromtimestamp(
                incident['started_at'])
        elif state == 'closed':
            severity = 'ok'
            create_time = datetime.datetime.fromtimestamp(incident['ended_at'])
        else:
            severity = 'indeterminate'
            create_time = None

        return state, Alert(
            resource=incident['resource_name'],
            event=incident['condition_name'],
            environment='Production',
            severity=severity,
            service=[incident['policy_name']],
            group='Cloud',
            text=incident['summary'],
            attributes={
                'incidentId':
                incident['incident_id'],
                'resourceId':
                incident['resource_id'],
                'moreInfo':
                '<a href="%s" target="_blank">Stackdriver Console</a>' %
                incident['url']
            },
            origin='Stackdriver',
            event_type='stackdriverAlert',
            create_time=create_time,
            raw_data=notification)
Esempio n. 5
0
def parse_pagerduty(message):

    incident_key = message["data"]["incident"]["incident_key"]
    incident_number = message["data"]["incident"]["incident_number"]
    html_url = message["data"]["incident"]["html_url"]
    incident_url = '<a href="%s">#%s</a>' % (html_url, incident_number)

    try:
        alert = db.get_alerts(query={"attributes.incidentKey": incident_key}, limit=1)[0]
    except IndexError:
        raise

    from alerta.app import status_code

    if message["type"] == "incident.trigger":
        status = status_code.OPEN
        user = message["data"]["incident"]["assigned_to_user"]["name"]
        text = "Incident %s assigned to %s" % (incident_url, user)
    elif message["type"] == "incident.acknowledge":
        status = status_code.ACK
        user = message["data"]["incident"]["assigned_to_user"]["name"]
        text = "Incident %s acknowledged by %s" % (incident_url, user)
    elif message["type"] == "incident.unacknowledge":
        status = status_code.OPEN
        text = "Incident %s unacknowledged due to timeout" % incident_url
    elif message["type"] == "incident.resolve":
        status = status_code.CLOSED
        if message["data"]["incident"]["resolved_by_user"]:
            user = message["data"]["incident"]["resolved_by_user"]["name"]
        else:
            user = "******"
        text = "Incident %s resolved by %s" % (incident_url, user)
    elif message["type"] == "incident.assign":
        status = status_code.ASSIGN
        user = message["data"]["incident"]["assigned_to_user"]["name"]
        text = "Incident %s manually assigned to %s" % (incident_url, user)
    elif message["type"] == "incident.escalate":
        status = status_code.OPEN
        user = message["data"]["incident"]["assigned_to_user"]["name"]
        text = "Incident %s escalated to %s" % (incident_url, user)
    elif message["type"] == "incident.delegate":
        status = status_code.OPEN
        user = message["data"]["incident"]["assigned_to_user"]["name"]
        text = "Incident %s reassigned due to escalation to %s" % (incident_url, user)
    else:
        status = status_code.UNKNOWN
        text = message["type"]

    return alert.id, status, text
Esempio n. 6
0
def parse_pagerduty(message):

    incident_key = message['data']['incident']['incident_key']
    incident_number = message['data']['incident']['incident_number']
    html_url = message['data']['incident']['html_url']
    incident_url = '<a href="%s">#%s</a>' % (html_url, incident_number)

    try:
        alert = db.get_alerts(query={'attributes.incidentKey': incident_key}, limit=1)[0]
    except IndexError:
        raise

    from alerta.app import status_code

    if message['type'] == 'incident.trigger':
        status = status_code.OPEN
        user = message['data']['incident']['assigned_to_user']['name']
        text = 'Incident %s assigned to %s' % (incident_url, user)
    elif message['type'] == 'incident.acknowledge':
        status = status_code.ACK
        user = message['data']['incident']['assigned_to_user']['name']
        text = 'Incident %s acknowledged by %s' % (incident_url, user)
    elif message['type'] == 'incident.unacknowledge':
        status = status_code.OPEN
        text = 'Incident %s unacknowledged due to timeout' % incident_url
    elif message['type'] == 'incident.resolve':
        status = status_code.CLOSED
        if message['data']['incident']['resolved_by_user']:
            user = message['data']['incident']['resolved_by_user']['name']
        else:
            user = '******'
        text = 'Incident %s resolved by %s' % (incident_url, user)
    elif message['type'] == 'incident.assign':
        status = status_code.ASSIGN
        user = message['data']['incident']['assigned_to_user']['name']
        text = 'Incident %s manually assigned to %s' % (incident_url, user)
    elif message['type'] == 'incident.escalate':
        status = status_code.OPEN
        user = message['data']['incident']['assigned_to_user']['name']
        text = 'Incident %s escalated to %s' % (incident_url, user)
    elif message['type'] == 'incident.delegate':
        status = status_code.OPEN
        user = message['data']['incident']['assigned_to_user']['name']
        text = 'Incident %s reassigned due to escalation to %s' % (incident_url, user)
    else:
        status = status_code.UNKNOWN
        text = message['type']

    return alert.id, status, text
Esempio n. 7
0
def parse_stackdriver(notification):

    notification = json.loads(notification)
    incident = notification["incident"]
    state = incident["state"]

    if state == "acknowledged":
        try:
            alert = db.get_alerts(query={"attributes.incidentId": incident["incident_id"]}, limit=1)[0]
        except IndexError:
            raise ValueError("unknown Stackdriver Incident ID: %s" % incident["incident_id"])
        return state, alert

    else:
        if state == "open":
            severity = "critical"
            create_time = datetime.datetime.fromtimestamp(incident["started_at"])
        elif state == "closed":
            severity = "ok"
            create_time = datetime.datetime.fromtimestamp(incident["ended_at"])
        else:
            severity = "indeterminate"
            create_time = None

        return (
            state,
            Alert(
                resource=incident["resource_name"],
                event=incident["condition_name"],
                environment="Production",
                severity=severity,
                service=[incident["policy_name"]],
                group="Cloud",
                text=incident["summary"],
                attributes={
                    "incidentId": incident["incident_id"],
                    "resourceId": incident["resource_id"],
                    "moreInfo": '<a href="%s" target="_blank">Stackdriver Console</a>' % incident["url"],
                },
                origin="Stackdriver",
                event_type="stackdriverAlert",
                create_time=create_time,
                raw_data=notification,
            ),
        )
Esempio n. 8
0
def parse_stackdriver(notification):

    notification = json.loads(notification)
    incident = notification['incident']
    state = incident['state']

    if state == 'acknowledged':
        try:
            alert = db.get_alerts(query={'attributes.incidentId': incident['incident_id']}, limit=1)[0]
        except IndexError:
            raise ValueError('unknown Stackdriver Incident ID: %s' % incident['incident_id'])
        return state, alert

    else:
        if state == 'open':
            severity = 'critical'
            create_time = datetime.datetime.fromtimestamp(incident['started_at'])
        elif state == 'closed':
            severity = 'ok'
            create_time = datetime.datetime.fromtimestamp(incident['ended_at'])
        else:
            severity = 'indeterminate'
            create_time = None

        return state, Alert(
            resource=incident['resource_name'],
            event=incident['condition_name'],
            environment='Production',
            severity=severity,
            service=[incident['policy_name']],
            group='Cloud',
            text=incident['summary'],
            attributes={
                'incidentId': incident['incident_id'],
                'resourceId': incident['resource_id'],
                'moreInfo': '<a href="%s" target="_blank">Stackdriver Console</a>' % incident['url']
            },
            origin='Stackdriver',
            event_type='stackdriverAlert',
            create_time=create_time,
            raw_data=notification
        )
Esempio n. 9
0
def get_alerts():

    gets_started = gets_timer.start_timer()
    try:
        query, fields, sort, _, page, limit, query_time = parse_fields(
            request.args)
    except Exception as e:
        gets_timer.stop_timer(gets_started)
        return jsonify(status="error", message=str(e)), 400

    try:
        severity_count = db.get_counts(query=query,
                                       fields={"severity": 1},
                                       group="severity")
    except Exception as e:
        return jsonify(status="error", message=str(e)), 500

    try:
        status_count = db.get_counts(query=query,
                                     fields={"status": 1},
                                     group="status")
    except Exception as e:
        return jsonify(status="error", message=str(e)), 500

    if limit < 1:
        return jsonify(status="error",
                       message="page 'limit' of %s is not valid" % limit), 416

    total = sum(severity_count.values())
    pages = ((total - 1) // limit) + 1

    if total and page > pages or page < 0:
        return jsonify(status="error",
                       message="page out of range: 1-%s" % pages), 416

    if 'history' not in fields:
        fields['history'] = {'$slice': app.config['HISTORY_LIMIT']}

    try:
        alerts = db.get_alerts(query=query,
                               fields=fields,
                               sort=sort,
                               page=page,
                               limit=limit)
    except Exception as e:
        return jsonify(status="error", message=str(e)), 500

    alert_response = list()
    if len(alerts) > 0:

        last_time = None

        for alert in alerts:
            body = alert.get_body()
            body['href'] = absolute_url('/alert/' + alert.id)

            if not last_time:
                last_time = body['lastReceiveTime']
            elif body['lastReceiveTime'] > last_time:
                last_time = body['lastReceiveTime']

            alert_response.append(body)

        gets_timer.stop_timer(gets_started)
        return jsonify(
            status="ok",
            total=total,
            page=page,
            pageSize=limit,
            pages=pages,
            more=page < pages,
            alerts=alert_response,
            severityCounts=severity_count,
            statusCounts=status_count,
            lastTime=last_time,
            autoRefresh=Switch.get('auto-refresh-allow').is_on(),
        )
    else:
        gets_timer.stop_timer(gets_started)
        return jsonify(status="ok",
                       message="not found",
                       total=total,
                       page=page,
                       pageSize=limit,
                       pages=pages,
                       more=False,
                       alerts=[],
                       severityCounts=severity_count,
                       statusCounts=status_count,
                       lastTime=query_time,
                       autoRefresh=Switch.get('auto-refresh-allow').is_on())
Esempio n. 10
0
def grafana():

    hook_started = webhook_timer.start_timer()

    alerts = []
    data = request.json
    if data and data['state'] == 'alerting':
        for match in data.get('evalMatches', []):
            try:
                incomingAlert = parse_grafana(data, match)
            except ValueError as e:
                webhook_timer.stop_timer(hook_started)
                return jsonify(status="error", message=str(e)), 400

            if g.get('customer', None):
                incomingAlert.customer = g.get('customer')

            add_remote_ip(request, incomingAlert)

            try:
                alert = process_alert(incomingAlert)
            except RejectException as e:
                webhook_timer.stop_timer(hook_started)
                return jsonify(status="error", message=str(e)), 403
            except Exception as e:
                webhook_timer.stop_timer(hook_started)
                return jsonify(status="error", message=str(e)), 500
            alerts.append(alert)

        webhook_timer.stop_timer(hook_started)

    elif data and data['state'] == 'ok' and data.get('ruleId', None):
        try:
            existingAlerts = db.get_alerts({'attributes.ruleId': data['ruleId'], 'customer': g.get('customer', None)})
        except Exception as e:
            webhook_timer.stop_timer(hook_started)
            return jsonify(status="error", message=str(e)), 500

        for updateAlert in existingAlerts:
            updateAlert.severity = 'normal'
            updateAlert.status = 'closed'

            try:
                alert = process_alert(updateAlert)
            except RejectException as e:
                webhook_timer.stop_timer(hook_started)
                return jsonify(status="error", message=str(e)), 403
            except Exception as e:
                webhook_timer.stop_timer(hook_started)
                return jsonify(status="error", message=str(e)), 500
            alerts.append(alert)

        webhook_timer.stop_timer(hook_started)
    else:
        return jsonify(status="error", message="no alerts in Grafana notification payload"), 400

    if len(alerts) == 1:
        body = alerts[0].get_body()
        body['href'] = absolute_url('/alert/' + alerts[0].id)
        return jsonify(status="ok", id=alerts[0].id, alert=body), 201, {'Location': body['href']}
    else:
        return jsonify(status="ok", ids=[alert.id for alert in alerts]), 201
Esempio n. 11
0
 def find_all(query=None, page=1, page_size=1000):
     return [
         Alert.from_db(alert)
         for alert in db.get_alerts(query, page, page_size)
     ]
Esempio n. 12
0
@auth_required
@jsonp
def get_alerts():

    gets_started = gets_timer.start_timer()
    try:
        query, sort, _, limit, query_time = parse_fields(request)
    except Exception, e:
        gets_timer.stop_timer(gets_started)
        return jsonify(status="error", message=str(e)), 400

    fields = dict()
    fields['history'] = {'$slice': app.config['HISTORY_LIMIT']}

    try:
        alerts = db.get_alerts(query=query, fields=fields, sort=sort, limit=limit)
    except Exception as e:
        return jsonify(status="error", message=str(e)), 500

    total = db.get_count(query=query)  # because total may be greater than limit

    found = 0
    severity_count = defaultdict(int)
    status_count = defaultdict(int)

    alert_response = list()
    if len(alerts) > 0:

        last_time = None

        for alert in alerts:
Esempio n. 13
0
@jsonp
def get_alerts():

    gets_started = gets_timer.start_timer()
    try:
        query, sort, _, limit, query_time = parse_fields(request)
    except Exception, e:
        gets_timer.stop_timer(gets_started)
        return jsonify(status="error", message=str(e)), 400

    fields = dict()
    fields['history'] = {'$slice': app.config['HISTORY_LIMIT']}

    try:
        alerts = db.get_alerts(query=query,
                               fields=fields,
                               sort=sort,
                               limit=limit)
    except Exception as e:
        return jsonify(status="error", message=str(e)), 500

    total = db.get_count(
        query=query)  # because total may be greater than limit

    found = 0
    severity_count = defaultdict(int)
    status_count = defaultdict(int)

    alert_response = list()
    if len(alerts) > 0:

        last_time = None
Esempio n. 14
0
 def find_all(query=None, page=1, page_size=100):
     return [Alert.from_db(alert) for alert in db.get_alerts(query, page, page_size)]
Esempio n. 15
0
 def find_all(query: Query=None, page: int=1, page_size: int=1000) -> List['Alert']:
     return [Alert.from_db(alert) for alert in db.get_alerts(query, page, page_size)]
Esempio n. 16
0
def get_alerts():

    gets_started = gets_timer.start_timer()
    try:
        query, fields, sort, _, page, limit, query_time = parse_fields(request.args)
    except Exception as e:
        gets_timer.stop_timer(gets_started)
        return jsonify(status="error", message=str(e)), 400

    try:
        severity_count = db.get_counts(query=query, fields={"severity": 1}, group="severity")
    except Exception as e:
        return jsonify(status="error", message=str(e)), 500

    try:
        status_count = db.get_counts(query=query, fields={"status": 1}, group="status")
    except Exception as e:
        return jsonify(status="error", message=str(e)), 500

    if limit < 1:
        return jsonify(status="error", message="page 'limit' of %s is not valid" % limit), 416

    total = sum(severity_count.values())
    pages = ((total - 1) // limit) + 1

    if total and page > pages or page < 0:
        return jsonify(status="error", message="page out of range: 1-%s" % pages), 416

    if 'history' not in fields:
        fields['history'] = {'$slice': app.config['HISTORY_LIMIT']}

    try:
        alerts = db.get_alerts(query=query, fields=fields, sort=sort, page=page, limit=limit)
    except Exception as e:
        return jsonify(status="error", message=str(e)), 500

    alert_response = list()
    if len(alerts) > 0:

        last_time = None

        for alert in alerts:
            body = alert.get_body()
            body['href'] = absolute_url('/alert/' + alert.id)

            if not last_time:
                last_time = body['lastReceiveTime']
            elif body['lastReceiveTime'] > last_time:
                last_time = body['lastReceiveTime']

            alert_response.append(body)

        gets_timer.stop_timer(gets_started)
        return jsonify(
            status="ok",
            total=total,
            page=page,
            pageSize=limit,
            pages=pages,
            more=page < pages,
            alerts=alert_response,
            severityCounts=severity_count,
            statusCounts=status_count,
            lastTime=last_time,
            autoRefresh=Switch.get('auto-refresh-allow').is_on(),
        )
    else:
        gets_timer.stop_timer(gets_started)
        return jsonify(
            status="ok",
            message="not found",
            total=total,
            page=page,
            pageSize=limit,
            pages=pages,
            more=False,
            alerts=[],
            severityCounts=severity_count,
            statusCounts=status_count,
            lastTime=query_time,
            autoRefresh=Switch.get('auto-refresh-allow').is_on()
        )
Esempio n. 17
0
def grafana():

    hook_started = webhook_timer.start_timer()

    alerts = []
    data = request.json
    if data and data['state'] == 'alerting':
        for match in data.get('evalMatches', []):
            try:
                incomingAlert = parse_grafana(data, match)
            except ValueError as e:
                webhook_timer.stop_timer(hook_started)
                return jsonify(status="error", message=str(e)), 400

            if g.get('customer', None):
                incomingAlert.customer = g.get('customer')

            add_remote_ip(request, incomingAlert)

            try:
                alert = process_alert(incomingAlert)
            except RejectException as e:
                webhook_timer.stop_timer(hook_started)
                return jsonify(status="error", message=str(e)), 403
            except Exception as e:
                webhook_timer.stop_timer(hook_started)
                return jsonify(status="error", message=str(e)), 500
            alerts.append(alert)

        webhook_timer.stop_timer(hook_started)

    elif data and data['state'] == 'ok' and data.get('ruleId', None):
        try:
            existingAlerts = db.get_alerts({'attributes.ruleId': data['ruleId'], 'customer': g.get('customer', None)})
        except Exception as e:
            webhook_timer.stop_timer(hook_started)
            return jsonify(status="error", message=str(e)), 500

        for updateAlert in existingAlerts:
            updateAlert.severity = 'normal'
            updateAlert.status = 'closed'

            try:
                alert = process_alert(updateAlert)
            except RejectException as e:
                webhook_timer.stop_timer(hook_started)
                return jsonify(status="error", message=str(e)), 403
            except Exception as e:
                webhook_timer.stop_timer(hook_started)
                return jsonify(status="error", message=str(e)), 500
            alerts.append(alert)

        webhook_timer.stop_timer(hook_started)
    else:
        return jsonify(status="error", message="no alerts in Grafana notification payload"), 400

    if len(alerts) == 1:
        body = alerts[0].get_body()
        body['href'] = absolute_url('/alert/' + alerts[0].id)
        return jsonify(status="ok", id=alerts[0].id, alert=body), 201, {'Location': body['href']}
    else:
        return jsonify(status="ok", ids=[alert.id for alert in alerts]), 201