mirror of
https://github.com/retspen/webvirtcloud
synced 2026-12-03 17:36:05 +00:00
feat(alerts): mail and webhook alerts, and /healthz for a monitor
A failed backup, a scheduled backup not taken, backups the daily check finds missing and a job that ends uncertain could only be seen on the pages. They are now queued as alerts in the transaction of the change they tell of (never stopping it) and sent by the worker's periodic pass to the Settings page's alert e-mail addresses (the server's SMTP settings) and alert webhook (a JSON POST), each tried again for ten passes on its own. A skip for a host in maintenance is no alert. Setting values take 255 characters. /healthz answers 200 while the database answers and the job worker runs, 503 otherwise, without a login.
This commit is contained in:
parent
2cf7dbe577
commit
dcd7e32df7
21 changed files with 695 additions and 74 deletions
35
appsettings/migrations/0027_alert_settings.py
Normal file
35
appsettings/migrations/0027_alert_settings.py
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
from django.core.management.color import no_style
|
||||
from django.db import migrations, models
|
||||
from django.utils.translation import gettext_noop as _
|
||||
|
||||
SETTINGS = (
|
||||
(_("Alert e-mail addresses"), "ALERT_EMAILS",
|
||||
_("Comma-separated; mailed when a backup fails, is skipped, missing or damaged, or a job is uncertain")),
|
||||
(_("Alert webhook"), "ALERT_WEBHOOK", _("An http(s) URL that gets the same alerts as a JSON POST (subject, text, time)")),
|
||||
)
|
||||
|
||||
|
||||
def add_settings(apps, schema_editor):
|
||||
setting = apps.get_model("appsettings", "AppSettings")
|
||||
db_alias = schema_editor.connection.alias
|
||||
# see 0014: move a PostgreSQL id sequence past the fixed ids first
|
||||
with schema_editor.connection.cursor() as cursor:
|
||||
for sql in schema_editor.connection.ops.sequence_reset_sql(no_style(), [setting]):
|
||||
cursor.execute(sql)
|
||||
for name, key, description in SETTINGS:
|
||||
setting.objects.using(db_alias).get_or_create(
|
||||
key=key, defaults={"name": name, "value": "", "choices": "", "description": description})
|
||||
|
||||
|
||||
def del_settings(apps, schema_editor):
|
||||
apps.get_model("appsettings", "AppSettings").objects.using(schema_editor.connection.alias).filter(
|
||||
key__in=[key for _name, key, _description in SETTINGS]).delete()
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
dependencies = [("appsettings", "0026_repair_number_settings")]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField("appsettings", "value", models.CharField(max_length=255, verbose_name="value")),
|
||||
migrations.RunPython(add_settings, del_settings),
|
||||
]
|
||||
|
|
@ -8,6 +8,6 @@ class AppSettings(models.Model):
|
|||
|
||||
name = models.CharField(_("name"), max_length=25, null=False)
|
||||
key = models.CharField(_("key"), db_index=True, max_length=50, unique=True)
|
||||
value = models.CharField(_("value"), max_length=25)
|
||||
value = models.CharField(_("value"), max_length=255)
|
||||
choices = models.CharField(_("choices"), max_length=70)
|
||||
description = models.CharField(_("description"), max_length=100, null=True)
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
from django.db import DatabaseError
|
||||
from django.db import Error as DatabaseError
|
||||
|
||||
from .models import AppSettings
|
||||
|
||||
|
|
|
|||
|
|
@ -86,7 +86,7 @@
|
|||
min="{{ setting.bounds.0 }}" max="{{ setting.bounds.1 }}" step="1" required aria-describedby="help-{{ setting.key }}">
|
||||
{% else %}
|
||||
<input id="setting-{{ setting.key }}" class="form-control" type="text" name="{{ setting.key }}" value="{{ setting.value }}"
|
||||
maxlength="25" aria-describedby="help-{{ setting.key }}">
|
||||
maxlength="255" aria-describedby="help-{{ setting.key }}">
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
<input type="hidden" name="was-{{ setting.key }}" value="{{ setting.value }}">
|
||||
|
|
|
|||
|
|
@ -1,8 +1,11 @@
|
|||
import re
|
||||
from urllib.parse import urlsplit
|
||||
from pathlib import Path
|
||||
|
||||
from admin.decorators import superuser_only
|
||||
from django.conf import settings
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.core.validators import URLValidator, validate_email
|
||||
from django.contrib import messages
|
||||
from django.db import DatabaseError, transaction
|
||||
from django.http import HttpResponseRedirect
|
||||
|
|
@ -33,6 +36,9 @@ NUMBERS = {
|
|||
}
|
||||
|
||||
|
||||
# settings whose values the audit log does not show
|
||||
SECRET_SETTINGS = {"ALERT_WEBHOOK"}
|
||||
|
||||
# the Settings page's groups, in order; a setting in none shows under Other. The
|
||||
# titles stay English here: the page links them by slug and translates them
|
||||
GROUPS = [
|
||||
|
|
@ -50,6 +56,7 @@ GROUPS = [
|
|||
(gettext_noop("Snapshots and jobs"), ["SNAPSHOT_FREE_SPACE_PERCENT", "SNAPSHOT_FREE_SPACE_MIN_GIB",
|
||||
"WORKER_CONCURRENCY", "JOB_RETENTION_DAYS"]),
|
||||
(gettext_noop("Login"), ["LOGIN_FAILURE_LIMIT", "LOGIN_LOCKOUT_MINUTES"]),
|
||||
(gettext_noop("Alerts"), ["ALERT_EMAILS", "ALERT_WEBHOOK"]),
|
||||
(gettext_noop("Lists and pages"), ["VIEW_INSTANCES_LIST_STYLE", "VM_DRBD_STATUS", "LOGS_PER_PAGE",
|
||||
"LOG_RETENTION_DAYS"]),
|
||||
]
|
||||
|
|
@ -71,6 +78,16 @@ def refusal(setting, value):
|
|||
elif setting.key == "CLONE_INSTANCE_DEFAULT_PREFIX" and not re.fullmatch(r"[a-zA-Z0-9-]+", value):
|
||||
# a clone's name is the prefix and a number, and takes only these
|
||||
return _("%(setting)s takes letters, digits and '-' only") % {"setting": setting.name}
|
||||
try:
|
||||
if setting.key == "ALERT_EMAILS":
|
||||
for address in filter(None, (a.strip() for a in value.split(","))):
|
||||
validate_email(address)
|
||||
elif setting.key == "ALERT_WEBHOOK" and value.strip():
|
||||
URLValidator(schemes=["http", "https"])(value.strip())
|
||||
if "@" in urlsplit(value.strip()).netloc: # urllib would take user:password@ for the host
|
||||
raise ValidationError("credentials in the URL")
|
||||
except ValidationError:
|
||||
return _("%(setting)s: %(value)s is not an e-mail address or an http(s) URL") % {"setting": setting.name, "value": value}
|
||||
return None
|
||||
|
||||
|
||||
|
|
@ -121,7 +138,9 @@ def appsettings(request):
|
|||
for refused in refusals:
|
||||
messages.error(request, refused)
|
||||
if changed and not refusals:
|
||||
action = "change " + ", ".join(f"{s.name} to {value}" for s, value in changed)
|
||||
# a webhook URL is a secret of its own (Slack's): not in the log
|
||||
action = "change " + ", ".join(f"{s.name} to {'(not shown)' if s.key in SECRET_SETTINGS else value}"
|
||||
for s, value in changed)
|
||||
try:
|
||||
# one row, and all of the group or none of it
|
||||
with audit(request, None, "Settings", action), transaction.atomic():
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue