activity: basic webhook alerts for automation failures

This commit is contained in:
Iain Learmonth 2022-05-14 10:18:00 +01:00
parent eb372bec59
commit ac4f9b4942
12 changed files with 300 additions and 3 deletions

View file

@ -3,6 +3,7 @@ from datetime import datetime, timedelta, timezone
from flask import Blueprint, render_template, request
from sqlalchemy import desc, or_
from app.models.activity import Activity
from app.models.alarms import Alarm
from app.models.mirrors import Origin, Proxy
from app.models.base import Group
@ -16,6 +17,7 @@ from app.portal.list import bp as list_
from app.portal.origin import bp as origin
from app.portal.onion import bp as onion
from app.portal.proxy import bp as proxy
from app.portal.webhook import bp as webhook
portal = Blueprint("portal", __name__, template_folder="templates", static_folder="static")
portal.register_blueprint(automation, url_prefix="/automation")
@ -27,6 +29,7 @@ portal.register_blueprint(list_, url_prefix="/list")
portal.register_blueprint(origin, url_prefix="/origin")
portal.register_blueprint(onion, url_prefix="/onion")
portal.register_blueprint(proxy, url_prefix="/proxy")
portal.register_blueprint(webhook, url_prefix="/webhook")
@portal.app_template_filter("mirror_expiry")
@ -57,8 +60,9 @@ def portal_home():
s: len(Alarm.query.filter(Alarm.alarm_state == s.upper(), Alarm.last_updated > (now - timedelta(days=1))).all())
for s in ["critical", "warning", "ok", "unknown"]
}
activity = Activity.query.filter(Activity.added > (now - timedelta(days=2))).order_by(desc(Activity.added)).all()
return render_template("home.html.j2", section="home", groups=groups, last24=last24, last72=last72,
lastweek=lastweek, proxies=proxies, **alarms)
lastweek=lastweek, proxies=proxies, **alarms, activity=activity)
@portal.route("/search")

View file

@ -114,6 +114,12 @@
{{ icon("file-earmark-excel") }} Block Lists
</a>
</li>
<li class="nav-item">
<a class="nav-link{% if section == "webhook" %} active{% endif %}"
href="{{ url_for("portal.webhook.webhook_list") }}">
{{ icon("activity") }} Webhooks
</a>
</li>
</ul>
<h6 class="sidebar-heading d-flex justify-content-between align-items-center px-3 mt-4 mb-1 text-muted">
<span>Infrastructure</span>

View file

@ -44,4 +44,21 @@
</div>
</div>
</div>
<div class="row mt-4">
<div class="col">
<div class="card h-100">
<h3 class="h4 card-header">Activity</h3>
<div class="card-body">
<table class="table table-striped">
{% for a in activity %}
<tr>
<td>{{ a.text }}</td>
<td>{{ a.added | format_datetime }}</td>
</tr>
{% endfor %}
</table>
</div>
</div>
</div>
</div>
{% endblock %}

View file

@ -1,5 +1,11 @@
{% macro icon(i) %}
{% if i == "arrow-down-up" %}
{% if i == "activity" %}
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-activity"
viewBox="0 0 16 16">
<path fill-rule="evenodd"
d="M6 2a.5.5 0 0 1 .47.33L10 12.036l1.53-4.208A.5.5 0 0 1 12 7.5h3.5a.5.5 0 0 1 0 1h-3.15l-1.88 5.17a.5.5 0 0 1-.94 0L6 3.964 4.47 8.171A.5.5 0 0 1 4 8.5H.5a.5.5 0 0 1 0-1h3.15l1.88-5.17A.5.5 0 0 1 6 2Z"/>
</svg>
{% elif i == "arrow-down-up" %}
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-down-up"
viewBox="0 0 16 16">
<path fill-rule="evenodd"

View file

@ -1,6 +1,7 @@
{% extends "base.html.j2" %}
{% from "tables.html.j2" import alarms_table, automations_table, bridgeconfs_table, bridges_table, eotk_table,
groups_table, mirrorlists_table, origins_table, origin_onion_table, onions_table, proxies_table %}
groups_table, mirrorlists_table, origins_table, origin_onion_table, onions_table, proxies_table,
webhook_table %}
{% block content %}
<h1 class="h2 mt-3">{{ title }}</h1>
@ -34,5 +35,7 @@
{{ origins_table(items) }}
{% elif item == "proxy" %}
{{ proxies_table(items) }}
{% elif item == "webhook" %}
{{ webhook_table(items) }}
{% endif %}
{% endblock %}

View file

