Example #1
0
def get_alert(alertid):

    alert = db.get_alert(alertid=alertid)

    if alert:
        return jsonify(response={"alert": alert.get_body(), "status": "ok", "total": 1})
    else:
        return jsonify(response={"alert": None, "status": "ok", "message": "not found", "total": 0})
Example #2
0
def get_alert(alertid):

    alert = db.get_alert(alertid=alertid)

    if alert:
        return jsonify(response={
            "alert": alert.get_body(),
            "status": "ok",
            "total": 1
        })
    else:
        return jsonify(response={
            "alert": None,
            "status": "ok",
            "message": "not found",
            "total": 0
        })
Example #3
0
def modify_alert(alertid):

    error = None

    # Return a single alert
    if request.method == 'GET':
        alert = db.get_alert(alertid=alertid)
        if alert:
            return jsonify(response={"alert": alert.get_body(), "status": "ok", "total": 1})
        else:
            return jsonify(response={"alert": None, "status": "ok", "message": "not found", "total": 0})

    # Update a single alert
    elif request.method == 'PUT':
        if request.json:
            modifiedAlert = db.modify_alert(alertid=alertid, update=request.json)
            if 'status' in request.json:
                modifiedAlert = db.update_status(alertid=alertid, status=request.json['status'])

                # Forward alert to notify topic and logger queue
                mq.send(modifiedAlert, CONF.outbound_queue)
                mq.send(modifiedAlert, CONF.outbound_topic)
                LOG.info('%s : Alert forwarded to %s and %s', modifiedAlert.get_id(), CONF.outbound_queue, CONF.outbound_topic)

        else:
            modifiedAlert = None
            error = "no post data"

        if modifiedAlert:
            return jsonify(response={"status": "ok"})
        else:
            return jsonify(response={"status": "error", "message": error})

    # Delete a single alert
    elif request.method == 'DELETE' or (request.method == 'POST' and request.json['_method'] == 'delete'):
        response = db.delete_alert(alertid)

        if response:
            return jsonify(response={"status": "ok"})
        else:
            return jsonify(response={"status": "error", "message": error})

    else:
        return jsonify(response={"status": "error", "message": "POST request without '_method' override?"})
Example #4
0
def pagerduty():

    if not request.json or not 'messages' in request.json:
        abort(400)

    for message in request.json['messages']:

        LOG.debug('%s', json.dumps(message))

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

        LOG.info('PagerDuty incident #%s webhook for alert %s', incident_number, alertid)

        LOG.error('previous status %s', db.get_alert(alertid=alertid).status)

        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']
            LOG.warn('Unknown PagerDuty message type: %s', message)

        LOG.info('PagerDuty webhook %s change status to %s', message['type'], status)

        pdAlert = db.update_status(alertid=alertid, status=status, text=text)
        db.tag_alert(alertid=alertid, tag='incident=#%s' % incident_number)

        LOG.error('returned status %s', pdAlert.status)
        LOG.error('current status %s', db.get_alert(alertid=alertid).status)

        # Forward alert to notify topic and logger queue
        if pdAlert:
            pdAlert.origin = 'pagerduty/webhook'
            mq.send(pdAlert, CONF.outbound_queue)
            mq.send(pdAlert, CONF.outbound_topic)
            LOG.info('%s : Alert forwarded to %s and %s', pdAlert.get_id(), CONF.outbound_queue, CONF.outbound_topic)

    return jsonify(response={"status": "ok"})
Example #5
0
def pagerduty():

    if not request.json or not 'messages' in request.json:
        abort(400)

    for message in request.json['messages']:

        LOG.debug('%s', json.dumps(message))

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

        LOG.info('PagerDuty incident #%s webhook for alert %s',
                 incident_number, alertid)

        LOG.error('previous status %s', db.get_alert(alertid=alertid).status)

        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']
            LOG.warn('Unknown PagerDuty message type: %s', message)

        LOG.info('PagerDuty webhook %s change status to %s', message['type'],
                 status)

        pdAlert = db.update_status(alertid=alertid, status=status, text=text)
        db.tag_alert(alertid=alertid, tag='incident=#%s' % incident_number)

        LOG.error('returned status %s', pdAlert.status)
        LOG.error('current status %s', db.get_alert(alertid=alertid).status)

        # Forward alert to notify topic and logger queue
        if pdAlert:
            pdAlert.origin = 'pagerduty/webhook'
            mq.send(pdAlert, CONF.outbound_queue)
            mq.send(pdAlert, CONF.outbound_topic)
            LOG.info('%s : Alert forwarded to %s and %s', pdAlert.get_id(),
                     CONF.outbound_queue, CONF.outbound_topic)

    return jsonify(response={"status": "ok"})