diff --git a/DATABASE.md b/DATABASE.md index 3d89b05..8f40e06 100644 --- a/DATABASE.md +++ b/DATABASE.md @@ -24,7 +24,7 @@ what the app actually creates. | **File** | `DATABASE_PATH`, default `../data/homelab-manager.sqlite` (relative to `server/`; `/app/data/...` in Docker). The folder is created if missing. | | **Foreign keys** | Switched on for every connection (`PRAGMA foreign_keys = ON`), so the `ON DELETE` rules below are enforced. | | **Migrations** | SQL files in [`server/drizzle/`](server/drizzle) (`0000` … `0014`), applied automatically when the server starts. Which ones have run is recorded in the table `__drizzle_migrations`. | -| **Tables** | 22 application tables, plus drizzle's own `__drizzle_migrations`. | +| **Tables** | 23 application tables, plus drizzle's own `__drizzle_migrations`. | **Not in the database:** @@ -63,7 +63,7 @@ erDiagram Eight tables stand alone, with no foreign keys: `settings`, `secrets`, `ipam_entries`, `domains`, `diag_log`, `notification_queue`, -`consistency_ignores`, `tag_definitions`. `maintenance_windows` also has no +`consistency_ignores`, `tag_definitions`. `maintenance_windows` and `maintenance_aliases` also have no foreign key — see [soft references](#soft-references-not-foreign-keys). ## Tables @@ -143,6 +143,21 @@ required, so a forgotten window can't silence real problems forever. | `created_by` | text | Name of the person who opened it. `Uptime Kuma sync` for mirrored ones. | | `source` | text | `kuma::` on windows mirrored from Uptime Kuma's schedule, so a later sync can find them again; NULL for ones started by hand. A window whose `started_at` is still in the future is *scheduled*: it silences nothing until it begins. A scheduled one that is cancelled has `ends_at` set equal to `started_at`. | +#### `maintenance_aliases` + +An address that Uptime Kuma's monitors use for something this app knows by another address, so the Uptime Kuma +maintenance sync can find it. Added on the Maintenance page. + +| Column | Type | Notes | +|---|---|---| +| `id` | integer PK | | +| `address` | text NOT NULL | A bare lowercase host: a name or an IP. | +| `target_type` | text NOT NULL | `server` | `integration` | `dns_provider`. | +| `target_id` | integer NOT NULL | The ID in the table `target_type` names. **Not a foreign key**; an alias whose target was deleted is ignored. | +| `created_at` | text NOT NULL | | + +Unique on (`address`, `target_type`, `target_id`). + ### Servers #### `servers` @@ -498,6 +513,7 @@ These point at other rows by ID or name without the database enforcing it, so a stale value is possible and the app treats it as "nothing there": - `maintenance_windows.target_id` (→ a server, integration or DNS provider, by `target_type`) +- `maintenance_aliases.target_id` (the same, by `target_type`) - `audit_log.target_id` - `dns_records_cache.zone_id` (→ `dns_zones_cache.zone_id`, same provider) - `servers.tags` (→ `tag_definitions.name`) diff --git a/INTEGRATIONS.md b/INTEGRATIONS.md index b39563e..c92ea51 100644 --- a/INTEGRATIONS.md +++ b/INTEGRATIONS.md @@ -93,8 +93,11 @@ the dashboard views themselves load fine. that hasn't started shows as *Scheduled* and silences nothing yet. - Matching is exact. Nothing is worked out through DNS, so a website that only resolves to the same address as a reverse proxy does not put the proxy into maintenance. A monitor that matches nothing is listed in the - result as `name (address)` so you can see why. The usual fix is to make the address agree: a Proxmox - integration added by IP will not match a monitor that watches its hostname, and vice versa. + result as `name (address)` so you can see why. The usual cause is that Uptime Kuma watches a thing by a different + address than the one it was added with here (a Proxmox host added by its LAN address but monitored over Tailscale). + Add that address under **Address aliases** on the Maintenance page, saying which server, integration or DNS + provider it belongs to; matching stays exact, and an alias applies to every sync. Monitors that are not in + Uptime Kuma's metrics (paused ones) and hosted services outside your network can't be matched and are left alone. - A window on an integration silences that integration's alerts, including "integration down" for that type of service — see the Maintenance page for the full list. - Tick *Keep maintenance windows in step with Uptime Kuma's schedule* to run this automatically every diff --git a/server/drizzle/0018_even_marvel_zombies.sql b/server/drizzle/0018_even_marvel_zombies.sql new file mode 100644 index 0000000..a2a949e --- /dev/null +++ b/server/drizzle/0018_even_marvel_zombies.sql @@ -0,0 +1,9 @@ +CREATE TABLE `maintenance_aliases` ( + `id` integer PRIMARY KEY AUTOINCREMENT NOT NULL, + `address` text NOT NULL, + `target_type` text NOT NULL, + `target_id` integer NOT NULL, + `created_at` text DEFAULT (current_timestamp) NOT NULL +); +--> statement-breakpoint +CREATE UNIQUE INDEX `maintenance_aliases_unique_idx` ON `maintenance_aliases` (`address`,`target_type`,`target_id`); \ No newline at end of file diff --git a/server/drizzle/meta/0018_snapshot.json b/server/drizzle/meta/0018_snapshot.json new file mode 100644 index 0000000..3f3c83e --- /dev/null +++ b/server/drizzle/meta/0018_snapshot.json @@ -0,0 +1,1883 @@ +{ + "version": "6", + "dialect": "sqlite", + "id": "5a0fcda1-18a6-43bb-92ed-6d25abf31ffb", + "prevId": "52470861-33df-441f-baa6-3f66f776058e", + "tables": { + "alert_tickets": { + "name": "alert_tickets", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "autoincrement": true + }, + "alert_key": { + "name": "alert_key", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "status": { + "name": "status", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "seen_count": { + "name": "seen_count", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": 1 + }, + "integration_id": { + "name": "integration_id", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "ticket_number": { + "name": "ticket_number", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "grouped": { + "name": "grouped", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": false + }, + "severity": { + "name": "severity", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "message": { + "name": "message", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "first_seen_at": { + "name": "first_seen_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "last_seen_at": { + "name": "last_seen_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "ticketed_at": { + "name": "ticketed_at", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "cleared_at": { + "name": "cleared_at", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + } + }, + "indexes": { + "alert_tickets_key_idx": { + "name": "alert_tickets_key_idx", + "columns": [ + "alert_key" + ], + "isUnique": false + } + }, + "foreignKeys": { + "alert_tickets_integration_id_integrations_id_fk": { + "name": "alert_tickets_integration_id_integrations_id_fk", + "tableFrom": "alert_tickets", + "tableTo": "integrations", + "columnsFrom": [ + "integration_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "audit_log": { + "name": "audit_log", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "autoincrement": true + }, + "actor_user_id": { + "name": "actor_user_id", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "actor_label": { + "name": "actor_label", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "category": { + "name": "category", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "action": { + "name": "action", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "target_type": { + "name": "target_type", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "target_id": { + "name": "target_id", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "detail": { + "name": "detail", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "created_at": { + "name": "created_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "(current_timestamp)" + } + }, + "indexes": {}, + "foreignKeys": { + "audit_log_actor_user_id_users_id_fk": { + "name": "audit_log_actor_user_id_users_id_fk", + "tableFrom": "audit_log", + "tableTo": "users", + "columnsFrom": [ + "actor_user_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "consistency_ignores": { + "name": "consistency_ignores", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "autoincrement": true + }, + "key": { + "name": "key", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "title": { + "name": "title", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "reason": { + "name": "reason", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "created_by": { + "name": "created_by", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "created_at": { + "name": "created_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "(current_timestamp)" + } + }, + "indexes": { + "consistency_ignores_key_unique": { + "name": "consistency_ignores_key_unique", + "columns": [ + "key" + ], + "isUnique": true + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "diag_log": { + "name": "diag_log", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "autoincrement": true + }, + "source": { + "name": "source", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "operation": { + "name": "operation", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "ok": { + "name": "ok", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "latency_ms": { + "name": "latency_ms", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "error": { + "name": "error", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "created_at": { + "name": "created_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "(current_timestamp)" + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "dns_providers": { + "name": "dns_providers", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "autoincrement": true + }, + "provider_type": { + "name": "provider_type", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "credential_id": { + "name": "credential_id", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "config": { + "name": "config", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "enabled": { + "name": "enabled", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": true + }, + "created_at": { + "name": "created_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "(current_timestamp)" + } + }, + "indexes": {}, + "foreignKeys": { + "dns_providers_credential_id_integration_credentials_id_fk": { + "name": "dns_providers_credential_id_integration_credentials_id_fk", + "tableFrom": "dns_providers", + "tableTo": "integration_credentials", + "columnsFrom": [ + "credential_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "dns_records_cache": { + "name": "dns_records_cache", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "autoincrement": true + }, + "provider_id": { + "name": "provider_id", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "zone_id": { + "name": "zone_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "record_id": { + "name": "record_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "type": { + "name": "type", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "content": { + "name": "content", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "ttl": { + "name": "ttl", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "priority": { + "name": "priority", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "proxied": { + "name": "proxied", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + } + }, + "indexes": {}, + "foreignKeys": { + "dns_records_cache_provider_id_dns_providers_id_fk": { + "name": "dns_records_cache_provider_id_dns_providers_id_fk", + "tableFrom": "dns_records_cache", + "tableTo": "dns_providers", + "columnsFrom": [ + "provider_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "dns_zones_cache": { + "name": "dns_zones_cache", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "autoincrement": true + }, + "provider_id": { + "name": "provider_id", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "zone_id": { + "name": "zone_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "zone_name": { + "name": "zone_name", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "synced_at": { + "name": "synced_at", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + } + }, + "indexes": { + "dns_zones_cache_provider_zone_idx": { + "name": "dns_zones_cache_provider_zone_idx", + "columns": [ + "provider_id", + "zone_id" + ], + "isUnique": true + } + }, + "foreignKeys": { + "dns_zones_cache_provider_id_dns_providers_id_fk": { + "name": "dns_zones_cache_provider_id_dns_providers_id_fk", + "tableFrom": "dns_zones_cache", + "tableTo": "dns_providers", + "columnsFrom": [ + "provider_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "domains": { + "name": "domains", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "autoincrement": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "origin": { + "name": "origin", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "'manual'" + }, + "expires_at": { + "name": "expires_at", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "registrar": { + "name": "registrar", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "lookup_source": { + "name": "lookup_source", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "last_checked_at": { + "name": "last_checked_at", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "last_checked_ok_at": { + "name": "last_checked_ok_at", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "last_check_error": { + "name": "last_check_error", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "created_at": { + "name": "created_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "(current_timestamp)" + } + }, + "indexes": { + "domains_name_unique": { + "name": "domains_name_unique", + "columns": [ + "name" + ], + "isUnique": true + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "integration_credentials": { + "name": "integration_credentials", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "autoincrement": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "encrypted_secret": { + "name": "encrypted_secret", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "created_at": { + "name": "created_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "(current_timestamp)" + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "integrations": { + "name": "integrations", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "autoincrement": true + }, + "type": { + "name": "type", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "base_url": { + "name": "base_url", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "credential_id": { + "name": "credential_id", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "config": { + "name": "config", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "enabled": { + "name": "enabled", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": true + }, + "created_at": { + "name": "created_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "(current_timestamp)" + } + }, + "indexes": {}, + "foreignKeys": { + "integrations_credential_id_integration_credentials_id_fk": { + "name": "integrations_credential_id_integration_credentials_id_fk", + "tableFrom": "integrations", + "tableTo": "integration_credentials", + "columnsFrom": [ + "credential_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "ipam_entries": { + "name": "ipam_entries", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "autoincrement": true + }, + "ip_address": { + "name": "ip_address", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "label": { + "name": "label", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "vendor": { + "name": "vendor", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "location": { + "name": "location", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "notes": { + "name": "notes", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "source": { + "name": "source", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "created_at": { + "name": "created_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "(current_timestamp)" + }, + "updated_at": { + "name": "updated_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "(current_timestamp)" + } + }, + "indexes": { + "ipam_entries_ip_address_unique": { + "name": "ipam_entries_ip_address_unique", + "columns": [ + "ip_address" + ], + "isUnique": true + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "maintenance_aliases": { + "name": "maintenance_aliases", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "autoincrement": true + }, + "address": { + "name": "address", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "target_type": { + "name": "target_type", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "target_id": { + "name": "target_id", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "created_at": { + "name": "created_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "(current_timestamp)" + } + }, + "indexes": { + "maintenance_aliases_unique_idx": { + "name": "maintenance_aliases_unique_idx", + "columns": [ + "address", + "target_type", + "target_id" + ], + "isUnique": true + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "maintenance_windows": { + "name": "maintenance_windows", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "autoincrement": true + }, + "target_type": { + "name": "target_type", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "target_id": { + "name": "target_id", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "reason": { + "name": "reason", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "started_at": { + "name": "started_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "ends_at": { + "name": "ends_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "created_by": { + "name": "created_by", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "source": { + "name": "source", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "notification_queue": { + "name": "notification_queue", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "autoincrement": true + }, + "title": { + "name": "title", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "message": { + "name": "message", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "created_at": { + "name": "created_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "(current_timestamp)" + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "port_forwards": { + "name": "port_forwards", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "autoincrement": true + }, + "label": { + "name": "label", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "external_port": { + "name": "external_port", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "protocol": { + "name": "protocol", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "'tcp'" + }, + "server_id": { + "name": "server_id", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "destination": { + "name": "destination", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "internal_port": { + "name": "internal_port", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "source": { + "name": "source", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "comment": { + "name": "comment", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "created_at": { + "name": "created_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "(current_timestamp)" + }, + "updated_at": { + "name": "updated_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "(current_timestamp)" + } + }, + "indexes": {}, + "foreignKeys": { + "port_forwards_server_id_servers_id_fk": { + "name": "port_forwards_server_id_servers_id_fk", + "tableFrom": "port_forwards", + "tableTo": "servers", + "columnsFrom": [ + "server_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "scheduled_tasks": { + "name": "scheduled_tasks", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "autoincrement": true + }, + "server_id": { + "name": "server_id", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "schedule_type": { + "name": "schedule_type", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "origin": { + "name": "origin", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "'agent'" + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "command": { + "name": "command", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "schedule_expression": { + "name": "schedule_expression", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "source": { + "name": "source", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "enabled": { + "name": "enabled", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": true + }, + "next_run_at": { + "name": "next_run_at", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "raw_metadata": { + "name": "raw_metadata", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "is_stale": { + "name": "is_stale", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": false + }, + "first_seen_at": { + "name": "first_seen_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "(current_timestamp)" + }, + "last_seen_at": { + "name": "last_seen_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "(current_timestamp)" + } + }, + "indexes": {}, + "foreignKeys": { + "scheduled_tasks_server_id_servers_id_fk": { + "name": "scheduled_tasks_server_id_servers_id_fk", + "tableFrom": "scheduled_tasks", + "tableTo": "servers", + "columnsFrom": [ + "server_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "secrets": { + "name": "secrets", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "autoincrement": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "type": { + "name": "type", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "'generic'" + }, + "description": { + "name": "description", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "expiry_date": { + "name": "expiry_date", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "warn_days": { + "name": "warn_days", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": 30 + }, + "notes": { + "name": "notes", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "check_host": { + "name": "check_host", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "check_port": { + "name": "check_port", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "last_checked_at": { + "name": "last_checked_at", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "last_check_error": { + "name": "last_check_error", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "created_at": { + "name": "created_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "(current_timestamp)" + }, + "updated_at": { + "name": "updated_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "(current_timestamp)" + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "server_links": { + "name": "server_links", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "autoincrement": true + }, + "server_id": { + "name": "server_id", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "label": { + "name": "label", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "url": { + "name": "url", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "created_at": { + "name": "created_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "(current_timestamp)" + } + }, + "indexes": {}, + "foreignKeys": { + "server_links_server_id_servers_id_fk": { + "name": "server_links_server_id_servers_id_fk", + "tableFrom": "server_links", + "tableTo": "servers", + "columnsFrom": [ + "server_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "server_ports": { + "name": "server_ports", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "autoincrement": true + }, + "server_id": { + "name": "server_id", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "port": { + "name": "port", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "protocol": { + "name": "protocol", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "'tcp'" + }, + "label": { + "name": "label", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "comment": { + "name": "comment", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "open": { + "name": "open", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": false + }, + "last_seen_open_at": { + "name": "last_seen_open_at", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "updated_at": { + "name": "updated_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "(current_timestamp)" + } + }, + "indexes": { + "server_ports_unique": { + "name": "server_ports_unique", + "columns": [ + "server_id", + "port", + "protocol" + ], + "isUnique": true + } + }, + "foreignKeys": { + "server_ports_server_id_servers_id_fk": { + "name": "server_ports_server_id_servers_id_fk", + "tableFrom": "server_ports", + "tableTo": "servers", + "columnsFrom": [ + "server_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "servers": { + "name": "servers", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "autoincrement": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "hostname": { + "name": "hostname", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "os_type": { + "name": "os_type", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "'linux'" + }, + "description": { + "name": "description", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "api_token_hash": { + "name": "api_token_hash", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "api_token_prefix": { + "name": "api_token_prefix", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "created_at": { + "name": "created_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "(current_timestamp)" + }, + "last_seen_at": { + "name": "last_seen_at", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "ip_addresses": { + "name": "ip_addresses", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "cpu_model": { + "name": "cpu_model", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "cpu_cores": { + "name": "cpu_cores", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "cpu_load_percent": { + "name": "cpu_load_percent", + "type": "real", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "mem_total_bytes": { + "name": "mem_total_bytes", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "mem_used_bytes": { + "name": "mem_used_bytes", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "disks": { + "name": "disks", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "listening_ports": { + "name": "listening_ports", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "last_port_scan": { + "name": "last_port_scan", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "tags": { + "name": "tags", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "proxmox_integration_id": { + "name": "proxmox_integration_id", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "proxmox_node": { + "name": "proxmox_node", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "proxmox_guest_type": { + "name": "proxmox_guest_type", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "proxmox_vmid": { + "name": "proxmox_vmid", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "hide_proxmox_link": { + "name": "hide_proxmox_link", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": false + } + }, + "indexes": {}, + "foreignKeys": { + "servers_proxmox_integration_id_integrations_id_fk": { + "name": "servers_proxmox_integration_id_integrations_id_fk", + "tableFrom": "servers", + "tableTo": "integrations", + "columnsFrom": [ + "proxmox_integration_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "settings": { + "name": "settings", + "columns": { + "key": { + "name": "key", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "value": { + "name": "value", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "updated_at": { + "name": "updated_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "(current_timestamp)" + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "tag_definitions": { + "name": "tag_definitions", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "autoincrement": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "color": { + "name": "color", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "created_at": { + "name": "created_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "(current_timestamp)" + } + }, + "indexes": { + "tag_definitions_name_unique": { + "name": "tag_definitions_name_unique", + "columns": [ + "name" + ], + "isUnique": true + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "users": { + "name": "users", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "autoincrement": true + }, + "oidc_sub": { + "name": "oidc_sub", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "email": { + "name": "email", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "role": { + "name": "role", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "'viewer'" + }, + "created_at": { + "name": "created_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "(current_timestamp)" + }, + "last_login_at": { + "name": "last_login_at", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + } + }, + "indexes": { + "users_oidc_sub_unique": { + "name": "users_oidc_sub_unique", + "columns": [ + "oidc_sub" + ], + "isUnique": true + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + } + }, + "views": {}, + "enums": {}, + "_meta": { + "schemas": {}, + "tables": {}, + "columns": {} + }, + "internal": { + "indexes": {} + } +} \ No newline at end of file diff --git a/server/drizzle/meta/_journal.json b/server/drizzle/meta/_journal.json index f94fb7b..cafffb3 100644 --- a/server/drizzle/meta/_journal.json +++ b/server/drizzle/meta/_journal.json @@ -127,6 +127,13 @@ "when": 1791754643361, "tag": "0017_yummy_lady_ursula", "breakpoints": true + }, + { + "idx": 18, + "version": "6", + "when": 1791757001527, + "tag": "0018_even_marvel_zombies", + "breakpoints": true } ] } \ No newline at end of file diff --git a/server/src/db/schema.ts b/server/src/db/schema.ts index 0f9b798..4f2b796 100644 --- a/server/src/db/schema.ts +++ b/server/src/db/schema.ts @@ -76,6 +76,23 @@ export const maintenanceWindows = sqliteTable("maintenance_windows", { source: text("source"), }); +// An address that Uptime Kuma's monitors use for a thing this app knows by another address (a Tailscale IP for a host added by its LAN +// address, say), so a maintenance window in Uptime Kuma lands on the right server, integration or DNS provider. The address is stored as a +// bare lowercase host. +export const maintenanceAliases = sqliteTable( + "maintenance_aliases", + { + id: integer("id").primaryKey({ autoIncrement: true }), + address: text("address").notNull(), + targetType: text("target_type").$type().notNull(), + targetId: integer("target_id").notNull(), // polymorphic, like maintenance_windows — no FK; an alias whose target was deleted is ignored + createdAt: text("created_at") + .notNull() + .default(sql`(current_timestamp)`), + }, + (table) => [uniqueIndex("maintenance_aliases_unique_idx").on(table.address, table.targetType, table.targetId)], +); + // ─── Settings (key/value) ─────────────────────────────────────────────────── export const settings = sqliteTable("settings", { diff --git a/server/src/integrations/uptimekuma/adapter.ts b/server/src/integrations/uptimekuma/adapter.ts index b179a8f..2dc46a6 100644 --- a/server/src/integrations/uptimekuma/adapter.ts +++ b/server/src/integrations/uptimekuma/adapter.ts @@ -111,15 +111,17 @@ export function monitorsFromSamples(samples: PromSample[]): UptimeKumaMonitor[] uptime: Partial>; } const byId = new Map(); + // Uptime Kuma writes the text "null" for a field a monitor doesn't have; that is no address. + const present = (v: string | undefined): string => (v === undefined || v === "null" || v === "undefined" ? "" : v); const get = (id: string, labels: Record) => { let acc = byId.get(id); if (!acc) { acc = { name: labels.monitor_name ?? id, type: labels.monitor_type ?? "unknown", - url: labels.monitor_url ?? "", - hostname: labels.monitor_hostname ?? "", - port: labels.monitor_port ?? "", + url: present(labels.monitor_url), + hostname: present(labels.monitor_hostname), + port: present(labels.monitor_port), status: "unknown", responseTimeMs: null, certDaysRemaining: null, diff --git a/server/src/routes/maintenance.ts b/server/src/routes/maintenance.ts index c1b66e4..139e0ac 100644 --- a/server/src/routes/maintenance.ts +++ b/server/src/routes/maintenance.ts @@ -1,8 +1,9 @@ import { Router } from "express"; -import { eq } from "drizzle-orm"; +import { and, eq } from "drizzle-orm"; import { z } from "zod"; import { db } from "../db/client.js"; -import { dnsProviders, integrations, maintenanceTargetTypes, maintenanceWindows, servers } from "../db/schema.js"; +import { dnsProviders, integrations, maintenanceAliases, maintenanceTargetTypes, maintenanceWindows, servers } from "../db/schema.js"; +import { hostOfAddress } from "../services/kumaTargets.js"; import { requireAuth, requireRole } from "../auth/middleware.js"; import { recordAudit } from "../services/audit.js"; import { describeTarget, listActiveWindows, listUpcomingWindows, startOrExtendWindow } from "../services/maintenance.js"; @@ -38,7 +39,13 @@ maintenanceRouter.get("/", asyncHandler(async (_req, res) => { db.select({ id: integrations.id, name: integrations.name, type: integrations.type }).from(integrations).orderBy(integrations.name), db.select({ id: dnsProviders.id, name: dnsProviders.name, providerType: dnsProviders.providerType }).from(dnsProviders).orderBy(dnsProviders.name), ]); - res.json({ windows, upcoming, targets: { servers: serverRows, integrations: integrationRows, dnsProviders: providerRows } }); + const aliases = []; + for (const a of await db.select().from(maintenanceAliases).orderBy(maintenanceAliases.address)) { + const target = await describeTarget(a.targetType, a.targetId); + if (!target) continue; // target was deleted — the alias has nothing left to point at + aliases.push({ id: a.id, address: a.address, targetType: a.targetType, targetId: a.targetId, targetName: target.name, targetKind: target.kind }); + } + res.json({ windows, upcoming, aliases, targets: { servers: serverRows, integrations: integrationRows, dnsProviders: providerRows } }); })); const startSchema = z.object({ @@ -145,6 +152,59 @@ maintenanceRouter.post("/import/uptimekuma", requireRole("operator"), asyncHandl res.json({ imported, unmatched, monitorsInMaintenance: inMaintenance.length, monitorsChecked: monitors.length }); })); +const aliasSchema = z.object({ + address: z.string().min(1).max(253), + targetType: z.enum(maintenanceTargetTypes), + targetId: z.number().int().positive(), +}); + +maintenanceRouter.post("/aliases", requireRole("operator"), asyncHandler(async (req, res) => { + const parsed = aliasSchema.safeParse(req.body); + if (!parsed.success) { + return res.status(400).json({ error: "invalid_body", message: "Give an address and say what it belongs to.", details: parsed.error.flatten() }); + } + const host = hostOfAddress(parsed.data.address); + if (!host) { + return res.status(400).json({ error: "invalid_address", message: "That isn't an address — use a host name or IP, like 100.64.0.7 or pve.example.lan." }); + } + const target = await describeTarget(parsed.data.targetType, parsed.data.targetId); + if (!target) return res.status(404).json({ error: "not_found", message: "That target doesn't exist." }); + + const [existing] = await db + .select() + .from(maintenanceAliases) + .where(and(eq(maintenanceAliases.address, host), eq(maintenanceAliases.targetType, parsed.data.targetType), eq(maintenanceAliases.targetId, parsed.data.targetId))) + .limit(1); + if (existing) return res.status(409).json({ error: "exists", message: `${host} is already an alias of ${target.name}.` }); + + const [created] = await db.insert(maintenanceAliases).values({ address: host, targetType: parsed.data.targetType, targetId: parsed.data.targetId }).returning(); + await recordAudit({ + actor: req.currentUser!, + category: "maintenance", + action: "alias_add", + targetType: parsed.data.targetType, + targetId: parsed.data.targetId, + detail: { name: target.name, address: host }, + }); + res.status(201).json({ alias: { ...created, targetName: target.name, targetKind: target.kind } }); +})); + +maintenanceRouter.delete("/aliases/:id", requireRole("operator"), asyncHandler(async (req, res) => { + const id = Number(req.params.id); + const [existing] = await db.select().from(maintenanceAliases).where(eq(maintenanceAliases.id, id)).limit(1); + if (!existing) return res.status(404).json({ error: "not_found" }); + await db.delete(maintenanceAliases).where(eq(maintenanceAliases.id, id)); + await recordAudit({ + actor: req.currentUser!, + category: "maintenance", + action: "alias_remove", + targetType: existing.targetType, + targetId: existing.targetId, + detail: { address: existing.address }, + }); + res.status(204).end(); +})); + const syncUptimeKumaSchema = z.object({ integrationId: z.number().int().positive() }); /** Reads Uptime Kuma's maintenance schedule now and brings the mirrored windows in line with it (the same pass that runs every 15 minutes when switched on). */ diff --git a/server/src/services/kumaMaintenanceSync.ts b/server/src/services/kumaMaintenanceSync.ts index 4c57a28..7c3cd92 100644 --- a/server/src/services/kumaMaintenanceSync.ts +++ b/server/src/services/kumaMaintenanceSync.ts @@ -13,7 +13,7 @@ */ import { eq, like, lt } from "drizzle-orm"; import { db } from "../db/client.js"; -import { dnsProviders, integrations, maintenanceWindows, servers, type MaintenanceTargetType } from "../db/schema.js"; +import { dnsProviders, integrations, maintenanceAliases, maintenanceWindows, servers, type MaintenanceTargetType } from "../db/schema.js"; import { loadIntegrationConfig } from "../integrations/loadIntegration.js"; import { createUptimeKumaAdapter } from "../integrations/uptimekuma/adapter.js"; import type { KumaMaintenance } from "../integrations/uptimekuma/maintenanceFeed.js"; @@ -232,6 +232,16 @@ export async function syncKumaMaintenance(integrationId: number, now: Date = new const host = typeof url === "string" ? hostOfAddress(url) : null; if (host) addressed.push({ type: "dns_provider", id: p.id, name: p.name, host }); } + // Addresses someone has said belong to a server, integration or DNS provider (the Tailscale IP of a host that was added by its LAN address). + const names = new Map(); + for (const r of serverRows) names.set(`server:${r.id}`, r.name); + for (const t of addressed) names.set(`${t.type}:${t.id}`, t.name); + for (const i of await db.select({ id: integrations.id, name: integrations.name }).from(integrations)) names.set(`integration:${i.id}`, i.name); + for (const p of await db.select({ id: dnsProviders.id, name: dnsProviders.name }).from(dnsProviders)) names.set(`dns_provider:${p.id}`, p.name); + for (const a of await db.select().from(maintenanceAliases)) { + const name = names.get(`${a.targetType}:${a.targetId}`); + if (name) addressed.push({ type: a.targetType, id: a.targetId, name, host: a.address }); + } const byMonitor = new Map(monitors.map((m) => [m.id, m])); const { desired, summary } = desiredWindows( @@ -241,7 +251,7 @@ export async function syncKumaMaintenance(integrationId: number, now: Date = new (id) => targetsForHost(byMonitor.get(id)?.target ?? null, matchableServers, addressed), (id) => { const m = byMonitor.get(id); - if (!m) return `monitor #${id}`; + if (!m) return `monitor #${id} (not in Uptime Kuma's metrics — paused?)`; return m.target ? `${m.name} (${m.target}${m.port ? `:${m.port}` : ""})` : `${m.name} (no address)`; }, now, diff --git a/server/test/kumaMaintenanceSync.test.ts b/server/test/kumaMaintenanceSync.test.ts index f445e35..fb1a542 100644 --- a/server/test/kumaMaintenanceSync.test.ts +++ b/server/test/kumaMaintenanceSync.test.ts @@ -23,7 +23,9 @@ process.env.CREDENTIALS_ENCRYPTION_KEY = "ab".repeat(32); const express = (await import("express")).default; const { Server: IOServer } = await import("socket.io"); const { db } = await import("../src/db/client.js"); -const { users, servers, integrations, dnsProviders, maintenanceWindows } = await import("../src/db/schema.js"); +const { users, servers, integrations, dnsProviders, maintenanceAliases, maintenanceWindows } = await import("../src/db/schema.js"); +const { eq } = await import("drizzle-orm"); +const { parsePrometheusText, monitorsFromSamples } = await import("../src/integrations/uptimekuma/adapter.js"); const { runMigrations } = await import("../src/db/migrate.js"); const { maintenanceRouter } = await import("../src/routes/maintenance.js"); const { kumaInstant, normalizeMaintenanceList, monitorIdsFromReply } = await import("../src/integrations/uptimekuma/maintenanceFeed.js"); @@ -78,6 +80,21 @@ describe("reading Kuma's lists", () => { }); }); +describe("Uptime Kuma's text \"null\"", () => { + test("a monitor field that is the text null is no address, and the URL is used instead", () => { + const text = [ + 'monitor_status{monitor_id="1",monitor_name="Site",monitor_type="keyword",monitor_url="https://www.example.com/x",monitor_hostname="null",monitor_port="null"} 1', + 'monitor_status{monitor_id="2",monitor_name="Empty",monitor_type="group",monitor_url="null",monitor_hostname="null",monitor_port="null"} 1', + ].join("\n"); + const monitors = monitorsFromSamples(parsePrometheusText(text)); + const site = monitors.find((m) => m.name === "Site")!; + const empty = monitors.find((m) => m.name === "Empty")!; + assert.equal(site.target, "www.example.com"); + assert.equal(site.port, null); + assert.equal(empty.target, null); + }); +}); + describe("which things a monitor's address belongs to", () => { const serverList = [{ id: 1, name: "elsa", hostname: "elsa.lab.example", ips: ["10.0.0.5", "fd00::5"] }]; const addressed = [ @@ -371,6 +388,54 @@ describe("syncing from a stand-in Uptime Kuma", () => { await sync(); }); + test("an alias sends a monitor on some other address to the thing it belongs to, and removing it takes the window away again", async () => { + await db.delete(maintenanceWindows); + const [felicia] = await db.select().from(integrations).where(eq(integrations.name, "Proxmox Felicia")); + const start = Date.now() + 4 * HOUR; + kuma.list = { "7": daily(start, start + HOUR, { id: 7, title: "Over Tailscale" }) }; + kuma.monitorsOf = { "7": [3] }; // watches 10.9.9.9, which nothing here is called + + assert.equal((await sync()).body.started, 0, "before the alias there is nothing to attach to"); + + const added = await fetch(`${base}/aliases`, { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ address: "https://10.9.9.9:8006/", targetType: "integration", targetId: felicia.id }), + }); + assert.equal(added.status, 201, await added.clone().text()); + const alias = (await added.json()).alias; + assert.equal(alias.address, "10.9.9.9", "stored as a bare host"); + + const { body } = await sync(); + assert.equal(body.started, 1); + assert.deepEqual(body.maintenances[0].targets, [{ type: "integration", name: "Proxmox Felicia" }]); + + const listed = await (await fetch(base)).json(); + assert.equal(listed.aliases.length, 1); + assert.equal(listed.aliases[0].targetName, "Proxmox Felicia"); + + assert.equal((await fetch(`${base}/aliases/${alias.id}`, { method: "DELETE" })).status, 204); + const after = await sync(); + assert.equal(after.body.removed, 1, "with the alias gone, the window that depended on it goes too"); + assert.equal((await listUpcomingWindows()).length, 0); + + kuma.list = { "5": daily(start, start + HOUR) }; + kuma.monitorsOf = { "5": [1, 2, 3] }; + await sync(); + }); + + test("an alias must be a real address, can't be added twice, and must point at something that exists", async () => { + const [felicia] = await db.select().from(integrations).where(eq(integrations.name, "Proxmox Felicia")); + const post = (body: unknown) => + fetch(`${base}/aliases`, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(body) }); + assert.equal((await post({ address: " ", targetType: "integration", targetId: felicia.id })).status, 400); + assert.equal((await post({ address: "10.1.1.1", targetType: "integration", targetId: 99999 })).status, 404); + assert.equal((await post({ address: "10.1.1.1", targetType: "integration", targetId: felicia.id })).status, 201); + assert.equal((await post({ address: "10.1.1.1", targetType: "integration", targetId: felicia.id })).status, 409); + const [row] = await db.select().from(maintenanceAliases); + await fetch(`${base}/aliases/${row.id}`, { method: "DELETE" }); + }); + test("syncing again adds nothing", async () => { const { body } = await sync(); assert.deepEqual([body.started, body.updated, body.removed], [0, 0, 0]); diff --git a/web/src/api/client.ts b/web/src/api/client.ts index 516c4cb..e167217 100644 --- a/web/src/api/client.ts +++ b/web/src/api/client.ts @@ -46,6 +46,16 @@ export interface MaintenanceWindow { source: string | null; } +export interface MaintenanceAlias { + id: number; + /** A bare host: a name or an IP. */ + address: string; + targetType: MaintenanceTargetType; + targetId: number; + targetName: string; + targetKind: string; +} + export interface MaintenanceTargets { servers: { id: number; name: string }[]; integrations: { id: number; name: string; type: string }[]; @@ -1063,7 +1073,10 @@ export const api = { }), }, maintenance: { - list: () => request<{ windows: MaintenanceWindow[]; upcoming: MaintenanceWindow[]; targets: MaintenanceTargets }>("/api/maintenance"), + list: () => request<{ windows: MaintenanceWindow[]; upcoming: MaintenanceWindow[]; aliases: MaintenanceAlias[]; targets: MaintenanceTargets }>("/api/maintenance"), + addAlias: (data: { address: string; targetType: MaintenanceTargetType; targetId: number }) => + request<{ alias: MaintenanceAlias }>("/api/maintenance/aliases", { method: "POST", body: JSON.stringify(data) }), + removeAlias: (id: number) => request(`/api/maintenance/aliases/${id}`, { method: "DELETE" }), start: (data: { targetType: MaintenanceTargetType; targetId: number; minutes: number; reason?: string }) => request<{ window: MaintenanceWindow }>("/api/maintenance", { method: "POST", body: JSON.stringify(data) }), end: (id: number) => request(`/api/maintenance/${id}`, { method: "DELETE" }), diff --git a/web/src/pages/Maintenance.tsx b/web/src/pages/Maintenance.tsx index a1be192..07473b5 100644 --- a/web/src/pages/Maintenance.tsx +++ b/web/src/pages/Maintenance.tsx @@ -3,6 +3,7 @@ import { api, type CurrentUser, type IntegrationSummary, + type MaintenanceAlias, type MaintenanceTargets, type MaintenanceTargetType, type MaintenanceWindow, @@ -32,6 +33,11 @@ export default function Maintenance({ user }: { user: CurrentUser }) { const canEdit = user.role === "admin" || user.role === "operator"; const [windows, setWindows] = useState(null); const [upcoming, setUpcoming] = useState([]); + const [aliases, setAliases] = useState([]); + const [aliasAddress, setAliasAddress] = useState(""); + const [aliasTarget, setAliasTarget] = useState(""); + const [aliasBusy, setAliasBusy] = useState(false); + const [aliasError, setAliasError] = useState(null); const [targets, setTargets] = useState(null); const [error, setError] = useState(null); const [now, setNow] = useState(() => Date.now()); @@ -69,6 +75,7 @@ export default function Maintenance({ user }: { user: CurrentUser }) { .then((res) => { setWindows(res.windows); setUpcoming(res.upcoming ?? []); + setAliases(res.aliases ?? []); setTargets(res.targets); }) .catch((err) => setError(readableError(err))); @@ -104,6 +111,34 @@ export default function Maintenance({ user }: { user: CurrentUser }) { } } + async function addAlias(e: React.FormEvent) { + e.preventDefault(); + const [targetType, id] = aliasTarget.split(":"); + if (!targetType || !id) return; + setAliasError(null); + setAliasBusy(true); + try { + await api.maintenance.addAlias({ address: aliasAddress.trim(), targetType: targetType as MaintenanceTargetType, targetId: Number(id) }); + setAliasAddress(""); + setAliasTarget(""); + load(); + } catch (err) { + setAliasError(readableError(err)); + } finally { + setAliasBusy(false); + } + } + + async function removeAlias(a: MaintenanceAlias) { + setAliasError(null); + try { + await api.maintenance.removeAlias(a.id); + load(); + } catch (err) { + setAliasError(readableError(err)); + } + } + async function end(w: MaintenanceWindow) { setError(null); try { @@ -298,6 +333,11 @@ export default function Maintenance({ user }: { user: CurrentUser }) { {m.targets.length > 0 ? ` → ${m.targets.map((t) => `${t.name} (${t.type === "dns_provider" ? "DNS provider" : t.type})`).join(", ")}` : m.note ? ` — nothing to schedule: ${m.note}` : ""} {m.unmatched.length > 0 && · nothing here for {m.unmatched.join(", ")}} + {m.unmatched.length > 0 && ( +
+ Is one of those yours? Add its address under Address aliases below, saying what it belongs to. +
+ )}
Covers {m.monitorCount} monitor{m.monitorCount === 1 ? "" : "s"} in Uptime Kuma ·{" "} {m.slots.length === 0 @@ -365,6 +405,106 @@ export default function Maintenance({ user }: { user: CurrentUser }) {
)} + {canEdit && kumaIntegrations && kumaIntegrations.length > 0 && ( +
+
+

Address aliases

+
+
+
+
+ Uptime Kuma may watch something by a different address than the one you added it with here — a host + added by its LAN address but monitored over Tailscale, for instance. An alias says that an address + belongs to a server, integration or DNS provider, so the syncing above puts the maintenance window + there. Matching stays exact: only the address you list counts. +
+
+ + setAliasAddress(e.target.value)} + /> +
+
+ + +
+
+ +
+
+ {aliasError && ( +
+
{aliasError}
+
+ )} +
+ {aliases.length > 0 && ( +
+ + + + + + + + + + {aliases.map((a) => ( + + + + + + ))} + +
AddressBelongs toActions
{a.address} + {a.targetName} · {a.targetKind} + + +
+
+ )} +
+ )} + {upcoming.length > 0 && (