From 37b4a66ba42a4d2f048f9b9260c2b2edf7b1ab93 Mon Sep 17 00:00:00 2001 From: Peter van Arkel Date: Sat, 21 Nov 2026 17:43:17 +0100 Subject: [PATCH] Collect and display per-node service health observations --- pyproject.toml | 1 + src/coulobre/hosting/health_views.py | 81 +++++++ .../commands/collect_service_health.py | 23 ++ .../hosting/migrations/0033_service_health.py | 39 ++++ src/coulobre/hosting/models.py | 16 ++ src/coulobre/hosting/static/hosting/panel.css | 7 + .../hosting/templates/hosting/base.html | 6 + .../templates/hosting/service_health.html | 41 ++++ src/coulobre/hosting/test_service_health.py | 199 ++++++++++++++++ src/coulobre/hosting/urls.py | 4 + src/coulobre/service_health.py | 218 ++++++++++++++++++ src/coulobre/settings.py | 9 + 12 files changed, 644 insertions(+) create mode 100644 src/coulobre/hosting/health_views.py create mode 100644 src/coulobre/hosting/management/commands/collect_service_health.py create mode 100644 src/coulobre/hosting/migrations/0033_service_health.py create mode 100644 src/coulobre/hosting/templates/hosting/service_health.html create mode 100644 src/coulobre/hosting/test_service_health.py create mode 100644 src/coulobre/service_health.py diff --git a/pyproject.toml b/pyproject.toml index 9f4f0d4..e03589e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -22,6 +22,7 @@ coulobre-directadmin = "coulobre.directadmin:main" coulobre-dns-node = "coulobre.dns_node:main" coulobre-mail-node = "coulobre.mail_node:main" coulobre-service-credentials = "coulobre.service_credentials:main" +coulobre-service-health = "coulobre.service_health:main" coulobre-web-node = "coulobre.web_node:main" [project.optional-dependencies] diff --git a/src/coulobre/hosting/health_views.py b/src/coulobre/hosting/health_views.py new file mode 100644 index 0000000..a7252ee --- /dev/null +++ b/src/coulobre/hosting/health_views.py @@ -0,0 +1,81 @@ +from datetime import timedelta + +from django.conf import settings +from django.contrib.auth.mixins import PermissionRequiredMixin +from django.utils import timezone +from django.utils.decorators import method_decorator +from django.views.decorators.cache import never_cache +from django.views.generic import TemplateView + +from coulobre.service_health import HEALTH_DETAILS, SERVICES, HealthResult + +from .access import OperatorOnlyMixin +from .models import ServiceHealth + +STALE_AFTER = timedelta(minutes=5) +STATUS = { + HealthResult.ACTIVE: ("Active", "ready"), + HealthResult.INACTIVE: ("Unavailable", "suspended"), + HealthResult.MISSING: ("Unavailable", "suspended"), + HealthResult.FAILED: ("Unavailable", "suspended"), + HealthResult.TRANSITIONING: ("Transitioning", "pending"), +} + + +@method_decorator(never_cache, name="dispatch") +class ServiceHealthView( + OperatorOnlyMixin, PermissionRequiredMixin, TemplateView +): + permission_required = "hosting.view_servicehealth" + template_name = "hosting/service_health.html" + + def get_context_data(self, **kwargs): + targets = settings.SERVICE_HEALTH_TARGETS + readings = { + (reading.node, reading.service): reading + for reading in ServiceHealth.objects.filter( + node__in=[target.node for target in targets] + ) + } + cutoff = timezone.now() - STALE_AFTER + return super().get_context_data( + nodes=[ + ( + target, + [ + service_row( + service, + readings.get((target.node, service)), + cutoff, + ) + for service in target.services + ], + ) + for target in targets + ], + **kwargs, + ) + + +def service_row(service, reading, cutoff): + row = { + "name": SERVICES[service][0], + "status": "Unknown", + "style": "pending", + } + if reading is None: + return row | {"detail": "This service has never been checked."} + + status, style = STATUS.get(reading.result, ("Unknown", "pending")) + detail = HEALTH_DETAILS.get( + reading.result, "The stored result is unavailable." + ) + if reading.observed_at <= cutoff: + detail = f"Last check: {detail}" + status, style = "Stale", "pending" + return row | { + "status": status, + "style": style, + "detail": detail, + "observed_at": reading.observed_at, + } diff --git a/src/coulobre/hosting/management/commands/collect_service_health.py b/src/coulobre/hosting/management/commands/collect_service_health.py new file mode 100644 index 0000000..a172138 --- /dev/null +++ b/src/coulobre/hosting/management/commands/collect_service_health.py @@ -0,0 +1,23 @@ +from django.conf import settings +from django.core.management.base import BaseCommand +from django.utils import timezone + +from coulobre.hosting.models import ServiceHealth +from coulobre.service_health import read_health + + +class Command(BaseCommand): + help = "Record health observations for local and remote services." + + def handle(self, **options): + for target in settings.SERVICE_HEALTH_TARGETS: + observed_at = timezone.now() + for service, result in read_health(target).items(): + values = {"result": result, "observed_at": observed_at} + reading, created = ServiceHealth.objects.get_or_create( + node=target.node, service=service, defaults=values + ) + if not created: + ServiceHealth.objects.filter( + pk=reading.pk, observed_at__lte=observed_at + ).update(**values) diff --git a/src/coulobre/hosting/migrations/0033_service_health.py b/src/coulobre/hosting/migrations/0033_service_health.py new file mode 100644 index 0000000..667f233 --- /dev/null +++ b/src/coulobre/hosting/migrations/0033_service_health.py @@ -0,0 +1,39 @@ +# Generated by Django 5.2.19 on 2026-11-21 16:40 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("hosting", "0032_phpmyadmin_signon"), + ] + + operations = [ + migrations.CreateModel( + name="ServiceHealth", + fields=[ + ( + "id", + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("node", models.CharField(max_length=63)), + ("service", models.CharField(max_length=24)), + ("result", models.CharField(max_length=24)), + ("observed_at", models.DateTimeField()), + ], + options={ + "default_permissions": ("view",), + "constraints": [ + models.UniqueConstraint( + fields=("node", "service"), + name="hosting_service_health_identity", + ) + ], + }, + ), + ] diff --git a/src/coulobre/hosting/models.py b/src/coulobre/hosting/models.py index 32e8412..de2f2a5 100644 --- a/src/coulobre/hosting/models.py +++ b/src/coulobre/hosting/models.py @@ -809,3 +809,19 @@ class MailboxProvisioning(ProvisioningJob): HostingMailbox, on_delete=models.CASCADE, related_name="provisioning" ) quota_update = models.BooleanField(default=False) + + +class ServiceHealth(models.Model): + node = models.CharField(max_length=63) + service = models.CharField(max_length=24) + result = models.CharField(max_length=24) + observed_at = models.DateTimeField() + + class Meta: + default_permissions = ("view",) + constraints = [ + models.UniqueConstraint( + fields=("node", "service"), + name="hosting_service_health_identity", + ) + ] diff --git a/src/coulobre/hosting/static/hosting/panel.css b/src/coulobre/hosting/static/hosting/panel.css index 88d7e1a..39981e9 100644 --- a/src/coulobre/hosting/static/hosting/panel.css +++ b/src/coulobre/hosting/static/hosting/panel.css @@ -159,6 +159,13 @@ button, input, select, textarea { background: linear-gradient(currentColor, currentColor) center 35% / 40% 1px no-repeat; } +.nav-icon-health { + background: + linear-gradient(currentColor, currentColor) 25% bottom / 1px 40% no-repeat, + linear-gradient(currentColor, currentColor) center bottom / 1px 70% no-repeat, + linear-gradient(currentColor, currentColor) 75% bottom / 1px 55% no-repeat; +} + .nav-icon-customers { position: relative; height: 0.46rem; diff --git a/src/coulobre/hosting/templates/hosting/base.html b/src/coulobre/hosting/templates/hosting/base.html index 760ec1a..cd111dc 100644 --- a/src/coulobre/hosting/templates/hosting/base.html +++ b/src/coulobre/hosting/templates/hosting/base.html @@ -41,6 +41,12 @@ Web provisioning {% endif %} + {% if is_operator and perms.hosting.view_servicehealth %} + + + Service health + + {% endif %} {% endwith %}