def cloudwatch(): hook_started = webhook_timer.start_timer() try: incomingAlert = parse_notification(request.data) 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 webhook_timer.stop_timer(hook_started) if alert: body = alert.get_body() body['href'] = absolute_url('/alert/' + alert.id) return jsonify(status="ok", id=alert.id, alert=body), 201, {'Location': body['href']} else: return jsonify(status="error", message="insert or update of cloudwatch alarm failed"), 500
def cloudwatch(): hook_started = webhook_timer.start_timer() try: incomingAlert = parse_notification(request.data) 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 webhook_timer.stop_timer(hook_started) if alert: body = alert.get_body() body['href'] = absolute_url('/alert/' + alert.id) return jsonify(status="ok", id=alert.id, alert=body), 201, {'Location': body['href']} else: return jsonify(status="error", message="insert or update of cloudwatch alarm failed"), 500
def receive_alert(): if not Switch.get('sender-api-allow').is_on(): return jsonify( status="error", message="API not accepting alerts. Try again later."), 503 recv_started = receive_timer.start_timer() try: incomingAlert = Alert.parse_alert(request.data) except ValueError as e: receive_timer.stop_timer(recv_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: receive_timer.stop_timer(recv_started) return jsonify(status="error", message=str(e)), 403 except RateLimit as e: receive_timer.stop_timer(recv_started) return jsonify(status="error", id=incomingAlert.id, message=str(e)), 429 except BlackoutPeriod as e: receive_timer.stop_timer(recv_started) return jsonify(status="ok", id=incomingAlert.id, message=str(e)), 202 except Exception as e: receive_timer.stop_timer(recv_started) return jsonify(status="error", message=str(e)), 500 receive_timer.stop_timer(recv_started) if alert: body = alert.get_body() body['href'] = absolute_url('/alert/' + alert.id) return jsonify(status="ok", id=alert.id, alert=body), 201, { 'Location': body['href'] } else: return jsonify( status="error", message="insert or update of received alert failed"), 500
def prometheus(): alerts = [] if request.json and 'alerts' in request.json: hook_started = webhook_timer.start_timer() external_url = request.json.get('externalURL', None) for alert in request.json['alerts']: try: incomingAlert = parse_prometheus(alert, external_url) 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) else: return jsonify( status="error", message="no alerts in Prometheus 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
def receive_alert(): if not Switch.get('sender-api-allow').is_on(): return jsonify(status="error", message="API not accepting alerts. Try again later."), 503 recv_started = receive_timer.start_timer() try: incomingAlert = Alert.parse_alert(request.data) except ValueError as e: receive_timer.stop_timer(recv_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: receive_timer.stop_timer(recv_started) return jsonify(status="error", message=str(e)), 403 except RateLimit as e: receive_timer.stop_timer(recv_started) return jsonify(status="error", id=incomingAlert.id, message=str(e)), 429 except BlackoutPeriod as e: receive_timer.stop_timer(recv_started) return jsonify(status="ok", id=incomingAlert.id, message=str(e)), 202 except Exception as e: receive_timer.stop_timer(recv_started) return jsonify(status="error", message=str(e)), 500 receive_timer.stop_timer(recv_started) if alert: body = alert.get_body() body['href'] = absolute_url('/alert/' + alert.id) return jsonify(status="ok", id=alert.id, alert=body), 201, {'Location': body['href']} else: return jsonify(status="error", message="insert or update of received alert failed"), 500
def prometheus(): alerts = [] if request.json and 'alerts' in request.json: hook_started = webhook_timer.start_timer() for alert in request.json['alerts']: try: incomingAlert = parse_prometheus(alert) 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) else: return jsonify(status="error", message="no alerts in Prometheus 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
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
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