def custom(webhook): try: incomingAlert = custom_webhooks.webhooks[webhook].incoming( query_string=request.args, payload=request.get_json() or request.get_data(as_text=True) ) except ValueError as e: raise ApiError(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: raise ApiError(str(e), 403) except Exception as e: raise ApiError(str(e), 500) if alert: return jsonify(status="ok", id=alert.id, alert=alert.serialize), 201 else: raise ApiError("insert or update via %s webhook failed" % webhook, 500)
def prometheus(): alerts = [] if request.json and 'alerts' in request.json: external_url = request.json.get('externalURL', None) for alert in request.json['alerts']: try: incomingAlert = parse_prometheus(alert, external_url) except ValueError as e: raise ApiError(str(e), 400) incomingAlert.customer = assign_customer( wanted=incomingAlert.customer) add_remote_ip(request, incomingAlert) try: alert = process_alert(incomingAlert) except RejectException as e: raise ApiError(str(e), 403) except Exception as e: raise ApiError(str(e), 500) alerts.append(alert) else: raise ApiError('no alerts in Prometheus notification payload', 400) if len(alerts) == 1: return jsonify(status='ok', id=alerts[0].id, alert=alerts[0].serialize), 201 else: return jsonify(status='ok', ids=[alert.id for alert in alerts]), 201
def receive(): try: incomingAlert = Alert.parse(request.json) except ValueError as e: raise ApiError(str(e), 400) incomingAlert.customer = assign_customer(wanted=incomingAlert.customer) add_remote_ip(request, incomingAlert) try: alert = process_alert(incomingAlert) except RejectException as e: raise ApiError(str(e), 403) except RateLimit as e: return jsonify(status='error', message=str(e), id=incomingAlert.id), 429 except BlackoutPeriod as e: return jsonify(status='ok', message=str(e), id=incomingAlert.id), 202 except Exception as e: raise ApiError(str(e), 500) if alert: return jsonify(status='ok', id=alert.id, alert=alert.serialize), 201 else: raise ApiError('insert or update of received alert failed', 500)
def custom(webhook): if webhook not in custom_webhooks.webhooks: raise ApiError("Custom webhook '%s' not found." % webhook, 404) try: rv = custom_webhooks.webhooks[webhook].incoming( query_string=request.args, payload=request.get_json() or request.get_data(as_text=True) or request.form) except Exception as e: raise ApiError(str(e), 400) if isinstance(rv, Alert): rv = [rv] if isinstance(rv, list): alerts = [] for alert in rv: alert.customer = assign_customer(wanted=alert.customer) add_remote_ip(request, alert) try: alert = process_alert(alert) except RejectException as e: raise ApiError(str(e), 403) except Exception as e: raise ApiError(str(e), 500) text = 'alert received via {} webhook'.format(webhook) write_audit_trail.send(current_app._get_current_object(), event='webhook-received', message=text, user=g.login, customers=g.customers, scopes=g.scopes, resource_id=alert.id, type='alert', request=request) alerts.append(alert) if len(alerts) == 1: return jsonify(status='ok', id=alerts[0].id, alert=alerts[0].serialize), 201 else: return jsonify(status='ok', ids=[alert.id for alert in alerts]), 201 else: text = 'request received via {} webhook'.format(webhook) write_audit_trail.send(current_app._get_current_object(), event='webhook-received', message=text, user=g.login, customers=g.customers, scopes=g.scopes, resource_id=None, type='user-defined', request=request) return rv
def graylog(): try: incomingAlert = parse_graylog(request.json) except ValueError as e: raise ApiError(str(e), 400) if request.args.get('event', None): incomingAlert.event = request.args.get('event') if request.args.get('event_type', None): incomingAlert.event_type = request.args.get('event_type') if request.args.get('environment', None): incomingAlert.environment = request.args.get('environment') if request.args.get('service', None): incomingAlert.service = request.args.get('service').split(",") if request.args.get('severity', None): incomingAlert.severity = request.args.get('severity') add_remote_ip(request, incomingAlert) try: alert = process_alert(incomingAlert) except RejectException as e: raise ApiError(str(e), 403) except Exception as e: raise ApiError(str(e), 500) if alert: return jsonify(status="ok", id=alert.id, alert=alert.serialize), 201 else: raise ApiError("insert or update of graylog check failed", 500)
def newrelic(): try: incomingAlert = parse_newrelic(request.json) except ValueError as e: raise ApiError(str(e), 400) incomingAlert.customer = assign_customer(wanted=incomingAlert.customer) add_remote_ip(request, incomingAlert) try: alert = process_alert(incomingAlert) except RejectException as e: raise ApiError(str(e), 403) except Exception as e: raise ApiError(str(e), 500) text = 'newrelic alert received via webhook' write_audit_trail.send(current_app._get_current_object(), event='webhook-received', 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', id=alert.id, alert=alert.serialize), 201 else: raise ApiError('insert or update of New Relic alert failed', 500)
def receive(): try: incomingAlert = Alert.parse(request.json) except ValueError as e: raise ApiError(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: raise ApiError(str(e), 403) except RateLimit as e: return jsonify(status="error", message=str(e), id=incomingAlert.id), 429 except BlackoutPeriod as e: return jsonify(status="ok", message=str(e), id=incomingAlert.id), 202 except Exception as e: raise ApiError(str(e), 500) if alert: return jsonify(status="ok", id=alert.id, alert=alert.serialize), 201 else: raise ApiError("insert or update of received alert failed", 500)
def receive(): try: incomingAlert = Alert.parse(request.json) except ValueError as e: raise ApiError(str(e), 400) incomingAlert.customer = assign_customer(wanted=incomingAlert.customer) add_remote_ip(request, incomingAlert) try: alert = process_alert(incomingAlert) except RejectException as e: raise ApiError(str(e), 403) except RateLimit as e: return jsonify(status='error', message=str(e), id=incomingAlert.id), 429 except BlackoutPeriod as e: return jsonify(status='ok', message=str(e), id=incomingAlert.id), 202 except Exception as e: raise ApiError(str(e), 500) write_audit_trail.send(current_app._get_current_object(), event='alert-received', message=alert.text, user=g.user, customers=g.customers, scopes=g.scopes, resource_id=alert.id, type='alert', request=request) if alert: return jsonify(status='ok', id=alert.id, alert=alert.serialize), 201 else: raise ApiError('insert or update of received alert failed', 500)
def custom(webhook): try: incomingAlert = custom_webhooks.webhooks[webhook].incoming( query_string=request.args, payload=request.get_json() or request.get_data(as_text=True) or request.form) except KeyError as e: raise ApiError( "Webhook '%s' not found. Did you mean to use POST instead of GET?" % webhook, 404) except ValueError as e: raise ApiError(str(e), 400) incomingAlert.customer = assign_customer(wanted=incomingAlert.customer) add_remote_ip(request, incomingAlert) try: alert = process_alert(incomingAlert) except RejectException as e: raise ApiError(str(e), 403) except Exception as e: raise ApiError(str(e), 500) if alert: return jsonify(status='ok', id=alert.id, alert=alert.serialize), 201 else: raise ApiError('insert or update via %s webhook failed' % webhook, 500)
def prometheus(): alerts = [] if request.json and 'alerts' in request.json: external_url = request.json.get('externalURL', None) for alert in request.json['alerts']: try: incomingAlert = parse_prometheus(alert, external_url) except ValueError as e: raise ApiError(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: raise ApiError(str(e), 403) except Exception as e: raise ApiError(str(e), 500) alerts.append(alert) else: raise ApiError("no alerts in Prometheus notification payload", 400) if len(alerts) == 1: return jsonify(status="ok", id=alerts[0].id, alert=alerts[0].serialize), 201 else: return jsonify(status="ok", ids=[alert.id for alert in alerts]), 201
def grafana(): alerts = [] data = request.json if data and data['state'] == 'alerting': for match in data.get('evalMatches', []): try: incomingAlert = parse_grafana(data, match, request.args) except ValueError as e: return jsonify(status='error', message=str(e)), 400 incomingAlert.customer = assign_customer(wanted=incomingAlert.customer) add_remote_ip(request, incomingAlert) try: alert = process_alert(incomingAlert) except RejectException as e: return jsonify(status='error', message=str(e)), 403 except Exception as e: return jsonify(status='error', message=str(e)), 500 alerts.append(alert) elif data and data['state'] == 'ok' and data.get('ruleId', None): try: query = qb.from_dict({'attributes.ruleId': str(data['ruleId'])}) existingAlerts = Alert.find_all(query) except Exception as e: raise ApiError(str(e), 500) for updateAlert in existingAlerts: updateAlert.severity = 'normal' updateAlert.status = 'closed' try: alert = process_alert(updateAlert) except RejectException as e: raise ApiError(str(e), 403) except Exception as e: raise ApiError(str(e), 500) alerts.append(alert) else: raise ApiError('no alerts in Grafana notification payload', 400) for alert in alerts: text = 'grafana alert received via webhook' write_audit_trail.send(current_app._get_current_object(), event='webhook-received', message=text, user=g.user, customers=g.customers, scopes=g.scopes, resource_id=alert.id, type='alert', request=request) if len(alerts) == 1: return jsonify(status='ok', id=alerts[0].id, alert=alerts[0].serialize), 201 else: return jsonify(status='ok', ids=[alert.id for alert in alerts]), 201
def custom(webhook): try: response = custom_webhooks.webhooks[webhook].incoming( query_string=request.args, payload=request.get_json() or request.get_data(as_text=True) or request.form) except KeyError as e: raise ApiError( "Webhook '%s' not found. Did you mean to use POST instead of GET?" % webhook, 404) except ValueError as e: raise ApiError(str(e), 400) if isinstance(response, Alert): response.customer = assign_customer(wanted=response.customer) add_remote_ip(request, response) try: alert = process_alert(response) except RejectException as e: raise ApiError(str(e), 403) except Exception as e: raise ApiError(str(e), 500) text = '{} alert received via custom webhook'.format(webhook) write_audit_trail.send(current_app._get_current_object(), event='webhook-received', 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', id=alert.id, alert=alert.serialize), 201 else: raise ApiError('insert or update via %s webhook failed' % webhook, 500) else: text = '{} request received via custom webhook'.format(webhook) write_audit_trail.send(current_app._get_current_object(), event='webhook-received', message=text, user=g.user, customers=g.customers, scopes=g.scopes, resource_id=None, type='user-defined', request=request) return jsonify(**response)
def grafana(): 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: 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: return jsonify(status="error", message=str(e)), 403 except Exception as e: return jsonify(status="error", message=str(e)), 500 alerts.append(alert) elif data and data['state'] == 'ok' and data.get('ruleId', None): try: query = qb.from_dict({'attributes.ruleId': str(data['ruleId'])}) existingAlerts = Alert.find_all(query) except Exception as e: raise ApiError(str(e), 500) for updateAlert in existingAlerts: updateAlert.severity = 'normal' updateAlert.status = 'closed' try: alert = process_alert(updateAlert) except RejectException as e: raise ApiError(str(e), 403) except Exception as e: raise ApiError(str(e), 500) alerts.append(alert) else: raise ApiError("no alerts in Grafana notification payload", 400) if len(alerts) == 1: return jsonify(status="ok", id=alerts[0].id, alert=alerts[0].serialize), 201 else: return jsonify(status="ok", ids=[alert.id for alert in alerts]), 201
def graylog(): try: incomingAlert = parse_graylog(request.json) except ValueError as e: raise ApiError(str(e), 400) if request.args.get('event', None): incomingAlert.event = request.args.get('event') if request.args.get('event_type', None): incomingAlert.event_type = request.args.get('event_type') if request.args.get('environment', None): incomingAlert.environment = request.args.get('environment') if request.args.get('service', None): incomingAlert.service = request.args.get('service').split(',') if request.args.get('severity', None): incomingAlert.severity = request.args.get('severity') incomingAlert.customer = assign_customer(wanted=incomingAlert.customer) add_remote_ip(request, incomingAlert) try: alert = process_alert(incomingAlert) except RejectException as e: raise ApiError(str(e), 403) except Exception as e: raise ApiError(str(e), 500) text = 'graylog alert received via webhook' write_audit_trail.send(current_app._get_current_object(), event='webhook-received', 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', id=alert.id, alert=alert.serialize), 201 else: raise ApiError('insert or update of graylog check failed', 500)
def prometheus(): alerts = [] if request.json and 'alerts' in request.json: external_url = request.json.get('externalURL', None) for alert in request.json['alerts']: try: incomingAlert = parse_prometheus(alert, external_url) except ValueError as e: raise ApiError(str(e), 400) incomingAlert.customer = assign_customer( wanted=incomingAlert.customer) add_remote_ip(request, incomingAlert) try: alert = process_alert(incomingAlert) except RejectException as e: raise ApiError(str(e), 403) except Exception as e: raise ApiError(str(e), 500) alerts.append(alert) else: raise ApiError('no alerts in Prometheus notification payload', 400) for alert in alerts: text = 'prometheus alert received via webhook' write_audit_trail.send(current_app._get_current_object(), event='webhook-received', message=text, user=g.user, customers=g.customers, scopes=g.scopes, resource_id=alert.id, type='alert', request=request) if len(alerts) == 1: return jsonify(status='ok', id=alerts[0].id, alert=alerts[0].serialize), 201 else: return jsonify(status='ok', ids=[alert.id for alert in alerts]), 201
def serverdensity(): try: incomingAlert = parse_serverdensity(request.json) except ValueError as e: raise ApiError(str(e), 400) incomingAlert.customer = assign_customer(wanted=incomingAlert.customer) add_remote_ip(request, incomingAlert) try: alert = process_alert(incomingAlert) except RejectException as e: raise ApiError(str(e), 403) except Exception as e: raise ApiError(str(e), 500) if alert: return jsonify(status="ok", id=alert.id, alert=alert.serialize), 201 else: raise ApiError("insert or update of ServerDensity alert failed", 500)
def cloudwatch(): try: incomingAlert = parse_notification(request.get_json(force=True)) except ValueError as e: raise ApiError(str(e), 400) incomingAlert.customer = assign_customer(wanted=incomingAlert.customer) add_remote_ip(request, incomingAlert) try: alert = process_alert(incomingAlert) except RejectException as e: raise ApiError(str(e), 403) except Exception as e: raise ApiError(str(e), 500) if alert: return jsonify(status='ok', id=alert.id, alert=alert.serialize), 201 else: raise ApiError('insert or update of cloudwatch alarm failed', 500)
def pingdom(): try: incomingAlert = parse_pingdom(request.json) except ValueError as e: raise ApiError(str(e), 400) incomingAlert.customer = assign_customer(wanted=incomingAlert.customer) add_remote_ip(request, incomingAlert) try: alert = process_alert(incomingAlert) except RejectException as e: raise ApiError(str(e), 403) except Exception as e: raise ApiError(str(e), 500) if alert: return jsonify(status='ok', id=alert.id, alert=alert.serialize), 201 else: raise ApiError('insert or update of pingdom check failed', 500)
def stackdriver(): try: incomingAlert = parse_stackdriver(request.get_json(force=True)) except ValueError as e: raise ApiError(str(e), 400) incomingAlert.customer = assign_customer(wanted=incomingAlert.customer) add_remote_ip(request, incomingAlert) try: alert = process_alert(incomingAlert) except RejectException as e: raise ApiError(str(e), 403) except Exception as e: raise ApiError(str(e), 500) if alert: return jsonify(status="ok", id=alert.id, alert=alert.serialize), 201 else: raise ApiError("insert or update of StackDriver notification failed", 500)
def serverdensity(): try: incomingAlert = parse_serverdensity(request.json) except ValueError as e: raise ApiError(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: raise ApiError(str(e), 403) except Exception as e: raise ApiError(str(e), 500) if alert: return jsonify(status="ok", id=alert.id, alert=alert.serialize), 201 else: raise ApiError("insert or update of ServerDensity alert failed", 500)
def custom(webhook): try: incomingAlert = custom_webhooks.webhooks[webhook].incoming( query_string=request.args, payload=request.get_json() or request.get_data(as_text=True)) except ValueError as e: raise ApiError(str(e), 400) incomingAlert.customer = assign_customer(wanted=incomingAlert.customer) add_remote_ip(request, incomingAlert) try: alert = process_alert(incomingAlert) except RejectException as e: raise ApiError(str(e), 403) except Exception as e: raise ApiError(str(e), 500) if alert: return jsonify(status="ok", id=alert.id, alert=alert.serialize), 201 else: raise ApiError("insert or update via %s webhook failed" % webhook, 500)
def cloudwatch(): try: incomingAlert = parse_notification(request.data) except ValueError as e: raise ApiError(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: raise ApiError(str(e), 403) except Exception as e: raise ApiError(str(e), 500) if alert: return jsonify(status="ok", id=alert.id, alert=alert.serialize), 201 else: raise ApiError("insert or update of cloudwatch alarm failed", 500)