diff --git a/deploy/openssh/coulobre-health-request.conf.example b/deploy/openssh/coulobre-health-request.conf.example new file mode 100644 index 0000000..cf7142c --- /dev/null +++ b/deploy/openssh/coulobre-health-request.conf.example @@ -0,0 +1,15 @@ +Match User coulobre-health-request + AuthenticationMethods publickey + PubkeyAuthentication yes + PasswordAuthentication no + KbdInteractiveAuthentication no + AuthorizedKeysFile /etc/ssh/coulobre-node-keys/%u + AuthorizedKeysCommand none + TrustedUserCAKeys none + DisableForwarding yes + PermitTunnel no + PermitTTY no + PermitUserRC no + ForceCommand /opt/coulobre/bin/coulobre-service-health + +Match all diff --git a/deploy/openssh/health-client.conf.example b/deploy/openssh/health-client.conf.example new file mode 100644 index 0000000..f79822c --- /dev/null +++ b/deploy/openssh/health-client.conf.example @@ -0,0 +1,13 @@ +Host mail-01 + HostName mail.example.org + Port 22 + User coulobre-health-request + IdentityFile /etc/coulobre-health/id_ed25519 + UserKnownHostsFile /etc/coulobre-health/known_hosts + +Host dns-01 + HostName dns.example.org + Port 22 + User coulobre-health-request + IdentityFile /etc/coulobre-health/id_ed25519 + UserKnownHostsFile /etc/coulobre-health/known_hosts diff --git a/deploy/panel.env.example b/deploy/panel.env.example index bec5d85..0d04764 100644 --- a/deploy/panel.env.example +++ b/deploy/panel.env.example @@ -8,6 +8,7 @@ COULOBRE_DB_USER=coulobre COULOBRE_DB_PASSWORD= COULOBRE_DB_HOST=/run/mysqld/mysqld.sock COULOBRE_BROKER_URL=amqp://coulobre:@127.0.0.1:5672/coulobre +# COULOBRE_SERVICE_HEALTH='{"panel-01":{"local":["panel","worker","scheduler","apache","mariadb","rabbitmq"]}}' # COULOBRE_LOCAL_WEB_ACTIVATION=1 # COULOBRE_LOCAL_WEB_HTTPS=1 # COULOBRE_LOCAL_WEB_SFTP=1 diff --git a/deploy/systemd/coulobre-health.service b/deploy/systemd/coulobre-health.service new file mode 100644 index 0000000..6a8671d --- /dev/null +++ b/deploy/systemd/coulobre-health.service @@ -0,0 +1,18 @@ +[Unit] +Description=Coulobre service health collector +Wants=network-online.target +After=network-online.target mariadb.service + +[Service] +Type=oneshot +User=coulobre-health +Group=coulobre-health +EnvironmentFile=/etc/coulobre/panel.env +WorkingDirectory=/opt/coulobre +ExecStart=/opt/coulobre/bin/python -m django collect_service_health +TimeoutStartSec=5min +UMask=0077 +NoNewPrivileges=true +PrivateTmp=true +ProtectHome=true +ProtectSystem=strict diff --git a/deploy/systemd/coulobre-health.timer b/deploy/systemd/coulobre-health.timer new file mode 100644 index 0000000..0a3bfb0 --- /dev/null +++ b/deploy/systemd/coulobre-health.timer @@ -0,0 +1,10 @@ +[Unit] +Description=Refresh Coulobre service health observations + +[Timer] +OnActiveSec=10s +OnUnitInactiveSec=1min +AccuracySec=5s + +[Install] +WantedBy=timers.target diff --git a/docs/deployment.md b/docs/deployment.md index d4e9772..52d7f93 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -977,3 +977,87 @@ When moving an existing service, migrate its data and provisioning state and let outstanding jobs finish before switching destinations. For mail on a separate server, retrieve logins on the mail node, and use the [separate-node backup workflow](development.md#bundle-backups-from-separate-nodes). + +## Monitor local and remote services + +The panel's **Service health** page shows the latest observation and timestamp +for each configured service and node. Superusers and operators with **Can +view service health** can open the page. The collector checks systemd unit +states. Use the corresponding service tests to check mail delivery, DNS +answers, or website responses. The collector and dashboard both need the +panel database to record and display observations. + +Set `COULOBRE_SERVICE_HEALTH` in `/etc/coulobre/panel.env`. This example checks +local panel services and two records for remote mail and DNS services. + +```ini +COULOBRE_SERVICE_HEALTH='{"panel-01":{"local":["panel","worker","scheduler","apache","php-fpm","mariadb","rabbitmq"]},"mail-01":{"ssh":["postfix","dovecot"]},"dns-01":{"ssh":["bind9"]}}' +``` + +Choose an ID for each node using lower-case letters, numbers, and hyphens, +starting with a letter. Keep the IDs stable for the same hosts. List one +`local` node per panel installation and use `ssh` for each additional host. +Use `bind9`, `postfix`, and `dovecot` under `local` for co-located services. +Remote checks accept those same three service names. The SSH client matches +each remote node ID to a `Host` entry. + +Install the collector with a dedicated service identity after updating the +panel package. + +```sh +sudo useradd --system --user-group --no-create-home --home-dir /nonexistent \ + --shell /usr/sbin/nologin coulobre-health +sudo install -m 0644 deploy/systemd/coulobre-health.service \ + deploy/systemd/coulobre-health.timer /etc/systemd/system/ +panel_manage migrate --noinput +panel_manage collectstatic --noinput +sudo systemctl daemon-reload +sudo systemctl restart coulobre-panel +sudo systemctl enable --now coulobre-health.timer +``` + +The timer runs again one minute after each collection finishes. **Unknown** +means there is no reading or the collector could not obtain a unit state. +**Stale** shows the last observation when it is at least five minutes old. +To run a check immediately, use `sudo systemctl start coulobre-health.service`. +[systemctl unit properties](https://www.freedesktop.org/software/systemd/man/latest/systemctl.html) + +### Enable read-only remote checks + +Set up a collector key on the panel host. Configure an SSH alias for each +remote node and pin that server's verified SSH host public key in the +collector's `known_hosts` file. Use `[hostname]:port` for a nonstandard port. +Keep the configuration and host keys owned by root and readable by the +`coulobre-health` group. + +```sh +sudo apt install openssh-client +sudo install -d -o root -g coulobre-health -m 0750 /etc/coulobre-health +sudo install -o root -g coulobre-health -m 0640 \ + deploy/openssh/health-client.conf.example /etc/coulobre-health/ssh_config +sudo ssh-keygen -t ed25519 -N '' -f /etc/coulobre-health/id_ed25519 +sudo chown coulobre-health:coulobre-health /etc/coulobre-health/id_ed25519 +sudo cat /etc/coulobre-health/id_ed25519.pub +``` + +On each remote host, install the same Coulobre package in `/opt/coulobre`. +Save the collector public key as `panel-health.pub`. Use the key only for +the receiver below. The SSH account runs the fixed status command under its +own unprivileged identity. + +```sh +sudo apt install openssh-server +sudo useradd --system --user-group --no-create-home --home-dir / --shell /bin/sh \ + coulobre-health-request +sudo install -d -m 0755 /etc/ssh/coulobre-node-keys +sudo install -o root -g root -m 0644 panel-health.pub \ + /etc/ssh/coulobre-node-keys/coulobre-health-request +sudo install -m 0644 deploy/openssh/coulobre-health-request.conf.example \ + /etc/ssh/sshd_config.d/00-coulobre-health-request.conf +sudo sshd -t +sudo systemctl reload-or-restart ssh.service +``` + +Restart the panel after adding node IDs. Run the collector and open **Service +health** to check the remote readings. Recovery of the SSH connection or the +service is reflected by the next successful check. 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 %}