def status(): total_alert_gauge.set(db.get_count()) metrics = Gauge.get_gauges(format='json') metrics.extend(Counter.get_counters(format='json')) metrics.extend(Timer.get_timers(format='json')) auto_refresh_allow = { "group": "switch", "name": "auto_refresh_allow", "type": "text", "title": "Alert console auto-refresh", "description": "Allows auto-refresh of alert consoles to be turned off remotely", "value": "ON" if Switch.get('auto-refresh-allow').is_on() else "OFF", } metrics.append(auto_refresh_allow) now = int(time.time() * 1000) return jsonify(application="alerta", version=__version__, time=now, uptime=int(now - started), metrics=metrics)
def switchboard(): if request.method == "POST": for switch in Switch.get_all(): try: value = request.form[switch.name] switch.set_state(value) LOG.warning("Switch %s set to %s", switch.name, value) except KeyError: pass return render_template("management/switchboard.html", switches=switches) else: switch = request.args.get("switch", None) if switch: return render_template("management/switchboard.html", switches=[Switch.get(switch)]) else: return render_template("management/switchboard.html", switches=switches)
def switchboard(): if request.method == 'POST': for switch in Switch.get_all(): try: value = request.form[switch.name] switch.set_state(value) LOG.warning('Switch %s set to %s', switch.name, value) except KeyError: pass return render_template('management/switchboard.html', switches=switches) else: switch = request.args.get('switch', None) if switch: return render_template('management/switchboard.html', switches=[Switch.get(switch)]) else: return render_template('management/switchboard.html', switches=switches)
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 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') if request.headers.getlist("X-Forwarded-For"): incomingAlert.attributes.update( ip=request.headers.getlist("X-Forwarded-For")[0]) else: incomingAlert.attributes.update(ip=request.remote_addr) try: alert = process_alert(incomingAlert) except RejectException as e: receive_timer.stop_timer(recv_started) return jsonify(status="error", message=str(e)), 403 except RuntimeWarning 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 status(): total_alert_gauge.set(db.get_count()) metrics = Gauge.get_gauges() metrics.extend(Counter.get_counters()) metrics.extend(Timer.get_timers()) auto_refresh_allow = { "group": "switch", "name": "auto_refresh_allow", "type": "text", "title": "Alert console auto-refresh", "description": "Allows auto-refresh of alert consoles to be turned off remotely", "value": "ON" if Switch.get('auto-refresh-allow').is_on() else "OFF", } metrics.append(auto_refresh_allow) return jsonify(application="alerta", time=int(time.time() * 1000), metrics=metrics)
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 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') if request.headers.getlist("X-Forwarded-For"): incomingAlert.attributes.update(ip=request.headers.getlist("X-Forwarded-For")[0]) else: incomingAlert.attributes.update(ip=request.remote_addr) try: alert = process_alert(incomingAlert) except RejectException as e: receive_timer.stop_timer(recv_started) return jsonify(status="error", message=str(e)), 403 except RuntimeWarning 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 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())
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=found, more=total > limit, 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=0, more=False, alerts=[], severityCounts=severity_count, statusCounts=status_count, lastTime=query_time, autoRefresh=Switch.get('auto-refresh-allow').is_on() )
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() )
import logging from flask import request, Response, url_for, jsonify, render_template from flask_cors import cross_origin from alerta.app import app, db from alerta.app.auth import permission from alerta.app.switch import Switch, SwitchState from alerta.app.metrics import Gauge, Counter, Timer from alerta import build from alerta.version import __version__ LOG = app.logger switches = [ Switch('auto-refresh-allow', 'Allow consoles to auto-refresh alerts', SwitchState.to_state(app.config['AUTO_REFRESH_ALLOW'])), Switch('sender-api-allow', 'Allow alerts to be submitted via the API', SwitchState.to_state(app.config['SENDER_API_ALLOW'])) ] total_alert_gauge = Gauge('alerts', 'total', 'Total alerts', 'Total number of alerts in the database') started = time.time() * 1000 @app.route('/management', methods=['OPTIONS', 'GET']) @cross_origin() def management(): endpoints = [ url_for('manifest'), url_for('properties'),
import logging from flask import request, Response, url_for, jsonify, render_template from flask.ext.cors import cross_origin from alerta.app import app, db from alerta.app.auth import auth_required from alerta.app.switch import Switch, SwitchState from alerta.app.metrics import Gauge, Counter, Timer from alerta import build from alerta.version import __version__ LOG = logging.getLogger(__name__) switches = [ Switch('auto-refresh-allow', 'Allow consoles to auto-refresh alerts', SwitchState.ON), # Switch('console-api-allow', 'Allow consoles to use the alert API', SwitchState.ON), # TODO(nsatterl) # Switch('sender-api-allow', 'Allow alerts to be submitted via the API', SwitchState.ON), # TODO(nsatterl) ] total_alert_gauge = Gauge('alerts', 'total', 'Total alerts', 'Total number of alerts in the database') started = time.time() * 1000 @app.route('/management', methods=['OPTIONS', 'GET']) @cross_origin() def management(): endpoints = [ url_for('manifest'), url_for('properties'),
import logging from flask import request, Response, url_for, jsonify, render_template from flask_cors import cross_origin from alerta.app import app, db from alerta.app.auth import auth_required, admin_required from alerta.app.switch import Switch, SwitchState from alerta.app.metrics import Gauge, Counter, Timer from alerta import build from alerta.version import __version__ LOG = logging.getLogger(__name__) switches = [ Switch('auto-refresh-allow', 'Allow consoles to auto-refresh alerts', SwitchState.ON), Switch('sender-api-allow', 'Allow alerts to be submitted via the API', SwitchState.ON) ] total_alert_gauge = Gauge('alerts', 'total', 'Total alerts', 'Total number of alerts in the database') started = time.time() * 1000 @app.route('/management', methods=['OPTIONS', 'GET']) @cross_origin() def management(): endpoints = [ url_for('manifest'), url_for('properties'),
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=found, more=total > limit, 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=0, more=False, alerts=[], severityCounts=severity_count, statusCounts=status_count, lastTime=query_time, autoRefresh=Switch.get('auto-refresh-allow').is_on()) @app.route('/alerts/history', methods=['OPTIONS', 'GET'])