def tag_alert(alertid): tag = request.json if tag: response = db.tag_alert(alertid, tag['tag']) else: return jsonify(response={"status": "error", "message": "no data"}) if response: return jsonify(response={"status": "ok"}) else: return jsonify(response={"status": "error", "message": "error tagging alert"})
def tag_alert(alertid): tag = request.json if tag: response = db.tag_alert(alertid, tag['tag']) else: return jsonify(response={"status": "error", "message": "no data"}) if response: return jsonify(response={"status": "ok"}) else: return jsonify(response={ "status": "error", "message": "error tagging alert" })
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"})
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"})