@ -529,4 +529,33 @@
</tbody>
</table>
</div>
{% endmacro %}
{% macro webhook_table(webhooks) %}
<div class="table-responsive">
<table class="table table-striped table-sm">
<thead>
<tr>
<th scope="col">Description</th>
<th scope="col">Format</th>
<th scope="col">URL</th>
</tr>
</thead>
<tbody>
{% for webhook in webhooks %}
{% if not webhook.destroyed %}
<tr class="align-middle">
<td>{{ webhook.description }}</td>
<td>{{ webhook.format | webhook_format_name }}</td>
<td><code>{{ webhook.url }}</code></td>
<td>
<a href="{{ url_for("portal.webhook.webhook_destroy", webhook_id=webhook.id) }}"
class="btn btn-danger btn-sm">Destroy</a>
</td>
</tr>
{% endif %}
{% endfor %}
</tbody>
</table>
</div>
{% endmacro %}

View file

@ -4,6 +4,7 @@ from wtforms import SubmitField
from app import db
from app.models import AbstractResource
from app.models.activity import Activity
def response_404(message: str):
@ -37,7 +38,13 @@ def view_lifecycle(*,
else:
flash("Unknown action")
return redirect(url_for("portal.portal_home"))
activity = Activity(
activity_type="lifecycle",
text=f"Portal action: {message}. {success_message}"
)
db.session.add(activity)
db.session.commit()
activity.notify()
flash(success_message, "success")
return redirect(url_for(success_view))
return render_template("lifecycle.html.j2",

105
app/portal/webhook.py Normal file
View file

@ -0,0 +1,105 @@
from datetime import datetime
from flask import Blueprint, flash, Response, render_template, redirect, url_for
from flask_wtf import FlaskForm
from sqlalchemy import exc
from wtforms import StringField, SelectField, SubmitField
from wtforms.validators import DataRequired
from app.extensions import db
from app.models.activity import Webhook
from app.portal.util import response_404, view_lifecycle
bp = Blueprint("webhook", __name__)
@bp.app_template_filter("webhook_format_name")
def webhook_format_name(s: str) -> str:
if s == "telegram":
return "Telegram"
if s == "matrix":
return "Matrix"
class NewWebhookForm(FlaskForm):
description = StringField('Description', validators=[DataRequired()])
format = SelectField('Format', choices=[
("telegram", "Telegram"),
("matrix", "Matrix")
], validators=[DataRequired()])
url = StringField('URL', validators=[DataRequired()])
submit = SubmitField('Save Changes')
@bp.route("/new", methods=['GET', 'POST'])
def webhook_new():
form = NewWebhookForm()
if form.validate_on_submit():
webhook = Webhook(
description=form.description.data,
format=form.format.data,
url=form.url.data
)
try:
db.session.add(webhook)
db.session.commit()
flash(f"Created new webhook {webhook.url}.", "success")
return redirect(url_for("portal.webhook.webhook_edit", webhook_id=webhook.id))
except exc.SQLAlchemyError as e:
flash("Failed to create new webhook.", "danger")
return redirect(url_for("portal.webhook.webhook_list"))
return render_template("new.html.j2", section="webhook", form=form)
@bp.route('/edit/<webhook_id>', methods=['GET', 'POST'])
def webhook_edit(webhook_id):
webhook = Webhook.query.filter(Webhook.id == webhook_id).first()
if webhook is None:
return Response(render_template("error.html.j2",
section="webhook",
header="404 Webhook Not Found",
message="The requested webhook could not be found."),
status=404)
form = NewWebhookForm(description=webhook.description,
format=webhook.format,
url=webhook.url)
if form.validate_on_submit():
webhook.description = form.description.data
webhook.format = form.description.data
webhook.url = form.description.data
webhook.updated = datetime.utcnow()
try:
db.session.commit()
flash("Saved changes to webhook.", "success")
except exc.SQLAlchemyError:
flash("An error occurred saving the changes to the webhook.", "danger")
return render_template("edit.html.j2",
section="webhook",
title="Edit Webhook",
item=webhook, form=form)
@bp.route("/list")
def webhook_list():
webhooks = Webhook.query.all()
return render_template("list.html.j2",
section="webhook",
title="Webhooks",
item="webhook",
new_link=url_for("portal.webhook.webhook_new"),
items=webhooks)
@bp.route("/destroy/<webhook_id>", methods=['GET', 'POST'])
def webhook_destroy(webhook_id: int):
webhook = Webhook.query.filter(Webhook.id == webhook_id, Webhook.destroyed == None).first()
if webhook is None:
return response_404("The requested webhook could not be found.")
return view_lifecycle(
header=f"Destroy webhook {webhook.url}",
message=webhook.description,
success_message="Webhook destroyed.",
success_view="portal.webhook.webhook_list",
section="webhook",
resource=webhook,
action="destroy"
)