Racks, notes and to-do, OS support, suggested dependencies, Nginx Proxy Manager and Home Assistant
- Racks page and a network diagram (UniFi tree with servers, printable) - Notes on servers and devices; To do list shown in Coming up and My work - Agents report the operating system (Linux 7, Windows 6); end-of-life badge and alert - Suggested dependencies from where a service is reached - Nginx Proxy Manager integration: proxy hosts page, certificate and host alerts, suggested checks - Home Assistant integration: watch chosen entities, history chart, limit and unavailable alerts - TimeChart gets a "value" scale that fits negatives; docs updated Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
68b41b5586
commit
18ceed62e8
77 files changed
+20432
-35
No files matched your search
@@ -106,7 +106,7 @@ Authorization: Bearer hlm_p_…
|
||||
|---|---|
|
||||
| `GET /api/v1` | The list of these addresses. |
|
||||
| `GET /api/v1/me` | Whose token this is: the person's name and role, and the token's name and end. |
|
||||
| `GET /api/v1/servers` | Every server: `status` (`online`, `offline`, `unknown`), `lastSeenAt`, `tags`, `ipAddresses`, `cpu`, `memory`, `disks` (with `usedPercent`), `updates`, `hardware`, `url`. Filter with `?tag=` and `?status=`. |
|
||||
| `GET /api/v1/servers` | Every server: `status` (`online`, `offline`, `unknown`), `lastSeenAt`, `tags`, `ipAddresses`, `cpu`, `memory`, `disks` (with `usedPercent`), `updates`, `hardware`, `operatingSystem` (`name`, `version`, `endsOn`, `state`: `ok`, `soon`, `ended` or `unknown`, `daysLeft`; null until the agent says), `url`. Filter with `?tag=` and `?status=`. |
|
||||
| `GET /api/v1/servers/:id` | One server, the same shape. |
|
||||
| `GET /api/v1/servers/:id/metrics?days=` | Its load, memory and disks as lines of points, 1 to 365 days (7 by default). Points over more than 35 days are hours' or days' averages, with `lo` and `hi`. Disks also carry a `trend` and, when rising steadily, `daysToFull`. |
|
||||
| `GET /api/v1/alerts` | What is wrong now: `needingAttention` (critical, warning, acknowledged), `counts`, `couldntCheck`, and `alerts` with `severity`, `category`, `message`, `silenced`, `flapping`, `target`, `causedBy`, `acknowledgement` (who, when, note, until) and `url`. Filter with `?severity=`, `?category=`, `?acknowledged=true|false`, `?silenced=true|false`. |
|
||||
|
||||
+77
-1
@@ -61,6 +61,10 @@ erDiagram
|
||||
integrations ||--o{ network_devices : "cascade"
|
||||
integrations ||--o{ network_segments : "cascade"
|
||||
integrations ||--o{ network_server_links : "cascade"
|
||||
integrations ||--o{ ha_watched : "cascade"
|
||||
integrations ||--o{ ha_entities : "cascade"
|
||||
integrations ||--o{ ha_readings : "cascade"
|
||||
users ||--o{ todos : "assignee (set null)"
|
||||
servers ||--o{ network_server_links : "cascade"
|
||||
integration_credentials ||--o{ dns_providers : "credential (set null)"
|
||||
dns_providers ||--o{ dns_zones_cache : "cascade"
|
||||
@@ -74,7 +78,7 @@ erDiagram
|
||||
integrations ||--o{ alert_tickets : "osTicket used (set null)"
|
||||
```
|
||||
|
||||
Seventeen tables stand alone, with no foreign keys: `settings`, `secrets`,
|
||||
Eighteen tables stand alone, with no foreign keys: `settings`, `notes`, `secrets`,
|
||||
`ipam_entries`, `domains`, `diag_log`, `notification_queue`,
|
||||
`consistency_ignores`, `tag_definitions`, `share_links`, `maintenance_schedules`, `metrics_tokens`, `event_webhooks`, `alert_incidents`, `dependencies`, `endpoint_checks`, `restore_tests`, `heartbeats`. `maintenance_windows` and `maintenance_aliases` also have no
|
||||
foreign key — see [soft references](#soft-references-not-foreign-keys).
|
||||
@@ -238,6 +242,7 @@ what it last reported.
|
||||
| `host_health` | text | JSON `{"failedUnits":[{unit,description}],"smart":[{device,model,healthy,temperatureC,wearPercent}],"zfs":[{name,health,capacityPercent}],"reportedAt":"…"}`. Each part is present only if the agent reported it, and a report that leaves one out leaves it as it was. Null until an agent says anything. Judged by the health check and shown on the server's page. |
|
||||
| `hw_manufacturer`, `hw_model`, `hw_serial` | text | What the machine is, as its firmware says (the agent reports it; placeholders like "To Be Filled By O.E.M." are left out). Each is replaced only when a report has a value, so an agent that can't tell doesn't wipe what it said. Used to fill in the inventory. |
|
||||
| `agent_diagnostics` | text | JSON `{"items":[{check,status,reason?,detail?}],"reportedAt":"…"}`: what the agent says it could not collect (`status` is `unavailable` or `failed`). Replaced by each report that carries the list (an empty list means nothing was missed) and kept when a report doesn't. Turned into words and fixes by the server; never sent to the browser as it is. |
|
||||
| `os_id`, `os_name`, `os_version` | text | The operating system the agent reports: `os_id` is lower case (`ubuntu`, `debian`, `proxmox-ve`, `windows`…), `os_name` is what to show, `os_version` the release (a Windows build number on Windows). Replaced only by a report that says; null until then. The end-of-life dates are not stored: they are a table in the app. |
|
||||
| `agent_version` | integer | Which version of the agent script runs on it, as the agent says in each report. Null until it says; an agent that predates the number counts as version 1. Compared with the scripts the app serves to say which agents are behind. |
|
||||
|
||||
#### `scheduled_tasks`
|
||||
@@ -471,6 +476,12 @@ JSON) plus a few internal bookkeeping rows. Details under
|
||||
|
||||
Internal flag `unifiSync`: a JSON object keyed by integration id, `{ at (ISO — the last *successful* read), ok, error, sites, mode, devices, networks, ssids }`. A controller whose `at` is older than twenty minutes is not judged for offline devices.
|
||||
|
||||
Internal flag `homeAssistantSync`: a JSON object keyed by integration id, `{ at (ISO — the last *successful* read), ok, error, entities }`. A Home Assistant whose `at` is older than twenty minutes is not judged.
|
||||
|
||||
Internal flag `dependencySuggestionsDismissed`: a JSON array of the suggestion keys (`integration:3>server:1`) somebody said *Not that* to.
|
||||
|
||||
Setting `healthChecks.osSupportWarnDays` (default 90, 0–365): how many days before an operating system's end of support it is a warning.
|
||||
|
||||
Internal flag `flapState`: a JSON object keyed by alert key, `{ raises: number[] (epoch ms of raises in the last hour), flapping: boolean, since, lastEvent, message }`. It is what lets a restart remember which alerts are being held as flapping; entries are dropped once an alert has been calm for an hour.
|
||||
|
||||
#### `tag_definitions`
|
||||
@@ -728,6 +739,71 @@ The networks (VLANs) and Wi-Fi names of a UniFi controller, replaced as a whole
|
||||
| `security`, `network_name` | text | A Wi-Fi name's security, and the network its clients land on. |
|
||||
| `updated_at` | text NOT NULL | ISO. |
|
||||
|
||||
#### `notes`
|
||||
|
||||
A free-text note about a server or a network device.
|
||||
|
||||
| Column | Type | Notes |
|
||||
|---|---|---|
|
||||
| `id` | integer PK | |
|
||||
| `target_type`, `target_id` | text, integer NOT NULL | `server` or `device`, and its id. Unique together: one note each. Not a foreign key (a removed thing's note is ignored). |
|
||||
| `body` | text NOT NULL | The text, with the small formatting runbooks use. Saving an empty one deletes the row. |
|
||||
| `updated_by` | text | Who last saved it, as a name. |
|
||||
| `updated_at` | text NOT NULL | ISO. |
|
||||
|
||||
#### `todos`
|
||||
|
||||
The small jobs of **Operations → To do**.
|
||||
|
||||
| Column | Type | Notes |
|
||||
|---|---|---|
|
||||
| `id` | integer PK | |
|
||||
| `title` | text NOT NULL | |
|
||||
| `detail` | text | |
|
||||
| `due_date` | text | `YYYY-MM-DD`. A job with a day, while open, is on the Coming up calendar. |
|
||||
| `assignee_user_id` | integer → `users.id`, **set null** | Who it is handed to. |
|
||||
| `assignee_name` | text | Their name when it was handed over, so it still reads if they are removed. |
|
||||
| `target_type`, `target_id` | text, integer | `server` or `device`, and its id, when the job is about one. Not a foreign key. |
|
||||
| `created_by`, `created_at` | text | |
|
||||
| `done_at`, `done_by` | text | ISO and a name; null while open. |
|
||||
|
||||
#### `ha_watched`
|
||||
|
||||
The Home Assistant entities somebody chose to watch (see INTEGRATIONS.md).
|
||||
|
||||
| Column | Type | Notes |
|
||||
|---|---|---|
|
||||
| `id` | integer PK | |
|
||||
| `integration_id` | integer NOT NULL → `integrations.id`, **cascade** | The Home Assistant. Unique together with `entity_id`. |
|
||||
| `entity_id` | text NOT NULL | `sensor.ups_battery`. |
|
||||
| `name` | text | What to call it here; the entity's own friendly name when null. |
|
||||
| `above`, `below` | real | Limits for a numeric value: an alert when it is over `above` or under `below`. |
|
||||
| `created_at` | text NOT NULL | ISO. |
|
||||
|
||||
#### `ha_entities`
|
||||
|
||||
What the last read said about each watched entity. Primary key (`integration_id`, `entity_id`); `integration_id` is a foreign key with **cascade**. Rows of an entity no longer watched are removed on the next read.
|
||||
|
||||
| Column | Type | Notes |
|
||||
|---|---|---|
|
||||
| `friendly_name`, `unit`, `device_class` | text | |
|
||||
| `state` | text NOT NULL | As Home Assistant says it; `missing` when it is no longer in Home Assistant at all. |
|
||||
| `value` | real | The state as a number, when it is one. |
|
||||
| `last_changed` | text | ISO, from Home Assistant. |
|
||||
| `unavailable_since` | text | ISO. When it was first seen `unavailable`, `unknown` or missing; kept until it speaks again. The alert counts from here. |
|
||||
| `updated_at` | text NOT NULL | ISO. |
|
||||
|
||||
#### `ha_readings`
|
||||
|
||||
A numeric value of a watched entity at a moment, for the chart: one at most every two minutes, deleted after 35 days. `integration_id` is a foreign key with **cascade**; (`integration_id`, `entity_id`, `at`) is indexed.
|
||||
|
||||
| Column | Type | Notes |
|
||||
|---|---|---|
|
||||
| `id` | integer PK | |
|
||||
| `entity_id` | text NOT NULL | |
|
||||
| `at` | text NOT NULL | ISO. |
|
||||
| `value` | real NOT NULL | |
|
||||
|
||||
#### `push_subscriptions`
|
||||
|
||||
A browser or phone that asked for push notifications, one row per device.
|
||||
|
||||
+30
-2
@@ -6,10 +6,10 @@ Homelab Manager (Integrations → Add integration, or DNS → Add
|
||||
provider). All credentials are entered in-app and encrypted at rest —
|
||||
nothing is read from environment variables.
|
||||
|
||||
Every adapter listed here except UniFi performs write actions (starting/stopping
|
||||
Every adapter listed here except UniFi, Nginx Proxy Manager and Home Assistant performs write actions (starting/stopping
|
||||
things, editing DNS records, etc.), not just reads — a read-only
|
||||
credential will fail as soon as you use one of those actions, even if
|
||||
the dashboard views themselves load fine. (UniFi only ever reads.)
|
||||
the dashboard views themselves load fine. (UniFi, Nginx Proxy Manager and Home Assistant only ever read.)
|
||||
|
||||
## Integrations
|
||||
|
||||
@@ -248,6 +248,34 @@ the dashboard views themselves load fine. (UniFi only ever reads.)
|
||||
but nobody has run it against real hardware yet. Every field is read defensively, so one a version renames is left blank rather than breaking the read. If a read fails or comes back empty, the **Diagnostic Log** has
|
||||
what the controller answered; tell us and we'll adjust.
|
||||
|
||||
### Nginx Proxy Manager
|
||||
|
||||
- **Config fields:** NPM's admin URL (the address of its web page, usually port 81), an email and a password, and a checkbox to allow a self-signed certificate.
|
||||
- **Required access:** NPM has no API keys: its API is the one its own web page uses, so the app signs in with an email and password (each read signs in again; nothing is kept). Make a **dedicated user** under
|
||||
*Users* and, under its *Permissions*, give it **View only** on *Proxy Hosts* and *SSL Certificates* — that is all that is read. **Nothing is ever written** to NPM.
|
||||
- **What it does:** when **Network → Proxy hosts** is opened (and for the alerts and the suggested checks, at most once a minute) NPM is asked for its proxy hosts and certificates. Nothing is stored in the database:
|
||||
what is shown is what NPM said a minute ago.
|
||||
- Each host shows its names, where it forwards to — and the server that is, when the address (or name) is one of a server's own, exactly — its certificate and whether it is working.
|
||||
- A certificate that an *enabled* host uses and that ends within 14 days (critical within 3) or has ended is an **expiry** alert; a host NPM says could not be set up (its nginx did not accept the configuration)
|
||||
is a **monitoring** alert. A host that is switched off is left alone. Both link to the Proxy hosts page and can be silenced with maintenance on the integration.
|
||||
- The hosts it serves are offered as **Endpoints → Suggested** checks (a web check on the name people use; https when there is a certificate), linked to the server it forwards to.
|
||||
- If NPM cannot be read, the Alerts page says it could not check, and the integration is raised as failing like any other.
|
||||
- **Not verified against a live NPM** — built from the API its own web page uses (`/api/tokens`, `/api/nginx/proxy-hosts?expand=certificate`, `/api/nginx/certificates`) and tried against a pretend one. Every
|
||||
field is read defensively. If a read fails or comes back empty, the **Diagnostic Log** has what NPM answered.
|
||||
|
||||
### Home Assistant
|
||||
|
||||
- **Config fields:** Home Assistant's address (with the port, usually 8123), a long-lived access token, and a checkbox to allow a self-signed certificate.
|
||||
- **Required access:** read only. Make the token on a user's profile page (*Security → Long-lived access tokens*); the user needs no more rights than a viewer's. Only `GET /api/` and `GET /api/states` are used —
|
||||
**nothing is ever sent** to Home Assistant.
|
||||
- **What it does:** you choose which entities to watch on **Monitoring → Home Assistant** (search by name, then *Watch*; at most 60 per Home Assistant). Every five minutes (and on **Read now**) Home Assistant's
|
||||
entities are read in one call and the watched ones are kept: their state, and for a number a reading for the chart (one at most every two minutes, kept 35 days).
|
||||
- An entity that has been *unavailable* or *unknown* — or has disappeared from Home Assistant — for 15 minutes is an alert. A number past a limit you set (*alert when under / over*) is an alert. Both are in the
|
||||
*Monitoring* category, link to the page and can be silenced with maintenance on the integration.
|
||||
- If Home Assistant cannot be read for twenty minutes the watched entities are neither raised nor cleared, the Alerts page says it could not check, and the integration is raised as failing like any other.
|
||||
- A service reached at a server's address (the integration's URL) is offered as a suggested dependency on that server.
|
||||
- **Not verified against a live Home Assistant** — built from its published REST API and tried against a pretend one. Every field is read defensively. If a read fails, the **Diagnostic Log** has what it answered.
|
||||
|
||||
## DNS providers
|
||||
|
||||
### Cloudflare
|
||||
|
||||
@@ -853,6 +853,33 @@ what was there before (the earlier acknowledgement text, the earlier tags, the e
|
||||
- **Servers** (operators; press **Select** beside *Compare*): *Tag…* (add a tag to, or take one off, all of them), *Silence…* (maintenance, optionally with what depends on them too), and *Runs on…* (administrators — which server they run on, or back to working it out from Proxmox).
|
||||
- **Inventory** (operators): *Change where / dates…* (where it is, warranty until, end of support — each left as it is, set, or cleared) and *Print labels* for just those pieces.
|
||||
|
||||
**Operating-system support.** The agents now report which operating system a server runs (Linux agent 7, Windows agent 6; re-run the install one-liner to update). A server's page shows it in a *System* row with a
|
||||
badge — *supported*, *ends in N days* or *support ended* — worked out from a table of end-of-life dates for Ubuntu, Debian, CentOS, RHEL and its rebuilds, Fedora, Alpine, openSUSE Leap, Proxmox VE and Windows
|
||||
(client and server). A server whose system is past its end, or ends within **Settings → Notifications → *Flag an operating system whose support ends within*** (90 days; 0 turns it off) is on the Alerts page in the *Updates* category with a link to the server — a warning once
|
||||
support has ended, a note while it is only coming. The dates are a static table in the app, not looked up: a release it does not know is shown without a badge, never guessed at. The read API gives it as `operatingSystem`.
|
||||
|
||||
**Suggested dependencies.** Under **Map → Dependencies**, a *Suggested* card offers what the app can work out without being told: a service (Gitea, Semaphore, Uptime Kuma, phpIPAM, Home Assistant…) that is reached at a
|
||||
server's own address most likely runs there, so it needs that server. *Add* writes it as an ordinary dependency; *Not that* hides the suggestion (*Show the N you said no to* brings them back). Nothing is written until you add it.
|
||||
|
||||
**Notes and a to-do list.** Every server's page has a *Notes* card, and every network device a *Notes* button (operators), for what is worth remembering: free text with the same small formatting as runbooks
|
||||
(bold, lists, links). **Operations → To do** is a list of the small jobs — "replace the UPS battery", "move the NAS to the new switch" — each with a day it is due, a person it is handed to and a server or device it is
|
||||
about. A job with a day shows in **Coming up**, a job handed to someone in their **My work** (and its badge), and ticking it off, editing and removing it go in the change log. Operators and administrators write,
|
||||
everyone signed in reads.
|
||||
|
||||
**Racks and a network diagram.** **Inventory → Racks** draws each rack from the hardware's *where* (the rack's name) and *rack unit* ("U12", or "U12-13" for a tall piece), from the bottom up, with the units used,
|
||||
the power drawn and a piece that clashes with another in red; hardware that names a rack but no unit is listed beside it. It can be printed. **Network devices → Diagram** draws the UniFi gateway, switches and access
|
||||
points as a tree by what each is connected through, with the servers plugged into each, a red box for a device that is down, and a *Print* button.
|
||||
|
||||
**Nginx Proxy Manager.** Add it under Integrations (a user that may only *view* proxy hosts and certificates is enough; see INTEGRATIONS.md) and **Network → Proxy hosts** lists each name it serves, where it forwards to
|
||||
(and which server that is, when the address is one the app knows), its certificate and whether NPM could set the host up. A certificate in use that ends within 14 days (critical in the last three), or has ended, and a
|
||||
host NPM itself says is not working, are alerts; a host that is switched off is left alone. The hosts it serves are also offered as endpoint checks (Endpoints → Suggested) on the name people really use, linked to the
|
||||
server it forwards to. It only reads.
|
||||
|
||||
**Home Assistant.** Add it under Integrations with a long-lived access token (made at the bottom of your profile page in Home Assistant). **Monitoring → Home Assistant** is where you *choose* the few entities worth an
|
||||
alert — a UPS battery, a freezer's temperature, a pump — not all of them. Each is read every five minutes; a number keeps a history (35 days, with a chart), and you can set a limit under and/or over which it is an alert.
|
||||
An entity that has been *unavailable* (or has gone missing) for 15 minutes is an alert too. If Home Assistant cannot be read for twenty minutes its entities are neither raised nor cleared, and the Alerts page says it
|
||||
could not check. Nothing is ever sent to Home Assistant. Neither integration has been tried against the real thing — see INTEGRATIONS.md.
|
||||
|
||||
**Ports** — each server's detail page has a Ports card for finding free ports
|
||||
and remembering what each one is for. "Scan…" runs a TCP scan of a port range
|
||||
from the app against the server's address (private addresses only, up to
|
||||
|
||||
@@ -104,7 +104,12 @@ even further, to admin only:
|
||||
**Settings search** is part of Settings, which is administrators'.
|
||||
- **Network devices** (Network → Network devices): everyone can see the devices, networks and Wi-Fi names read from UniFi controllers; **Read now** and putting a device under maintenance need operator; adding the UniFi
|
||||
integration itself (a key or login) is an administrator's job, like any integration.
|
||||
- **Dependencies**: everyone can see what needs what; adding and removing one needs operator.
|
||||
- **Dependencies**: everyone can see what needs what; adding and removing one needs operator. So does adding or hiding a *suggested* one.
|
||||
- **To do and Notes** (Operations → To do, a server's *Notes*, a device's *Notes*): everyone signed in reads them; adding, editing, ticking off and removing a job, and writing a note, need operator.
|
||||
- **Racks** (Inventory → Racks) and the **network diagram**: everyone signed in. A rack is made from the hardware's *where* and *rack unit*, which operators edit on the inventory page.
|
||||
- **Proxy hosts** (Network → Proxy hosts): everyone signed in sees what Nginx Proxy Manager serves; **Read now** needs operator; adding the integration (its login) is an administrator's job.
|
||||
- **Home Assistant** (Monitoring → Home Assistant): everyone signed in sees what is watched, its state and history; choosing what to watch, its limits, stopping and **Read now** need operator (the list of all
|
||||
entities is read live, so only operators see it); adding the integration (its token) is an administrator's job.
|
||||
- **Endpoints**: everyone sees the checks and how they are doing; adding, changing and removing one is administrator-only (each makes the server connect somewhere);
|
||||
*Check now* needs operator.
|
||||
- **Restore tests**: everyone sees them on the Backup coverage page; recording one needs operator; removing one is administrator-only.
|
||||
|
||||
@@ -17,7 +17,7 @@ set -euo pipefail
|
||||
# 4 = also reports failed services, disk health (SMART) and ZFS pools, reads pacman, and can update itself when told to
|
||||
# 5 = also says what the machine is (maker, model, serial number), from the firmware, for the inventory
|
||||
# 6 = also says what it could not collect and why (no smartctl, no systemd, a package manager that failed ...), so the server can say what to fix
|
||||
AGENT_VERSION=6
|
||||
AGENT_VERSION=7
|
||||
|
||||
ENV_FILE="${ENV_FILE:-/etc/homelab-manager-agent.env}"
|
||||
if [[ -f "$ENV_FILE" ]]; then
|
||||
@@ -356,6 +356,30 @@ collect_hardware() {
|
||||
'{} | (if $maker != "" then . + {manufacturer: $maker} else . end) | (if $model != "" then . + {model: $model} else . end) | (if $serial != "" then . + {serial: $serial} else . end)') || HARDWARE_JSON="null"
|
||||
}
|
||||
|
||||
# What the machine runs, as /etc/os-release says (OS_RELEASE_FILE is for the tests): the distribution's id and version, so the app can say when that release stops
|
||||
# being supported. Proxmox VE is Debian underneath, so it is asked by its own command. "null" when there is nothing to say.
|
||||
collect_os() {
|
||||
OS_JSON="null"
|
||||
local file="${OS_RELEASE_FILE:-/etc/os-release}" id="" version="" pretty=""
|
||||
if [[ -r "$file" ]]; then
|
||||
id=$(sed -n 's/^ID=//p' "$file" | head -n 1 | tr -d '"' | tr 'A-Z' 'a-z' | head -c 40)
|
||||
version=$(sed -n 's/^VERSION_ID=//p' "$file" | head -n 1 | tr -d '"' | head -c 40)
|
||||
pretty=$(sed -n 's/^PRETTY_NAME=//p' "$file" | head -n 1 | tr -d '"' | head -c 80)
|
||||
fi
|
||||
if command -v pveversion >/dev/null 2>&1; then
|
||||
local pve
|
||||
pve=$(pveversion 2>/dev/null | sed -n 's#^pve-manager/\([0-9]*\.[0-9]*\).*#\1#p' | head -n 1)
|
||||
if [[ -n "$pve" ]]; then
|
||||
id="proxmox-ve"
|
||||
version="$pve"
|
||||
pretty="Proxmox VE $pve"
|
||||
fi
|
||||
fi
|
||||
[[ -n "$id" ]] || return 0
|
||||
OS_JSON=$(jq -n -c --arg id "$id" --arg version "$version" --arg name "$pretty" \
|
||||
'{id: $id} | (if $version != "" then . + {version: $version} else . end) | (if $name != "" then . + {name: $name} else . end)') || OS_JSON="null"
|
||||
}
|
||||
|
||||
# Services (systemd units) that have failed. systemctl is asked the way the timers are, so this works inside the sandbox.
|
||||
collect_failed_units() {
|
||||
FAILED_UNITS_JSON="null"
|
||||
@@ -604,6 +628,8 @@ collect_system_info() {
|
||||
fi
|
||||
HARDWARE_JSON="null"
|
||||
collect_hardware || HARDWARE_JSON="null"
|
||||
OS_JSON="null"
|
||||
collect_os || OS_JSON="null"
|
||||
collect_diagnostics
|
||||
HEALTH_JSON=$(jq -n -c --argjson failed "$FAILED_UNITS_JSON" --argjson smart "$SMART_JSON" --argjson zfs "$ZFS_JSON" \
|
||||
'{} | (if $failed != null then . + {failed_units: $failed} else . end) | (if $smart != null then . + {smart: $smart} else . end) | (if $zfs != null then . + {zfs: $zfs} else . end)') || HEALTH_JSON="{}"
|
||||
@@ -613,6 +639,7 @@ collect_system_info() {
|
||||
--argjson updates "$UPDATES_JSON" \
|
||||
--argjson health "$HEALTH_JSON" \
|
||||
--argjson hardware "$HARDWARE_JSON" \
|
||||
--argjson os "$OS_JSON" \
|
||||
--argjson diagnostics "$DIAGNOSTICS_JSON" \
|
||||
--argjson ip_addresses "$ip_json" \
|
||||
--argjson listening_ports "$ports_json" \
|
||||
@@ -631,6 +658,7 @@ collect_system_info() {
|
||||
updates: $updates,
|
||||
health: $health,
|
||||
hardware: $hardware,
|
||||
os: $os,
|
||||
diagnostics: $diagnostics
|
||||
}')
|
||||
}
|
||||
@@ -652,6 +680,9 @@ if [[ "$MODE" != "report" ]]; then
|
||||
echo "ZFS pools (null means there is no zpool here):"
|
||||
jq . <<<"$ZFS_JSON"
|
||||
collect_hardware || true
|
||||
collect_os || true
|
||||
echo "What it runs (null means there is no os-release to read):"
|
||||
jq . <<<"$OS_JSON"
|
||||
echo "What could not be collected (empty means nothing was missed):"
|
||||
diag_json | jq .
|
||||
self_update
|
||||
|
||||
@@ -36,7 +36,7 @@ $ErrorActionPreference = 'Stop'
|
||||
# 3 = can replace itself with the newer script when an administrator has switched that on in Homelab Manager
|
||||
# 4 = also reports services that have failed, disk health, Storage Spaces pools and what the machine is (maker, model, serial)
|
||||
# 5 = also says what it could not collect and why, so Homelab Manager can say what to fix
|
||||
$script:AgentVersion = 5
|
||||
$script:AgentVersion = 6
|
||||
|
||||
$script:DefaultConfigPath = Join-Path $env:ProgramData 'HomelabManager\agent.json'
|
||||
|
||||
@@ -451,10 +451,19 @@ function Get-SystemInfo($Config = $null) {
|
||||
if (-not $hardware) { Add-Diag 'hardware' 'unavailable' 'not_found' }
|
||||
} catch { Write-Warning "Reading the hardware identity failed. ($($_.Exception.Message))"; Add-Diag 'hardware' 'failed' 'error' $_.Exception.Message }
|
||||
|
||||
# What it runs, so the app can say when that release stops being supported: the product's name and its build number (a build is one release of Windows).
|
||||
# Best-effort and silent: nothing here is worth a note of its own.
|
||||
$osInfo = $null
|
||||
try {
|
||||
$caption = ([string]$os.Caption) -replace '^Microsoft\s+', ''
|
||||
if ($caption.Trim() -and $os.BuildNumber) { $osInfo = [ordered]@{ id = 'windows'; name = $caption.Trim(); version = [string]$os.BuildNumber } }
|
||||
} catch { }
|
||||
|
||||
return [ordered]@{
|
||||
updates = $updates
|
||||
health = $health
|
||||
hardware = $hardware
|
||||
os = $osInfo
|
||||
diagnostics = @($script:Diag)
|
||||
ip_addresses = @($ips)
|
||||
cpu = [ordered]@{ model = [string]($cpus[0].Name).Trim(); cores = [int]$cores; load_percent = $(if ($null -ne $load) { [math]::Round([double]$load, 1) } else { $null }) }
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
ALTER TABLE `servers` ADD `os_id` text;--> statement-breakpoint
|
||||
ALTER TABLE `servers` ADD `os_name` text;--> statement-breakpoint
|
||||
ALTER TABLE `servers` ADD `os_version` text;
|
||||
@@ -0,0 +1,25 @@
|
||||
CREATE TABLE `notes` (
|
||||
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||
`target_type` text NOT NULL,
|
||||
`target_id` integer NOT NULL,
|
||||
`body` text NOT NULL,
|
||||
`updated_by` text,
|
||||
`updated_at` text NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE UNIQUE INDEX `notes_target_idx` ON `notes` (`target_type`,`target_id`);--> statement-breakpoint
|
||||
CREATE TABLE `todos` (
|
||||
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||
`title` text NOT NULL,
|
||||
`detail` text,
|
||||
`due_date` text,
|
||||
`assignee_user_id` integer,
|
||||
`assignee_name` text,
|
||||
`target_type` text,
|
||||
`target_id` integer,
|
||||
`created_by` text,
|
||||
`created_at` text NOT NULL,
|
||||
`done_at` text,
|
||||
`done_by` text,
|
||||
FOREIGN KEY (`assignee_user_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE set null
|
||||
);
|
||||
@@ -0,0 +1,37 @@
|
||||
CREATE TABLE `ha_entities` (
|
||||
`integration_id` integer NOT NULL,
|
||||
`entity_id` text NOT NULL,
|
||||
`friendly_name` text,
|
||||
`state` text NOT NULL,
|
||||
`value` real,
|
||||
`unit` text,
|
||||
`device_class` text,
|
||||
`last_changed` text,
|
||||
`unavailable_since` text,
|
||||
`updated_at` text NOT NULL,
|
||||
PRIMARY KEY(`integration_id`, `entity_id`),
|
||||
FOREIGN KEY (`integration_id`) REFERENCES `integrations`(`id`) ON UPDATE no action ON DELETE cascade
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE `ha_readings` (
|
||||
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||
`integration_id` integer NOT NULL,
|
||||
`entity_id` text NOT NULL,
|
||||
`at` text NOT NULL,
|
||||
`value` real NOT NULL,
|
||||
FOREIGN KEY (`integration_id`) REFERENCES `integrations`(`id`) ON UPDATE no action ON DELETE cascade
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE INDEX `ha_readings_lookup_idx` ON `ha_readings` (`integration_id`,`entity_id`,`at`);--> statement-breakpoint
|
||||
CREATE TABLE `ha_watched` (
|
||||
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||
`integration_id` integer NOT NULL,
|
||||
`entity_id` text NOT NULL,
|
||||
`name` text,
|
||||
`above` real,
|
||||
`below` real,
|
||||
`created_at` text NOT NULL,
|
||||
FOREIGN KEY (`integration_id`) REFERENCES `integrations`(`id`) ON UPDATE no action ON DELETE cascade
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE UNIQUE INDEX `ha_watched_unique` ON `ha_watched` (`integration_id`,`entity_id`);
|
||||
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
@@ -337,6 +337,27 @@
|
||||
"when": 1792519188097,
|
||||
"tag": "0047_eager_dorian_gray",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 48,
|
||||
"version": "6",
|
||||
"when": 1792522721690,
|
||||
"tag": "0048_certain_bullseye",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 49,
|
||||
"version": "6",
|
||||
"when": 1792523247340,
|
||||
"tag": "0049_condemned_warhawk",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 50,
|
||||
"version": "6",
|
||||
"when": 1792523816802,
|
||||
"tag": "0050_useful_bulldozer",
|
||||
"breakpoints": true
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -48,6 +48,9 @@ import { mapRouter } from "./routes/map.js";
|
||||
import { networkRouter } from "./routes/network.js";
|
||||
import { myWorkRouter } from "./routes/myWork.js";
|
||||
import { apiTokensRouter } from "./routes/apiTokens.js";
|
||||
import { notesRouter, todosRouter } from "./routes/todos.js";
|
||||
import { npmRouter } from "./routes/npm.js";
|
||||
import { homeAssistantRouter } from "./routes/homeAssistant.js";
|
||||
import { publicApiRouter } from "./routes/publicApi.js";
|
||||
import { generatorRouter } from "./routes/generator.js";
|
||||
import { alertTicketsRouter } from "./routes/alertTickets.js";
|
||||
@@ -170,6 +173,10 @@ export function createApp() {
|
||||
app.use("/api/map", mapRouter);
|
||||
app.use("/api/network", networkRouter);
|
||||
app.use("/api/my-work", myWorkRouter);
|
||||
app.use("/api/npm", npmRouter);
|
||||
app.use("/api/home-assistant", homeAssistantRouter);
|
||||
app.use("/api/todos", todosRouter);
|
||||
app.use("/api/notes", notesRouter);
|
||||
app.use("/api/generator", generatorRouter);
|
||||
app.use("/api/alert-tickets", alertTicketsRouter);
|
||||
|
||||
|
||||
@@ -471,6 +471,12 @@ export const servers = sqliteTable("servers", {
|
||||
// not in this list, and not noted on the Ports page, is a "new port" (services/newPorts.ts). Null until the first report that carries ports.
|
||||
portBaseline: text("port_baseline"),
|
||||
|
||||
// What the machine runs, as its agent says (agent 7 and later): the distribution's id ("ubuntu", "debian", "proxmox-ve", "windows"), its pretty name and its release
|
||||
// (for Windows, the build number). What services/osLifecycle.ts works out when its support ends from.
|
||||
osId: text("os_id"),
|
||||
osName: text("os_name"),
|
||||
osVersion: text("os_version"),
|
||||
|
||||
// What the machine is, as its firmware says (agents report it): maker, model and serial number. Used to fill in the inventory.
|
||||
hwManufacturer: text("hw_manufacturer"),
|
||||
hwModel: text("hw_model"),
|
||||
@@ -536,6 +542,8 @@ export const integrationTypes = [
|
||||
"pbs",
|
||||
"osticket",
|
||||
"unifi",
|
||||
"npm",
|
||||
"homeassistant",
|
||||
] as const;
|
||||
export type IntegrationType = (typeof integrationTypes)[number];
|
||||
|
||||
@@ -1088,6 +1096,94 @@ export const metricsTokens = sqliteTable("metrics_tokens", {
|
||||
lastUsedAt: text("last_used_at"), // ISO
|
||||
});
|
||||
|
||||
// ─── Home Assistant: the few things somebody chose to watch ─────────────────
|
||||
|
||||
// An entity (a sensor, a binary sensor, a battery, a UPS) that somebody chose to watch on a Home Assistant integration, and the limits that make it an alert.
|
||||
export const haWatched = sqliteTable(
|
||||
"ha_watched",
|
||||
{
|
||||
id: integer("id").primaryKey({ autoIncrement: true }),
|
||||
integrationId: integer("integration_id")
|
||||
.notNull()
|
||||
.references(() => integrations.id, { onDelete: "cascade" }),
|
||||
entityId: text("entity_id").notNull(), // "sensor.ups_battery"
|
||||
name: text("name"), // what to call it here; the entity's own friendly name when null
|
||||
above: real("above"), // an alert when a numeric value is above this
|
||||
below: real("below"), // …or below this
|
||||
createdAt: text("created_at").notNull(),
|
||||
},
|
||||
(t) => [uniqueIndex("ha_watched_unique").on(t.integrationId, t.entityId)],
|
||||
);
|
||||
|
||||
// What the last read said about each watched entity. `unavailableSince` is when it was first seen "unavailable" or "unknown", kept until it is not.
|
||||
export const haEntities = sqliteTable(
|
||||
"ha_entities",
|
||||
{
|
||||
integrationId: integer("integration_id")
|
||||
.notNull()
|
||||
.references(() => integrations.id, { onDelete: "cascade" }),
|
||||
entityId: text("entity_id").notNull(),
|
||||
friendlyName: text("friendly_name"),
|
||||
state: text("state").notNull(),
|
||||
value: real("value"), // the state as a number, when it is one
|
||||
unit: text("unit"),
|
||||
deviceClass: text("device_class"),
|
||||
lastChanged: text("last_changed"),
|
||||
unavailableSince: text("unavailable_since"),
|
||||
updatedAt: text("updated_at").notNull(),
|
||||
},
|
||||
(t) => [primaryKey({ columns: [t.integrationId, t.entityId] })],
|
||||
);
|
||||
|
||||
// A numeric value of a watched entity at a moment, for the chart: one every few minutes, kept 35 days.
|
||||
export const haReadings = sqliteTable(
|
||||
"ha_readings",
|
||||
{
|
||||
id: integer("id").primaryKey({ autoIncrement: true }),
|
||||
integrationId: integer("integration_id")
|
||||
.notNull()
|
||||
.references(() => integrations.id, { onDelete: "cascade" }),
|
||||
entityId: text("entity_id").notNull(),
|
||||
at: text("at").notNull(), // ISO
|
||||
value: real("value").notNull(),
|
||||
},
|
||||
(t) => [index("ha_readings_lookup_idx").on(t.integrationId, t.entityId, t.at)],
|
||||
);
|
||||
|
||||
// ─── Notes and the to-do list ───────────────────────────────────────────────
|
||||
|
||||
// Free-form notes about a server or a network device — what is it, where does it sit, what did I do to it last time — one text per thing, drawn with the same small
|
||||
// formatting as a runbook. Polymorphic like runbooks and maintenance windows, so no foreign key: the note of something removed is simply not shown.
|
||||
export const notes = sqliteTable(
|
||||
"notes",
|
||||
{
|
||||
id: integer("id").primaryKey({ autoIncrement: true }),
|
||||
targetType: text("target_type").$type<"server" | "device">().notNull(),
|
||||
targetId: integer("target_id").notNull(),
|
||||
body: text("body").notNull(),
|
||||
updatedBy: text("updated_by"),
|
||||
updatedAt: text("updated_at").notNull(), // ISO
|
||||
},
|
||||
(t) => [uniqueIndex("notes_target_idx").on(t.targetType, t.targetId)],
|
||||
);
|
||||
|
||||
// Things to do: the small jobs of a homelab ("replace the UPS battery", "move the NAS to the new switch"), with a day they are due and optionally a person and
|
||||
// a thing they are about. Open ones with a due day are in Coming up; the ones handed to someone are in My work.
|
||||
export const todos = sqliteTable("todos", {
|
||||
id: integer("id").primaryKey({ autoIncrement: true }),
|
||||
title: text("title").notNull(),
|
||||
detail: text("detail"),
|
||||
dueDate: text("due_date"), // YYYY-MM-DD
|
||||
assigneeUserId: integer("assignee_user_id").references(() => users.id, { onDelete: "set null" }),
|
||||
assigneeName: text("assignee_name"), // as it was when handed over, so it still reads if the person is removed
|
||||
targetType: text("target_type").$type<"server" | "device">(),
|
||||
targetId: integer("target_id"),
|
||||
createdBy: text("created_by"),
|
||||
createdAt: text("created_at").notNull(), // ISO
|
||||
doneAt: text("done_at"), // ISO; null while open
|
||||
doneBy: text("done_by"),
|
||||
});
|
||||
|
||||
// ─── Personal API tokens: read-only access to /api/v1 for scripts and Home Assistant ─────
|
||||
|
||||
// One person's own: made by them, shown to them once, and gone when they are. Only a hash of the secret is kept. A token only ever reads (the API has no other verbs),
|
||||
|
||||
@@ -25,6 +25,7 @@ import { initEndpointMonitor } from "./services/endpointMonitor.js";
|
||||
import { initPatchPlans } from "./services/patchPlans.js";
|
||||
import { initFlapMonitor } from "./services/flapMonitor.js";
|
||||
import { initUnifiSync } from "./services/unifiSync.js";
|
||||
import { initHomeAssistantSync } from "./services/homeAssistantSync.js";
|
||||
import { backfillMetricRollups } from "./services/serverMetrics.js";
|
||||
import { backfillEndpointHourly } from "./services/endpointChecks.js";
|
||||
import { initSelfBackupScheduler } from "./services/selfBackupScheduler.js";
|
||||
@@ -71,6 +72,7 @@ initEndpointMonitor();
|
||||
initPatchPlans();
|
||||
initFlapMonitor();
|
||||
initUnifiSync();
|
||||
initHomeAssistantSync();
|
||||
// The hourly and daily figures are built from what is already kept, once, the first time after they were added (in the background: nothing waits for it).
|
||||
void Promise.all([backfillMetricRollups(), backfillEndpointHourly()]).catch((err) => console.error("[history] couldn't build the hourly and daily figures:", err));
|
||||
|
||||
|
||||
@@ -88,6 +88,17 @@ export const INTEGRATION_FIELDS: Partial<Record<IntegrationType, IntegrationFiel
|
||||
{ key: "selfHosted", label: "A self-hosted Network application, not a UniFi OS console (UDM, Cloud Gateway, Cloud Key)", secret: false, type: "checkbox" },
|
||||
{ key: "insecure", label: "Allow self-signed certificate", secret: false, type: "checkbox" },
|
||||
],
|
||||
npm: [
|
||||
{ key: "url", label: "Nginx Proxy Manager URL", secret: false, placeholder: "https://npm.example.lan:81 (its admin page)" },
|
||||
{ key: "email", label: "Email of a user that may view", secret: false, placeholder: "a dedicated user with view-only access to proxy hosts and certificates" },
|
||||
{ key: "password", label: "Password", secret: true, type: "password" },
|
||||
{ key: "insecure", label: "Allow self-signed certificate", secret: false, type: "checkbox" },
|
||||
],
|
||||
homeassistant: [
|
||||
{ key: "url", label: "Home Assistant URL", secret: false, placeholder: "https://homeassistant.example.lan:8123" },
|
||||
{ key: "token", label: "Long-lived access token", secret: true, type: "password", placeholder: "made on your profile page in Home Assistant" },
|
||||
{ key: "insecure", label: "Allow self-signed certificate", secret: false, type: "checkbox" },
|
||||
],
|
||||
osticket: [
|
||||
{ key: "host", label: "Database host", secret: false, placeholder: "osticket-db.example.lan" },
|
||||
{ key: "port", label: "Database port", secret: false, optional: true, placeholder: "3306" },
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
/**
|
||||
* Home Assistant adapter.
|
||||
* Requires config: url, token; optional: insecure
|
||||
*
|
||||
* Home Assistant's REST API takes a long-lived access token (made on a user's profile page, at the bottom) as a bearer token. This only reads: `GET /api/` says it
|
||||
* is running, `GET /api/states` lists every entity with its state and attributes. Which of them are worth watching is chosen in this app (services/homeAssistantSync.ts);
|
||||
* the token can belong to a user with no more rights than a viewer's.
|
||||
*
|
||||
* Endpoints and field names are from Home Assistant's published REST API, and have not been checked against a live instance here; every field is read defensively.
|
||||
*/
|
||||
import { requestJson } from "../httpJson.js";
|
||||
import { withDiagLogging } from "../../services/diagLog.js";
|
||||
|
||||
export interface HomeAssistantConfig {
|
||||
url: string;
|
||||
token: string;
|
||||
insecure?: boolean;
|
||||
}
|
||||
|
||||
export interface HaEntity {
|
||||
entityId: string;
|
||||
/** Its friendly name, or its id. */
|
||||
name: string;
|
||||
state: string;
|
||||
/** The state as a number, when it is one. */
|
||||
value: number | null;
|
||||
unit: string | null;
|
||||
deviceClass: string | null;
|
||||
lastChanged: string | null;
|
||||
/** "unavailable" or "unknown": it exists, but is not saying anything. */
|
||||
unavailable: boolean;
|
||||
}
|
||||
|
||||
export interface HomeAssistantAdapter {
|
||||
ping(): Promise<{ ok: boolean; latencyMs?: number; error?: string }>;
|
||||
listEntities(): Promise<HaEntity[]>;
|
||||
}
|
||||
|
||||
const obj = (v: unknown): Record<string, unknown> => (v !== null && typeof v === "object" && !Array.isArray(v) ? (v as Record<string, unknown>) : {});
|
||||
const str = (v: unknown): string | null => (typeof v === "string" && v.trim() !== "" ? v.trim() : null);
|
||||
|
||||
export function entityFrom(raw: unknown): HaEntity | null {
|
||||
const e = obj(raw);
|
||||
const entityId = str(e.entity_id);
|
||||
if (!entityId) return null;
|
||||
const attrs = obj(e.attributes);
|
||||
const state = typeof e.state === "string" ? e.state : e.state === null || e.state === undefined ? "unknown" : String(e.state);
|
||||
const n = state.trim() === "" ? NaN : Number(state);
|
||||
const changed = str(e.last_changed);
|
||||
return {
|
||||
entityId,
|
||||
name: str(attrs.friendly_name) ?? entityId,
|
||||
state,
|
||||
value: Number.isFinite(n) ? n : null,
|
||||
unit: str(attrs.unit_of_measurement),
|
||||
deviceClass: str(attrs.device_class),
|
||||
lastChanged: changed && Number.isFinite(Date.parse(changed)) ? new Date(changed).toISOString() : null,
|
||||
unavailable: state === "unavailable" || state === "unknown",
|
||||
};
|
||||
}
|
||||
|
||||
export function createHomeAssistantAdapter(config: HomeAssistantConfig): HomeAssistantAdapter {
|
||||
const base = config.url.replace(/\/+$/, "");
|
||||
const insecure = config.insecure === true;
|
||||
|
||||
async function get(path: string): Promise<unknown> {
|
||||
const res = await requestJson(`${base}${path}`, { headers: { Authorization: `Bearer ${config.token}` }, insecure });
|
||||
if (res.status === 401 || res.status === 403) throw new Error("Home Assistant did not accept the token (HTTP " + res.status + ")");
|
||||
if (res.status === 404) throw new Error("Home Assistant has no REST API at this address — check the address and port (8123), and that the api integration is on");
|
||||
if (res.status < 200 || res.status >= 300) throw new Error(`Home Assistant error: HTTP ${res.status}`);
|
||||
if (res.json === null) throw new Error("Home Assistant answered with something that isn't JSON — is the address its own?");
|
||||
return res.json;
|
||||
}
|
||||
|
||||
async function listEntities(): Promise<HaEntity[]> {
|
||||
const states = await get("/api/states");
|
||||
return (Array.isArray(states) ? states : []).map(entityFrom).filter((e): e is HaEntity => e !== null);
|
||||
}
|
||||
|
||||
async function ping(): Promise<{ ok: boolean; latencyMs?: number; error?: string }> {
|
||||
const start = Date.now();
|
||||
try {
|
||||
await get("/api/");
|
||||
return { ok: true, latencyMs: Date.now() - start };
|
||||
} catch (err) {
|
||||
return { ok: false, error: err instanceof Error ? err.message : String(err) };
|
||||
}
|
||||
}
|
||||
|
||||
return withDiagLogging("homeassistant", { ping, listEntities });
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
/**
|
||||
* One small way to ask a web API for JSON, for the integrations that speak plain HTTP: a method, headers, an optional JSON body, a timeout, and — for a controller with a
|
||||
* certificate of its own — a way to accept it. Answers with the status and what was said, parsed if it was JSON; a refused connection or a timeout throws.
|
||||
*/
|
||||
import * as http from "node:http";
|
||||
import * as https from "node:https";
|
||||
|
||||
export interface JsonResponse {
|
||||
status: number;
|
||||
json: unknown;
|
||||
text: string;
|
||||
headers: http.IncomingHttpHeaders;
|
||||
}
|
||||
|
||||
const TIMEOUT_MS = 15_000;
|
||||
|
||||
export function requestJson(url: string, opts: { method?: "GET" | "POST"; headers?: Record<string, string>; body?: unknown; insecure?: boolean; timeoutMs?: number } = {}): Promise<JsonResponse> {
|
||||
return new Promise((resolve, reject) => {
|
||||
let parsed: URL;
|
||||
try {
|
||||
parsed = new URL(url);
|
||||
} catch {
|
||||
return reject(new Error(`"${url}" is not a web address`));
|
||||
}
|
||||
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") return reject(new Error("The address must start with http:// or https://"));
|
||||
const isHttps = parsed.protocol === "https:";
|
||||
const lib = isHttps ? https : http;
|
||||
const payload = opts.body === undefined ? null : JSON.stringify(opts.body);
|
||||
const limit = opts.timeoutMs ?? TIMEOUT_MS;
|
||||
const req = lib.request(
|
||||
{
|
||||
hostname: parsed.hostname,
|
||||
port: parsed.port || (isHttps ? 443 : 80),
|
||||
path: parsed.pathname + parsed.search,
|
||||
method: opts.method ?? "GET",
|
||||
headers: { Accept: "application/json", ...(payload ? { "Content-Type": "application/json", "Content-Length": String(Buffer.byteLength(payload)) } : {}), ...opts.headers },
|
||||
timeout: limit,
|
||||
...(isHttps ? { rejectUnauthorized: !opts.insecure } : {}),
|
||||
},
|
||||
(res) => {
|
||||
let text = "";
|
||||
res.setEncoding("utf8");
|
||||
res.on("data", (chunk) => {
|
||||
text += chunk;
|
||||
});
|
||||
res.on("end", () => {
|
||||
let json: unknown = null;
|
||||
try {
|
||||
json = text ? JSON.parse(text) : null;
|
||||
} catch {
|
||||
// not JSON: the text is still there
|
||||
}
|
||||
resolve({ status: res.statusCode ?? 0, json, text, headers: res.headers });
|
||||
});
|
||||
},
|
||||
);
|
||||
req.on("timeout", () => req.destroy(new Error(`timed out after ${limit / 1000}s`)));
|
||||
req.on("error", reject);
|
||||
if (payload) req.write(payload);
|
||||
req.end();
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
/**
|
||||
* Nginx Proxy Manager adapter.
|
||||
* Requires config: url, email, password; optional: insecure
|
||||
*
|
||||
* NPM has no API keys: its API is the one its own web page uses, and signs in with the administrator's email and password (`POST /api/tokens` answers with a bearer
|
||||
* token, which the rest take). So this logs in for each read — and only ever reads: the proxy hosts (what is proxied to what, with its certificate) and the
|
||||
* certificates. Use a dedicated user with the "view only" permission on proxy hosts and certificates (Users → Permissions) rather than the administrator's own.
|
||||
*
|
||||
* Endpoints and field names are from NPM's own API as the web page uses it (`/api/nginx/proxy-hosts?expand=certificate`, `/api/nginx/certificates`), and have
|
||||
* not been checked against a live NPM here; every field is read defensively, so one a version renames is left blank rather than breaking the read.
|
||||
*/
|
||||
import { requestJson } from "../httpJson.js";
|
||||
import { withDiagLogging } from "../../services/diagLog.js";
|
||||
|
||||
export interface NpmConfig {
|
||||
url: string;
|
||||
email: string;
|
||||
password: string;
|
||||
insecure?: boolean;
|
||||
}
|
||||
|
||||
export interface NpmCertificate {
|
||||
id: number;
|
||||
name: string;
|
||||
provider: string;
|
||||
domains: string[];
|
||||
/** ISO, or null when NPM does not say. */
|
||||
expiresAt: string | null;
|
||||
}
|
||||
|
||||
export interface NpmProxyHost {
|
||||
id: number;
|
||||
domains: string[];
|
||||
forwardScheme: string;
|
||||
forwardHost: string;
|
||||
forwardPort: number | null;
|
||||
enabled: boolean;
|
||||
sslForced: boolean;
|
||||
certificate: { id: number; name: string; expiresAt: string | null } | null;
|
||||
/** NPM says its nginx did not accept this host's configuration. */
|
||||
online: boolean;
|
||||
error: string | null;
|
||||
}
|
||||
|
||||
export interface NpmSnapshot {
|
||||
version: string | null;
|
||||
hosts: NpmProxyHost[];
|
||||
certificates: NpmCertificate[];
|
||||
}
|
||||
|
||||
export interface NpmAdapter {
|
||||
ping(): Promise<{ ok: boolean; latencyMs?: number; error?: string }>;
|
||||
fetchSnapshot(): Promise<NpmSnapshot>;
|
||||
}
|
||||
|
||||
const obj = (v: unknown): Record<string, unknown> => (v !== null && typeof v === "object" && !Array.isArray(v) ? (v as Record<string, unknown>) : {});
|
||||
const str = (v: unknown): string | null => (typeof v === "string" && v.trim() !== "" ? v.trim() : null);
|
||||
const num = (v: unknown): number | null => {
|
||||
const n = typeof v === "number" ? v : typeof v === "string" && v.trim() !== "" ? Number(v) : NaN;
|
||||
return Number.isFinite(n) ? n : null;
|
||||
};
|
||||
const domainsOf = (v: unknown): string[] => (Array.isArray(v) ? v.filter((d): d is string => typeof d === "string" && d !== "") : []);
|
||||
|
||||
/** NPM writes times as "2026-12-31 23:59:59" (UTC) or as ISO; either becomes ISO. */
|
||||
export function isoOf(v: unknown): string | null {
|
||||
const s = str(v);
|
||||
if (!s) return null;
|
||||
const t = Date.parse(/[zZ]|[+-]\d{2}:?\d{2}$/.test(s) ? s : `${s.replace(" ", "T")}Z`);
|
||||
return Number.isFinite(t) ? new Date(t).toISOString() : null;
|
||||
}
|
||||
|
||||
export function certificateFrom(raw: unknown): NpmCertificate | null {
|
||||
const c = obj(raw);
|
||||
const id = num(c.id);
|
||||
if (id === null) return null;
|
||||
return { id, name: str(c.nice_name) ?? domainsOf(c.domain_names)[0] ?? `certificate ${id}`, provider: str(c.provider) ?? "other", domains: domainsOf(c.domain_names), expiresAt: isoOf(c.expires_on) };
|
||||
}
|
||||
|
||||
export function hostFrom(raw: unknown): NpmProxyHost | null {
|
||||
const h = obj(raw);
|
||||
const id = num(h.id);
|
||||
if (id === null) return null;
|
||||
const cert = h.certificate && typeof h.certificate === "object" ? certificateFrom(h.certificate) : null;
|
||||
const meta = obj(h.meta);
|
||||
const err = str(meta.nginx_err);
|
||||
return {
|
||||
id,
|
||||
domains: domainsOf(h.domain_names),
|
||||
forwardScheme: str(h.forward_scheme) ?? "http",
|
||||
forwardHost: str(h.forward_host) ?? "",
|
||||
forwardPort: num(h.forward_port),
|
||||
enabled: h.enabled !== false && h.enabled !== 0,
|
||||
sslForced: h.ssl_forced === true || h.ssl_forced === 1,
|
||||
certificate: cert ? { id: cert.id, name: cert.name, expiresAt: cert.expiresAt } : null,
|
||||
// NPM says "online" unless its own check of the generated configuration failed.
|
||||
online: meta.nginx_online !== false && !err,
|
||||
error: err,
|
||||
};
|
||||
}
|
||||
|
||||
export function createNpmAdapter(config: NpmConfig): NpmAdapter {
|
||||
const base = config.url.replace(/\/+$/, "");
|
||||
const insecure = config.insecure === true;
|
||||
|
||||
async function login(): Promise<string> {
|
||||
const res = await requestJson(`${base}/api/tokens`, { method: "POST", body: { identity: config.email, secret: config.password }, insecure });
|
||||
if (res.status === 401 || res.status === 400 || res.status === 403) throw new Error("Nginx Proxy Manager did not accept the email and password");
|
||||
const token = str(obj(res.json).token);
|
||||
if (res.status < 200 || res.status >= 300 || !token) throw new Error(`Nginx Proxy Manager login failed: HTTP ${res.status}`);
|
||||
return token;
|
||||
}
|
||||
|
||||
async function get(token: string, path: string): Promise<unknown> {
|
||||
const res = await requestJson(`${base}${path}`, { headers: { Authorization: `Bearer ${token}` }, insecure });
|
||||
if (res.status === 401 || res.status === 403) throw new Error(`Nginx Proxy Manager did not allow reading ${path.split("?")[0]} (HTTP ${res.status}) — the user needs view access to proxy hosts and certificates`);
|
||||
if (res.status < 200 || res.status >= 300) throw new Error(`Nginx Proxy Manager error: HTTP ${res.status}`);
|
||||
if (res.json === null) throw new Error("Nginx Proxy Manager answered with something that isn't JSON — is the address its admin page's?");
|
||||
return res.json;
|
||||
}
|
||||
|
||||
async function fetchSnapshot(): Promise<NpmSnapshot> {
|
||||
const token = await login();
|
||||
const [hosts, certs, status] = await Promise.all([get(token, "/api/nginx/proxy-hosts?expand=certificate"), get(token, "/api/nginx/certificates").catch(() => []), get(token, "/api/").catch(() => null)]);
|
||||
const v = obj(obj(status).version);
|
||||
const version = num(v.major) !== null ? `${v.major}.${v.minor ?? 0}.${v.revision ?? 0}` : null;
|
||||
return {
|
||||
version,
|
||||
hosts: (Array.isArray(hosts) ? hosts : []).map(hostFrom).filter((h): h is NpmProxyHost => h !== null),
|
||||
certificates: (Array.isArray(certs) ? certs : []).map(certificateFrom).filter((c): c is NpmCertificate => c !== null),
|
||||
};
|
||||
}
|
||||
|
||||
async function ping(): Promise<{ ok: boolean; latencyMs?: number; error?: string }> {
|
||||
const start = Date.now();
|
||||
try {
|
||||
const token = await login();
|
||||
await get(token, "/api/nginx/proxy-hosts");
|
||||
return { ok: true, latencyMs: Date.now() - start };
|
||||
} catch (err) {
|
||||
return { ok: false, error: err instanceof Error ? err.message : String(err) };
|
||||
}
|
||||
}
|
||||
|
||||
return withDiagLogging("npm", { ping, fetchSnapshot });
|
||||
}
|
||||
@@ -11,6 +11,8 @@ import { createPhpIpamAdapter } from "./phpipam/adapter.js";
|
||||
import { createPbsAdapter } from "./pbs/adapter.js";
|
||||
import { createOsTicketAdapter } from "./osticket/adapter.js";
|
||||
import { createUnifiAdapter } from "./unifi/adapter.js";
|
||||
import { createNpmAdapter } from "./npm/adapter.js";
|
||||
import { createHomeAssistantAdapter } from "./homeassistant/adapter.js";
|
||||
|
||||
export interface PingableAdapter {
|
||||
ping(): Promise<{ ok: boolean; latencyMs?: number; error?: string }>;
|
||||
@@ -47,6 +49,10 @@ export function createIntegrationAdapter(type: IntegrationType, config: Integrat
|
||||
return createOsTicketAdapter(config as any);
|
||||
case "unifi":
|
||||
return createUnifiAdapter(config as any);
|
||||
case "npm":
|
||||
return createNpmAdapter(config as any);
|
||||
case "homeassistant":
|
||||
return createHomeAssistantAdapter(config as any);
|
||||
default:
|
||||
throw new Error(`Integration type "${type}" is not implemented yet`);
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import { recordAudit } from "../services/audit.js";
|
||||
import { invalidateAlertsCache } from "../services/alerts.js";
|
||||
import { dependentsOf, impactOf, loadGraph, nodeKey, nodeTypes, wouldLoop } from "../services/dependencies.js";
|
||||
import { getSettings } from "../services/settingsStore.js";
|
||||
import { loadDependencySuggestions, loadDismissedDependencies, saveDismissedDependencies } from "../services/dependencySuggestions.js";
|
||||
import { getSourceStatus } from "../services/integrationHealthMonitor.js";
|
||||
import { serverStatus, type MapStatus } from "../services/topology.js";
|
||||
import { asyncHandler } from "../utils/asyncHandler.js";
|
||||
@@ -44,6 +45,43 @@ dependenciesRouter.get("/", asyncHandler(async (_req, res) => {
|
||||
});
|
||||
}));
|
||||
|
||||
// Dependencies worth writing, from what the app already knows (an integration reached at a server's own address). Everyone signed in can see them; operators
|
||||
// add or dismiss them, as they would any dependency.
|
||||
dependenciesRouter.get("/suggestions", asyncHandler(async (_req, res) => {
|
||||
res.json(await loadDependencySuggestions());
|
||||
}));
|
||||
|
||||
const keysSchema = z.object({ keys: z.array(z.string().min(1).max(100)).min(1).max(200) });
|
||||
|
||||
dependenciesRouter.post("/suggestions/add", requireRole("operator"), asyncHandler(async (req, res) => {
|
||||
const parsed = keysSchema.safeParse(req.body);
|
||||
if (!parsed.success) return res.status(400).json({ error: "invalid_body" });
|
||||
const { suggestions } = await loadDependencySuggestions();
|
||||
const wanted = new Set(parsed.data.keys);
|
||||
const added: { id: number; from: string; to: string }[] = [];
|
||||
for (const s of suggestions.filter((x) => wanted.has(x.key))) {
|
||||
const [row] = await db.insert(dependencies).values({ fromType: "integration", fromId: s.from.id, toType: "server", toId: s.to.id }).returning();
|
||||
await recordAudit({ actor: req.currentUser!, category: "dependency", action: "create", targetType: "dependency", targetId: row.id, detail: { from: s.from.name, to: s.to.name, fromKey: `integration:${s.from.id}`, toKey: `server:${s.to.id}`, suggested: true } });
|
||||
added.push({ id: row.id, from: s.from.name, to: s.to.name });
|
||||
}
|
||||
if (added.length > 0) invalidateAlertsCache();
|
||||
res.json({ added, ...(await loadDependencySuggestions()) });
|
||||
}));
|
||||
|
||||
dependenciesRouter.post("/suggestions/dismiss", requireRole("operator"), asyncHandler(async (req, res) => {
|
||||
const parsed = keysSchema.safeParse(req.body);
|
||||
if (!parsed.success) return res.status(400).json({ error: "invalid_body" });
|
||||
const dismissed = await loadDismissedDependencies();
|
||||
for (const k of parsed.data.keys) dismissed.add(k);
|
||||
await saveDismissedDependencies(dismissed);
|
||||
res.json(await loadDependencySuggestions());
|
||||
}));
|
||||
|
||||
dependenciesRouter.post("/suggestions/restore", requireRole("operator"), asyncHandler(async (_req, res) => {
|
||||
await saveDismissedDependencies(new Set());
|
||||
res.json(await loadDependencySuggestions());
|
||||
}));
|
||||
|
||||
// What a thing would take with it, and what it needs itself: for the maintenance and update pages, which say so before it is done.
|
||||
dependenciesRouter.get("/impact", asyncHandler(async (req, res) => {
|
||||
const type = nodeTypes.find((t) => t === req.query.type);
|
||||
|
||||
@@ -0,0 +1,137 @@
|
||||
import { Router } from "express";
|
||||
import { and, eq, gte } from "drizzle-orm";
|
||||
import { z } from "zod";
|
||||
import { db } from "../db/client.js";
|
||||
import { haEntities, haReadings, haWatched, integrations } from "../db/schema.js";
|
||||
import { requireAuth, requireRole } from "../auth/middleware.js";
|
||||
import { recordAudit } from "../services/audit.js";
|
||||
import { invalidateAlertsCache } from "../services/alerts.js";
|
||||
import { createHomeAssistantAdapter } from "../integrations/homeassistant/adapter.js";
|
||||
import { loadIntegrationConfig } from "../integrations/loadIntegration.js";
|
||||
import { haProblems, haSyncStatuses, isHaFresh, READING_KEEP_DAYS, syncAllHomeAssistants, syncHomeAssistant } from "../services/homeAssistantSync.js";
|
||||
import { bucketPoints } from "../services/serverMetrics.js";
|
||||
import { asyncHandler } from "../utils/asyncHandler.js";
|
||||
|
||||
export const homeAssistantRouter = Router();
|
||||
homeAssistantRouter.use(requireAuth);
|
||||
|
||||
const idOf = (v: string) => (/^\d+$/.test(v) ? Number(v) : NaN);
|
||||
const DAY_MS = 86_400_000;
|
||||
|
||||
async function haIntegration(id: number) {
|
||||
const [row] = await db.select({ id: integrations.id, name: integrations.name, type: integrations.type }).from(integrations).where(eq(integrations.id, id)).limit(1);
|
||||
return row && row.type === "homeassistant" ? row : null;
|
||||
}
|
||||
|
||||
/** What is watched in each Home Assistant, with what the last read said; everyone signed in can see it. */
|
||||
homeAssistantRouter.get("/", asyncHandler(async (_req, res) => {
|
||||
const [intRows, watched, entities, statuses] = await Promise.all([
|
||||
db.select({ id: integrations.id, name: integrations.name, enabled: integrations.enabled }).from(integrations).where(eq(integrations.type, "homeassistant")),
|
||||
db.select().from(haWatched),
|
||||
db.select().from(haEntities),
|
||||
haSyncStatuses(),
|
||||
]);
|
||||
const now = new Date();
|
||||
res.json({
|
||||
integrations: intRows.map((i) => {
|
||||
const status = statuses[i.id] ?? null;
|
||||
const mine = watched.filter((w) => w.integrationId === i.id).sort((a, b) => (a.name ?? a.entityId).localeCompare(b.name ?? b.entityId));
|
||||
const rows = new Map(entities.filter((e) => e.integrationId === i.id).map((e) => [e.entityId, e]));
|
||||
return {
|
||||
id: i.id,
|
||||
name: i.name,
|
||||
enabled: i.enabled,
|
||||
status,
|
||||
fresh: isHaFresh(status ?? undefined, now.getTime()),
|
||||
watched: mine.map((w) => {
|
||||
const e = rows.get(w.entityId);
|
||||
return {
|
||||
entityId: w.entityId,
|
||||
name: w.name,
|
||||
above: w.above,
|
||||
below: w.below,
|
||||
entity: e ? { friendlyName: e.friendlyName, state: e.state, value: e.value, unit: e.unit, deviceClass: e.deviceClass, lastChanged: e.lastChanged, unavailableSince: e.unavailableSince, updatedAt: e.updatedAt } : null,
|
||||
};
|
||||
}),
|
||||
problems: isHaFresh(status ?? undefined, now.getTime()) ? haProblems(i.id, i.name, mine, rows, now) : [],
|
||||
};
|
||||
}),
|
||||
});
|
||||
}));
|
||||
|
||||
// Every entity Home Assistant has, to pick from: read live, so only those who may change what is watched see them.
|
||||
homeAssistantRouter.get("/:id/entities", requireRole("operator"), asyncHandler(async (req, res) => {
|
||||
const id = idOf(req.params.id);
|
||||
if (!Number.isInteger(id) || !(await haIntegration(id))) return res.status(404).json({ error: "not_found" });
|
||||
const loaded = await loadIntegrationConfig(id);
|
||||
if (!loaded) return res.status(404).json({ error: "not_found" });
|
||||
const q = typeof req.query.q === "string" ? req.query.q.trim().toLowerCase() : "";
|
||||
try {
|
||||
const all = await createHomeAssistantAdapter(loaded.config as never).listEntities();
|
||||
const watched = new Set((await db.select({ entityId: haWatched.entityId }).from(haWatched).where(eq(haWatched.integrationId, id))).map((w) => w.entityId));
|
||||
const found = all.filter((e) => !q || e.entityId.toLowerCase().includes(q) || e.name.toLowerCase().includes(q)).sort((a, b) => a.entityId.localeCompare(b.entityId)).slice(0, 60);
|
||||
res.json({ total: all.length, entities: found.map((e) => ({ entityId: e.entityId, name: e.name, state: e.state, unit: e.unit, deviceClass: e.deviceClass, watched: watched.has(e.entityId) })) });
|
||||
} catch (err) {
|
||||
res.status(502).json({ error: "unreachable", message: err instanceof Error ? err.message : String(err) });
|
||||
}
|
||||
}));
|
||||
|
||||
const watchSchema = z.object({
|
||||
entityId: z.string().regex(/^[a-z_]+\.[A-Za-z0-9_]+$/, "Not an entity id, like sensor.ups_battery").max(120),
|
||||
name: z.string().trim().max(80).nullable().optional(),
|
||||
above: z.number().finite().nullable().optional(),
|
||||
below: z.number().finite().nullable().optional(),
|
||||
});
|
||||
|
||||
// Watch an entity, or change what is said about one already watched (its name, its limits).
|
||||
homeAssistantRouter.put("/:id/watched", requireRole("operator"), asyncHandler(async (req, res) => {
|
||||
const id = idOf(req.params.id);
|
||||
const parsed = watchSchema.safeParse(req.body);
|
||||
if (!Number.isInteger(id) || !(await haIntegration(id))) return res.status(404).json({ error: "not_found" });
|
||||
if (!parsed.success) return res.status(400).json({ error: "invalid_body", message: parsed.error.issues[0]?.message ?? "Not an entity." });
|
||||
const d = parsed.data;
|
||||
if (d.above != null && d.below != null && d.below >= d.above) return res.status(400).json({ error: "invalid_limits", message: "The lower limit has to be below the upper one." });
|
||||
const [count] = [(await db.select({ id: haWatched.id }).from(haWatched).where(eq(haWatched.integrationId, id))).length];
|
||||
const existing = (await db.select().from(haWatched).where(and(eq(haWatched.integrationId, id), eq(haWatched.entityId, d.entityId))).limit(1))[0];
|
||||
if (!existing && count >= 60) return res.status(400).json({ error: "too_many", message: "That is as many as can be watched on one Home Assistant (60)." });
|
||||
const values = { name: d.name?.trim() || null, above: d.above ?? null, below: d.below ?? null };
|
||||
if (existing) await db.update(haWatched).set(values).where(eq(haWatched.id, existing.id));
|
||||
else await db.insert(haWatched).values({ integrationId: id, entityId: d.entityId, ...values, createdAt: new Date().toISOString() });
|
||||
await recordAudit({ actor: req.currentUser!, category: "integration", action: existing ? "ha_watch_changed" : "ha_watch", targetType: "integration", targetId: id, detail: { entity: d.entityId, above: values.above, below: values.below } });
|
||||
const status = await syncHomeAssistant(id);
|
||||
invalidateAlertsCache();
|
||||
res.json({ ok: true, status });
|
||||
}));
|
||||
|
||||
homeAssistantRouter.delete("/:id/watched/:entityId", requireRole("operator"), asyncHandler(async (req, res) => {
|
||||
const id = idOf(req.params.id);
|
||||
const entityId = req.params.entityId;
|
||||
if (!Number.isInteger(id) || !(await haIntegration(id))) return res.status(404).json({ error: "not_found" });
|
||||
const [row] = await db.select().from(haWatched).where(and(eq(haWatched.integrationId, id), eq(haWatched.entityId, entityId))).limit(1);
|
||||
if (!row) return res.status(404).json({ error: "not_found" });
|
||||
await db.delete(haWatched).where(eq(haWatched.id, row.id));
|
||||
await db.delete(haEntities).where(and(eq(haEntities.integrationId, id), eq(haEntities.entityId, entityId)));
|
||||
await db.delete(haReadings).where(and(eq(haReadings.integrationId, id), eq(haReadings.entityId, entityId)));
|
||||
await recordAudit({ actor: req.currentUser!, category: "integration", action: "ha_unwatch", targetType: "integration", targetId: id, detail: { entity: entityId } });
|
||||
invalidateAlertsCache();
|
||||
res.status(204).end();
|
||||
}));
|
||||
|
||||
// The chart of a watched number: up to the 35 days that are kept.
|
||||
homeAssistantRouter.get("/:id/history", asyncHandler(async (req, res) => {
|
||||
const id = idOf(req.params.id);
|
||||
const entity = typeof req.query.entity === "string" ? req.query.entity : "";
|
||||
if (!Number.isInteger(id) || !entity) return res.status(400).json({ error: "invalid_query" });
|
||||
const requested = Number(req.query.days);
|
||||
const days = Number.isFinite(requested) ? Math.min(READING_KEEP_DAYS, Math.max(1, Math.round(requested))) : 7;
|
||||
const to = Date.now();
|
||||
const from = to - days * DAY_MS;
|
||||
const rows = await db.select({ at: haReadings.at, value: haReadings.value }).from(haReadings).where(and(eq(haReadings.integrationId, id), eq(haReadings.entityId, entity), gte(haReadings.at, new Date(from).toISOString()))).orderBy(haReadings.at);
|
||||
res.json({ days, points: bucketPoints(rows.map((r) => ({ t: Date.parse(r.at), v: r.value })), from, to, 96) });
|
||||
}));
|
||||
|
||||
homeAssistantRouter.post("/sync", requireRole("operator"), asyncHandler(async (_req, res) => {
|
||||
const statuses = await syncAllHomeAssistants();
|
||||
invalidateAlertsCache();
|
||||
res.json({ statuses });
|
||||
}));
|
||||
@@ -26,6 +26,7 @@ import { createOsTicketAdapter } from "../integrations/osticket/adapter.js";
|
||||
import { attachMatchedServers, summarizeMonitors, toMatchableServers } from "../services/uptimeKumaMatch.js";
|
||||
import { getSourceStatus } from "../services/integrationHealthMonitor.js";
|
||||
import { removeIntegrationData, syncUnifiIntegration } from "../services/unifiSync.js";
|
||||
import { removeHomeAssistantData, syncHomeAssistant } from "../services/homeAssistantSync.js";
|
||||
import { invalidateAlertsCache } from "../services/alerts.js";
|
||||
import { asyncHandler } from "../utils/asyncHandler.js";
|
||||
import { ID_PATTERNS, isValidId } from "../utils/pathSegment.js";
|
||||
@@ -125,6 +126,7 @@ integrationsRouter.post("/", requireRole("admin"), asyncHandler(async (req, res)
|
||||
|
||||
// A new UniFi controller is read at once, so its equipment is there when the person opens the page (not waited for: a slow controller must not slow this).
|
||||
if (type === "unifi") void syncUnifiIntegration(created.id).then(() => invalidateAlertsCache());
|
||||
if (type === "homeassistant") void syncHomeAssistant(created.id).then(() => invalidateAlertsCache());
|
||||
|
||||
res.status(201).json({
|
||||
integration: {
|
||||
@@ -226,6 +228,7 @@ integrationsRouter.patch("/:id", requireRole("admin"), asyncHandler(async (req,
|
||||
});
|
||||
|
||||
if (updated.type === "unifi" && updated.enabled && (parsed.data.config || parsed.data.enabled === true)) void syncUnifiIntegration(updated.id).then(() => invalidateAlertsCache());
|
||||
if (updated.type === "homeassistant" && updated.enabled && (parsed.data.config || parsed.data.enabled === true)) void syncHomeAssistant(updated.id).then(() => invalidateAlertsCache());
|
||||
|
||||
res.json({
|
||||
integration: {
|
||||
@@ -303,6 +306,7 @@ integrationsRouter.delete("/:id", requireRole("admin"), asyncHandler(async (req,
|
||||
.returning({ id: servers.id });
|
||||
|
||||
if (existing.type === "unifi") await removeIntegrationData(id);
|
||||
if (existing.type === "homeassistant") await removeHomeAssistantData(id);
|
||||
await db.delete(integrations).where(eq(integrations.id, id));
|
||||
if (existing.credentialId) {
|
||||
await db.delete(integrationCredentials).where(eq(integrationCredentials.id, existing.credentialId));
|
||||
|
||||
@@ -6,6 +6,7 @@ import { assetKinds, assets, servers } from "../db/schema.js";
|
||||
import { requireAuth, requireRole } from "../auth/middleware.js";
|
||||
import { recordAudit } from "../services/audit.js";
|
||||
import { isRealDate, lifecycleReport } from "../services/inventory.js";
|
||||
import { buildRacks } from "../services/racks.js";
|
||||
import { blanksToFill, differences, hardwareOf } from "../services/inventoryAutofill.js";
|
||||
import { getSettings, updateSettings } from "../services/settingsStore.js";
|
||||
import { asyncHandler } from "../utils/asyncHandler.js";
|
||||
@@ -88,6 +89,12 @@ inventoryRouter.get("/", asyncHandler(async (_req, res) => {
|
||||
}));
|
||||
|
||||
// What ages out when, for planning what to replace: this year, the next few, later, and what has no date.
|
||||
// The racks, drawn from the hardware that says which unit it sits in and where. Everyone signed in can see it, as they can the hardware.
|
||||
inventoryRouter.get("/racks", asyncHandler(async (_req, res) => {
|
||||
const rows = await db.select().from(assets);
|
||||
res.json({ racks: buildRacks(rows) });
|
||||
}));
|
||||
|
||||
inventoryRouter.get("/lifecycle", asyncHandler(async (req, res) => {
|
||||
const asked = Number(req.query.years);
|
||||
const years = Number.isInteger(asked) && asked >= 1 && asked <= 10 ? asked : 5;
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
import { Router } from "express";
|
||||
import { db } from "../db/client.js";
|
||||
import { servers } from "../db/schema.js";
|
||||
import { requireAuth } from "../auth/middleware.js";
|
||||
import { npmIntegrationIds, npmProblems, readNpm } from "../services/npmReader.js";
|
||||
import { matchServerForTarget } from "../services/uptimeKumaMatch.js";
|
||||
import { asyncHandler } from "../utils/asyncHandler.js";
|
||||
|
||||
export const npmRouter = Router();
|
||||
npmRouter.use(requireAuth);
|
||||
|
||||
/**
|
||||
* What Nginx Proxy Manager proxies: each host with where it forwards to (and which server that is, when the address is one the app knows), its certificate and how it is
|
||||
* doing. Everyone signed in can see it (it is the same picture the alerts and suggested checks draw on). \`?refresh=1\` asks NPM again instead of using the last minute.
|
||||
*/
|
||||
npmRouter.get("/", asyncHandler(async (req, res) => {
|
||||
const force = req.query.refresh === "1";
|
||||
const rows = await db.select({ id: servers.id, name: servers.name, hostname: servers.hostname, ipAddresses: servers.ipAddresses }).from(servers);
|
||||
const matchable = rows.map((s) => {
|
||||
let ips: string[] = [];
|
||||
try {
|
||||
const v = s.ipAddresses ? JSON.parse(s.ipAddresses) : [];
|
||||
ips = Array.isArray(v) ? v.filter((x): x is string => typeof x === "string") : [];
|
||||
} catch {
|
||||
// none
|
||||
}
|
||||
return { id: s.id, name: s.name, hostname: s.hostname, ips };
|
||||
});
|
||||
const out = [];
|
||||
for (const i of await npmIntegrationIds()) {
|
||||
const read = await readNpm(i.id, force);
|
||||
out.push({
|
||||
id: i.id,
|
||||
name: i.name,
|
||||
readAt: read.at,
|
||||
error: read.error,
|
||||
version: read.snapshot?.version ?? null,
|
||||
hosts: (read.snapshot?.hosts ?? []).map((h) => {
|
||||
const server = matchServerForTarget(h.forwardHost, matchable);
|
||||
return { ...h, server: server ? { id: server.id, name: server.name } : null };
|
||||
}),
|
||||
certificates: read.snapshot?.certificates ?? [],
|
||||
problems: read.snapshot ? npmProblems(i.id, i.name, read.snapshot, new Date()) : [],
|
||||
});
|
||||
}
|
||||
res.json({ integrations: out });
|
||||
}));
|
||||
@@ -24,6 +24,7 @@ import { buildServerSummary } from "../services/serverSummary.js";
|
||||
import { activeSubjects } from "../services/maintenance.js";
|
||||
import { invalidateAlertsCache } from "../services/alerts.js";
|
||||
import { getSettings, updateSettings } from "../services/settingsStore.js";
|
||||
import { osSummary } from "../services/osLifecycle.js";
|
||||
|
||||
export const serversRouter = Router();
|
||||
|
||||
@@ -397,7 +398,8 @@ serversRouter.get("/:id/detail", asyncHandler(async (req, res) => {
|
||||
children: [...links2.entries()].filter(([, l]) => l.parentId === id).map(([childId, l]) => ({ id: childId, name: nameOf.get(childId) ?? "", auto: l.auto })).sort((a, b) => a.name.localeCompare(b.name)),
|
||||
};
|
||||
|
||||
res.json({ server: { ...serverOut, tags: parseTags(rawTags), pendingUpdates: parsePendingUpdates(rawUpdates), hostHealth: markIgnoredServices(parseHostHealth(rawHealth), parseThresholds(rawThresholds)?.ignoreServices), thresholds: parseThresholds(rawThresholds) }, diagnostics: describeDiagnostics(parseDiagnostics(rawDiagnostics), { osType: server.osType, lastSeenAt: server.lastSeenAt, agentVersion: server.agentVersion, latest: latestAgentVersions() }), thresholdDefaults: (await getSettings()).healthChecks, ipAddresses, hardware, dnsMatches, links, relations, agentLatest: latestAgentVersions() });
|
||||
const settings = await getSettings();
|
||||
res.json({ os: osSummary(server, new Date(), settings.healthChecks.osSupportWarnDays), server: { ...serverOut, tags: parseTags(rawTags), pendingUpdates: parsePendingUpdates(rawUpdates), hostHealth: markIgnoredServices(parseHostHealth(rawHealth), parseThresholds(rawThresholds)?.ignoreServices), thresholds: parseThresholds(rawThresholds) }, diagnostics: describeDiagnostics(parseDiagnostics(rawDiagnostics), { osType: server.osType, lastSeenAt: server.lastSeenAt, agentVersion: server.agentVersion, latest: latestAgentVersions() }), thresholdDefaults: (await getSettings()).healthChecks, ipAddresses, hardware, dnsMatches, links, relations, agentLatest: latestAgentVersions() });
|
||||
}));
|
||||
|
||||
const tagsSchema = z.object({ tags: z.array(z.string().max(100)).max(50) });
|
||||
|
||||
@@ -144,7 +144,7 @@ const updateSchema = z.object({
|
||||
.partial()
|
||||
.optional(),
|
||||
healthChecks: z
|
||||
.object({ serverOfflineMinutes: z.number().int().min(15).max(10080), diskUsagePercent: z.number().int().min(50).max(99), domainWarnDays: z.number().int().min(1).max(365), diskForecastDays: z.number().int().min(0).max(90), backupStaleHours: z.number().int().min(0).max(2160), restoreTestDays: z.number().int().min(0).max(730), newPortAlerts: z.boolean(), flapRaises: z.number().int().min(0).max(20), deviceOfflineMinutes: z.number().int().min(5).max(1440) })
|
||||
.object({ serverOfflineMinutes: z.number().int().min(15).max(10080), diskUsagePercent: z.number().int().min(50).max(99), domainWarnDays: z.number().int().min(1).max(365), diskForecastDays: z.number().int().min(0).max(90), backupStaleHours: z.number().int().min(0).max(2160), restoreTestDays: z.number().int().min(0).max(730), newPortAlerts: z.boolean(), flapRaises: z.number().int().min(0).max(20), deviceOfflineMinutes: z.number().int().min(5).max(1440), osSupportWarnDays: z.number().int().min(0).max(365) })
|
||||
.partial()
|
||||
.optional(),
|
||||
quietHours: z
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
import { Router } from "express";
|
||||
import { z } from "zod";
|
||||
import { requireAuth, requireRole } from "../auth/middleware.js";
|
||||
import { recordAudit } from "../services/audit.js";
|
||||
import { assignableUsers } from "../services/alertAcks.js";
|
||||
import { createTodo, getNote, listTodos, removeTodo, setNote, TARGET_TYPES, TodoError, updateTodo } from "../services/todos.js";
|
||||
import { asyncHandler } from "../utils/asyncHandler.js";
|
||||
|
||||
/** The to-do list: everyone signed in reads it; operators write. */
|
||||
export const todosRouter = Router();
|
||||
todosRouter.use(requireAuth);
|
||||
|
||||
const idOf = (v: string) => (/^\d+$/.test(v) ? Number(v) : NaN);
|
||||
const label = (u: { name: string | null; email: string | null; oidcSub: string }) => u.name ?? u.email ?? u.oidcSub;
|
||||
|
||||
const fields = {
|
||||
title: z.string(),
|
||||
detail: z.string().nullable().optional(),
|
||||
dueDate: z.string().nullable().optional(),
|
||||
assigneeId: z.number().int().positive().nullable().optional(),
|
||||
targetType: z.enum(TARGET_TYPES).nullable().optional(),
|
||||
targetId: z.number().int().positive().nullable().optional(),
|
||||
};
|
||||
|
||||
todosRouter.get("/", asyncHandler(async (_req, res) => {
|
||||
res.json({ todos: await listTodos(), people: await assignableUsers() });
|
||||
}));
|
||||
|
||||
todosRouter.post("/", requireRole("operator"), asyncHandler(async (req, res) => {
|
||||
const parsed = z.object(fields).safeParse(req.body);
|
||||
if (!parsed.success) return res.status(400).json({ error: "invalid_body", message: "Say what is to be done." });
|
||||
try {
|
||||
const actor = req.currentUser!;
|
||||
const item = await createTodo(parsed.data, label(actor));
|
||||
await recordAudit({ actor, category: "todo", action: "create", targetType: "todo", targetId: item.id, detail: { title: item.title, dueDate: item.dueDate, assignee: item.assignee?.name ?? null, about: item.target?.name ?? null } });
|
||||
res.status(201).json({ todo: item });
|
||||
} catch (err) {
|
||||
if (err instanceof TodoError) return res.status(err.status).json({ error: err.code, message: err.message });
|
||||
throw err;
|
||||
}
|
||||
}));
|
||||
|
||||
todosRouter.patch("/:id", requireRole("operator"), asyncHandler(async (req, res) => {
|
||||
const id = idOf(req.params.id);
|
||||
const parsed = z.object({ ...fields, title: z.string().optional(), done: z.boolean().optional() }).safeParse(req.body);
|
||||
if (!Number.isInteger(id) || !parsed.success) return res.status(400).json({ error: "invalid_body" });
|
||||
try {
|
||||
const actor = req.currentUser!;
|
||||
const result = await updateTodo(id, parsed.data, label(actor));
|
||||
if (!result) return res.status(404).json({ error: "not_found" });
|
||||
const action = parsed.data.done === true && !result.before.doneAt ? "done" : parsed.data.done === false && result.before.doneAt ? "reopen" : "update";
|
||||
await recordAudit({ actor, category: "todo", action, targetType: "todo", targetId: id, detail: { title: result.after.title } });
|
||||
res.json({ todo: result.after });
|
||||
} catch (err) {
|
||||
if (err instanceof TodoError) return res.status(err.status).json({ error: err.code, message: err.message });
|
||||
throw err;
|
||||
}
|
||||
}));
|
||||
|
||||
todosRouter.delete("/:id", requireRole("operator"), asyncHandler(async (req, res) => {
|
||||
const id = idOf(req.params.id);
|
||||
if (!Number.isInteger(id)) return res.status(400).json({ error: "invalid_id" });
|
||||
const removed = await removeTodo(id);
|
||||
if (!removed) return res.status(404).json({ error: "not_found" });
|
||||
await recordAudit({ actor: req.currentUser!, category: "todo", action: "delete", targetType: "todo", targetId: id, detail: { title: removed.title } });
|
||||
res.status(204).end();
|
||||
}));
|
||||
|
||||
/** The notes on servers and devices: everyone reads; operators write. */
|
||||
export const notesRouter = Router();
|
||||
notesRouter.use(requireAuth);
|
||||
|
||||
notesRouter.get("/", asyncHandler(async (req, res) => {
|
||||
const type = z.enum(TARGET_TYPES).safeParse(req.query.targetType);
|
||||
const id = idOf(String(req.query.targetId ?? ""));
|
||||
if (!type.success || !Number.isInteger(id)) return res.status(400).json({ error: "invalid_target" });
|
||||
res.json({ note: await getNote(type.data, id) });
|
||||
}));
|
||||
|
||||
notesRouter.put("/", requireRole("operator"), asyncHandler(async (req, res) => {
|
||||
const parsed = z.object({ targetType: z.enum(TARGET_TYPES), targetId: z.number().int().positive(), body: z.string() }).safeParse(req.body);
|
||||
if (!parsed.success) return res.status(400).json({ error: "invalid_body" });
|
||||
try {
|
||||
const actor = req.currentUser!;
|
||||
const note = await setNote(parsed.data.targetType, parsed.data.targetId, parsed.data.body, label(actor));
|
||||
await recordAudit({ actor, category: "note", action: note ? "save" : "delete", targetType: parsed.data.targetType, targetId: parsed.data.targetId });
|
||||
res.json({ note });
|
||||
} catch (err) {
|
||||
if (err instanceof TodoError) return res.status(err.status).json({ error: err.code, message: err.message });
|
||||
throw err;
|
||||
}
|
||||
}));
|
||||
@@ -9,7 +9,8 @@
|
||||
import { createHash } from "node:crypto";
|
||||
import { and, eq } from "drizzle-orm";
|
||||
import { db } from "../db/client.js";
|
||||
import { assets, domains, endpointChecks, integrations, maintenanceSchedules, patchPlans, secrets, servers } from "../db/schema.js";
|
||||
import { assets, domains, endpointChecks, integrations, maintenanceSchedules, patchPlans, secrets, servers, todos } from "../db/schema.js";
|
||||
import { isNull } from "drizzle-orm";
|
||||
import { collectTailscaleKeyExpiries } from "./tailscaleKeyExpiryScheduler.js";
|
||||
import { describeTarget, listUpcomingWindows } from "./maintenance.js";
|
||||
import { occurrencesBetween, targetsOf } from "./maintenanceSchedules.js";
|
||||
@@ -19,7 +20,7 @@ import { latestTests } from "./restoreTests.js";
|
||||
import { getSettings } from "./settingsStore.js";
|
||||
import { env } from "../env.js";
|
||||
|
||||
export const AGENDA_KINDS = ["secret", "certificate", "domain", "tailscale", "warranty", "support", "maintenance", "restore", "updates"] as const;
|
||||
export const AGENDA_KINDS = ["secret", "certificate", "domain", "tailscale", "warranty", "support", "maintenance", "restore", "updates", "todo"] as const;
|
||||
export type AgendaKind = (typeof AGENDA_KINDS)[number];
|
||||
|
||||
export const KIND_LABELS: Record<AgendaKind, string> = {
|
||||
@@ -32,6 +33,7 @@ export const KIND_LABELS: Record<AgendaKind, string> = {
|
||||
maintenance: "Maintenance",
|
||||
restore: "Restore tests",
|
||||
updates: "Updates waiting",
|
||||
todo: "To do",
|
||||
};
|
||||
|
||||
export interface AgendaItem {
|
||||
@@ -96,6 +98,8 @@ export interface AgendaData {
|
||||
restoreLimitDays: number;
|
||||
/** Servers with updates waiting. */
|
||||
updates: { name: string; count: number; security: number | null; since: string | null }[];
|
||||
/** Open jobs that have a day they are due. */
|
||||
todos: { id: number; title: string; dueDate: string | null; assigneeName: string | null; serverId: number | null }[];
|
||||
notes: string[];
|
||||
}
|
||||
|
||||
@@ -127,6 +131,8 @@ export async function loadAgendaData(now: Date = new Date()): Promise<AgendaData
|
||||
latestTests(),
|
||||
]);
|
||||
|
||||
const todoRows = (await db.select().from(todos).where(isNull(todos.doneAt))).filter((t) => t.dueDate !== null);
|
||||
|
||||
const windows: AgendaData["windows"] = [];
|
||||
for (const w of await listUpcomingWindows(now)) {
|
||||
const target = await describeTarget(w.targetType, w.targetId);
|
||||
@@ -174,6 +180,7 @@ export async function loadAgendaData(now: Date = new Date()): Promise<AgendaData
|
||||
restoreTests: [...tests.values()].map((t) => ({ subjectKey: t.subjectKey, label: t.label, testedAt: t.testedAt, result: t.result })),
|
||||
restoreLimitDays: settings.healthChecks.restoreTestDays,
|
||||
updates,
|
||||
todos: todoRows.map((t) => ({ id: t.id, title: t.title, dueDate: t.dueDate, assigneeName: t.assigneeName, serverId: t.targetType === "server" ? t.targetId : null })),
|
||||
notes,
|
||||
};
|
||||
}
|
||||
@@ -213,6 +220,11 @@ export function agendaFromData(data: AgendaData, now: Date, tz: string, days: nu
|
||||
day(`support:${a.id}`, "support", a.endOfSupport, (past) => ({ title: `Support for ${a.name} ${past ? "ended" : "ends"}`, detail: "time to plan its replacement", link: `/inventory?asset=${a.id}`, serverId: a.serverId }));
|
||||
}
|
||||
|
||||
// Jobs on the to-do list that have a day: an open one that is past due stays until it is done (or for as long as anything past is kept).
|
||||
for (const t of data.todos ?? []) {
|
||||
day(`todo:${t.id}`, "todo", t.dueDate, (past) => ({ title: `${past ? "Overdue" : "To do"}: ${t.title}`, detail: t.assigneeName ? `for ${t.assigneeName}` : null, link: "/todo", serverId: t.serverId }));
|
||||
}
|
||||
|
||||
// A restore test is due a set time after the last good one — only if a time limit is set (Settings → Notifications).
|
||||
if (data.restoreLimitDays > 0) {
|
||||
for (const t of data.restoreTests) {
|
||||
@@ -317,6 +329,7 @@ const GENERIC: Record<AgendaKind, string> = {
|
||||
maintenance: "Planned maintenance",
|
||||
restore: "A restore test is due",
|
||||
updates: "Updates are waiting",
|
||||
todo: "A job is due",
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
* Windows: also reports failed services, disk health and Storage Spaces pools, and what the machine is (maker, model, serial)
|
||||
* 5 — Linux: also says what the machine is (maker, model, serial), for the inventory
|
||||
* 6 — Linux (and Windows 5): also says what it could not collect and why, so the server can say what to fix
|
||||
* 7 — Linux (and Windows 6): also says which operating system and release it runs, so the server can say when that release stops being supported
|
||||
*/
|
||||
import { createHash } from "node:crypto";
|
||||
import { readFileSync } from "node:fs";
|
||||
|
||||
@@ -11,8 +11,11 @@
|
||||
*/
|
||||
import { eq } from "drizzle-orm";
|
||||
import { db } from "../db/client.js";
|
||||
import { endpointChecks, heartbeats, integrations, networkDevices, secrets, servers } from "../db/schema.js";
|
||||
import { endpointChecks, haEntities, haWatched, heartbeats, integrations, networkDevices, secrets, servers } from "../db/schema.js";
|
||||
import { disksFillingSoon, getForecastSeries } from "./serverMetrics.js";
|
||||
import { daysOfSupportLeft, lifecycleOf } from "./osLifecycle.js";
|
||||
import { npmIntegrationIds, npmProblems, readNpm } from "./npmReader.js";
|
||||
import { haProblems, haSyncStatuses, isHaFresh } from "./homeAssistantSync.js";
|
||||
import { diskLimit, effectiveThresholds, parseThresholds } from "./serverThresholds.js";
|
||||
import { getBackupCoverage, staleBackups } from "./backupCoverage.js";
|
||||
import { recordFromReport } from "./incidents.js";
|
||||
@@ -65,6 +68,8 @@ const SOURCES: Record<string, { label: string; link: string }> = {
|
||||
uptimekuma: { label: "Uptime Kuma", link: "/uptime-kuma" },
|
||||
osticket: { label: "osTicket", link: "/osticket" },
|
||||
unifi: { label: "UniFi", link: "/network/devices" },
|
||||
npm: { label: "Nginx Proxy Manager", link: "/proxy-hosts" },
|
||||
homeassistant: { label: "Home Assistant", link: "/home-assistant" },
|
||||
secrets: { label: "Secrets", link: "/secrets" },
|
||||
domains: { label: "Domains", link: "/dns?view=domains" },
|
||||
endpoint: { label: "Endpoint", link: "/endpoints" },
|
||||
@@ -515,6 +520,67 @@ export async function collectAlerts(): Promise<AlertsReport> {
|
||||
}
|
||||
}),
|
||||
|
||||
// Certificates in use by Nginx Proxy Manager that are running out, and hosts it could not set up.
|
||||
check("Nginx Proxy Manager", async () => {
|
||||
for (const i of await npmIntegrationIds()) {
|
||||
const read = await readNpm(i.id);
|
||||
if (!read.snapshot) {
|
||||
cannotCheck(`Nginx Proxy Manager “${i.name}”`, read.error ?? "couldn't be read", "Nginx Proxy Manager");
|
||||
continue;
|
||||
}
|
||||
for (const p of npmProblems(i.id, i.name, read.snapshot, new Date())) {
|
||||
push({ category: p.kind === "certificate" ? "expiry" : "monitoring", severity: p.severity, type: "npm", message: p.message, key: p.key, link: "/proxy-hosts", silenced: silencedIntegration(i.id), target: integrationTarget(i.id) });
|
||||
}
|
||||
}
|
||||
}),
|
||||
|
||||
// What was chosen to watch in Home Assistant: an entity that has said nothing for a while, or a number past its limit.
|
||||
check("Home Assistant", async () => {
|
||||
const statuses = await haSyncStatuses();
|
||||
const nowMs = Date.now();
|
||||
const watched = await db.select().from(haWatched);
|
||||
if (watched.length === 0) return;
|
||||
const entities = await db.select().from(haEntities);
|
||||
for (const i of intRows.filter((r) => r.type === "homeassistant" && r.enabled)) {
|
||||
const mine = watched.filter((w) => w.integrationId === i.id);
|
||||
if (mine.length === 0) continue;
|
||||
if (!isHaFresh(statuses[i.id], nowMs)) {
|
||||
cannotCheck(`Home Assistant “${i.name}”`, statuses[i.id]?.error ?? "hasn't been read yet", "Home Assistant");
|
||||
continue;
|
||||
}
|
||||
const rows = new Map(entities.filter((e) => e.integrationId === i.id).map((e) => [e.entityId, e]));
|
||||
for (const p of haProblems(i.id, i.name, mine, rows, new Date(nowMs))) {
|
||||
push({ category: "monitoring", severity: p.severity, type: "homeassistant", message: p.message, key: p.key, link: "/home-assistant", silenced: silencedIntegration(i.id), target: integrationTarget(i.id) });
|
||||
}
|
||||
}
|
||||
}),
|
||||
|
||||
// Not a notification either: an operating system whose support has ended (or is about to) is seen here, with the date, from what the agent says it runs.
|
||||
check("Operating system support", async () => {
|
||||
const { healthChecks } = await getSettings();
|
||||
if (!(healthChecks.osSupportWarnDays > 0)) return;
|
||||
const now = new Date();
|
||||
for (const s of await db.select({ id: servers.id, name: servers.name, osId: servers.osId, osName: servers.osName, osVersion: servers.osVersion }).from(servers)) {
|
||||
if (!s.osId) continue;
|
||||
const life = lifecycleOf({ id: s.osId, name: s.osName, version: s.osVersion });
|
||||
const left = life ? daysOfSupportLeft(life.endsOn, now) : null;
|
||||
if (!life || life.endsOn === null || left === null || left > healthChecks.osSupportWarnDays) continue;
|
||||
const ended = left < 0;
|
||||
push({
|
||||
category: "updates",
|
||||
severity: ended ? "warning" : "info",
|
||||
type: "server",
|
||||
message: ended
|
||||
? `${s.name} runs ${life.product}, whose support ended on ${life.endsOn} — it no longer gets security fixes`
|
||||
: `${s.name} runs ${life.product}, whose support ends on ${life.endsOn} (in ${plural(left, "day")})`,
|
||||
key: `os-support:${s.id}`,
|
||||
link: `/servers/${s.id}`,
|
||||
silenced: subjects.has(`server:${s.id}`),
|
||||
target: targetOf("server", s.id),
|
||||
});
|
||||
}
|
||||
}),
|
||||
|
||||
check("Integration failures", async () => {
|
||||
const { notifications } = await getSettings();
|
||||
const failing = getFailingSources();
|
||||
|
||||
@@ -40,6 +40,8 @@ const KIND_OF_CATEGORY: Record<string, ChangeKind> = {
|
||||
share_link: "settings",
|
||||
metrics_token: "settings",
|
||||
api_token: "settings",
|
||||
todo: "other",
|
||||
note: "servers",
|
||||
webhook: "settings",
|
||||
integration: "integrations",
|
||||
dns: "integrations",
|
||||
|
||||
@@ -0,0 +1,133 @@
|
||||
/**
|
||||
* Suggested dependencies: things the app already knows enough about to offer "this needs that" for, so the map fills in with a click instead of typing.
|
||||
*
|
||||
* - **An integration that runs on a server.** An integration is reached at an address; when that address is one of a server's own (its IP, its hostname, or its
|
||||
* name), the service almost certainly runs on that server — Gitea at git.lab on the server that answers to it — so when that server is down, so is the service.
|
||||
* (A server it runs on that is not in the app, or a service in the cloud, matches nothing and is not offered.)
|
||||
*
|
||||
* Network links (which switch a device or a server is plugged into) and which Proxmox node a guest runs on are not offered: they are already part of the map
|
||||
* by themselves. It only reads what is stored, asks nobody, leaves out what is already so (or implied, or would make a loop), and remembers what somebody said
|
||||
* no to — like the suggested checks.
|
||||
*/
|
||||
import { db } from "../db/client.js";
|
||||
import { integrations, servers } from "../db/schema.js";
|
||||
import { matchServerForTarget, type MatchableServer } from "./uptimeKumaMatch.js";
|
||||
import { loadGraph, nodeKey, wouldLoop, type Edges } from "./dependencies.js";
|
||||
import { getInternalFlag, setInternalFlag } from "./settingsStore.js";
|
||||
|
||||
export interface DependencySuggestion {
|
||||
/** Stable, so a dismissed suggestion stays dismissed: "integration:3>server:7". */
|
||||
key: string;
|
||||
from: { type: "integration"; id: number; name: string };
|
||||
to: { type: "server"; id: number; name: string };
|
||||
/** Why it is offered, in a sentence. */
|
||||
reason: string;
|
||||
}
|
||||
|
||||
export interface IntegrationFacts {
|
||||
id: number;
|
||||
name: string;
|
||||
type: string;
|
||||
enabled: boolean;
|
||||
/** The address it is reached at: a URL's host, or a database host. */
|
||||
host: string | null;
|
||||
}
|
||||
|
||||
const TYPE_WORDS: Record<string, string> = {
|
||||
gitea: "Gitea",
|
||||
semaphore: "Semaphore",
|
||||
dockhand: "Docker (Dockhand)",
|
||||
proxmox: "Proxmox",
|
||||
synology: "Synology",
|
||||
uptimekuma: "Uptime Kuma",
|
||||
phpipam: "phpIPAM",
|
||||
pbs: "Proxmox Backup Server",
|
||||
osticket: "osTicket",
|
||||
unifi: "the UniFi controller",
|
||||
homeassistant: "Home Assistant",
|
||||
npm: "Nginx Proxy Manager",
|
||||
};
|
||||
|
||||
/** The host an integration is reached at, from the address it was set up with (or its database host, for osTicket). */
|
||||
export function hostOfIntegration(baseUrl: string | null, config: string | null): string | null {
|
||||
try {
|
||||
const c = config ? (JSON.parse(config) as Record<string, unknown>) : {};
|
||||
if (typeof c.host === "string" && c.host.trim()) return c.host.trim();
|
||||
} catch {
|
||||
// a config that is not JSON says nothing
|
||||
}
|
||||
if (!baseUrl) return null;
|
||||
try {
|
||||
return new URL(baseUrl).hostname || null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/** Pure: the dependencies worth offering, from what is known. */
|
||||
export function suggestDependencies(input: { integrations: IntegrationFacts[]; servers: MatchableServer[]; edges: Edges; dismissed: Set<string> }): { suggestions: DependencySuggestion[]; dismissed: DependencySuggestion[] } {
|
||||
const all: DependencySuggestion[] = [];
|
||||
for (const i of input.integrations) {
|
||||
if (!i.enabled || !i.host) continue;
|
||||
const server = matchServerForTarget(i.host, input.servers);
|
||||
if (!server) continue;
|
||||
const from = nodeKey("integration", i.id);
|
||||
const to = nodeKey("server", server.id);
|
||||
if (input.edges.get(from)?.includes(to)) continue;
|
||||
if (wouldLoop(input.edges, from, to)) continue;
|
||||
all.push({
|
||||
key: `${from}>${to}`,
|
||||
from: { type: "integration", id: i.id, name: i.name },
|
||||
to: { type: "server", id: server.id, name: server.name },
|
||||
reason: `${TYPE_WORDS[i.type] ?? i.name} is reached at ${i.host}, which is ${server.name}'s own address — so it most likely runs there, and is down when ${server.name} is.`,
|
||||
});
|
||||
}
|
||||
all.sort((a, b) => a.from.name.localeCompare(b.from.name) || a.to.name.localeCompare(b.to.name));
|
||||
return { suggestions: all.filter((s) => !input.dismissed.has(s.key)), dismissed: all.filter((s) => input.dismissed.has(s.key)) };
|
||||
}
|
||||
|
||||
// ─── What was said no to ───────────────────────────────────────────────────
|
||||
|
||||
const DISMISSED_FLAG = "dependencySuggestionsDismissed";
|
||||
const MAX_DISMISSED = 500;
|
||||
|
||||
export async function loadDismissedDependencies(): Promise<Set<string>> {
|
||||
const raw = await getInternalFlag(DISMISSED_FLAG);
|
||||
if (!raw) return new Set();
|
||||
try {
|
||||
const v = JSON.parse(raw);
|
||||
return new Set(Array.isArray(v) ? v.filter((k): k is string => typeof k === "string") : []);
|
||||
} catch {
|
||||
return new Set();
|
||||
}
|
||||
}
|
||||
|
||||
export async function saveDismissedDependencies(keys: Set<string>): Promise<void> {
|
||||
await setInternalFlag(DISMISSED_FLAG, JSON.stringify([...keys].slice(-MAX_DISMISSED)));
|
||||
}
|
||||
|
||||
// ─── Reading what is stored ────────────────────────────────────────────────
|
||||
|
||||
export async function loadDependencySuggestions() {
|
||||
const [intRows, serverRows, graph, dismissed] = await Promise.all([
|
||||
db.select({ id: integrations.id, name: integrations.name, type: integrations.type, enabled: integrations.enabled, baseUrl: integrations.baseUrl, config: integrations.config }).from(integrations),
|
||||
db.select({ id: servers.id, name: servers.name, hostname: servers.hostname, ipAddresses: servers.ipAddresses }).from(servers),
|
||||
loadGraph(),
|
||||
loadDismissedDependencies(),
|
||||
]);
|
||||
return suggestDependencies({
|
||||
integrations: intRows.map((i) => ({ id: i.id, name: i.name, type: i.type, enabled: i.enabled, host: hostOfIntegration(i.baseUrl, i.config) })),
|
||||
servers: serverRows.map((s) => {
|
||||
let ips: string[] = [];
|
||||
try {
|
||||
const v = s.ipAddresses ? JSON.parse(s.ipAddresses) : [];
|
||||
ips = Array.isArray(v) ? v.filter((x): x is string => typeof x === "string") : [];
|
||||
} catch {
|
||||
// none
|
||||
}
|
||||
return { id: s.id, name: s.name, hostname: s.hostname, ips };
|
||||
}),
|
||||
edges: graph.edges,
|
||||
dismissed,
|
||||
});
|
||||
}
|
||||
@@ -3,6 +3,8 @@
|
||||
*
|
||||
* - A web port a server's agent sees open (80, 443, 8080, 8443), with a name that points at it in DNS if there is one — "git.example.lan answers on
|
||||
* 443 on nas" — as a web check, linked to that server so its maintenance silences the alerts.
|
||||
* - A host Nginx Proxy Manager serves ("wiki.example.com" forwarding to a server the app knows), as a web check on the name people really use, linked to the
|
||||
* server it forwards to.
|
||||
* - A secret that already has a host to read the certificate from, as a certificate check: the secret only warns about the expiry, this also hears
|
||||
* when the service stops answering.
|
||||
*
|
||||
@@ -12,6 +14,8 @@
|
||||
import { db } from "../db/client.js";
|
||||
import { dnsRecordsCache, endpointChecks, secrets, servers } from "../db/schema.js";
|
||||
import { isLoopback } from "./agentPorts.js";
|
||||
import { npmIntegrationIds, readNpm } from "./npmReader.js";
|
||||
import { matchServerForTarget } from "./uptimeKumaMatch.js";
|
||||
import { getInternalFlag, setInternalFlag } from "./settingsStore.js";
|
||||
|
||||
export interface Suggestion {
|
||||
@@ -33,6 +37,8 @@ export interface SuggestionInput {
|
||||
secrets: { name: string; checkHost: string | null; checkPort: number | null }[];
|
||||
existing: { kind: string; target: string; port: number | null }[];
|
||||
dismissed: Set<string>;
|
||||
/** Hosts an Nginx Proxy Manager serves, with the server they forward to when the app knows it. */
|
||||
proxied?: { domain: string; secure: boolean; serverId: number | null; serverName: string | null; integrationName: string }[];
|
||||
}
|
||||
|
||||
/** The web ports worth offering, and what they speak. */
|
||||
@@ -112,6 +118,25 @@ export function suggestChecks(input: SuggestionInput): { suggestions: Suggestion
|
||||
}
|
||||
}
|
||||
|
||||
for (const p of input.proxied ?? []) {
|
||||
const host = cleanHost(p.domain);
|
||||
if (!host.includes(".") || host.startsWith("*") || /s/.test(host) || isIPv4(host)) continue;
|
||||
const port = p.secure ? 443 : 80;
|
||||
if (covered.has(`web:${host}:${port}`)) continue;
|
||||
const key = `http|${p.secure ? "https" : "http"}://${host}/`;
|
||||
if (all.has(key)) continue;
|
||||
all.set(key, {
|
||||
key,
|
||||
kind: "http",
|
||||
name: host,
|
||||
target: `${p.secure ? "https" : "http"}://${host}/`,
|
||||
port: null,
|
||||
serverId: p.serverId,
|
||||
serverName: p.serverName,
|
||||
reason: `${p.integrationName} serves ${host}${p.serverName ? `, forwarding to ${p.serverName}` : ""}.`,
|
||||
});
|
||||
}
|
||||
|
||||
for (const sec of input.secrets) {
|
||||
if (!sec.checkHost || sec.checkPort === null) continue;
|
||||
const host = cleanHost(sec.checkHost);
|
||||
@@ -176,7 +201,19 @@ export async function loadSuggestions(): Promise<{ suggestions: Suggestion[]; di
|
||||
db.select({ kind: endpointChecks.kind, target: endpointChecks.target, port: endpointChecks.port }).from(endpointChecks),
|
||||
loadDismissed(),
|
||||
]);
|
||||
// What Nginx Proxy Manager serves (a read is kept for a minute, and an NPM that can't be reached just adds nothing).
|
||||
const matchable = serverRows.map((s) => ({ id: s.id, name: s.name, hostname: s.hostname, ips: jsonArray<string>(s.ipAddresses).filter((i) => typeof i === "string") }));
|
||||
const proxied: NonNullable<SuggestionInput["proxied"]> = [];
|
||||
for (const i of await npmIntegrationIds()) {
|
||||
const read = await readNpm(i.id);
|
||||
for (const h of read.snapshot?.hosts ?? []) {
|
||||
if (!h.enabled) continue;
|
||||
const server = matchServerForTarget(h.forwardHost, matchable);
|
||||
for (const domain of h.domains) proxied.push({ domain, secure: h.sslForced || h.certificate !== null, serverId: server?.id ?? null, serverName: server?.name ?? null, integrationName: i.name });
|
||||
}
|
||||
}
|
||||
return suggestChecks({
|
||||
proxied,
|
||||
servers: serverRows.map((s) => ({ id: s.id, name: s.name, hostname: s.hostname, ips: jsonArray<string>(s.ipAddresses).filter((i) => typeof i === "string"), ports: jsonArray<{ protocol?: string; port: number; address?: string }>(s.listeningPorts).filter((p) => p && Number.isInteger(p.port)) })),
|
||||
dns: dnsRows,
|
||||
secrets: secretRows,
|
||||
|
||||
@@ -0,0 +1,182 @@
|
||||
/**
|
||||
* Keeps what was chosen to watch in each Home Assistant up to date: every few minutes the entities are read (one call lists them all), the watched ones are
|
||||
* stored with their state, and a numeric one adds a point to its history. The page, the chart and the alerts are built from what is stored — so none of them talks to
|
||||
* Home Assistant — and a Home Assistant that is down costs only how fresh they are.
|
||||
*
|
||||
* What makes an alert: a watched entity that has been "unavailable" or "unknown" for a while, or a number past a limit somebody set (a UPS battery under 30, a
|
||||
* freezer over -10).
|
||||
*/
|
||||
import { and, desc, eq, lt } from "drizzle-orm";
|
||||
import { db } from "../db/client.js";
|
||||
import { haEntities, haReadings, haWatched, integrations } from "../db/schema.js";
|
||||
import { loadIntegrationConfig } from "../integrations/loadIntegration.js";
|
||||
import { createHomeAssistantAdapter, type HaEntity } from "../integrations/homeassistant/adapter.js";
|
||||
import { getInternalFlag, setInternalFlag } from "./settingsStore.js";
|
||||
|
||||
export const SYNC_INTERVAL_MS = 5 * 60_000;
|
||||
/** The stored states are only as good as the last good pass: after this long, alerts for them are held rather than judged. */
|
||||
export const STALE_AFTER_MS = 20 * 60_000;
|
||||
/** An entity must have said nothing for this long before it is an alert. */
|
||||
export const UNAVAILABLE_MINUTES = 15;
|
||||
export const READING_KEEP_DAYS = 35;
|
||||
/** Two passes closer than this add one point to a history, not two. */
|
||||
const MIN_READING_GAP_MS = 2 * 60_000;
|
||||
const STATUS_FLAG = "homeAssistantSync";
|
||||
|
||||
export interface HaSyncStatus {
|
||||
at: string;
|
||||
ok: boolean;
|
||||
error: string | null;
|
||||
entities: number;
|
||||
}
|
||||
|
||||
async function readStatuses(): Promise<Record<string, HaSyncStatus>> {
|
||||
try {
|
||||
const raw = await getInternalFlag(STATUS_FLAG);
|
||||
return raw ? (JSON.parse(raw) as Record<string, HaSyncStatus>) : {};
|
||||
} catch {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
export async function haSyncStatuses(): Promise<Record<number, HaSyncStatus>> {
|
||||
return Object.fromEntries(Object.entries(await readStatuses()).map(([k, v]) => [Number(k), v]));
|
||||
}
|
||||
|
||||
export function isHaFresh(status: HaSyncStatus | undefined, nowMs: number): boolean {
|
||||
return !!status && status.ok && nowMs - Date.parse(status.at) <= STALE_AFTER_MS;
|
||||
}
|
||||
|
||||
/** Pure: when an entity first stopped saying anything, kept until it does. */
|
||||
export function trackUnavailable(previous: string | null | undefined, unavailable: boolean, nowIso: string): string | null {
|
||||
return unavailable ? (previous ?? nowIso) : null;
|
||||
}
|
||||
|
||||
/** Stores one read of one Home Assistant: the watched entities' states and, for the numeric ones, a reading. */
|
||||
export async function storeEntities(integrationId: number, entities: HaEntity[], now: Date = new Date()): Promise<number> {
|
||||
const nowIso = now.toISOString();
|
||||
const watched = await db.select().from(haWatched).where(eq(haWatched.integrationId, integrationId));
|
||||
if (watched.length === 0) return 0;
|
||||
const byId = new Map(entities.map((e) => [e.entityId, e]));
|
||||
const before = new Map((await db.select().from(haEntities).where(eq(haEntities.integrationId, integrationId))).map((r) => [r.entityId, r]));
|
||||
for (const w of watched) {
|
||||
const e = byId.get(w.entityId);
|
||||
// An entity that is no longer in Home Assistant at all is as good as unavailable.
|
||||
const state = e?.state ?? "missing";
|
||||
const unavailable = e ? e.unavailable : true;
|
||||
const values = {
|
||||
friendlyName: e?.name ?? null,
|
||||
state,
|
||||
value: e?.value ?? null,
|
||||
unit: e?.unit ?? null,
|
||||
deviceClass: e?.deviceClass ?? null,
|
||||
lastChanged: e?.lastChanged ?? null,
|
||||
unavailableSince: trackUnavailable(before.get(w.entityId)?.unavailableSince, unavailable, nowIso),
|
||||
updatedAt: nowIso,
|
||||
};
|
||||
await db.insert(haEntities).values({ integrationId, entityId: w.entityId, ...values }).onConflictDoUpdate({ target: [haEntities.integrationId, haEntities.entityId], set: values });
|
||||
if (e?.value !== null && e?.value !== undefined && !unavailable) {
|
||||
const [newest] = await db.select({ at: haReadings.at }).from(haReadings).where(and(eq(haReadings.integrationId, integrationId), eq(haReadings.entityId, w.entityId))).orderBy(desc(haReadings.at)).limit(1);
|
||||
if (!newest || now.getTime() - Date.parse(newest.at) >= MIN_READING_GAP_MS) await db.insert(haReadings).values({ integrationId, entityId: w.entityId, at: nowIso, value: e.value });
|
||||
}
|
||||
}
|
||||
// What is no longer watched is no longer kept.
|
||||
const watchedIds = watched.map((w) => w.entityId);
|
||||
for (const id of before.keys()) if (!watchedIds.includes(id)) await db.delete(haEntities).where(and(eq(haEntities.integrationId, integrationId), eq(haEntities.entityId, id)));
|
||||
await db.delete(haReadings).where(and(eq(haReadings.integrationId, integrationId), lt(haReadings.at, new Date(now.getTime() - READING_KEEP_DAYS * 86_400_000).toISOString())));
|
||||
return watched.length;
|
||||
}
|
||||
|
||||
/** Reads one Home Assistant and stores what is watched. Never throws: how it went is the answer, and what was stored is left alone when it failed. */
|
||||
export async function syncHomeAssistant(integrationId: number, now: Date = new Date()): Promise<HaSyncStatus> {
|
||||
let status: HaSyncStatus;
|
||||
try {
|
||||
const loaded = await loadIntegrationConfig(integrationId);
|
||||
if (!loaded || loaded.integration.type !== "homeassistant") throw new Error("not a Home Assistant integration");
|
||||
const entities = await createHomeAssistantAdapter(loaded.config as never).listEntities();
|
||||
const count = await storeEntities(integrationId, entities, now);
|
||||
status = { at: now.toISOString(), ok: true, error: null, entities: count };
|
||||
} catch (err) {
|
||||
const previous = (await readStatuses())[String(integrationId)];
|
||||
status = { at: previous?.at ?? now.toISOString(), ok: false, error: err instanceof Error ? err.message : String(err), entities: previous?.entities ?? 0 };
|
||||
console.error(`[home-assistant] couldn't read integration ${integrationId}:`, status.error);
|
||||
}
|
||||
try {
|
||||
const all = await readStatuses();
|
||||
all[String(integrationId)] = status;
|
||||
await setInternalFlag(STATUS_FLAG, JSON.stringify(all));
|
||||
} catch (err) {
|
||||
console.error("[home-assistant] couldn't record the result:", err);
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
export async function syncAllHomeAssistants(now: Date = new Date()): Promise<Record<number, HaSyncStatus>> {
|
||||
const rows = await db.select({ id: integrations.id }).from(integrations).where(and(eq(integrations.type, "homeassistant"), eq(integrations.enabled, true)));
|
||||
const out: Record<number, HaSyncStatus> = {};
|
||||
for (const r of rows) out[r.id] = await syncHomeAssistant(r.id, now);
|
||||
return out;
|
||||
}
|
||||
|
||||
/** Forgets everything of one integration when it is removed (and its status). */
|
||||
export async function removeHomeAssistantData(integrationId: number): Promise<void> {
|
||||
await db.delete(haReadings).where(eq(haReadings.integrationId, integrationId));
|
||||
await db.delete(haEntities).where(eq(haEntities.integrationId, integrationId));
|
||||
await db.delete(haWatched).where(eq(haWatched.integrationId, integrationId));
|
||||
const all = await readStatuses();
|
||||
if (String(integrationId) in all) {
|
||||
delete all[String(integrationId)];
|
||||
await setInternalFlag(STATUS_FLAG, JSON.stringify(all));
|
||||
}
|
||||
}
|
||||
|
||||
export interface HaProblem {
|
||||
key: string;
|
||||
kind: "unavailable" | "limit";
|
||||
severity: "critical" | "warning";
|
||||
message: string;
|
||||
}
|
||||
|
||||
const durationWords = (ms: number) => {
|
||||
const m = Math.floor(ms / 60_000);
|
||||
return m < 60 ? `${m} min` : m < 48 * 60 ? `${Math.floor(m / 60)} h` : `${Math.floor(m / 1440)} days`;
|
||||
};
|
||||
|
||||
/** Pure: what is wrong with the watched entities, from what is stored. */
|
||||
export function haProblems(
|
||||
integrationId: number,
|
||||
integrationName: string,
|
||||
watched: { entityId: string; name: string | null; above: number | null; below: number | null }[],
|
||||
entities: Map<string, { friendlyName: string | null; state: string; value: number | null; unit: string | null; unavailableSince: string | null }>,
|
||||
now: Date,
|
||||
): HaProblem[] {
|
||||
const out: HaProblem[] = [];
|
||||
for (const w of watched) {
|
||||
const e = entities.get(w.entityId);
|
||||
if (!e) continue;
|
||||
const name = w.name ?? e.friendlyName ?? w.entityId;
|
||||
if (e.unavailableSince !== null) {
|
||||
const ms = now.getTime() - Date.parse(e.unavailableSince);
|
||||
if (ms >= UNAVAILABLE_MINUTES * 60_000) {
|
||||
out.push({ key: `ha-unavailable:${integrationId}:${w.entityId}`, kind: "unavailable", severity: "warning", message: `${name} has been ${e.state === "missing" ? "missing from" : e.state} in Home Assistant for ${durationWords(ms)} [${integrationName}]` });
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (e.value === null) continue;
|
||||
const unit = e.unit ? ` ${e.unit}` : "";
|
||||
if (w.above !== null && e.value > w.above) out.push({ key: `ha-limit:${integrationId}:${w.entityId}`, kind: "limit", severity: "warning", message: `${name} is ${e.value}${unit}, above its limit of ${w.above}${unit} [${integrationName}]` });
|
||||
else if (w.below !== null && e.value < w.below) out.push({ key: `ha-limit:${integrationId}:${w.entityId}`, kind: "limit", severity: "warning", message: `${name} is ${e.value}${unit}, below its limit of ${w.below}${unit} [${integrationName}]` });
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
let timer: ReturnType<typeof setInterval> | null = null;
|
||||
|
||||
export function initHomeAssistantSync(): void {
|
||||
if (timer) return;
|
||||
const pass = () => void syncAllHomeAssistants().catch((err) => console.error("[home-assistant] pass failed:", err));
|
||||
timer = setInterval(pass, SYNC_INTERVAL_MS);
|
||||
setTimeout(pass, 7_000);
|
||||
console.log(`[home-assistant] reading watched entities every ${SYNC_INTERVAL_MS / 60000} minutes`);
|
||||
}
|
||||
|
||||
@@ -11,11 +11,14 @@ import { servers, updateRuns } from "../db/schema.js";
|
||||
import { getAlerts } from "./alerts.js";
|
||||
import { loadAcks } from "./alertAcks.js";
|
||||
import { describeTarget, listActiveWindows } from "./maintenance.js";
|
||||
import { openTodosFor } from "./todos.js";
|
||||
|
||||
/** An acknowledgement that runs out within this long is flagged. */
|
||||
export const ACK_ENDS_SOON_MS = 24 * 3600_000;
|
||||
/** A maintenance window that ends within this long is flagged. */
|
||||
export const WINDOW_ENDS_SOON_MS = 2 * 3600_000;
|
||||
/** A job handed to somebody is flagged from this many days before it is due (and once it is past). */
|
||||
export const TODO_DUE_SOON_DAYS = 2;
|
||||
/** A finished update run stays on the list this long, so the result is seen. */
|
||||
export const RUN_KEPT_MS = 24 * 3600_000;
|
||||
|
||||
@@ -44,6 +47,8 @@ export interface MyWork {
|
||||
}[];
|
||||
windows: { id: number; targetType: string; targetId: number; targetName: string; targetKind: string; reason: string | null; startedAt: string; endsAt: string; endsSoon: boolean }[];
|
||||
updateRuns: { id: number; serverId: number; serverName: string; templateName: string; status: string; message: string | null; startedAt: string; finishedAt: string | null; running: boolean; updatesBefore: number | null; updatesAfter: number | null; rebootRequired: boolean }[];
|
||||
/** Open jobs from the to-do list handed to this person, the soonest due first. */
|
||||
todos: { id: number; title: string; detail: string | null; dueDate: string | null; overdue: boolean; dueSoon: boolean; target: { type: string; id: number; name: string } | null }[];
|
||||
/** How many of the above want a look: acknowledgements and windows that end soon, alerts handed to them, update runs still going. */
|
||||
needsAttention: number;
|
||||
}
|
||||
@@ -111,6 +116,18 @@ export async function buildMyWork(user: MyWorkUser, now: Date = new Date()): Pro
|
||||
}
|
||||
runs.sort((a, b) => Number(b.running) - Number(a.running) || b.startedAt.localeCompare(a.startedAt));
|
||||
|
||||
const needsAttention = alerts.filter((a) => a.role === "handed" || a.endsSoon).length + windows.filter((w) => w.endsSoon).length + runs.filter((r) => r.running).length;
|
||||
return { alerts, windows, updateRuns: runs, needsAttention };
|
||||
const today = now.toISOString().slice(0, 10);
|
||||
const soon = new Date(nowMs + TODO_DUE_SOON_DAYS * 86_400_000).toISOString().slice(0, 10);
|
||||
const jobs: MyWork["todos"] = (await openTodosFor(user.id)).map((t) => ({
|
||||
id: t.id,
|
||||
title: t.title,
|
||||
detail: t.detail,
|
||||
dueDate: t.dueDate,
|
||||
overdue: t.dueDate !== null && t.dueDate < today,
|
||||
dueSoon: t.dueDate !== null && t.dueDate >= today && t.dueDate <= soon,
|
||||
target: t.target,
|
||||
}));
|
||||
|
||||
const needsAttention = alerts.filter((a) => a.role === "handed" || a.endsSoon).length + windows.filter((w) => w.endsSoon).length + runs.filter((r) => r.running).length + jobs.filter((j) => j.overdue || j.dueSoon).length;
|
||||
return { alerts, windows, updateRuns: runs, todos: jobs, needsAttention };
|
||||
}
|
||||
@@ -607,6 +607,8 @@ const SOURCE_LABELS: Record<string, string> = {
|
||||
pbs: "Proxmox Backup Server",
|
||||
osticket: "osTicket",
|
||||
unifi: "UniFi",
|
||||
npm: "Nginx Proxy Manager",
|
||||
homeassistant: "Home Assistant",
|
||||
};
|
||||
|
||||
export function sourceLabel(source: string): string {
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
/**
|
||||
* Reads Nginx Proxy Manager for the page, the alerts and the suggested checks, and works out what is wrong: a certificate in use that is about to run out (or has),
|
||||
* and a host NPM itself says it could not set up. A read is kept for a minute, so the page, the alerts and the suggestions together cost one call, and a failed read is
|
||||
* kept too (briefly), so an unreachable NPM is not asked again by every one of them.
|
||||
*/
|
||||
import { and, eq } from "drizzle-orm";
|
||||
import { db } from "../db/client.js";
|
||||
import { integrations } from "../db/schema.js";
|
||||
import { loadIntegrationConfig } from "../integrations/loadIntegration.js";
|
||||
import { createNpmAdapter, type NpmProxyHost, type NpmSnapshot } from "../integrations/npm/adapter.js";
|
||||
|
||||
const CACHE_MS = 60_000;
|
||||
/** A certificate is raised from this many days before it ends; critical in the last three. */
|
||||
export const CERT_WARN_DAYS = 14;
|
||||
export const CERT_CRITICAL_DAYS = 3;
|
||||
|
||||
interface Entry {
|
||||
at: number;
|
||||
snapshot: NpmSnapshot | null;
|
||||
error: string | null;
|
||||
}
|
||||
const cache = new Map<number, Entry>();
|
||||
export const clearNpmCacheForTests = () => cache.clear();
|
||||
|
||||
/** What NPM says now (a minute old at most), or why it could not be read. */
|
||||
export async function readNpm(integrationId: number, force = false): Promise<{ snapshot: NpmSnapshot | null; error: string | null; at: string }> {
|
||||
const hit = cache.get(integrationId);
|
||||
if (hit && !force && Date.now() - hit.at < CACHE_MS) return { snapshot: hit.snapshot, error: hit.error, at: new Date(hit.at).toISOString() };
|
||||
let entry: Entry;
|
||||
try {
|
||||
const loaded = await loadIntegrationConfig(integrationId);
|
||||
if (!loaded || loaded.integration.type !== "npm") throw new Error("not an Nginx Proxy Manager integration");
|
||||
entry = { at: Date.now(), snapshot: await createNpmAdapter(loaded.config as never).fetchSnapshot(), error: null };
|
||||
} catch (err) {
|
||||
entry = { at: Date.now(), snapshot: null, error: err instanceof Error ? err.message : String(err) };
|
||||
}
|
||||
cache.set(integrationId, entry);
|
||||
return { snapshot: entry.snapshot, error: entry.error, at: new Date(entry.at).toISOString() };
|
||||
}
|
||||
|
||||
export async function npmIntegrationIds(): Promise<{ id: number; name: string }[]> {
|
||||
return db.select({ id: integrations.id, name: integrations.name }).from(integrations).where(and(eq(integrations.type, "npm"), eq(integrations.enabled, true)));
|
||||
}
|
||||
|
||||
export interface NpmProblem {
|
||||
key: string;
|
||||
kind: "certificate" | "host";
|
||||
severity: "critical" | "warning";
|
||||
message: string;
|
||||
}
|
||||
|
||||
const DAY_MS = 86_400_000;
|
||||
|
||||
/** Pure: what is wrong, from one read. Only certificates that an enabled host uses count; a host that is switched off is not a problem. */
|
||||
export function npmProblems(integrationId: number, integrationName: string, snapshot: NpmSnapshot, now: Date): NpmProblem[] {
|
||||
const out: NpmProblem[] = [];
|
||||
const inUse = new Map<number, NpmProxyHost[]>();
|
||||
for (const h of snapshot.hosts) {
|
||||
if (!h.enabled || !h.certificate) continue;
|
||||
inUse.set(h.certificate.id, [...(inUse.get(h.certificate.id) ?? []), h]);
|
||||
}
|
||||
for (const cert of snapshot.certificates) {
|
||||
const users = inUse.get(cert.id);
|
||||
if (!users || !cert.expiresAt) continue;
|
||||
const days = Math.floor((Date.parse(cert.expiresAt) - now.getTime()) / DAY_MS);
|
||||
if (days > CERT_WARN_DAYS) continue;
|
||||
const names = users.flatMap((h) => h.domains).slice(0, 3).join(", ");
|
||||
// The certificate's own name is only worth saying when it isn't one of the names it is used for.
|
||||
const called = users.some((h) => h.domains.includes(cert.name)) ? "" : ` (${cert.name})`;
|
||||
out.push({
|
||||
key: `npm-cert:${integrationId}:${cert.id}`,
|
||||
kind: "certificate",
|
||||
severity: days <= CERT_CRITICAL_DAYS ? "critical" : "warning",
|
||||
message: days < 0 ? `The certificate for ${names}${called} expired ${-days} ${days === -1 ? "day" : "days"} ago [${integrationName}]` : `The certificate for ${names}${called} expires in ${days} ${days === 1 ? "day" : "days"} [${integrationName}]`,
|
||||
});
|
||||
}
|
||||
for (const h of snapshot.hosts) {
|
||||
if (!h.enabled || h.online) continue;
|
||||
out.push({ key: `npm-host:${integrationId}:${h.id}`, kind: "host", severity: "warning", message: `${h.domains[0] ?? `Proxy host ${h.id}`} is not working in Nginx Proxy Manager${h.error ? `: ${h.error.slice(0, 160)}` : ""} [${integrationName}]` });
|
||||
}
|
||||
return out;
|
||||
}
|
||||
@@ -0,0 +1,212 @@
|
||||
/**
|
||||
* When the operating system a server runs stops being supported. An agent says which one it runs (the distribution's id and release; for Windows, the product and
|
||||
* build); this knows the vendors' published end-of-support dates for the common ones, and says how long a release has left — so "Ubuntu 20.04 ended in May" is
|
||||
* on the Alerts page before a security fix is missed, the way a disk forecast is.
|
||||
*
|
||||
* The dates are the end of the vendor's standard security support (for Debian, including its long-term support; Proxmox VE's are the project's own announced
|
||||
* ones). The table is only as new as this version of the app: a release it does not know has no date, and is never called unsupported — it is simply not
|
||||
* judged. Rolling distributions (Arch, Gentoo, NixOS unstable) have no end and are not in it.
|
||||
*/
|
||||
|
||||
export interface OsFacts {
|
||||
id?: string | null;
|
||||
name?: string | null;
|
||||
version?: string | null;
|
||||
}
|
||||
|
||||
export interface Lifecycle {
|
||||
/** What to call it: "Ubuntu 22.04 LTS". */
|
||||
product: string;
|
||||
/** YYYY-MM-DD the support ends, or null when this release is not in the table. */
|
||||
endsOn: string | null;
|
||||
}
|
||||
|
||||
interface Row {
|
||||
product: string;
|
||||
ends: string;
|
||||
}
|
||||
|
||||
// Keyed by family, then by the release as it is looked up (see releaseKey).
|
||||
const TABLE: Record<string, Record<string, Row>> = {
|
||||
ubuntu: {
|
||||
"16.04": { product: "Ubuntu 16.04 LTS", ends: "2021-04-30" },
|
||||
"18.04": { product: "Ubuntu 18.04 LTS", ends: "2023-05-31" },
|
||||
"20.04": { product: "Ubuntu 20.04 LTS", ends: "2025-05-31" },
|
||||
"22.04": { product: "Ubuntu 22.04 LTS", ends: "2027-04-30" },
|
||||
"24.04": { product: "Ubuntu 24.04 LTS", ends: "2029-04-30" },
|
||||
"26.04": { product: "Ubuntu 26.04 LTS", ends: "2031-04-30" },
|
||||
"22.10": { product: "Ubuntu 22.10", ends: "2023-07-20" },
|
||||
"23.04": { product: "Ubuntu 23.04", ends: "2024-01-25" },
|
||||
"23.10": { product: "Ubuntu 23.10", ends: "2024-07-11" },
|
||||
"24.10": { product: "Ubuntu 24.10", ends: "2025-07-10" },
|
||||
"25.04": { product: "Ubuntu 25.04", ends: "2026-01-15" },
|
||||
"25.10": { product: "Ubuntu 25.10", ends: "2026-07-09" },
|
||||
},
|
||||
debian: {
|
||||
"9": { product: "Debian 9 (stretch)", ends: "2022-06-30" },
|
||||
"10": { product: "Debian 10 (buster)", ends: "2024-06-30" },
|
||||
"11": { product: "Debian 11 (bullseye)", ends: "2026-08-31" },
|
||||
"12": { product: "Debian 12 (bookworm)", ends: "2028-06-30" },
|
||||
"13": { product: "Debian 13 (trixie)", ends: "2030-06-30" },
|
||||
},
|
||||
"centos-classic": {
|
||||
"7": { product: "CentOS 7", ends: "2024-06-30" },
|
||||
"8": { product: "CentOS 8", ends: "2021-12-31" },
|
||||
},
|
||||
"centos-stream": {
|
||||
"8": { product: "CentOS Stream 8", ends: "2024-05-31" },
|
||||
"9": { product: "CentOS Stream 9", ends: "2027-05-31" },
|
||||
"10": { product: "CentOS Stream 10", ends: "2030-01-01" },
|
||||
},
|
||||
rhel: {
|
||||
"7": { product: "Red Hat Enterprise Linux 7", ends: "2024-06-30" },
|
||||
"8": { product: "Red Hat Enterprise Linux 8", ends: "2029-05-31" },
|
||||
"9": { product: "Red Hat Enterprise Linux 9", ends: "2032-05-31" },
|
||||
"10": { product: "Red Hat Enterprise Linux 10", ends: "2035-05-31" },
|
||||
},
|
||||
rocky: {
|
||||
"8": { product: "Rocky Linux 8", ends: "2029-05-31" },
|
||||
"9": { product: "Rocky Linux 9", ends: "2032-05-31" },
|
||||
"10": { product: "Rocky Linux 10", ends: "2035-05-31" },
|
||||
},
|
||||
almalinux: {
|
||||
"8": { product: "AlmaLinux 8", ends: "2029-05-31" },
|
||||
"9": { product: "AlmaLinux 9", ends: "2032-05-31" },
|
||||
"10": { product: "AlmaLinux 10", ends: "2035-05-31" },
|
||||
},
|
||||
fedora: {
|
||||
"38": { product: "Fedora 38", ends: "2024-05-21" },
|
||||
"39": { product: "Fedora 39", ends: "2024-11-26" },
|
||||
"40": { product: "Fedora 40", ends: "2025-05-13" },
|
||||
"41": { product: "Fedora 41", ends: "2025-12-15" },
|
||||
"42": { product: "Fedora 42", ends: "2026-05-13" },
|
||||
"43": { product: "Fedora 43", ends: "2026-12-02" },
|
||||
},
|
||||
alpine: {
|
||||
"3.17": { product: "Alpine 3.17", ends: "2024-11-22" },
|
||||
"3.18": { product: "Alpine 3.18", ends: "2025-05-09" },
|
||||
"3.19": { product: "Alpine 3.19", ends: "2025-11-01" },
|
||||
"3.20": { product: "Alpine 3.20", ends: "2026-04-01" },
|
||||
"3.21": { product: "Alpine 3.21", ends: "2026-11-01" },
|
||||
"3.22": { product: "Alpine 3.22", ends: "2027-05-01" },
|
||||
},
|
||||
"opensuse-leap": {
|
||||
"15.3": { product: "openSUSE Leap 15.3", ends: "2022-12-31" },
|
||||
"15.4": { product: "openSUSE Leap 15.4", ends: "2023-12-07" },
|
||||
"15.5": { product: "openSUSE Leap 15.5", ends: "2024-12-31" },
|
||||
"15.6": { product: "openSUSE Leap 15.6", ends: "2025-12-31" },
|
||||
},
|
||||
"proxmox-ve": {
|
||||
"6": { product: "Proxmox VE 6", ends: "2022-07-31" },
|
||||
"7": { product: "Proxmox VE 7", ends: "2024-07-31" },
|
||||
"8": { product: "Proxmox VE 8", ends: "2026-08-31" },
|
||||
},
|
||||
// Windows is told apart by its build number, and a client from a server by the product's name (Windows 11 24H2 and Windows Server 2025 share a build).
|
||||
"windows-client": {
|
||||
"19045": { product: "Windows 10 22H2", ends: "2025-10-14" },
|
||||
"22000": { product: "Windows 11 21H2", ends: "2023-10-10" },
|
||||
"22621": { product: "Windows 11 22H2", ends: "2024-10-08" },
|
||||
"22631": { product: "Windows 11 23H2", ends: "2025-11-11" },
|
||||
"26100": { product: "Windows 11 24H2", ends: "2026-10-13" },
|
||||
"26200": { product: "Windows 11 25H2", ends: "2027-10-12" },
|
||||
},
|
||||
"windows-server": {
|
||||
"9600": { product: "Windows Server 2012 R2", ends: "2023-10-10" },
|
||||
"14393": { product: "Windows Server 2016", ends: "2027-01-12" },
|
||||
"17763": { product: "Windows Server 2019", ends: "2029-01-09" },
|
||||
"20348": { product: "Windows Server 2022", ends: "2031-10-14" },
|
||||
"26100": { product: "Windows Server 2025", ends: "2034-11-14" },
|
||||
},
|
||||
};
|
||||
|
||||
/** Which family of the table an agent's answer belongs to, and which release within it; null when it is none this knows. */
|
||||
export function releaseKey(os: OsFacts): { family: string; key: string } | null {
|
||||
const id = (os.id ?? "").trim().toLowerCase();
|
||||
const version = (os.version ?? "").trim();
|
||||
if (!id || !version) return null;
|
||||
const name = (os.name ?? "").toLowerCase();
|
||||
switch (id) {
|
||||
case "ubuntu":
|
||||
return { family: "ubuntu", key: version.match(/^\d+\.\d+/)?.[0] ?? version };
|
||||
case "debian":
|
||||
case "raspbian":
|
||||
return { family: "debian", key: version.match(/^\d+/)?.[0] ?? version };
|
||||
case "centos":
|
||||
return { family: name.includes("stream") ? "centos-stream" : "centos-classic", key: version.match(/^\d+/)?.[0] ?? version };
|
||||
case "rhel":
|
||||
case "rocky":
|
||||
case "almalinux":
|
||||
return { family: id, key: version.match(/^\d+/)?.[0] ?? version };
|
||||
case "fedora":
|
||||
return { family: "fedora", key: version.match(/^\d+/)?.[0] ?? version };
|
||||
case "alpine":
|
||||
return { family: "alpine", key: version.match(/^\d+\.\d+/)?.[0] ?? version };
|
||||
case "opensuse-leap":
|
||||
return { family: "opensuse-leap", key: version.match(/^\d+\.\d+/)?.[0] ?? version };
|
||||
case "proxmox-ve":
|
||||
return { family: "proxmox-ve", key: version.match(/^\d+/)?.[0] ?? version };
|
||||
case "windows":
|
||||
return { family: name.includes("server") ? "windows-server" : "windows-client", key: version.match(/^\d+/)?.[0] ?? version };
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/** What the table says about this operating system: its name and when its support ends. Null when the agent's answer is nothing this recognises at all. */
|
||||
export function lifecycleOf(os: OsFacts): Lifecycle | null {
|
||||
const found = releaseKey(os);
|
||||
if (!found) return null;
|
||||
const row = TABLE[found.family]?.[found.key];
|
||||
if (row) return { product: row.product, endsOn: row.ends };
|
||||
// A release not in the table: named as the machine named itself, with no date, so nothing is claimed about it.
|
||||
const name = (os.name ?? "").trim();
|
||||
return name ? { product: name, endsOn: null } : null;
|
||||
}
|
||||
|
||||
export interface OsSummary {
|
||||
id: string;
|
||||
name: string | null;
|
||||
version: string | null;
|
||||
/** What to call it. */
|
||||
product: string;
|
||||
endsOn: string | null;
|
||||
state: SupportState;
|
||||
/** Days of support left (negative once it has ended); null with no date. */
|
||||
daysLeft: number | null;
|
||||
}
|
||||
|
||||
/** What a server's page and the read API say about the operating system it runs; null while its agent has not said. */
|
||||
export function osSummary(row: { osId: string | null; osName: string | null; osVersion: string | null }, now: Date, warnDays: number): OsSummary | null {
|
||||
if (!row.osId) return null;
|
||||
const life = lifecycleOf({ id: row.osId, name: row.osName, version: row.osVersion });
|
||||
const endsOn = life?.endsOn ?? null;
|
||||
return {
|
||||
id: row.osId,
|
||||
name: row.osName,
|
||||
version: row.osVersion,
|
||||
product: life?.product ?? row.osName ?? row.osId,
|
||||
endsOn,
|
||||
// With the warning switched off the date is still known: "soon" then means the usual three months.
|
||||
state: supportState(endsOn, now, warnDays > 0 ? warnDays : 90),
|
||||
daysLeft: daysOfSupportLeft(endsOn, now),
|
||||
};
|
||||
}
|
||||
|
||||
export type SupportState = "ended" | "soon" | "ok" | "unknown";
|
||||
|
||||
const DAY_MS = 86_400_000;
|
||||
|
||||
/** Whole days from `now` to the end of the day support ends (negative once it has passed); null with no date. */
|
||||
export function daysOfSupportLeft(endsOn: string | null, now: Date): number | null {
|
||||
if (!endsOn) return null;
|
||||
const end = Date.parse(`${endsOn}T23:59:59Z`);
|
||||
return Number.isFinite(end) ? Math.floor((end - now.getTime()) / DAY_MS) : null;
|
||||
}
|
||||
|
||||
/** Ended once the day is over; "soon" within `warnDays` of the end; unknown with no date. */
|
||||
export function supportState(endsOn: string | null, now: Date, warnDays: number): SupportState {
|
||||
const left = daysOfSupportLeft(endsOn, now);
|
||||
if (left === null) return "unknown";
|
||||
if (left < 0) return "ended";
|
||||
return left <= warnDays ? "soon" : "ok";
|
||||
}
|
||||
@@ -15,6 +15,7 @@ import { parsePendingUpdates } from "./pendingUpdates.js";
|
||||
import { parseTags } from "./serverTags.js";
|
||||
import { parseThresholds } from "./serverThresholds.js";
|
||||
import { getSettings } from "./settingsStore.js";
|
||||
import { osSummary } from "./osLifecycle.js";
|
||||
import { serverStatus } from "./topology.js";
|
||||
|
||||
/** An address inside the app as a full address, for a script that has no idea where the app lives. */
|
||||
@@ -33,7 +34,7 @@ const json = <T>(text: string | null, fallback: T): T => {
|
||||
|
||||
type ServerRow = typeof servers.$inferSelect;
|
||||
|
||||
export function serverV1(row: ServerRow, offlineMinutes: number, now: Date) {
|
||||
export function serverV1(row: ServerRow, offlineMinutes: number, now: Date, osWarnDays = 90) {
|
||||
const status = serverStatus(row, offlineMinutes, now);
|
||||
const disks = json<{ mount: string; sizeBytes: number; usedBytes: number }[]>(row.disks, []);
|
||||
const updates = parsePendingUpdates(row.pendingUpdates);
|
||||
@@ -53,6 +54,8 @@ export function serverV1(row: ServerRow, offlineMinutes: number, now: Date) {
|
||||
agentVersion: row.agentVersion ?? null,
|
||||
runsOnServerId: row.parentServerId ?? null,
|
||||
hardware: { manufacturer: row.hwManufacturer ?? null, model: row.hwModel ?? null, serial: row.hwSerial ?? null },
|
||||
// What it runs and when that release stops being supported ("ended", "soon", "ok", "unknown"), once its agent has said.
|
||||
operatingSystem: osSummary(row, now, osWarnDays),
|
||||
cpu: { model: row.cpuModel, cores: row.cpuCores, loadPercent: row.cpuLoadPercent },
|
||||
memory: { totalBytes: row.memTotalBytes, usedBytes: row.memUsedBytes },
|
||||
disks: disks.map((d) => ({ mount: d.mount, sizeBytes: d.sizeBytes, usedBytes: d.usedBytes, usedPercent: d.sizeBytes > 0 ? Math.round((d.usedBytes / d.sizeBytes) * 1000) / 10 : null })),
|
||||
@@ -65,13 +68,14 @@ export function serverV1(row: ServerRow, offlineMinutes: number, now: Date) {
|
||||
|
||||
export async function serversV1(now: Date = new Date()) {
|
||||
const [rows, settings] = await Promise.all([db.select().from(servers).orderBy(servers.name), getSettings()]);
|
||||
return rows.map((r) => serverV1(r, settings.healthChecks.serverOfflineMinutes, now));
|
||||
return rows.map((r) => serverV1(r, settings.healthChecks.serverOfflineMinutes, now, settings.healthChecks.osSupportWarnDays));
|
||||
}
|
||||
|
||||
export async function oneServerV1(id: number, now: Date = new Date()) {
|
||||
const [row] = await db.select().from(servers).where(eq(servers.id, id)).limit(1);
|
||||
if (!row) return null;
|
||||
return serverV1(row, (await getSettings()).healthChecks.serverOfflineMinutes, now);
|
||||
const { healthChecks } = await getSettings();
|
||||
return serverV1(row, healthChecks.serverOfflineMinutes, now, healthChecks.osSupportWarnDays);
|
||||
}
|
||||
|
||||
export async function alertsV1() {
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
/**
|
||||
* The racks, drawn from the inventory: each piece of hardware that says which unit it sits in (U12, or U12-13 for a tall one) and where (the rack's name in its
|
||||
* location) is placed in its rack, units numbered from the bottom as racks are. Two pieces claiming the same unit are said to clash rather than hidden; hardware
|
||||
* that names a rack but no unit is listed beside it as not placed.
|
||||
*/
|
||||
|
||||
export interface RackAsset {
|
||||
id: number;
|
||||
name: string;
|
||||
kind: string;
|
||||
manufacturer: string | null;
|
||||
model: string | null;
|
||||
location: string | null;
|
||||
rackUnit: string | null;
|
||||
powerWatts: number | null;
|
||||
serverId: number | null;
|
||||
}
|
||||
|
||||
export interface RackSlot {
|
||||
assetId: number;
|
||||
name: string;
|
||||
kind: string;
|
||||
model: string | null;
|
||||
/** The lowest and highest unit it takes, from 1 (the bottom). */
|
||||
from: number;
|
||||
to: number;
|
||||
powerWatts: number | null;
|
||||
serverId: number | null;
|
||||
/** Another piece takes one of the same units. */
|
||||
clash: boolean;
|
||||
}
|
||||
|
||||
export interface Rack {
|
||||
/** What its pieces call it in their location; "(no rack named)" for pieces that gave a unit but no place. */
|
||||
name: string;
|
||||
/** How many units tall to draw it. */
|
||||
units: number;
|
||||
slots: RackSlot[];
|
||||
/** Pieces that name this rack and no unit. */
|
||||
unplaced: { assetId: number; name: string; kind: string }[];
|
||||
/** Units taken, and the watts of what stands in it. */
|
||||
usedUnits: number;
|
||||
watts: number;
|
||||
}
|
||||
|
||||
/** The units of a text like "U12", "12", "U12-13", "12–14" or "U12 - U14": lowest and highest. Null when it is not a unit. */
|
||||
export function parseRackUnit(text: string | null | undefined): { from: number; to: number } | null {
|
||||
if (!text) return null;
|
||||
const m = /^\s*U?\s*(\d{1,2})\s*(?:[-–—]\s*U?\s*(\d{1,2}))?\s*$/i.exec(text);
|
||||
if (!m) return null;
|
||||
const a = Number(m[1]);
|
||||
const b = m[2] !== undefined ? Number(m[2]) : a;
|
||||
if (a < 1 || b < 1) return null;
|
||||
return { from: Math.min(a, b), to: Math.max(a, b) };
|
||||
}
|
||||
|
||||
/** Rack sizes that exist, so a rack is drawn as a real one. */
|
||||
const SIZES = [12, 18, 22, 24, 27, 42, 45, 47, 48];
|
||||
const NO_RACK = "(no rack named)";
|
||||
|
||||
export function buildRacks(assets: RackAsset[]): Rack[] {
|
||||
const byRack = new Map<string, RackAsset[]>();
|
||||
for (const a of assets) {
|
||||
const place = (a.location ?? "").trim();
|
||||
const unit = parseRackUnit(a.rackUnit);
|
||||
if (!place && !unit) continue;
|
||||
const key = place || NO_RACK;
|
||||
byRack.set(key, [...(byRack.get(key) ?? []), a]);
|
||||
}
|
||||
|
||||
const racks: Rack[] = [];
|
||||
for (const [name, members] of byRack) {
|
||||
const placed = members.filter((a) => parseRackUnit(a.rackUnit));
|
||||
// A "rack" that is only a room with things in it (no piece says a unit) is not a rack to draw.
|
||||
if (placed.length === 0) continue;
|
||||
const taken = new Map<number, number>();
|
||||
const slots: RackSlot[] = placed.map((a) => {
|
||||
const u = parseRackUnit(a.rackUnit)!;
|
||||
for (let n = u.from; n <= u.to; n++) taken.set(n, (taken.get(n) ?? 0) + 1);
|
||||
return { assetId: a.id, name: a.name, kind: a.kind, model: a.model, from: u.from, to: u.to, powerWatts: a.powerWatts, serverId: a.serverId, clash: false };
|
||||
});
|
||||
for (const s of slots) for (let n = s.from; n <= s.to; n++) if ((taken.get(n) ?? 0) > 1) s.clash = true;
|
||||
slots.sort((x, y) => x.from - y.from || x.name.localeCompare(y.name));
|
||||
const top = Math.max(...slots.map((s) => s.to));
|
||||
racks.push({
|
||||
name,
|
||||
units: SIZES.find((s) => s >= top) ?? top,
|
||||
slots,
|
||||
unplaced: members.filter((a) => !parseRackUnit(a.rackUnit)).map((a) => ({ assetId: a.id, name: a.name, kind: a.kind })).sort((x, y) => x.name.localeCompare(y.name)),
|
||||
usedUnits: taken.size,
|
||||
watts: slots.reduce((n, s) => n + (s.powerWatts ?? 0), 0),
|
||||
});
|
||||
}
|
||||
return racks.sort((a, b) => (a.name === NO_RACK ? 1 : 0) - (b.name === NO_RACK ? 1 : 0) || a.name.localeCompare(b.name, undefined, { numeric: true }));
|
||||
}
|
||||
@@ -119,6 +119,8 @@ export interface HealthCheckSettings {
|
||||
flapRaises: number;
|
||||
/** A network device (switch, access point, gateway) that a UniFi controller has not seen online for this many minutes is raised as offline. */
|
||||
deviceOfflineMinutes: number;
|
||||
/** Days before the end of an operating system's support that a server is first flagged on the Alerts page. 0 turns it off. */
|
||||
osSupportWarnDays: number;
|
||||
}
|
||||
|
||||
/** The monthly report: last month's availability, incidents, updates and renewals, in one message on a day of the month. Off until switched on. */
|
||||
@@ -289,7 +291,7 @@ const DEFAULTS: AppSettings = {
|
||||
selfBackup: { enabled: true, time: "03:30", keep: 14 },
|
||||
profilePictures: { gravatar: false, identityProvider: false },
|
||||
signIn: { roleFromGroups: false, groupsClaim: "groups", adminGroups: [], operatorGroups: [], otherwise: "viewer" },
|
||||
healthChecks: { serverOfflineMinutes: 60, diskUsagePercent: 90, domainWarnDays: 30, diskForecastDays: 14, backupStaleHours: 48, restoreTestDays: 0, newPortAlerts: true, flapRaises: 3, deviceOfflineMinutes: 10 },
|
||||
healthChecks: { serverOfflineMinutes: 60, diskUsagePercent: 90, domainWarnDays: 30, diskForecastDays: 14, backupStaleHours: 48, restoreTestDays: 0, newPortAlerts: true, flapRaises: 3, deviceOfflineMinutes: 10, osSupportWarnDays: 90 },
|
||||
// Docker's default bridge (172.17.0.0/16) and the pool it hands custom networks from (172.18–31) live here, and every
|
||||
// Docker host repeats them. Shown on the Consistency page, where it can be removed if 172.16/12 is used as a real LAN.
|
||||
consistency: { excludedRanges: ["172.16.0.0/12"] },
|
||||
|
||||
@@ -32,6 +32,8 @@ export interface IncomingSystemInfo {
|
||||
health?: IncomingHostHealth;
|
||||
/** What the machine is, from its firmware: each part present only if it says anything. */
|
||||
hardware?: { manufacturer?: string; model?: string; serial?: string };
|
||||
/** What the machine runs: the distribution's id and release (for Windows, the product and build). Absent from an agent that predates it. */
|
||||
os?: { id?: string; name?: string; version?: string } | null;
|
||||
/** What the agent could not collect; an empty list says nothing was missed. */
|
||||
diagnostics?: AgentDiagnostic[];
|
||||
}
|
||||
@@ -123,6 +125,8 @@ export async function syncServerTasks(serverId: number, report: AgentReport) {
|
||||
...(system?.updates && before ? nextUpdateState(before, system.updates, now) : {}),
|
||||
...(system?.health && before ? { hostHealth: nextHostHealth(before.hostHealth, system.health, now) } : {}),
|
||||
...(system?.diagnostics ? { agentDiagnostics: nextDiagnostics(system.diagnostics, now) } : {}),
|
||||
// Only stored when it was said, so an older agent doesn't wipe what a newer one told us; the text is bounded, whatever sent it.
|
||||
...(typeof system?.os?.id === "string" && system.os.id ? { osId: system.os.id.slice(0, 40).toLowerCase(), osName: typeof system.os.name === "string" ? system.os.name.slice(0, 80) : null, osVersion: typeof system.os.version === "string" ? system.os.version.slice(0, 40) : null } : {}),
|
||||
...(system?.hardware?.manufacturer ? { hwManufacturer: system.hardware.manufacturer } : {}),
|
||||
...(system?.hardware?.model ? { hwModel: system.hardware.model } : {}),
|
||||
...(system?.hardware?.serial ? { hwSerial: system.hardware.serial } : {}),
|
||||
|
||||
@@ -0,0 +1,190 @@
|
||||
/**
|
||||
* The to-do list and notes. A to-do is a small job with a title, maybe a day it is due, a person it is handed to and a server or network device it is about; ticking it
|
||||
* off keeps it (so what was done is still there to read) and un-ticking brings it back. A note is one free-form text about a server or a device.
|
||||
*/
|
||||
import { and, desc, eq, isNull, isNotNull } from "drizzle-orm";
|
||||
import { db } from "../db/client.js";
|
||||
import { networkDevices, notes, servers, todos, users } from "../db/schema.js";
|
||||
|
||||
export const TARGET_TYPES = ["server", "device"] as const;
|
||||
export type TargetType = (typeof TARGET_TYPES)[number];
|
||||
|
||||
export const MAX_TITLE = 160;
|
||||
export const MAX_DETAIL = 2000;
|
||||
export const MAX_NOTE_BODY = 8000;
|
||||
|
||||
export class TodoError extends Error {
|
||||
constructor(
|
||||
readonly status: number,
|
||||
readonly code: string,
|
||||
message: string,
|
||||
) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
||||
/** A real calendar day, written YYYY-MM-DD. */
|
||||
export const isRealDay = (v: unknown): v is string => typeof v === "string" && /^\d{4}-\d{2}-\d{2}$/.test(v) && new Date(`${v}T00:00:00Z`).toISOString().startsWith(v);
|
||||
|
||||
export async function targetName(type: TargetType, id: number): Promise<string | null> {
|
||||
if (type === "server") return (await db.select({ name: servers.name }).from(servers).where(eq(servers.id, id)).limit(1))[0]?.name ?? null;
|
||||
return (await db.select({ name: networkDevices.name }).from(networkDevices).where(eq(networkDevices.id, id)).limit(1))[0]?.name ?? null;
|
||||
}
|
||||
|
||||
export interface TodoItem {
|
||||
id: number;
|
||||
title: string;
|
||||
detail: string | null;
|
||||
dueDate: string | null;
|
||||
assignee: { id: number | null; name: string } | null;
|
||||
target: { type: TargetType; id: number; name: string } | null;
|
||||
createdBy: string | null;
|
||||
createdAt: string;
|
||||
doneAt: string | null;
|
||||
doneBy: string | null;
|
||||
}
|
||||
|
||||
type Row = typeof todos.$inferSelect;
|
||||
|
||||
async function shape(rows: Row[]): Promise<TodoItem[]> {
|
||||
const serverNames = new Map((await db.select({ id: servers.id, name: servers.name }).from(servers)).map((s) => [s.id, s.name]));
|
||||
const deviceNames = new Map((await db.select({ id: networkDevices.id, name: networkDevices.name }).from(networkDevices)).map((d) => [d.id, d.name]));
|
||||
return rows.map((r) => {
|
||||
const name = r.targetType && r.targetId !== null ? (r.targetType === "server" ? serverNames.get(r.targetId) : deviceNames.get(r.targetId)) : undefined;
|
||||
return {
|
||||
id: r.id,
|
||||
title: r.title,
|
||||
detail: r.detail,
|
||||
dueDate: r.dueDate,
|
||||
assignee: r.assigneeName ? { id: r.assigneeUserId, name: r.assigneeName } : null,
|
||||
// What it was about, if that is still there.
|
||||
target: r.targetType && r.targetId !== null && name ? { type: r.targetType, id: r.targetId, name } : null,
|
||||
createdBy: r.createdBy,
|
||||
createdAt: r.createdAt,
|
||||
doneAt: r.doneAt,
|
||||
doneBy: r.doneBy,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
/** Open ones first, the soonest due first (those with no day last); done ones after, the latest done first. */
|
||||
export async function listTodos(): Promise<TodoItem[]> {
|
||||
const open = await shape(await db.select().from(todos).where(isNull(todos.doneAt)));
|
||||
open.sort((a, b) => (a.dueDate ?? "9999-99-99").localeCompare(b.dueDate ?? "9999-99-99") || a.createdAt.localeCompare(b.createdAt) || a.id - b.id);
|
||||
const done = await shape(await db.select().from(todos).where(isNotNull(todos.doneAt)).orderBy(desc(todos.doneAt)).limit(200));
|
||||
return [...open, ...done];
|
||||
}
|
||||
|
||||
export interface TodoInput {
|
||||
title: string;
|
||||
detail?: string | null;
|
||||
dueDate?: string | null;
|
||||
assigneeId?: number | null;
|
||||
targetType?: TargetType | null;
|
||||
targetId?: number | null;
|
||||
}
|
||||
|
||||
async function resolveAssignee(id: number | null | undefined): Promise<{ id: number | null; name: string | null }> {
|
||||
if (id === null || id === undefined) return { id: null, name: null };
|
||||
const [u] = await db.select().from(users).where(eq(users.id, id)).limit(1);
|
||||
if (!u || (u.role !== "admin" && u.role !== "operator")) throw new TodoError(400, "invalid_assignee", "A job can be handed to an administrator or an operator.");
|
||||
return { id: u.id, name: u.name ?? u.email ?? u.oidcSub };
|
||||
}
|
||||
|
||||
async function resolveTarget(type: TargetType | null | undefined, id: number | null | undefined): Promise<{ type: TargetType | null; id: number | null }> {
|
||||
if (!type || id === null || id === undefined) return { type: null, id: null };
|
||||
if (!(await targetName(type, id))) throw new TodoError(400, "unknown_target", "That server or network device is not there.");
|
||||
return { type, id };
|
||||
}
|
||||
|
||||
function checked(input: TodoInput) {
|
||||
const title = input.title.trim();
|
||||
if (!title) throw new TodoError(400, "invalid_body", "Say what is to be done.");
|
||||
if (title.length > MAX_TITLE) throw new TodoError(400, "too_long", `Keep the title under ${MAX_TITLE} characters.`);
|
||||
const detail = (input.detail ?? "").trim();
|
||||
if (detail.length > MAX_DETAIL) throw new TodoError(400, "too_long", `Keep the details under ${MAX_DETAIL} characters.`);
|
||||
if (input.dueDate !== null && input.dueDate !== undefined && input.dueDate !== "" && !isRealDay(input.dueDate)) throw new TodoError(400, "invalid_date", "That is not a date.");
|
||||
return { title, detail: detail || null, dueDate: input.dueDate ? input.dueDate : null };
|
||||
}
|
||||
|
||||
export async function createTodo(input: TodoInput, actor: string, now: Date = new Date()): Promise<TodoItem> {
|
||||
const base = checked(input);
|
||||
const who = await resolveAssignee(input.assigneeId);
|
||||
const target = await resolveTarget(input.targetType, input.targetId);
|
||||
const [row] = await db
|
||||
.insert(todos)
|
||||
.values({ ...base, assigneeUserId: who.id, assigneeName: who.name, targetType: target.type, targetId: target.id, createdBy: actor, createdAt: now.toISOString() })
|
||||
.returning();
|
||||
return (await shape([row]))[0];
|
||||
}
|
||||
|
||||
/** Changes what is given; "done" ticks it off or brings it back. Null if there is no such job. */
|
||||
export async function updateTodo(id: number, input: Partial<TodoInput> & { done?: boolean }, actor: string, now: Date = new Date()): Promise<{ before: TodoItem; after: TodoItem } | null> {
|
||||
const [existing] = await db.select().from(todos).where(eq(todos.id, id)).limit(1);
|
||||
if (!existing) return null;
|
||||
const before = (await shape([existing]))[0];
|
||||
const set: Partial<Row> = {};
|
||||
if (input.title !== undefined || input.detail !== undefined || input.dueDate !== undefined) {
|
||||
const c = checked({ title: input.title ?? existing.title, detail: input.detail === undefined ? existing.detail : input.detail, dueDate: input.dueDate === undefined ? existing.dueDate : input.dueDate });
|
||||
Object.assign(set, c);
|
||||
}
|
||||
if (input.assigneeId !== undefined) {
|
||||
const who = await resolveAssignee(input.assigneeId);
|
||||
set.assigneeUserId = who.id;
|
||||
set.assigneeName = who.name;
|
||||
}
|
||||
if (input.targetType !== undefined || input.targetId !== undefined) {
|
||||
const t = await resolveTarget(input.targetType === undefined ? existing.targetType : input.targetType, input.targetId === undefined ? existing.targetId : input.targetId);
|
||||
set.targetType = t.type;
|
||||
set.targetId = t.id;
|
||||
}
|
||||
if (input.done === true && !existing.doneAt) Object.assign(set, { doneAt: now.toISOString(), doneBy: actor });
|
||||
if (input.done === false) Object.assign(set, { doneAt: null, doneBy: null });
|
||||
if (Object.keys(set).length > 0) await db.update(todos).set(set).where(eq(todos.id, id));
|
||||
const [after] = await db.select().from(todos).where(eq(todos.id, id)).limit(1);
|
||||
return { before, after: (await shape([after]))[0] };
|
||||
}
|
||||
|
||||
export async function removeTodo(id: number): Promise<TodoItem | null> {
|
||||
const [row] = await db.select().from(todos).where(eq(todos.id, id)).limit(1);
|
||||
if (!row) return null;
|
||||
const item = (await shape([row]))[0];
|
||||
await db.delete(todos).where(eq(todos.id, id));
|
||||
return item;
|
||||
}
|
||||
|
||||
/** Open jobs handed to a person, for My work. */
|
||||
export async function openTodosFor(userId: number): Promise<TodoItem[]> {
|
||||
const rows = await db.select().from(todos).where(and(isNull(todos.doneAt), eq(todos.assigneeUserId, userId)));
|
||||
const items = await shape(rows);
|
||||
return items.sort((a, b) => (a.dueDate ?? "9999-99-99").localeCompare(b.dueDate ?? "9999-99-99") || a.id - b.id);
|
||||
}
|
||||
|
||||
// ─── Notes ─────────────────────────────────────────────────────────────────
|
||||
|
||||
export interface NoteItem {
|
||||
targetType: TargetType;
|
||||
targetId: number;
|
||||
body: string;
|
||||
updatedBy: string | null;
|
||||
updatedAt: string;
|
||||
}
|
||||
|
||||
export async function getNote(type: TargetType, id: number): Promise<NoteItem | null> {
|
||||
const [row] = await db.select().from(notes).where(and(eq(notes.targetType, type), eq(notes.targetId, id))).limit(1);
|
||||
return row ? { targetType: row.targetType, targetId: row.targetId, body: row.body, updatedBy: row.updatedBy, updatedAt: row.updatedAt } : null;
|
||||
}
|
||||
|
||||
/** Writes the note of something; an empty text takes it away. Returns what is there now (null when there is none). */
|
||||
export async function setNote(type: TargetType, id: number, body: string, actor: string, now: Date = new Date()): Promise<NoteItem | null> {
|
||||
const text = body.replace(/\r\n/g, "\n").trimEnd();
|
||||
if (text.length > MAX_NOTE_BODY) throw new TodoError(400, "too_long", `Keep the note under ${MAX_NOTE_BODY} characters.`);
|
||||
if (!(await targetName(type, id))) throw new TodoError(404, "unknown_target", "That server or network device is not there.");
|
||||
if (text.trim() === "") {
|
||||
await db.delete(notes).where(and(eq(notes.targetType, type), eq(notes.targetId, id)));
|
||||
return null;
|
||||
}
|
||||
const values = { body: text, updatedBy: actor, updatedAt: now.toISOString() };
|
||||
await db.insert(notes).values({ targetType: type, targetId: id, ...values }).onConflictDoUpdate({ target: [notes.targetType, notes.targetId], set: values });
|
||||
return getNote(type, id);
|
||||
}
|
||||
@@ -129,7 +129,7 @@ describe("through a report, the page, the alerts and the figures", () => {
|
||||
|
||||
before(async () => {
|
||||
await db.insert(users).values([{ oidcSub: "admin", name: "Ada", role: "admin" }]);
|
||||
const [s] = await db.insert(servers).values({ name: "elsa", apiTokenHash: hashToken("tok"), apiTokenPrefix: "t", lastSeenAt: new Date().toISOString(), agentVersion: 6 }).returning();
|
||||
const [s] = await db.insert(servers).values({ name: "elsa", apiTokenHash: hashToken("tok"), apiTokenPrefix: "t", lastSeenAt: new Date().toISOString(), agentVersion: 7 }).returning();
|
||||
id = s.id;
|
||||
const a = express();
|
||||
a.use("/api/agent/report", agentReportRouter);
|
||||
@@ -155,7 +155,7 @@ describe("through a report, the page, the alerts and the figures", () => {
|
||||
});
|
||||
|
||||
const report = (system: unknown) =>
|
||||
fetch(`${base}/agent/report`, { method: "POST", headers: { "content-type": "application/json", authorization: "Bearer tok" }, body: JSON.stringify({ hostname: "h", os_type: "linux", agent_version: 6, tasks: [], system }) });
|
||||
fetch(`${base}/agent/report`, { method: "POST", headers: { "content-type": "application/json", authorization: "Bearer tok" }, body: JSON.stringify({ hostname: "h", os_type: "linux", agent_version: 7, tasks: [], system }) });
|
||||
const detail = async () => await (await fetch(`${base}/servers/${id}/detail`)).json();
|
||||
const patch = (body: unknown) => fetch(`${base}/servers/${id}`, { method: "PATCH", headers: { "content-type": "application/json" }, body: JSON.stringify(body) });
|
||||
|
||||
|
||||
@@ -0,0 +1,137 @@
|
||||
/**
|
||||
* Suggested dependencies: an integration reached at a server's own address is offered as needing that server; what is already so, would make a loop, or was
|
||||
* said no to is left out; and adding one writes an ordinary dependency.
|
||||
*
|
||||
* Run with: npm test --workspace server
|
||||
*/
|
||||
import { test, describe, before, after } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { mkdtempSync, rmSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import type { AddressInfo } from "node:net";
|
||||
import type { Server } from "node:http";
|
||||
|
||||
const dir = mkdtempSync(join(tmpdir(), "hm-depsug-"));
|
||||
process.env.DATABASE_PATH = join(dir, "test.sqlite");
|
||||
process.env.SESSION_DIR = join(dir, "sessions");
|
||||
process.env.CREDENTIALS_ENCRYPTION_KEY = "ab".repeat(32);
|
||||
|
||||
const express = (await import("express")).default;
|
||||
const { db } = await import("../src/db/client.js");
|
||||
const { users, servers, integrations, dependencies, auditLog } = await import("../src/db/schema.js");
|
||||
const { runMigrations } = await import("../src/db/migrate.js");
|
||||
const { suggestDependencies, hostOfIntegration } = await import("../src/services/dependencySuggestions.js");
|
||||
const { dependenciesRouter } = await import("../src/routes/dependencies.js");
|
||||
const { eq } = await import("drizzle-orm");
|
||||
|
||||
await runMigrations();
|
||||
|
||||
describe("what is offered", () => {
|
||||
const servers = [
|
||||
{ id: 1, name: "nas", hostname: "nas.lan", ips: ["10.0.0.5"] },
|
||||
{ id: 2, name: "elsa", hostname: null, ips: ["10.0.0.9"] },
|
||||
];
|
||||
const integ = (id: number, name: string, type: string, host: string | null, enabled = true) => ({ id, name, type, enabled, host });
|
||||
const none = new Map<string, string[]>();
|
||||
|
||||
test("an integration at a server's own address (its IP, hostname or short name) needs that server", () => {
|
||||
const { suggestions } = suggestDependencies({
|
||||
integrations: [integ(1, "Gitea", "gitea", "10.0.0.5"), integ(2, "Kuma", "uptimekuma", "nas.lan"), integ(3, "IPAM", "phpipam", "elsa"), integ(4, "Cloud", "tailscale", "api.tailscale.com")],
|
||||
servers,
|
||||
edges: none,
|
||||
dismissed: new Set(),
|
||||
});
|
||||
assert.deepEqual(suggestions.map((x) => x.key), ["integration:1>server:1", "integration:3>server:2", "integration:2>server:1"], "by name (Gitea, IPAM, Kuma); the one in the cloud matches nothing");
|
||||
assert.ok(suggestions.every((x) => x.reason.includes("most likely runs there")));
|
||||
});
|
||||
|
||||
test("a disabled integration, one with no address, and one with an unknown host are not offered", () => {
|
||||
const { suggestions } = suggestDependencies({ integrations: [integ(1, "Off", "gitea", "10.0.0.5", false), integ(2, "None", "gitea", null), integ(3, "Elsewhere", "gitea", "10.9.9.9")], servers, edges: none, dismissed: new Set() });
|
||||
assert.equal(suggestions.length, 0);
|
||||
});
|
||||
|
||||
test("what is already so, and what would make a loop, is left out", () => {
|
||||
const already = new Map([["integration:1", ["server:1"]]]);
|
||||
assert.equal(suggestDependencies({ integrations: [integ(1, "Gitea", "gitea", "10.0.0.5")], servers, edges: already, dismissed: new Set() }).suggestions.length, 0);
|
||||
// the server needs the integration already: the other way round would make them wait for each other
|
||||
const loop = new Map([["server:1", ["integration:1"]]]);
|
||||
assert.equal(suggestDependencies({ integrations: [integ(1, "Gitea", "gitea", "10.0.0.5")], servers, edges: loop, dismissed: new Set() }).suggestions.length, 0);
|
||||
});
|
||||
|
||||
test("what somebody said no to is kept apart, and can come back", () => {
|
||||
const r = suggestDependencies({ integrations: [integ(1, "Gitea", "gitea", "10.0.0.5")], servers, edges: none, dismissed: new Set(["integration:1>server:1"]) });
|
||||
assert.deepEqual([r.suggestions.length, r.dismissed.length], [0, 1]);
|
||||
});
|
||||
|
||||
test("where an integration is reached: a URL's host, or osTicket's database host", () => {
|
||||
assert.equal(hostOfIntegration("https://git.example.lan:3000/", "{}"), "git.example.lan");
|
||||
assert.equal(hostOfIntegration("", JSON.stringify({ host: "osticket-db.lan" })), "osticket-db.lan");
|
||||
assert.equal(hostOfIntegration("not a url", null), null);
|
||||
assert.equal(hostOfIntegration(null, "not json"), null);
|
||||
});
|
||||
});
|
||||
|
||||
describe("over the routes", () => {
|
||||
let app: Server;
|
||||
let base = "";
|
||||
let who = "viewer";
|
||||
before(async () => {
|
||||
await db.insert(users).values([
|
||||
{ oidcSub: "viewer", name: "Vic", role: "viewer" },
|
||||
{ oidcSub: "operator", name: "Olga", role: "operator" },
|
||||
]);
|
||||
await db.insert(servers).values({ name: "nas", hostname: "nas.lan", apiTokenHash: "n", apiTokenPrefix: "n", ipAddresses: JSON.stringify(["10.0.0.5"]) });
|
||||
await db.insert(integrations).values({ type: "gitea", name: "Gitea", baseUrl: "http://10.0.0.5:3000", config: JSON.stringify({ url: "http://10.0.0.5:3000" }) });
|
||||
const a = express();
|
||||
a.use(express.json());
|
||||
a.use((req, _res, next) => {
|
||||
(req as unknown as { session: unknown }).session = { user: { sub: who } };
|
||||
next();
|
||||
});
|
||||
a.use("/api/dependencies", dependenciesRouter);
|
||||
app = await new Promise<Server>((resolve) => {
|
||||
const s = a.listen(0, "127.0.0.1", () => resolve(s));
|
||||
});
|
||||
base = `http://127.0.0.1:${(app.address() as AddressInfo).port}/api/dependencies`;
|
||||
});
|
||||
after(() => {
|
||||
app.close();
|
||||
try {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
} catch {
|
||||
// Windows may still hold the database file
|
||||
}
|
||||
});
|
||||
const post = (path: string, body: unknown = {}) => fetch(`${base}${path}`, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(body) });
|
||||
|
||||
test("anyone signed in sees them; only an operator adds or dismisses", async () => {
|
||||
who = "viewer";
|
||||
const body = (await (await fetch(`${base}/suggestions`)).json()) as any;
|
||||
assert.deepEqual(body.suggestions.map((s: any) => s.key), ["integration:1>server:1"]);
|
||||
assert.equal((await post("/suggestions/add", { keys: ["integration:1>server:1"] })).status, 403);
|
||||
assert.equal((await post("/suggestions/dismiss", { keys: ["integration:1>server:1"] })).status, 403);
|
||||
});
|
||||
|
||||
test("dismissing hides it, restoring brings it back, and a bad body is refused", async () => {
|
||||
who = "operator";
|
||||
const hidden = (await (await post("/suggestions/dismiss", { keys: ["integration:1>server:1"] })).json()) as any;
|
||||
assert.deepEqual([hidden.suggestions.length, hidden.dismissed.length], [0, 1]);
|
||||
const back = (await (await post("/suggestions/restore")).json()) as any;
|
||||
assert.equal(back.suggestions.length, 1);
|
||||
assert.equal((await post("/suggestions/add", { keys: [] })).status, 400);
|
||||
});
|
||||
|
||||
test("adding writes an ordinary dependency, written down, and it is no longer offered", async () => {
|
||||
who = "operator";
|
||||
const res = (await (await post("/suggestions/add", { keys: ["integration:1>server:1", "integration:9>server:9"] })).json()) as any;
|
||||
assert.deepEqual(res.added.map((a: any) => [a.from, a.to]), [["Gitea", "nas"]], "a key that is not offered adds nothing");
|
||||
assert.equal(res.suggestions.length, 0);
|
||||
const rows = await db.select().from(dependencies);
|
||||
assert.deepEqual(rows.map((r) => [r.fromType, r.fromId, r.toType, r.toId]), [["integration", 1, "server", 1]]);
|
||||
const written = (await db.select().from(auditLog).where(eq(auditLog.category, "dependency")))[0];
|
||||
assert.equal(written.action, "create");
|
||||
const list = (await (await fetch(base)).json()) as any;
|
||||
assert.equal(list.dependencies.length, 1, "and the page lists it with the rest");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,415 @@
|
||||
/**
|
||||
* Nginx Proxy Manager and Home Assistant: reading each (against pretend ones), what is kept, what makes an alert, and the suggestions NPM feeds.
|
||||
*
|
||||
* Run with: npm test --workspace server
|
||||
*/
|
||||
import { test, describe, before, after } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { mkdtempSync, rmSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { createServer, type IncomingMessage, type Server, type ServerResponse } from "node:http";
|
||||
import type { AddressInfo } from "node:net";
|
||||
|
||||
const dir = mkdtempSync(join(tmpdir(), "hm-npmha-"));
|
||||
process.env.DATABASE_PATH = join(dir, "test.sqlite");
|
||||
process.env.SESSION_DIR = join(dir, "sessions");
|
||||
process.env.CREDENTIALS_ENCRYPTION_KEY = "ab".repeat(32);
|
||||
process.env.HEALTH_STARTUP_GRACE_MS = "0";
|
||||
|
||||
const express = (await import("express")).default;
|
||||
const { db } = await import("../src/db/client.js");
|
||||
const { users, servers, integrations, integrationCredentials, haEntities, haReadings, haWatched } = await import("../src/db/schema.js");
|
||||
const { runMigrations } = await import("../src/db/migrate.js");
|
||||
const { encryptSecret } = await import("../src/crypto.js");
|
||||
const { createNpmAdapter, isoOf, hostFrom } = await import("../src/integrations/npm/adapter.js");
|
||||
const { createHomeAssistantAdapter, entityFrom } = await import("../src/integrations/homeassistant/adapter.js");
|
||||
const { npmProblems, readNpm, clearNpmCacheForTests } = await import("../src/services/npmReader.js");
|
||||
const { haProblems, trackUnavailable, syncHomeAssistant, removeHomeAssistantData, isHaFresh, haSyncStatuses } = await import("../src/services/homeAssistantSync.js");
|
||||
const { suggestChecks, loadSuggestions } = await import("../src/services/endpointSuggestions.js");
|
||||
const { getAlerts, invalidateAlertsCache } = await import("../src/services/alerts.js");
|
||||
const { npmRouter } = await import("../src/routes/npm.js");
|
||||
const { homeAssistantRouter } = await import("../src/routes/homeAssistant.js");
|
||||
const { validateIntegrationConfig } = await import("../src/integrations/fieldSchemas.js");
|
||||
const { eq } = await import("drizzle-orm");
|
||||
|
||||
await runMigrations();
|
||||
|
||||
const DAY = 86_400_000;
|
||||
const inDays = (n: number) => new Date(Date.now() + n * DAY).toISOString();
|
||||
const npmTime = (n: number) => inDays(n).replace("T", " ").slice(0, 19);
|
||||
|
||||
// ─── A pretend Nginx Proxy Manager ──────────────────────────────────────────
|
||||
|
||||
let npmPassword = "pw";
|
||||
const npmCertificates = [
|
||||
{ id: 1, nice_name: "wiki.example.com", provider: "letsencrypt", domain_names: ["wiki.example.com"], expires_on: npmTime(40) },
|
||||
{ id: 2, nice_name: "git.example.com", provider: "letsencrypt", domain_names: ["git.example.com"], expires_on: npmTime(2) },
|
||||
{ id: 3, nice_name: "old.example.com", provider: "letsencrypt", domain_names: ["old.example.com"], expires_on: npmTime(-5) },
|
||||
];
|
||||
const npmHosts = [
|
||||
{ id: 10, domain_names: ["wiki.example.com"], forward_scheme: "http", forward_host: "10.0.0.5", forward_port: 8080, enabled: 1, ssl_forced: 1, certificate: npmCertificates[0], meta: { nginx_online: true } },
|
||||
{ id: 11, domain_names: ["git.example.com"], forward_scheme: "http", forward_host: "10.0.0.6", forward_port: 3000, enabled: 1, ssl_forced: 1, certificate: npmCertificates[1], meta: { nginx_online: true } },
|
||||
// A certificate that ran out, but on a host that is switched off: nobody is served it, so it is not a problem.
|
||||
{ id: 12, domain_names: ["old.example.com"], forward_scheme: "http", forward_host: "10.0.0.7", forward_port: 80, enabled: 0, ssl_forced: 0, certificate: npmCertificates[2], meta: {} },
|
||||
{ id: 13, domain_names: ["broken.example.com"], forward_scheme: "http", forward_host: "10.0.0.8", forward_port: 80, enabled: 1, ssl_forced: 0, certificate: null, meta: { nginx_online: false, nginx_err: "host not found in upstream" } },
|
||||
];
|
||||
let npmRequests: string[] = [];
|
||||
|
||||
function reply(res: ServerResponse, status: number, body: unknown) {
|
||||
res.writeHead(status, { "content-type": "application/json" });
|
||||
res.end(JSON.stringify(body));
|
||||
}
|
||||
|
||||
function npmRoute(req: IncomingMessage, res: ServerResponse) {
|
||||
const url = req.url ?? "";
|
||||
npmRequests.push(`${req.method} ${url.split("?")[0]}`);
|
||||
if (url === "/api/tokens" && req.method === "POST") {
|
||||
let raw = "";
|
||||
req.on("data", (c) => (raw += c));
|
||||
req.on("end", () => {
|
||||
const b = JSON.parse(raw || "{}");
|
||||
if (b.identity === "viewer@example.com" && b.secret === npmPassword) return reply(res, 200, { token: "npm-token", expires: inDays(1) });
|
||||
return reply(res, 400, { error: { message: "Invalid email or password" } });
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (req.headers.authorization !== "Bearer npm-token") return reply(res, 401, {});
|
||||
if (url.startsWith("/api/nginx/proxy-hosts")) return reply(res, 200, npmHosts);
|
||||
if (url.startsWith("/api/nginx/certificates")) return reply(res, 200, npmCertificates);
|
||||
if (url === "/api/") return reply(res, 200, { status: "OK", version: { major: 2, minor: 11, revision: 3 } });
|
||||
return reply(res, 404, {});
|
||||
}
|
||||
|
||||
// ─── A pretend Home Assistant ───────────────────────────────────────────────
|
||||
|
||||
let haToken = "good-token";
|
||||
let haStates: Record<string, unknown>[] = [];
|
||||
const state = (entity_id: string, s: string, attributes: Record<string, unknown> = {}) => ({ entity_id, state: s, attributes, last_changed: "2026-10-20T08:00:00+00:00" });
|
||||
function haRoute(req: IncomingMessage, res: ServerResponse) {
|
||||
if (req.headers.authorization !== `Bearer ${haToken}`) return reply(res, 401, { message: "401: Unauthorized" });
|
||||
if (req.url === "/api/") return reply(res, 200, { message: "API running." });
|
||||
if (req.url === "/api/states") return reply(res, 200, haStates);
|
||||
return reply(res, 404, {});
|
||||
}
|
||||
|
||||
let npmServer: Server;
|
||||
let haServer: Server;
|
||||
let npmUrl = "";
|
||||
let haUrl = "";
|
||||
let npmId = 0;
|
||||
let haId = 0;
|
||||
|
||||
before(async () => {
|
||||
npmServer = await new Promise<Server>((resolve) => {
|
||||
const s = createServer(npmRoute).listen(0, "127.0.0.1", () => resolve(s));
|
||||
});
|
||||
haServer = await new Promise<Server>((resolve) => {
|
||||
const s = createServer(haRoute).listen(0, "127.0.0.1", () => resolve(s));
|
||||
});
|
||||
npmUrl = `http://127.0.0.1:${(npmServer.address() as AddressInfo).port}`;
|
||||
haUrl = `http://127.0.0.1:${(haServer.address() as AddressInfo).port}`;
|
||||
});
|
||||
after(() => {
|
||||
npmServer.close();
|
||||
haServer.close();
|
||||
try {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
} catch {
|
||||
// Windows may still hold the database file
|
||||
}
|
||||
});
|
||||
|
||||
describe("normalising what they say", () => {
|
||||
test("NPM writes its times without a zone, and they are UTC", () => {
|
||||
assert.equal(isoOf("2026-12-31 23:59:59"), "2026-12-31T23:59:59.000Z");
|
||||
assert.equal(isoOf("2026-12-31T23:59:59+01:00"), "2026-12-31T22:59:59.000Z");
|
||||
assert.equal(isoOf("nonsense"), null);
|
||||
assert.equal(isoOf(null), null);
|
||||
});
|
||||
|
||||
test("a proxy host: where it goes, whether it is on, and that NPM's own error makes it not working; a host with no id is nothing", () => {
|
||||
const h = hostFrom(npmHosts[3])!;
|
||||
assert.deepEqual([h.domains, h.forwardHost, h.forwardPort, h.enabled, h.online, h.error], [["broken.example.com"], "10.0.0.8", 80, true, false, "host not found in upstream"]);
|
||||
const ok = hostFrom(npmHosts[0])!;
|
||||
assert.deepEqual([ok.sslForced, ok.online, ok.certificate?.id], [true, true, 1]);
|
||||
assert.equal(hostFrom({ domain_names: ["x"] }), null);
|
||||
assert.equal(hostFrom(npmHosts[2])!.enabled, false);
|
||||
});
|
||||
|
||||
test("a Home Assistant entity: a number when its state is one, unavailable when it says nothing, its friendly name or else its id", () => {
|
||||
const e = entityFrom(state("sensor.ups_battery", "87.5", { friendly_name: "UPS battery", unit_of_measurement: "%", device_class: "battery" }))!;
|
||||
assert.deepEqual([e.name, e.value, e.unit, e.deviceClass, e.unavailable], ["UPS battery", 87.5, "%", "battery", false]);
|
||||
const off = entityFrom(state("switch.pump", "unavailable"))!;
|
||||
assert.deepEqual([off.name, off.value, off.unavailable], ["switch.pump", null, true]);
|
||||
assert.equal(entityFrom(state("light.x", "on"))!.value, null, "on/off is not a number");
|
||||
assert.equal(entityFrom(state("sensor.blank", ""))!.value, null);
|
||||
assert.equal(entityFrom({ state: "1" }), null);
|
||||
});
|
||||
});
|
||||
|
||||
describe("Nginx Proxy Manager", () => {
|
||||
const cfg = (extra: Record<string, unknown> = {}) => ({ url: npmUrl, email: "viewer@example.com", password: npmPassword, ...extra }) as never;
|
||||
|
||||
test("signs in, then only reads: hosts with their certificate, the certificates, the version", async () => {
|
||||
npmRequests = [];
|
||||
const snap = await createNpmAdapter(cfg()).fetchSnapshot();
|
||||
assert.equal(snap.version, "2.11.3");
|
||||
assert.equal(snap.hosts.length, 4);
|
||||
assert.equal(snap.certificates.length, 3);
|
||||
assert.ok(npmRequests.every((r) => r.startsWith("GET") || r === "POST /api/tokens"), "nothing but the sign-in and reads");
|
||||
});
|
||||
|
||||
test("a wrong password is said plainly, and the login check works as a ping", async () => {
|
||||
await assert.rejects(createNpmAdapter(cfg({ password: "wrong" })).fetchSnapshot(), /did not accept the email and password/);
|
||||
assert.equal((await createNpmAdapter(cfg()).ping()).ok, true);
|
||||
assert.equal((await createNpmAdapter(cfg({ password: "wrong" })).ping()).ok, false);
|
||||
});
|
||||
|
||||
test("certificates in use that run out soon are a problem — critical in the last days, and an expired one too — but not on a host that is off", async () => {
|
||||
const snap = await createNpmAdapter(cfg()).fetchSnapshot();
|
||||
const problems = npmProblems(1, "Edge NPM", snap, new Date());
|
||||
const by = Object.fromEntries(problems.map((p) => [p.key, p]));
|
||||
assert.deepEqual(Object.keys(by).sort(), ["npm-cert:1:2", "npm-host:1:13"], "wiki is fine (40 days), old is on a host that is off");
|
||||
assert.equal(by["npm-cert:1:2"].severity, "critical");
|
||||
assert.match(by["npm-cert:1:2"].message, /git\.example\.com.*expires in [12] days?/);
|
||||
assert.match(by["npm-host:1:13"].message, /broken\.example\.com is not working.*host not found in upstream/);
|
||||
const later = npmProblems(1, "Edge NPM", snap, new Date(Date.now() + 30 * DAY));
|
||||
assert.ok(later.some((p) => p.key === "npm-cert:1:1"), "and the wiki one in time");
|
||||
assert.match(later.find((p) => p.key === "npm-cert:1:2")!.message, /expired 2[89] days ago/);
|
||||
});
|
||||
});
|
||||
|
||||
describe("Home Assistant", () => {
|
||||
const cfg = (extra: Record<string, unknown> = {}) => ({ url: haUrl, token: haToken, ...extra }) as never;
|
||||
|
||||
test("lists every entity with the token; a refused token and a wrong address are said plainly", async () => {
|
||||
haStates = [state("sensor.ups_battery", "87", { friendly_name: "UPS battery", unit_of_measurement: "%" }), state("light.hall", "on")];
|
||||
assert.equal((await createHomeAssistantAdapter(cfg()).listEntities()).length, 2);
|
||||
await assert.rejects(createHomeAssistantAdapter(cfg({ token: "nope" })).listEntities(), /did not accept the token/);
|
||||
const ping = await createHomeAssistantAdapter(cfg()).ping();
|
||||
assert.equal(ping.ok, true);
|
||||
});
|
||||
|
||||
test("an entity that says nothing is only a problem after a while, and a number only past a limit that was set", () => {
|
||||
const now = new Date("2026-10-20T12:00:00Z");
|
||||
const watched = [
|
||||
{ entityId: "sensor.ups", name: "UPS", above: null, below: 30 },
|
||||
{ entityId: "sensor.freezer", name: null, above: -10, below: null },
|
||||
{ entityId: "switch.pump", name: "Pump", above: null, below: null },
|
||||
{ entityId: "sensor.fine", name: null, above: 50, below: 10 },
|
||||
];
|
||||
const rows = new Map<string, { friendlyName: string | null; state: string; value: number | null; unit: string | null; unavailableSince: string | null }>([
|
||||
["sensor.ups", { friendlyName: "UPS battery", state: "22", value: 22, unit: "%", unavailableSince: null }],
|
||||
["sensor.freezer", { friendlyName: "Freezer", state: "-18", value: -18, unit: "°C", unavailableSince: null }],
|
||||
["switch.pump", { friendlyName: null, state: "unavailable", value: null, unit: null, unavailableSince: "2026-10-20T11:30:00Z" }],
|
||||
["sensor.fine", { friendlyName: null, state: "20", value: 20, unit: null, unavailableSince: null }],
|
||||
]);
|
||||
const problems = haProblems(3, "Home", watched, rows, now);
|
||||
assert.deepEqual(problems.map((p) => p.key), ["ha-limit:3:sensor.ups", "ha-unavailable:3:switch.pump"]);
|
||||
assert.match(problems[0].message, /UPS is 22 %, below its limit of 30 %/);
|
||||
assert.match(problems[1].message, /Pump has been unavailable in Home Assistant for 30 min/);
|
||||
rows.get("switch.pump")!.unavailableSince = "2026-10-20T11:55:00Z";
|
||||
assert.equal(haProblems(3, "Home", watched, rows, now).length, 1, "five minutes of silence is nothing");
|
||||
});
|
||||
|
||||
test("when an entity first went quiet is kept until it speaks again", () => {
|
||||
assert.equal(trackUnavailable(null, true, "T1"), "T1");
|
||||
assert.equal(trackUnavailable("T0", true, "T1"), "T0");
|
||||
assert.equal(trackUnavailable("T0", false, "T1"), null);
|
||||
});
|
||||
});
|
||||
|
||||
describe("kept in the app", () => {
|
||||
before(async () => {
|
||||
await db.insert(users).values([
|
||||
{ oidcSub: "viewer", name: "Vic", role: "viewer" },
|
||||
{ oidcSub: "operator", name: "Olga", role: "operator" },
|
||||
]);
|
||||
const [npmCred] = await db.insert(integrationCredentials).values({ name: "npm:Edge", encryptedSecret: encryptSecret(JSON.stringify({ password: "pw" })) }).returning();
|
||||
const [npmRow] = await db.insert(integrations).values({ type: "npm", name: "Edge NPM", baseUrl: npmUrl, credentialId: npmCred.id, config: JSON.stringify({ url: npmUrl, email: "viewer@example.com" }) }).returning();
|
||||
npmId = npmRow.id;
|
||||
const [haCred] = await db.insert(integrationCredentials).values({ name: "ha:Home", encryptedSecret: encryptSecret(JSON.stringify({ token: "good-token" })) }).returning();
|
||||
const [haRow] = await db.insert(integrations).values({ type: "homeassistant", name: "Home", baseUrl: haUrl, credentialId: haCred.id, config: JSON.stringify({ url: haUrl }) }).returning();
|
||||
haId = haRow.id;
|
||||
await db.insert(servers).values({ name: "wiki-host", hostname: "wiki-host.example.lan", ipAddresses: JSON.stringify(["10.0.0.5"]), osType: "linux", apiTokenHash: "x", apiTokenPrefix: "x" } as never);
|
||||
});
|
||||
|
||||
test("the field forms need what each one needs", () => {
|
||||
assert.deepEqual(validateIntegrationConfig("npm", { url: "https://npm", email: "a@b" }), ["password"]);
|
||||
assert.deepEqual(validateIntegrationConfig("homeassistant", { url: "https://ha" }), ["token"]);
|
||||
assert.deepEqual(validateIntegrationConfig("homeassistant", { url: "https://ha", token: "t" }), []);
|
||||
});
|
||||
|
||||
test("NPM is read once a minute at most, and a failed read is kept just as briefly", async () => {
|
||||
clearNpmCacheForTests();
|
||||
npmRequests = [];
|
||||
const a = await readNpm(npmId);
|
||||
const b = await readNpm(npmId);
|
||||
assert.ok(a.snapshot && b.snapshot);
|
||||
assert.equal(npmRequests.filter((r) => r === "POST /api/tokens").length, 1);
|
||||
await readNpm(npmId, true);
|
||||
assert.equal(npmRequests.filter((r) => r === "POST /api/tokens").length, 2, "asking again is possible");
|
||||
});
|
||||
|
||||
test("on the Alerts page: the certificate in its last days and the host NPM can't set up, from the right place", async () => {
|
||||
clearNpmCacheForTests();
|
||||
invalidateAlertsCache();
|
||||
const report = await getAlerts(true);
|
||||
const cert = report.alerts.find((a) => a.id === `expiry:npm-cert:${npmId}:2`)!;
|
||||
assert.ok(cert, "the certificate that runs out");
|
||||
assert.deepEqual([cert.severity, cert.source, cert.link, cert.target?.type], ["critical", "Nginx Proxy Manager", "/proxy-hosts", "integration"]);
|
||||
const host = report.alerts.find((a) => a.id === `monitoring:npm-host:${npmId}:13`)!;
|
||||
assert.ok(host);
|
||||
assert.equal(report.alerts.some((a) => a.id === `expiry:npm-cert:${npmId}:3`), false, "the one on the host that is off is left alone");
|
||||
});
|
||||
|
||||
test("an NPM that can't be read says so rather than being quiet", async () => {
|
||||
npmPassword = "changed";
|
||||
clearNpmCacheForTests();
|
||||
invalidateAlertsCache();
|
||||
const report = await getAlerts(true);
|
||||
assert.ok(report.couldntCheck.some((c) => /Nginx Proxy Manager/.test(c.name)));
|
||||
assert.equal(report.alerts.some((a) => a.id.startsWith("expiry:npm-cert")), false, "and raises nothing from what it could not read");
|
||||
npmPassword = "pw";
|
||||
clearNpmCacheForTests();
|
||||
});
|
||||
|
||||
test("hosts NPM serves are offered as checks on the name people use, linked to the server it forwards to — and not when a check already covers it", async () => {
|
||||
invalidateAlertsCache();
|
||||
const { suggestions } = await loadSuggestions();
|
||||
const wiki = suggestions.find((s) => s.target === "https://wiki.example.com/")!;
|
||||
assert.ok(wiki);
|
||||
assert.deepEqual([wiki.kind, wiki.serverName], ["http", "wiki-host"]);
|
||||
assert.match(wiki.reason, /Edge NPM serves wiki\.example\.com, forwarding to wiki-host/);
|
||||
assert.ok(suggestions.some((s) => s.target === "http://broken.example.com/"), "a host with no certificate is a plain web check");
|
||||
assert.ok(!suggestions.some((s) => /old\.example\.com/.test(s.target)), "a host that is off is not offered");
|
||||
const none = suggestChecks({ servers: [], dns: [], secrets: [], dismissed: new Set(), existing: [{ kind: "http", target: "https://wiki.example.com/", port: null }], proxied: [{ domain: "wiki.example.com", secure: true, serverId: null, serverName: null, integrationName: "x" }, { domain: "*.example.com", secure: true, serverId: null, serverName: null, integrationName: "x" }, { domain: "10.0.0.1", secure: false, serverId: null, serverName: null, integrationName: "x" }] });
|
||||
assert.deepEqual(none.suggestions, []);
|
||||
});
|
||||
|
||||
test("the NPM page: each host with the server it forwards to, its certificate, and the problems; anyone signed in may see it", async () => {
|
||||
const a = express();
|
||||
a.use(express.json());
|
||||
a.use((req, _res, next) => {
|
||||
(req as unknown as { session: unknown }).session = { user: { sub: "viewer" } };
|
||||
next();
|
||||
});
|
||||
a.use("/api/npm", npmRouter);
|
||||
const s = await new Promise<Server>((resolve) => {
|
||||
const x = a.listen(0, "127.0.0.1", () => resolve(x));
|
||||
});
|
||||
const res = await fetch(`http://127.0.0.1:${(s.address() as AddressInfo).port}/api/npm?refresh=1`);
|
||||
assert.equal(res.status, 200);
|
||||
const body = (await res.json()) as any;
|
||||
const npm = body.integrations[0];
|
||||
assert.equal(npm.version, "2.11.3");
|
||||
const wiki = npm.hosts.find((h: any) => h.id === 10);
|
||||
assert.deepEqual([wiki.server?.name, wiki.certificate.name], ["wiki-host", "wiki.example.com"]);
|
||||
assert.equal(npm.hosts.find((h: any) => h.id === 11).server, null, "an address no server has is left unmatched");
|
||||
assert.equal(npm.problems.length, 2);
|
||||
s.close();
|
||||
});
|
||||
|
||||
describe("Home Assistant stored", () => {
|
||||
let app: Server;
|
||||
let base = "";
|
||||
let who = "viewer";
|
||||
before(async () => {
|
||||
const a = express();
|
||||
a.use(express.json());
|
||||
a.use((req, _res, next) => {
|
||||
(req as unknown as { session: unknown }).session = { user: { sub: who } };
|
||||
next();
|
||||
});
|
||||
a.use("/api/home-assistant", homeAssistantRouter);
|
||||
app = await new Promise<Server>((resolve) => {
|
||||
const s = a.listen(0, "127.0.0.1", () => resolve(s));
|
||||
});
|
||||
base = `http://127.0.0.1:${(app.address() as AddressInfo).port}/api/home-assistant`;
|
||||
});
|
||||
after(() => app.close());
|
||||
|
||||
const put = (body: unknown) => fetch(`${base}/${haId}/watched`, { method: "PUT", headers: { "content-type": "application/json" }, body: JSON.stringify(body) });
|
||||
|
||||
test("choosing what to watch needs an operator; only entities that exist and sensible limits are accepted", async () => {
|
||||
haStates = [state("sensor.ups_battery", "87", { friendly_name: "UPS battery", unit_of_measurement: "%" }), state("switch.pump", "on", { friendly_name: "Pump" }), state("light.hall", "on")];
|
||||
who = "viewer";
|
||||
assert.equal((await put({ entityId: "sensor.ups_battery" })).status, 403);
|
||||
assert.equal((await fetch(`${base}/${haId}/entities`)).status, 403);
|
||||
who = "operator";
|
||||
assert.equal((await put({ entityId: "not an entity" })).status, 400);
|
||||
assert.equal((await put({ entityId: "sensor.ups_battery", above: 10, below: 20 })).status, 400, "the lower limit has to be lower");
|
||||
assert.equal((await fetch(`${base}/999/entities`)).status, 404);
|
||||
const list = (await (await fetch(`${base}/${haId}/entities?q=ups`)).json()) as any;
|
||||
assert.deepEqual(list.entities.map((e: any) => e.entityId), ["sensor.ups_battery"]);
|
||||
const ok = await put({ entityId: "sensor.ups_battery", name: "UPS", below: 30 });
|
||||
assert.equal(ok.status, 200);
|
||||
assert.equal(((await ok.json()) as any).status.ok, true);
|
||||
assert.equal((await put({ entityId: "switch.pump" })).status, 200);
|
||||
const [row] = await db.select().from(haEntities).where(eq(haEntities.entityId, "sensor.ups_battery"));
|
||||
assert.deepEqual([row.state, row.value, row.unit], ["87", 87, "%"]);
|
||||
});
|
||||
|
||||
test("the page lists what is watched with its state; a number keeps a point of history, but two passes close together keep one", async () => {
|
||||
who = "viewer";
|
||||
const body = (await (await fetch(base)).json()) as any;
|
||||
const ha = body.integrations[0];
|
||||
assert.deepEqual(ha.watched.map((w) => [w.entityId, w.entity?.state]), [["switch.pump", "on"], ["sensor.ups_battery", "87"]], "by the name it is shown under: Pump, then UPS");
|
||||
assert.equal(ha.fresh, true);
|
||||
await syncHomeAssistant(haId);
|
||||
const readings = await db.select().from(haReadings).where(eq(haReadings.entityId, "sensor.ups_battery"));
|
||||
assert.equal(readings.length, 1, "the pump is not a number, the second pass is too soon");
|
||||
const hist = (await (await fetch(`${base}/${haId}/history?entity=sensor.ups_battery&days=7`)).json()) as any;
|
||||
assert.equal(hist.days, 7);
|
||||
assert.ok(hist.points.length >= 1);
|
||||
});
|
||||
|
||||
test("a number past its limit is an alert, an entity that goes quiet becomes one after a while, and a Home Assistant that is down holds them rather than clearing", async () => {
|
||||
haStates = [state("sensor.ups_battery", "22", { friendly_name: "UPS battery", unit_of_measurement: "%" }), state("switch.pump", "unavailable", { friendly_name: "Pump" })];
|
||||
await syncHomeAssistant(haId, new Date());
|
||||
invalidateAlertsCache();
|
||||
let report = await getAlerts(true);
|
||||
const limit = report.alerts.find((a) => a.id === `monitoring:ha-limit:${haId}:sensor.ups_battery`)!;
|
||||
assert.ok(limit);
|
||||
assert.deepEqual([limit.source, limit.link], ["Home Assistant", "/home-assistant"]);
|
||||
assert.equal(report.alerts.some((a) => a.id.includes("ha-unavailable")), false, "the pump just went quiet");
|
||||
|
||||
await db.update(haEntities).set({ unavailableSince: new Date(Date.now() - 40 * 60_000).toISOString() }).where(eq(haEntities.entityId, "switch.pump"));
|
||||
invalidateAlertsCache();
|
||||
report = await getAlerts(true);
|
||||
assert.ok(report.alerts.some((a) => a.id === `monitoring:ha-unavailable:${haId}:switch.pump`));
|
||||
|
||||
haToken = "revoked";
|
||||
const down = await syncHomeAssistant(haId);
|
||||
assert.equal(down.ok, false);
|
||||
assert.match(down.error ?? "", /did not accept the token/);
|
||||
invalidateAlertsCache();
|
||||
report = await getAlerts(true);
|
||||
assert.ok(report.couldntCheck.some((c) => /Home Assistant/.test(c.name)));
|
||||
assert.equal(report.alerts.some((a) => a.id.includes("ha-limit")), false, "neither raised nor cleared while it can't be read");
|
||||
assert.equal(isHaFresh((await haSyncStatuses())[haId], Date.now()), false);
|
||||
haToken = "good-token";
|
||||
assert.equal((await syncHomeAssistant(haId)).ok, true);
|
||||
});
|
||||
|
||||
test("an entity that disappears from Home Assistant counts as quiet; stopping watching forgets it and its history", async () => {
|
||||
haStates = [state("sensor.ups_battery", "50", { unit_of_measurement: "%" })];
|
||||
await syncHomeAssistant(haId);
|
||||
const [pump] = await db.select().from(haEntities).where(eq(haEntities.entityId, "switch.pump"));
|
||||
assert.equal(pump.state, "missing");
|
||||
who = "operator";
|
||||
assert.equal((await fetch(`${base}/${haId}/watched/sensor.ups_battery`, { method: "DELETE" })).status, 204);
|
||||
assert.equal((await db.select().from(haReadings).where(eq(haReadings.entityId, "sensor.ups_battery"))).length, 0);
|
||||
assert.equal((await fetch(`${base}/${haId}/watched/sensor.ups_battery`, { method: "DELETE" })).status, 404);
|
||||
});
|
||||
|
||||
test("removing the integration removes everything read from it", async () => {
|
||||
await removeHomeAssistantData(haId);
|
||||
assert.equal((await db.select().from(haWatched)).length, 0);
|
||||
assert.equal((await db.select().from(haEntities)).length, 0);
|
||||
assert.equal((await haSyncStatuses())[haId], undefined);
|
||||
assert.equal((await db.select({ id: integrationCredentials.id }).from(integrationCredentials)).length >= 1, true);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,140 @@
|
||||
/**
|
||||
* When the operating system a server runs stops being supported: what the table knows, how an agent's answer is read, the alert, and that the agent's own
|
||||
* script says it (the shell part is checked by reading it; the answer it would give is a plain JSON object).
|
||||
*
|
||||
* Run with: npm test --workspace server
|
||||
*/
|
||||
import { test, describe, before, after } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { mkdtempSync, readFileSync, rmSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
const dir = mkdtempSync(join(tmpdir(), "hm-oslife-"));
|
||||
process.env.DATABASE_PATH = join(dir, "test.sqlite");
|
||||
process.env.SESSION_DIR = join(dir, "sessions");
|
||||
process.env.CREDENTIALS_ENCRYPTION_KEY = "ab".repeat(32);
|
||||
process.env.HEALTH_STARTUP_GRACE_MS = "0";
|
||||
|
||||
const { db } = await import("../src/db/client.js");
|
||||
const { servers, maintenanceWindows } = await import("../src/db/schema.js");
|
||||
const { runMigrations } = await import("../src/db/migrate.js");
|
||||
const { lifecycleOf, releaseKey, supportState, daysOfSupportLeft } = await import("../src/services/osLifecycle.js");
|
||||
const { syncServerTasks } = await import("../src/services/taskSync.js");
|
||||
const { getAlerts, invalidateAlertsCache } = await import("../src/services/alerts.js");
|
||||
const { updateSettings } = await import("../src/services/settingsStore.js");
|
||||
const { latestAgentVersions } = await import("../src/services/agentVersions.js");
|
||||
const { eq } = await import("drizzle-orm");
|
||||
|
||||
await runMigrations();
|
||||
|
||||
const repo = fileURLToPath(new URL("../../", import.meta.url));
|
||||
after(() => {
|
||||
try {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
} catch {
|
||||
// Windows may still hold the database file
|
||||
}
|
||||
});
|
||||
|
||||
describe("reading what an agent says it runs", () => {
|
||||
test("a release is found by its distribution and version, however the version is written", () => {
|
||||
assert.deepEqual(releaseKey({ id: "ubuntu", version: "22.04" }), { family: "ubuntu", key: "22.04" });
|
||||
assert.deepEqual(releaseKey({ id: "debian", version: "12" }), { family: "debian", key: "12" });
|
||||
assert.deepEqual(releaseKey({ id: "raspbian", version: "11" }), { family: "debian", key: "11" }, "Raspberry Pi OS is Debian");
|
||||
assert.deepEqual(releaseKey({ id: "rocky", version: "9.4" }), { family: "rocky", key: "9" });
|
||||
assert.deepEqual(releaseKey({ id: "alpine", version: "3.19.1" }), { family: "alpine", key: "3.19" });
|
||||
assert.deepEqual(releaseKey({ id: "proxmox-ve", version: "8.2" }), { family: "proxmox-ve", key: "8" });
|
||||
assert.deepEqual(releaseKey({ id: "centos", name: "CentOS Stream 9", version: "9" }), { family: "centos-stream", key: "9" });
|
||||
assert.deepEqual(releaseKey({ id: "centos", name: "CentOS Linux 7 (Core)", version: "7" }), { family: "centos-classic", key: "7" });
|
||||
assert.deepEqual(releaseKey({ id: "windows", name: "Windows Server 2019 Standard", version: "17763" }), { family: "windows-server", key: "17763" });
|
||||
assert.deepEqual(releaseKey({ id: "windows", name: "Windows 11 Pro", version: "22631" }), { family: "windows-client", key: "22631" });
|
||||
assert.equal(releaseKey({ id: "arch", version: "rolling" }), null, "a rolling distribution has no end");
|
||||
assert.equal(releaseKey({ id: "", version: "1" }), null);
|
||||
assert.equal(releaseKey({ id: "ubuntu" }), null);
|
||||
});
|
||||
|
||||
test("the same build number is a different product on a server and a client", () => {
|
||||
assert.equal(lifecycleOf({ id: "windows", name: "Windows Server 2025 Datacenter", version: "26100" })?.product, "Windows Server 2025");
|
||||
assert.equal(lifecycleOf({ id: "windows", name: "Windows 11 Pro", version: "26100" })?.product, "Windows 11 24H2");
|
||||
});
|
||||
|
||||
test("what the table knows: a name and a date; a release it does not know is named as the machine named itself, with no date", () => {
|
||||
assert.deepEqual(lifecycleOf({ id: "ubuntu", name: "Ubuntu 20.04.6 LTS", version: "20.04" }), { product: "Ubuntu 20.04 LTS", endsOn: "2025-05-31" });
|
||||
assert.deepEqual(lifecycleOf({ id: "debian", version: "12" }), { product: "Debian 12 (bookworm)", endsOn: "2028-06-30" });
|
||||
assert.deepEqual(lifecycleOf({ id: "ubuntu", name: "Ubuntu 31.04", version: "31.04" }), { product: "Ubuntu 31.04", endsOn: null });
|
||||
assert.equal(lifecycleOf({ id: "ubuntu", version: "31.04" }), null, "nothing to call it, nothing to say");
|
||||
assert.equal(lifecycleOf({ id: "templeos", version: "5" }), null);
|
||||
});
|
||||
|
||||
test("ended once the day is over, soon within the warning, ok beyond it, unknown with no date", () => {
|
||||
const now = new Date("2026-10-20T12:00:00Z");
|
||||
assert.equal(supportState("2026-10-19", now, 90), "ended");
|
||||
assert.equal(supportState("2026-10-20", now, 90), "soon", "the last day is still a day of support");
|
||||
assert.equal(supportState("2027-01-10", now, 90), "soon");
|
||||
assert.equal(supportState("2027-06-01", now, 90), "ok");
|
||||
assert.equal(supportState(null, now, 90), "unknown");
|
||||
assert.equal(daysOfSupportLeft("2026-10-30", now), 10);
|
||||
assert.equal(daysOfSupportLeft("2026-10-10", now), -10);
|
||||
});
|
||||
});
|
||||
|
||||
describe("what the agent says, and what the Alerts page makes of it", () => {
|
||||
let id = 0;
|
||||
before(async () => {
|
||||
id = (await db.insert(servers).values({ name: "old-box", apiTokenHash: "o", apiTokenPrefix: "o", lastSeenAt: new Date().toISOString() }).returning({ id: servers.id }))[0].id;
|
||||
});
|
||||
const row = () => db.select().from(servers).where(eq(servers.id, id)).then((r) => r[0]);
|
||||
const report = (os: unknown) => syncServerTasks(id, { osType: "linux", tasks: [], system: { os } as never });
|
||||
|
||||
test("it is stored, bounded; an agent that says nothing leaves what was said", async () => {
|
||||
await report({ id: "Ubuntu", name: "Ubuntu 20.04.6 LTS", version: "20.04" });
|
||||
let r = await row();
|
||||
assert.deepEqual([r.osId, r.osName, r.osVersion], ["ubuntu", "Ubuntu 20.04.6 LTS", "20.04"]);
|
||||
await syncServerTasks(id, { osType: "linux", tasks: [], system: {} });
|
||||
r = await row();
|
||||
assert.equal(r.osId, "ubuntu", "an older agent does not wipe it");
|
||||
await report({ id: "x".repeat(200), name: "n".repeat(500), version: "v".repeat(200) });
|
||||
r = await row();
|
||||
assert.deepEqual([r.osId!.length, r.osName!.length, r.osVersion!.length], [40, 80, 40]);
|
||||
await report({ id: "ubuntu", name: "Ubuntu 20.04.6 LTS", version: "20.04" });
|
||||
});
|
||||
|
||||
test("a release past its end is a warning with the date, on the Alerts page, under the server", async () => {
|
||||
invalidateAlertsCache();
|
||||
const alert = (await getAlerts(true)).alerts.find((a) => a.id === `updates:os-support:${id}`)!;
|
||||
assert.ok(alert, "it is raised");
|
||||
assert.deepEqual([alert.severity, alert.category, alert.link, alert.target?.name, alert.silenced], ["warning", "updates", `/servers/${id}`, "old-box", false]);
|
||||
assert.match(alert.message, /old-box runs Ubuntu 20\.04 LTS, whose support ended on 2025-05-31/);
|
||||
});
|
||||
|
||||
test("one with a supported release is not raised, and the warning is a setting (0 turns it off)", async () => {
|
||||
await report({ id: "debian", name: "Debian GNU/Linux 12 (bookworm)", version: "12" });
|
||||
invalidateAlertsCache();
|
||||
assert.equal((await getAlerts(true)).alerts.some((a) => a.id === `updates:os-support:${id}`), false);
|
||||
await report({ id: "ubuntu", name: "Ubuntu 20.04.6 LTS", version: "20.04" });
|
||||
await updateSettings({ healthChecks: { osSupportWarnDays: 0 } });
|
||||
invalidateAlertsCache();
|
||||
assert.equal((await getAlerts(true)).alerts.some((a) => a.id === `updates:os-support:${id}`), false, "off");
|
||||
await updateSettings({ healthChecks: { osSupportWarnDays: 90 } });
|
||||
});
|
||||
|
||||
test("a maintenance window on the server holds it back", async () => {
|
||||
await db.insert(maintenanceWindows).values({ targetType: "server", targetId: id, startedAt: new Date(Date.now() - 1000).toISOString(), endsAt: new Date(Date.now() + 3600_000).toISOString() });
|
||||
invalidateAlertsCache();
|
||||
assert.equal((await getAlerts(true)).alerts.find((a) => a.id === `updates:os-support:${id}`)?.silenced, true);
|
||||
await db.delete(maintenanceWindows).where(eq(maintenanceWindows.targetId, id));
|
||||
});
|
||||
});
|
||||
|
||||
describe("the agent scripts say it", () => {
|
||||
test("Linux reads /etc/os-release (and asks Proxmox by its own command) and reports it; Windows reports the product and the build; both are a new version", () => {
|
||||
const linux = readFileSync(join(repo, "agent", "linux", "report-tasks.sh"), "utf8");
|
||||
assert.ok(linux.includes("collect_os()") && linux.includes("OS_RELEASE_FILE") && linux.includes("pveversion") && linux.includes("os: $os"));
|
||||
const windows = readFileSync(join(repo, "agent", "windows", "report-tasks.ps1"), "utf8");
|
||||
assert.ok(windows.includes("Win32_OperatingSystem") && windows.includes("os = $osInfo") && windows.includes("BuildNumber"));
|
||||
const latest = latestAgentVersions();
|
||||
assert.ok((latest.linux ?? 0) >= 7 && (latest.windows ?? 0) >= 6, "an agent that does not say it is behind");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,59 @@
|
||||
/**
|
||||
* The rack view: which unit a piece of hardware says it sits in, how the racks are made from that, clashes and what is not placed.
|
||||
*
|
||||
* Run with: npm test --workspace server
|
||||
*/
|
||||
import { test, describe } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { buildRacks, parseRackUnit, type RackAsset } from "../src/services/racks.js";
|
||||
|
||||
const asset = (id: number, name: string, location: string | null, rackUnit: string | null, extra: Partial<RackAsset> = {}): RackAsset => ({ id, name, kind: "server", manufacturer: null, model: null, location, rackUnit, powerWatts: null, serverId: null, ...extra });
|
||||
|
||||
describe("reading a unit", () => {
|
||||
test("a unit, or a run of them, in the ways people write them", () => {
|
||||
assert.deepEqual(parseRackUnit("U12"), { from: 12, to: 12 });
|
||||
assert.deepEqual(parseRackUnit("u3"), { from: 3, to: 3 });
|
||||
assert.deepEqual(parseRackUnit("12"), { from: 12, to: 12 });
|
||||
assert.deepEqual(parseRackUnit("U12-13"), { from: 12, to: 13 });
|
||||
assert.deepEqual(parseRackUnit("12 – 14"), { from: 12, to: 14 });
|
||||
assert.deepEqual(parseRackUnit("U14-U12"), { from: 12, to: 14 }, "either way round");
|
||||
assert.equal(parseRackUnit("top shelf"), null);
|
||||
assert.equal(parseRackUnit("U0"), null);
|
||||
assert.equal(parseRackUnit(""), null);
|
||||
assert.equal(parseRackUnit(null), null);
|
||||
});
|
||||
});
|
||||
|
||||
describe("making racks", () => {
|
||||
test("hardware is grouped by where it says it is, placed by unit, in a rack of a real size", () => {
|
||||
const racks = buildRacks([asset(1, "Switch", "Rack A", "U40", { kind: "network", powerWatts: 40 }), asset(2, "NAS", "Rack A", "U10-13", { powerWatts: 120 }), asset(3, "UPS", "Rack A", "U1-3", { kind: "power" }), asset(4, "Laptop", "Desk", null)]);
|
||||
assert.equal(racks.length, 1, "a desk with no unit is not a rack");
|
||||
const a = racks[0];
|
||||
assert.deepEqual([a.name, a.units, a.usedUnits, a.watts], ["Rack A", 42, 1 + 4 + 3, 160]);
|
||||
assert.deepEqual(a.slots.map((s) => [s.name, s.from, s.to]), [["UPS", 1, 3], ["NAS", 10, 13], ["Switch", 40, 40]], "from the bottom up");
|
||||
assert.ok(a.slots.every((s) => !s.clash));
|
||||
});
|
||||
|
||||
test("a small rack is drawn small, and a tall piece makes it as tall as it needs", () => {
|
||||
assert.equal(buildRacks([asset(1, "A", "R", "U5")])[0].units, 12);
|
||||
assert.equal(buildRacks([asset(1, "A", "R", "U13")])[0].units, 18);
|
||||
assert.equal(buildRacks([asset(1, "A", "R", "U43-48")])[0].units, 48);
|
||||
assert.equal(buildRacks([asset(1, "A", "R", "U60-70")])[0].units, 70, "beyond any real rack it just fits");
|
||||
});
|
||||
|
||||
test("two pieces on the same unit clash, and both are said to", () => {
|
||||
const [rack] = buildRacks([asset(1, "A", "R", "U4-5"), asset(2, "B", "R", "U5"), asset(3, "C", "R", "U8")]);
|
||||
assert.deepEqual(rack.slots.map((s) => [s.name, s.clash]), [["A", true], ["B", true], ["C", false]]);
|
||||
assert.equal(rack.usedUnits, 3, "a unit is used once however many claim it");
|
||||
});
|
||||
|
||||
test("hardware that names a rack and no unit is listed beside it, not placed", () => {
|
||||
const [rack] = buildRacks([asset(1, "NAS", "Rack A", "U10"), asset(2, "Cable box", "Rack A", null), asset(3, "Patch panel", "Rack A", "somewhere")]);
|
||||
assert.deepEqual(rack.unplaced.map((u) => u.name), ["Cable box", "Patch panel"]);
|
||||
});
|
||||
|
||||
test("a unit with no place goes in a rack of its own, last; racks are in order, numbers as numbers", () => {
|
||||
const racks = buildRacks([asset(1, "X", null, "U2"), asset(2, "Y", "Rack 10", "U1"), asset(3, "Z", "Rack 2", "U1")]);
|
||||
assert.deepEqual(racks.map((r) => r.name), ["Rack 2", "Rack 10", "(no rack named)"]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,181 @@
|
||||
/**
|
||||
* The to-do list and notes: who may write, what is checked, ticking off and bringing back, what shows in Coming up and My work.
|
||||
*
|
||||
* Run with: npm test --workspace server
|
||||
*/
|
||||
import { test, describe, before, after } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { mkdtempSync, rmSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import type { AddressInfo } from "node:net";
|
||||
import type { Server } from "node:http";
|
||||
|
||||
const dir = mkdtempSync(join(tmpdir(), "hm-todos-"));
|
||||
process.env.DATABASE_PATH = join(dir, "test.sqlite");
|
||||
process.env.SESSION_DIR = join(dir, "sessions");
|
||||
process.env.CREDENTIALS_ENCRYPTION_KEY = "ab".repeat(32);
|
||||
process.env.HEALTH_STARTUP_GRACE_MS = "0";
|
||||
|
||||
const express = (await import("express")).default;
|
||||
const { db } = await import("../src/db/client.js");
|
||||
const { users, servers, notes, todos, auditLog } = await import("../src/db/schema.js");
|
||||
const { runMigrations } = await import("../src/db/migrate.js");
|
||||
const { todosRouter, notesRouter } = await import("../src/routes/todos.js");
|
||||
const { loadAgendaData, agendaFromData } = await import("../src/services/agenda.js");
|
||||
const { buildMyWork } = await import("../src/services/myWork.js");
|
||||
const { isRealDay } = await import("../src/services/todos.js");
|
||||
const { eq } = await import("drizzle-orm");
|
||||
|
||||
await runMigrations();
|
||||
|
||||
let app: Server;
|
||||
let base = "";
|
||||
let who = "operator";
|
||||
const ids: Record<string, number> = {};
|
||||
const send = async (method: string, path: string, body?: unknown) => {
|
||||
const res = await fetch(`${base}${path}`, { method, headers: { "content-type": "application/json" }, body: body === undefined ? undefined : JSON.stringify(body) });
|
||||
return { status: res.status, body: res.status === 204 ? null : ((await res.json().catch(() => null)) as any) };
|
||||
};
|
||||
const day = (offset: number) => new Date(Date.now() + offset * 86_400_000).toISOString().slice(0, 10);
|
||||
|
||||
before(async () => {
|
||||
const rows = await db
|
||||
.insert(users)
|
||||
.values([
|
||||
{ oidcSub: "admin", name: "Ada", role: "admin" },
|
||||
{ oidcSub: "operator", name: "Olga", role: "operator" },
|
||||
{ oidcSub: "viewer", name: "Vic", role: "viewer" },
|
||||
])
|
||||
.returning({ id: users.id, oidcSub: users.oidcSub });
|
||||
for (const r of rows) ids[r.oidcSub] = r.id;
|
||||
ids.web = (await db.insert(servers).values({ name: "web", apiTokenHash: "w", apiTokenPrefix: "w" }).returning({ id: servers.id }))[0].id;
|
||||
const a = express();
|
||||
a.use(express.json());
|
||||
a.use((req, _res, next) => {
|
||||
(req as unknown as { session: unknown }).session = { user: { sub: who } };
|
||||
next();
|
||||
});
|
||||
a.use("/api/todos", todosRouter);
|
||||
a.use("/api/notes", notesRouter);
|
||||
app = await new Promise<Server>((resolve) => {
|
||||
const s = a.listen(0, "127.0.0.1", () => resolve(s));
|
||||
});
|
||||
base = `http://127.0.0.1:${(app.address() as AddressInfo).port}/api`;
|
||||
});
|
||||
after(() => {
|
||||
app.close();
|
||||
try {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
} catch {
|
||||
// Windows may still hold the database file
|
||||
}
|
||||
});
|
||||
|
||||
describe("the to-do list", () => {
|
||||
let jobId = 0;
|
||||
|
||||
test("a real day is a real day", () => {
|
||||
assert.equal(isRealDay("2026-10-20"), true);
|
||||
assert.equal(isRealDay("2026-02-30"), false);
|
||||
assert.equal(isRealDay("next week"), false);
|
||||
});
|
||||
|
||||
test("everyone signed in reads it; only an operator writes", async () => {
|
||||
who = "viewer";
|
||||
assert.equal((await send("GET", "/todos")).status, 200);
|
||||
assert.equal((await send("POST", "/todos", { title: "x" })).status, 403);
|
||||
assert.equal((await send("PATCH", "/todos/1", { done: true })).status, 403);
|
||||
assert.equal((await send("DELETE", "/todos/1")).status, 403);
|
||||
});
|
||||
|
||||
test("a job needs a title; the day must be a day; it can be about a server and handed to an operator, and not to a viewer", async () => {
|
||||
who = "operator";
|
||||
assert.equal((await send("POST", "/todos", { title: " " })).status, 400);
|
||||
assert.equal((await send("POST", "/todos", { title: "x", dueDate: "tomorrow" })).body.error, "invalid_date");
|
||||
assert.equal((await send("POST", "/todos", { title: "x", assigneeId: ids.viewer })).body.error, "invalid_assignee");
|
||||
assert.equal((await send("POST", "/todos", { title: "x", targetType: "server", targetId: 9999 })).body.error, "unknown_target");
|
||||
assert.equal((await send("POST", "/todos", { title: "x".repeat(200) })).body.error, "too_long");
|
||||
const made = await send("POST", "/todos", { title: "Replace the UPS battery", detail: "APC, model in the rack", dueDate: day(1), assigneeId: ids.operator, targetType: "server", targetId: ids.web });
|
||||
assert.equal(made.status, 201);
|
||||
jobId = made.body.todo.id;
|
||||
assert.deepEqual([made.body.todo.title, made.body.todo.assignee.name, made.body.todo.target.name, made.body.todo.doneAt], ["Replace the UPS battery", "Olga", "web", null]);
|
||||
});
|
||||
|
||||
test("open jobs come soonest-due first, with those without a day last; ticking one off keeps it, and it comes back", async () => {
|
||||
await send("POST", "/todos", { title: "Someday" });
|
||||
await send("POST", "/todos", { title: "Next week", dueDate: day(7) });
|
||||
const open = (await send("GET", "/todos")).body.todos.map((t: any) => t.title);
|
||||
assert.deepEqual(open, ["Replace the UPS battery", "Next week", "Someday"]);
|
||||
const done = await send("PATCH", `/todos/${jobId}`, { done: true });
|
||||
assert.ok(done.body.todo.doneAt && done.body.todo.doneBy === "Olga");
|
||||
const after = (await send("GET", "/todos")).body.todos;
|
||||
assert.deepEqual(after.map((t: any) => [t.title, t.doneAt !== null]), [["Next week", false], ["Someday", false], ["Replace the UPS battery", true]], "done ones after the open ones");
|
||||
const back = await send("PATCH", `/todos/${jobId}`, { done: false });
|
||||
assert.equal(back.body.todo.doneAt, null);
|
||||
});
|
||||
|
||||
test("a job can be changed, and removed; one that is not there is a 404", async () => {
|
||||
const changed = await send("PATCH", `/todos/${jobId}`, { title: "Replace the UPS battery (APC)", dueDate: null, assigneeId: null });
|
||||
assert.deepEqual([changed.body.todo.title, changed.body.todo.dueDate, changed.body.todo.assignee], ["Replace the UPS battery (APC)", null, null]);
|
||||
assert.equal((await send("PATCH", "/todos/99999", { done: true })).status, 404);
|
||||
assert.equal((await send("DELETE", `/todos/${jobId}`)).status, 204);
|
||||
assert.equal((await send("DELETE", `/todos/${jobId}`)).status, 404);
|
||||
const kinds = (await db.select().from(auditLog).where(eq(auditLog.category, "todo"))).map((e) => e.action);
|
||||
assert.ok(["create", "done", "reopen", "update", "delete"].every((a) => kinds.includes(a)), "each is written down");
|
||||
});
|
||||
|
||||
test("a job about a server that is later removed is still there, about nothing", async () => {
|
||||
const gone = (await db.insert(servers).values({ name: "temp", apiTokenHash: "t", apiTokenPrefix: "t" }).returning({ id: servers.id }))[0].id;
|
||||
const made = await send("POST", "/todos", { title: "About temp", targetType: "server", targetId: gone });
|
||||
await db.delete(servers).where(eq(servers.id, gone));
|
||||
const found = (await send("GET", "/todos")).body.todos.find((t: any) => t.id === made.body.todo.id);
|
||||
assert.equal(found.target, null);
|
||||
await send("DELETE", `/todos/${made.body.todo.id}`);
|
||||
});
|
||||
|
||||
test("an open job with a day is in Coming up (overdue ones too), a done one is not; one handed to somebody is in their My work", async () => {
|
||||
const soon = (await send("POST", "/todos", { title: "Move the NAS", dueDate: day(3), assigneeId: ids.operator })).body.todo;
|
||||
const late = (await send("POST", "/todos", { title: "Order a spare disk", dueDate: day(-2), assigneeId: ids.operator })).body.todo;
|
||||
const finished = (await send("POST", "/todos", { title: "Already done", dueDate: day(2) })).body.todo;
|
||||
await send("PATCH", `/todos/${finished.id}`, { done: true });
|
||||
const agenda = agendaFromData(await loadAgendaData(), new Date(), "UTC", 30);
|
||||
const titles = agenda.items.filter((i) => i.kind === "todo").map((i) => i.title);
|
||||
assert.ok(titles.includes("To do: Move the NAS") && titles.includes("Overdue: Order a spare disk"));
|
||||
assert.ok(!titles.some((t) => t.includes("Already done")));
|
||||
const mine = await buildMyWork({ id: ids.operator, name: "Olga", email: null, oidcSub: "operator" });
|
||||
assert.deepEqual(mine.todos.map((t) => [t.title, t.overdue, t.dueSoon]), [["Order a spare disk", true, false], ["Move the NAS", false, false]], "overdue first; three days off is not yet soon");
|
||||
assert.ok(mine.needsAttention >= 1, "an overdue job wants a look");
|
||||
const theirs = await buildMyWork({ id: ids.admin, name: "Ada", email: null, oidcSub: "admin" });
|
||||
assert.equal(theirs.todos.length, 0);
|
||||
await db.delete(todos);
|
||||
assert.ok(soon && late);
|
||||
});
|
||||
});
|
||||
|
||||
describe("notes", () => {
|
||||
test("read by everyone; written by an operator; an empty text takes it away", async () => {
|
||||
who = "viewer";
|
||||
assert.equal((await send("GET", `/notes?targetType=server&targetId=${ids.web}`)).body.note, null);
|
||||
assert.equal((await send("PUT", "/notes", { targetType: "server", targetId: ids.web, body: "x" })).status, 403);
|
||||
who = "operator";
|
||||
const saved = await send("PUT", "/notes", { targetType: "server", targetId: ids.web, body: "Lives in the **rack**.\n\n- PSU is on the left" });
|
||||
assert.deepEqual([saved.status, saved.body.note.updatedBy, saved.body.note.body.includes("**rack**")], [200, "Olga", true]);
|
||||
const again = await send("PUT", "/notes", { targetType: "server", targetId: ids.web, body: "Replaced." });
|
||||
assert.equal((await db.select().from(notes)).length, 1, "one text per thing");
|
||||
assert.equal(again.body.note.body, "Replaced.");
|
||||
who = "viewer";
|
||||
assert.equal((await send("GET", `/notes?targetType=server&targetId=${ids.web}`)).body.note.body, "Replaced.");
|
||||
who = "operator";
|
||||
assert.equal((await send("PUT", "/notes", { targetType: "server", targetId: ids.web, body: " \n " })).body.note, null);
|
||||
assert.equal((await db.select().from(notes)).length, 0);
|
||||
});
|
||||
|
||||
test("a note for nothing, or too long, or about something unknown, is refused", async () => {
|
||||
who = "operator";
|
||||
assert.equal((await send("PUT", "/notes", { targetType: "server", targetId: 99999, body: "x" })).status, 404);
|
||||
assert.equal((await send("PUT", "/notes", { targetType: "server", targetId: ids.web, body: "x".repeat(9000) })).body.error, "too_long");
|
||||
assert.equal((await send("PUT", "/notes", { targetType: "rack", targetId: 1, body: "x" })).status, 400);
|
||||
assert.equal((await send("GET", "/notes?targetType=server")).status, 400);
|
||||
});
|
||||
});
|
||||
@@ -34,7 +34,7 @@ function run(snippet: string): unknown {
|
||||
describe("the Windows agent's health parts", { skip: !havePwsh }, () => {
|
||||
test("it says it is version 5, and stays plain ASCII", () => {
|
||||
const text = readFileSync(script, "utf8");
|
||||
assert.match(text, /^\$script:AgentVersion = 5\s*$/m);
|
||||
assert.match(text, /^\$script:AgentVersion = 6\s*$/m);
|
||||
assert.ok(!/[^\x00-\x7f]/.test(text));
|
||||
});
|
||||
|
||||
|
||||
@@ -7,8 +7,12 @@ import Users from "./pages/Users";
|
||||
import Secrets from "./pages/Secrets";
|
||||
import Ipam from "./pages/Ipam";
|
||||
import NetworkDevices from "./pages/NetworkDevices";
|
||||
import ProxyHosts from "./pages/ProxyHosts";
|
||||
import HomeAssistant from "./pages/HomeAssistant";
|
||||
import MyWorkPage from "./pages/MyWork";
|
||||
import ApiTokens from "./pages/ApiTokens";
|
||||
import Todo from "./pages/Todo";
|
||||
import InventoryRacks from "./pages/InventoryRacks";
|
||||
import Ports from "./pages/Ports";
|
||||
import Dns from "./pages/Dns";
|
||||
import Servers from "./pages/Servers";
|
||||
@@ -138,6 +142,7 @@ function Shelled({ user }: { user: CurrentUser }) {
|
||||
<Route path="/" element={<Dashboard user={user} />} />
|
||||
<Route path="/my-work" element={<MyWorkPage user={user} />} />
|
||||
<Route path="/api-tokens" element={<ApiTokens />} />
|
||||
<Route path="/todo" element={<Todo user={user} />} />
|
||||
<Route path="/map" element={<MapPage user={user} />} />
|
||||
<Route path="/servers" element={<Servers user={user} />} />
|
||||
<Route path="/servers/compare" element={<ServerCompare />} />
|
||||
@@ -145,6 +150,8 @@ function Shelled({ user }: { user: CurrentUser }) {
|
||||
<Route path="/dns" element={<Dns user={user} />} />
|
||||
<Route path="/ipam" element={<Ipam user={user} />} />
|
||||
<Route path="/network/devices" element={<NetworkDevices user={user} />} />
|
||||
<Route path="/proxy-hosts" element={<ProxyHosts user={user} />} />
|
||||
<Route path="/home-assistant" element={<HomeAssistant user={user} />} />
|
||||
<Route path="/ports" element={<Ports user={user} />} />
|
||||
<Route path="/secrets" element={<Secrets user={user} />} />
|
||||
<Route path="/integrations" element={<Integrations user={user} />} />
|
||||
@@ -155,6 +162,7 @@ function Shelled({ user }: { user: CurrentUser }) {
|
||||
<Route path="/maintenance" element={<Maintenance user={user} />} />
|
||||
<Route path="/updates" element={<Updates user={user} />} />
|
||||
<Route path="/inventory" element={<Inventory user={user} />} />
|
||||
<Route path="/inventory/racks" element={<InventoryRacks />} />
|
||||
<Route path="/inventory/labels" element={<InventoryLabels />} />
|
||||
<Route path="/inventory/lifecycle" element={<InventoryLifecycle />} />
|
||||
{/* These were pages of their own; they are sections of other pages now, and old links, bookmarks and notifications still arrive. */}
|
||||
|
||||
+159
-2
@@ -517,6 +517,8 @@ export interface HealthCheckSettings {
|
||||
flapRaises: number;
|
||||
/** Minutes a UniFi device may be offline before it is raised. */
|
||||
deviceOfflineMinutes: number;
|
||||
/** Days before an operating system's support ends that a server is flagged (0 = off). */
|
||||
osSupportWarnDays: number;
|
||||
}
|
||||
|
||||
/** The monthly report: last month's availability, incidents, updates and renewals, in one message on a day of the month. */
|
||||
@@ -780,7 +782,19 @@ export interface ServerLink {
|
||||
url: string;
|
||||
}
|
||||
|
||||
/** What a server runs and when that release stops being supported (from what its agent says). */
|
||||
export interface ServerOs {
|
||||
id: string;
|
||||
name: string | null;
|
||||
version: string | null;
|
||||
product: string;
|
||||
endsOn: string | null;
|
||||
state: "ended" | "soon" | "ok" | "unknown";
|
||||
daysLeft: number | null;
|
||||
}
|
||||
|
||||
export interface ServerDetail {
|
||||
os: ServerOs | null;
|
||||
server: ServerRecord;
|
||||
ipAddresses: string[];
|
||||
hardware: ServerHardware;
|
||||
@@ -1081,7 +1095,36 @@ export interface HeartbeatInput {
|
||||
enabled?: boolean;
|
||||
}
|
||||
|
||||
export type AgendaKind = "secret" | "certificate" | "domain" | "tailscale" | "warranty" | "support" | "maintenance" | "restore" | "updates";
|
||||
export type AgendaKind = "secret" | "certificate" | "domain" | "tailscale" | "warranty" | "support" | "maintenance" | "restore" | "updates" | "todo";
|
||||
|
||||
/** One job on the to-do list. */
|
||||
export interface TodoItem {
|
||||
id: number;
|
||||
title: string;
|
||||
detail: string | null;
|
||||
dueDate: string | null;
|
||||
assignee: { id: number | null; name: string } | null;
|
||||
target: { type: "server" | "device"; id: number; name: string } | null;
|
||||
createdBy: string | null;
|
||||
createdAt: string;
|
||||
doneAt: string | null;
|
||||
doneBy: string | null;
|
||||
}
|
||||
export interface TodoInput {
|
||||
title: string;
|
||||
detail?: string | null;
|
||||
dueDate?: string | null;
|
||||
assigneeId?: number | null;
|
||||
targetType?: "server" | "device" | null;
|
||||
targetId?: number | null;
|
||||
}
|
||||
export interface NoteItem {
|
||||
targetType: "server" | "device";
|
||||
targetId: number;
|
||||
body: string;
|
||||
updatedBy: string | null;
|
||||
updatedAt: string;
|
||||
}
|
||||
export interface AgendaItem {
|
||||
id: string;
|
||||
kind: AgendaKind;
|
||||
@@ -1172,6 +1215,18 @@ export interface DependencyNode {
|
||||
id: number;
|
||||
name: string;
|
||||
}
|
||||
/** A dependency the app offers to write, from what it already knows. */
|
||||
export interface DependencySuggestion {
|
||||
key: string;
|
||||
from: { type: "integration"; id: number; name: string };
|
||||
to: { type: "server"; id: number; name: string };
|
||||
reason: string;
|
||||
}
|
||||
export interface DependencySuggestions {
|
||||
suggestions: DependencySuggestion[];
|
||||
dismissed: DependencySuggestion[];
|
||||
}
|
||||
|
||||
export interface DependenciesResponse {
|
||||
/** "A needs B", written here. */
|
||||
dependencies: { id: number; from: { type: DependencyNodeType; id: number; name: string }; to: { type: DependencyNodeType; id: number; name: string }; createdAt: string }[];
|
||||
@@ -1185,6 +1240,8 @@ export interface MyWork {
|
||||
alerts: { alertId: string; message: string; severity: string; source: string; link: string | null; note: string; role: "handed" | "acked"; ackedBy: string; ackedAt: string; until: string | null; endsSoon: boolean }[];
|
||||
windows: { id: number; targetType: string; targetId: number; targetName: string; targetKind: string; reason: string | null; startedAt: string; endsAt: string; endsSoon: boolean }[];
|
||||
updateRuns: { id: number; serverId: number; serverName: string; templateName: string; status: string; message: string | null; startedAt: string; finishedAt: string | null; running: boolean; updatesBefore: number | null; updatesAfter: number | null; rebootRequired: boolean }[];
|
||||
/** Open jobs from the to-do list handed to this person. */
|
||||
todos: { id: number; title: string; detail: string | null; dueDate: string | null; overdue: boolean; dueSoon: boolean; target: { type: string; id: number; name: string } | null }[];
|
||||
needsAttention: number;
|
||||
}
|
||||
|
||||
@@ -1257,6 +1314,68 @@ export interface NetworkOverview {
|
||||
ssids: NetworkSegment[];
|
||||
}
|
||||
|
||||
export interface NpmHost {
|
||||
id: number;
|
||||
domains: string[];
|
||||
forwardScheme: string;
|
||||
forwardHost: string;
|
||||
forwardPort: number | null;
|
||||
enabled: boolean;
|
||||
sslForced: boolean;
|
||||
certificate: { id: number; name: string; expiresAt: string | null } | null;
|
||||
online: boolean;
|
||||
error: string | null;
|
||||
/** The server the address it forwards to belongs to, when the app knows it. */
|
||||
server: { id: number; name: string } | null;
|
||||
}
|
||||
|
||||
export interface NpmCertificate {
|
||||
id: number;
|
||||
name: string;
|
||||
provider: string;
|
||||
domains: string[];
|
||||
expiresAt: string | null;
|
||||
}
|
||||
|
||||
export interface IntegrationProblem {
|
||||
key: string;
|
||||
kind: string;
|
||||
severity: "critical" | "warning";
|
||||
message: string;
|
||||
}
|
||||
|
||||
export interface NpmOverview {
|
||||
integrations: { id: number; name: string; readAt: string; error: string | null; version: string | null; hosts: NpmHost[]; certificates: NpmCertificate[]; problems: IntegrationProblem[] }[];
|
||||
}
|
||||
|
||||
export interface HaSyncStatus {
|
||||
at: string;
|
||||
ok: boolean;
|
||||
error: string | null;
|
||||
entities: number;
|
||||
}
|
||||
|
||||
export interface HaWatchedEntity {
|
||||
entityId: string;
|
||||
name: string | null;
|
||||
above: number | null;
|
||||
below: number | null;
|
||||
entity: { friendlyName: string | null; state: string; value: number | null; unit: string | null; deviceClass: string | null; lastChanged: string | null; unavailableSince: string | null; updatedAt: string } | null;
|
||||
}
|
||||
|
||||
export interface HaOverview {
|
||||
integrations: { id: number; name: string; enabled: boolean; status: HaSyncStatus | null; fresh: boolean; watched: HaWatchedEntity[]; problems: IntegrationProblem[] }[];
|
||||
}
|
||||
|
||||
export interface HaEntityChoice {
|
||||
entityId: string;
|
||||
name: string;
|
||||
state: string;
|
||||
unit: string | null;
|
||||
deviceClass: string | null;
|
||||
watched: boolean;
|
||||
}
|
||||
|
||||
/** A piece of hardware in the inventory. */
|
||||
export interface Asset {
|
||||
id: number;
|
||||
@@ -1671,7 +1790,7 @@ export interface ManualTaskInput {
|
||||
enabled?: boolean;
|
||||
}
|
||||
|
||||
export type IntegrationType = "proxmox" | "synology" | "semaphore" | "tailscale" | "gitea" | "dockhand" | "uptimekuma" | "phpipam" | "pbs" | "osticket" | "unifi";
|
||||
export type IntegrationType = "proxmox" | "synology" | "semaphore" | "tailscale" | "gitea" | "dockhand" | "uptimekuma" | "phpipam" | "pbs" | "osticket" | "unifi" | "npm" | "homeassistant";
|
||||
|
||||
export interface IntegrationField {
|
||||
key: string;
|
||||
@@ -2268,18 +2387,56 @@ export const api = {
|
||||
myWork: {
|
||||
get: () => request<MyWork>("/api/my-work"),
|
||||
},
|
||||
todos: {
|
||||
list: () => request<{ todos: TodoItem[]; people: { id: number; name: string; role: string }[] }>("/api/todos"),
|
||||
create: (data: TodoInput) => request<{ todo: TodoItem }>("/api/todos", { method: "POST", body: JSON.stringify(data) }),
|
||||
update: (id: number, data: Partial<TodoInput> & { done?: boolean }) => request<{ todo: TodoItem }>(`/api/todos/${id}`, { method: "PATCH", body: JSON.stringify(data) }),
|
||||
remove: (id: number) => request<void>(`/api/todos/${id}`, { method: "DELETE" }),
|
||||
},
|
||||
notes: {
|
||||
get: (targetType: "server" | "device", targetId: number) => request<{ note: NoteItem | null }>(`/api/notes?targetType=${targetType}&targetId=${targetId}`),
|
||||
save: (targetType: "server" | "device", targetId: number, body: string) => request<{ note: NoteItem | null }>("/api/notes", { method: "PUT", body: JSON.stringify({ targetType, targetId, body }) }),
|
||||
},
|
||||
network: {
|
||||
overview: () => request<NetworkOverview>("/api/network"),
|
||||
sync: (integrationId?: number) => request<{ statuses: Record<number, UnifiSyncStatus> }>("/api/network/sync", { method: "POST", body: JSON.stringify(integrationId ? { integrationId } : {}) }),
|
||||
},
|
||||
npm: {
|
||||
overview: (refresh = false) => request<NpmOverview>(`/api/npm${refresh ? "?refresh=1" : ""}`),
|
||||
},
|
||||
homeAssistant: {
|
||||
overview: () => request<HaOverview>("/api/home-assistant"),
|
||||
entities: (id: number, q: string) => request<{ total: number; entities: HaEntityChoice[] }>(`/api/home-assistant/${id}/entities?q=${encodeURIComponent(q)}`),
|
||||
watch: (id: number, body: { entityId: string; name?: string | null; above?: number | null; below?: number | null }) =>
|
||||
request<{ ok: boolean; status: HaSyncStatus }>(`/api/home-assistant/${id}/watched`, { method: "PUT", body: JSON.stringify(body) }),
|
||||
unwatch: (id: number, entityId: string) => request<void>(`/api/home-assistant/${id}/watched/${encodeURIComponent(entityId)}`, { method: "DELETE" }),
|
||||
history: (id: number, entityId: string, days: number) =>
|
||||
request<{ days: number; points: { t: string; v: number }[] }>(`/api/home-assistant/${id}/history?entity=${encodeURIComponent(entityId)}&days=${days}`),
|
||||
sync: () => request<{ statuses: Record<number, HaSyncStatus> }>("/api/home-assistant/sync", { method: "POST", body: "{}" }),
|
||||
},
|
||||
dependencies: {
|
||||
list: () => request<DependenciesResponse>("/api/dependencies"),
|
||||
impact: (type: DependencyNodeType, id: number) => request<DependencyImpact>(`/api/dependencies/impact?type=${type}&id=${id}`),
|
||||
graph: () => request<DependencyGraph>("/api/dependencies/graph"),
|
||||
suggestions: () => request<DependencySuggestions>("/api/dependencies/suggestions"),
|
||||
addSuggestions: (keys: string[]) => request<DependencySuggestions & { added: { id: number; from: string; to: string }[] }>("/api/dependencies/suggestions/add", { method: "POST", body: JSON.stringify({ keys }) }),
|
||||
dismissSuggestions: (keys: string[]) => request<DependencySuggestions>("/api/dependencies/suggestions/dismiss", { method: "POST", body: JSON.stringify({ keys }) }),
|
||||
restoreSuggestions: () => request<DependencySuggestions>("/api/dependencies/suggestions/restore", { method: "POST" }),
|
||||
create: (data: { fromType: DependencyNodeType; fromId: number; toType: DependencyNodeType; toId: number }) => request<{ id: number }>("/api/dependencies", { method: "POST", body: JSON.stringify(data) }),
|
||||
remove: (id: number) => request<void>(`/api/dependencies/${id}`, { method: "DELETE" }),
|
||||
},
|
||||
inventory: {
|
||||
racks: () =>
|
||||
request<{
|
||||
racks: {
|
||||
name: string;
|
||||
units: number;
|
||||
slots: { assetId: number; name: string; kind: string; model: string | null; from: number; to: number; powerWatts: number | null; serverId: number | null; clash: boolean }[];
|
||||
unplaced: { assetId: number; name: string; kind: string }[];
|
||||
usedUnits: number;
|
||||
watts: number;
|
||||
}[];
|
||||
}>("/api/inventory/racks"),
|
||||
list: () => request<{ assets: Asset[]; config: InventoryConfig; servers: { id: number; name: string }[] }>("/api/inventory"),
|
||||
create: (data: AssetInput) => request<{ asset: Asset }>("/api/inventory", { method: "POST", body: JSON.stringify(data) }),
|
||||
update: (id: number, data: AssetInput) => request<{ asset: Asset }>(`/api/inventory/${id}`, { method: "PUT", body: JSON.stringify(data) }),
|
||||
|
||||
@@ -44,6 +44,8 @@ const INTEGRATION_TYPE_LABELS: Record<IntegrationType, string> = {
|
||||
pbs: "Proxmox Backup Server",
|
||||
osticket: "osTicket",
|
||||
unifi: "UniFi",
|
||||
npm: "Nginx Proxy Manager",
|
||||
homeassistant: "Home Assistant",
|
||||
};
|
||||
|
||||
const DNS_PROVIDER_LABELS: Record<DnsProviderType, string> = {
|
||||
|
||||
@@ -15,6 +15,8 @@ const TYPE_LABELS: Record<IntegrationType, string> = {
|
||||
pbs: "Proxmox Backup Server",
|
||||
osticket: "osTicket",
|
||||
unifi: "UniFi",
|
||||
npm: "Nginx Proxy Manager",
|
||||
homeassistant: "Home Assistant",
|
||||
};
|
||||
|
||||
export default function IntegrationEditForm({
|
||||
|
||||
@@ -14,6 +14,8 @@ const TYPE_LABELS: Record<IntegrationType, string> = {
|
||||
pbs: "Proxmox Backup Server",
|
||||
osticket: "osTicket",
|
||||
unifi: "UniFi",
|
||||
npm: "Nginx Proxy Manager",
|
||||
homeassistant: "Home Assistant",
|
||||
};
|
||||
|
||||
export default function IntegrationForm({ onCreated, onCancel }: { onCreated: () => void; onCancel: () => void }) {
|
||||
|
||||
@@ -0,0 +1,160 @@
|
||||
import { useMemo } from "react";
|
||||
import type { NetworkDevice } from "../api/client";
|
||||
|
||||
const KIND_WORDS: Record<NetworkDevice["kind"], string> = { gateway: "Gateway", switch: "Switch", access_point: "Access point", other: "Device" };
|
||||
const STATE_COLOR: Record<NetworkDevice["state"], string> = { online: "#2fb344", offline: "#f03e3e", updating: "#f59f00", pending: "#4299e1", other: "#8896ab" };
|
||||
|
||||
const NODE_W = 168;
|
||||
const SLOT_W = 188;
|
||||
const HEAD_H = 46;
|
||||
const LINE_H = 14;
|
||||
const MAX_SERVERS = 4;
|
||||
const ROW_GAP = 46;
|
||||
|
||||
interface Node {
|
||||
id: string;
|
||||
device: NetworkDevice | null; // null: the internet, or "unknown uplink"
|
||||
label: string;
|
||||
children: Node[];
|
||||
width: number; // in slots
|
||||
x: number; // centre, in px
|
||||
y: number; // top, in px
|
||||
h: number;
|
||||
}
|
||||
|
||||
const heightOf = (d: NetworkDevice | null) => (d ? HEAD_H + Math.min(d.servers.length, MAX_SERVERS + (d.servers.length > MAX_SERVERS ? 1 : 0)) * LINE_H + (d.servers.length > 0 ? 6 : 0) : 30);
|
||||
|
||||
/**
|
||||
* How the network hangs together, from what the UniFi controller says each device is connected through: the internet, the gateway, the switches below it, the access
|
||||
* points below those, and the servers plugged into each. A tree drawn top-down; a device whose state is not online is red, and a device whose uplink is not known sits
|
||||
* apart under "Unknown uplink". Print it for the cabinet door.
|
||||
*/
|
||||
export default function NetworkDiagram({ devices }: { devices: NetworkDevice[] }) {
|
||||
const layout = useMemo(() => {
|
||||
const childrenOf = new Map<number, NetworkDevice[]>();
|
||||
const roots: NetworkDevice[] = [];
|
||||
const orphans: NetworkDevice[] = [];
|
||||
for (const d of devices) {
|
||||
if (d.uplink) {
|
||||
const parent = devices.find((p) => p.id === d.uplink!.id);
|
||||
if (parent) {
|
||||
childrenOf.set(parent.id, [...(childrenOf.get(parent.id) ?? []), d]);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
(d.kind === "gateway" ? roots : orphans).push(d);
|
||||
}
|
||||
const build = (d: NetworkDevice, seen: Set<number>): Node => {
|
||||
const kids = (childrenOf.get(d.id) ?? []).filter((k) => !seen.has(k.id)).sort((a, b) => a.name.localeCompare(b.name));
|
||||
const next = new Set(seen).add(d.id);
|
||||
const children = kids.map((k) => build(k, next));
|
||||
return { id: `d${d.id}`, device: d, label: d.name, children, width: Math.max(1, children.reduce((n, c) => n + c.width, 0)), x: 0, y: 0, h: heightOf(d) };
|
||||
};
|
||||
const virtual = (id: string, label: string, kids: NetworkDevice[]): Node => {
|
||||
const children = kids.sort((a, b) => a.name.localeCompare(b.name)).map((k) => build(k, new Set()));
|
||||
return { id, device: null, label, children, width: Math.max(1, children.reduce((n, c) => n + c.width, 0)), x: 0, y: 0, h: heightOf(null) };
|
||||
};
|
||||
const top: Node[] = [];
|
||||
if (roots.length > 0) top.push(virtual("internet", "Internet", roots));
|
||||
if (orphans.length > 0) top.push(virtual("unknown", "Unknown uplink", orphans));
|
||||
// Position: each node is centred over its children; a leaf takes one slot; the top-level groups sit side by side.
|
||||
let cursor = 0;
|
||||
const levelBottom: number[] = [];
|
||||
const place = (n: Node, depth: number, left: number) => {
|
||||
let at = left;
|
||||
for (const c of n.children) {
|
||||
place(c, depth + 1, at);
|
||||
at += c.width * SLOT_W;
|
||||
}
|
||||
n.x = n.children.length > 0 ? (n.children[0].x + n.children[n.children.length - 1].x) / 2 : left + (n.width * SLOT_W) / 2;
|
||||
levelBottom[depth] = Math.max(levelBottom[depth] ?? 0, n.h);
|
||||
(n as Node & { depth: number }).depth = depth;
|
||||
};
|
||||
for (const t of top) {
|
||||
place(t, 0, cursor);
|
||||
cursor += t.width * SLOT_W + SLOT_W / 2;
|
||||
}
|
||||
// Rows: each level is as tall as its tallest node, then a gap.
|
||||
const rowTop: number[] = [];
|
||||
levelBottom.reduce((y, h, i) => {
|
||||
rowTop[i] = y;
|
||||
return y + h + ROW_GAP;
|
||||
}, 8);
|
||||
const all: Node[] = [];
|
||||
const walk = (n: Node) => {
|
||||
n.y = rowTop[(n as Node & { depth: number }).depth];
|
||||
all.push(n);
|
||||
n.children.forEach(walk);
|
||||
};
|
||||
top.forEach(walk);
|
||||
const width = Math.max(cursor - SLOT_W / 2, SLOT_W);
|
||||
const height = (rowTop[rowTop.length - 1] ?? 0) + (levelBottom[levelBottom.length - 1] ?? 0) + 8;
|
||||
return { all, width, height };
|
||||
}, [devices]);
|
||||
|
||||
if (devices.length === 0) return <div className="text-secondary">No devices to draw yet.</div>;
|
||||
|
||||
return (
|
||||
<div className="card">
|
||||
<div className="card-body" style={{ overflowX: "auto" }}>
|
||||
<svg viewBox={`0 0 ${layout.width} ${layout.height}`} width={layout.width} height={layout.height} role="img" aria-label={`Network diagram: ${devices.length} devices, by what each is connected through`} style={{ maxWidth: "none", color: "var(--tblr-body-color)" }}>
|
||||
{layout.all.flatMap((n) =>
|
||||
n.children.map((c) => {
|
||||
const x1 = n.x;
|
||||
const y1 = n.y + n.h;
|
||||
const x2 = c.x;
|
||||
const y2 = c.y;
|
||||
const mid = (y1 + y2) / 2;
|
||||
return <path key={`${n.id}-${c.id}`} d={`M${x1},${y1} V${mid} H${x2} V${y2}`} fill="none" stroke="currentColor" strokeOpacity="0.35" strokeWidth="1.5" />;
|
||||
}),
|
||||
)}
|
||||
{layout.all.map((n) => {
|
||||
const d = n.device;
|
||||
if (!d) {
|
||||
return (
|
||||
<g key={n.id}>
|
||||
<rect x={n.x - 60} y={n.y} width="120" height={n.h} rx="15" fill="currentColor" fillOpacity="0.08" stroke="currentColor" strokeOpacity="0.4" />
|
||||
<text x={n.x} y={n.y + 19} textAnchor="middle" fontSize="12" fill="currentColor" fontWeight="600">
|
||||
{n.label}
|
||||
</text>
|
||||
</g>
|
||||
);
|
||||
}
|
||||
const shown = d.servers.slice(0, MAX_SERVERS);
|
||||
return (
|
||||
<g key={n.id}>
|
||||
<title>{`${d.name} — ${KIND_WORDS[d.kind]}${d.model ? `, ${d.model}` : ""}${d.ip ? `, ${d.ip}` : ""} — ${d.state}${d.servers.length > 0 ? `\nPlugged in: ${d.servers.map((s) => s.serverName).join(", ")}` : ""}`}</title>
|
||||
<rect x={n.x - NODE_W / 2} y={n.y} width={NODE_W} height={n.h} rx="6" fill="var(--tblr-bg-surface, #fff)" stroke={STATE_COLOR[d.state]} strokeWidth="2" />
|
||||
<circle cx={n.x - NODE_W / 2 + 12} cy={n.y + 14} r="5" fill={STATE_COLOR[d.state]} />
|
||||
<text x={n.x - NODE_W / 2 + 24} y={n.y + 18} fontSize="12" fill="currentColor" fontWeight="600">
|
||||
{d.name.length > 20 ? `${d.name.slice(0, 19)}…` : d.name}
|
||||
</text>
|
||||
<text x={n.x - NODE_W / 2 + 10} y={n.y + 35} fontSize="10.5" fill="currentColor" fillOpacity="0.7">
|
||||
{[KIND_WORDS[d.kind], d.ip].filter(Boolean).join(" · ")}
|
||||
</text>
|
||||
{shown.map((s, i) => (
|
||||
<text key={s.serverId} x={n.x - NODE_W / 2 + 10} y={n.y + HEAD_H + 8 + i * LINE_H} fontSize="10.5" fill="currentColor">
|
||||
▪ {s.serverName}
|
||||
{s.port !== null ? ` (${s.port})` : s.wired ? "" : " (Wi-Fi)"}
|
||||
</text>
|
||||
))}
|
||||
{d.servers.length > MAX_SERVERS && (
|
||||
<text x={n.x - NODE_W / 2 + 10} y={n.y + HEAD_H + 8 + MAX_SERVERS * LINE_H} fontSize="10.5" fill="currentColor" fillOpacity="0.7">
|
||||
+ {d.servers.length - MAX_SERVERS} more
|
||||
</text>
|
||||
)}
|
||||
</g>
|
||||
);
|
||||
})}
|
||||
</svg>
|
||||
</div>
|
||||
<div className="card-footer text-secondary small d-flex flex-wrap gap-3">
|
||||
<span style={{ color: STATE_COLOR.online }}>● online</span>
|
||||
<span style={{ color: STATE_COLOR.offline }}>● offline</span>
|
||||
<span style={{ color: STATE_COLOR.updating }}>● updating</span>
|
||||
<span>▪ a server plugged in (the port, or Wi-Fi)</span>
|
||||
<span className="ms-auto">As the UniFi controller reports what each device is connected through.</span>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
import { useEffect, useState } from "react";
|
||||
import { api, type NoteItem } from "../api/client";
|
||||
import { BlockSkeleton } from "./Skeleton";
|
||||
import RunbookText from "./RunbookText";
|
||||
import TimeAgo from "./TimeAgo";
|
||||
import { readableError } from "../utils/errors";
|
||||
import { toast } from "../utils/toast";
|
||||
|
||||
/**
|
||||
* Free-form notes about a server or a network device — what it is, where it sits, what was done to it last time. One text, written with the same small formatting
|
||||
* as a runbook (lists, **bold**, `code`, links). Everyone signed in reads it; operators write it. Emptying it takes the note away.
|
||||
*/
|
||||
export default function NotesCard({ targetType, targetId, canEdit, bare = false }: { targetType: "server" | "device"; targetId: number; canEdit: boolean; /** Without the card round it (inside a drawer). */ bare?: boolean }) {
|
||||
const [note, setNote] = useState<NoteItem | null | undefined>(undefined);
|
||||
const [draft, setDraft] = useState<string | null>(null);
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [failed, setFailed] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
let alive = true;
|
||||
setNote(undefined);
|
||||
setDraft(null);
|
||||
api.notes
|
||||
.get(targetType, targetId)
|
||||
.then((res) => alive && setNote(res.note))
|
||||
.catch(() => alive && setFailed(true));
|
||||
return () => {
|
||||
alive = false;
|
||||
};
|
||||
}, [targetType, targetId]);
|
||||
|
||||
async function save() {
|
||||
if (draft === null) return;
|
||||
setBusy(true);
|
||||
try {
|
||||
const res = await api.notes.save(targetType, targetId, draft);
|
||||
setNote(res.note);
|
||||
setDraft(null);
|
||||
toast.success(res.note ? "Saved." : "Note removed.");
|
||||
} catch (err) {
|
||||
toast.error(readableError(err));
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
}
|
||||
|
||||
const body = failed ? (
|
||||
<div className="text-secondary">Couldn’t load the note.</div>
|
||||
) : note === undefined ? (
|
||||
<BlockSkeleton lines={2} />
|
||||
) : draft !== null ? (
|
||||
<>
|
||||
<textarea className="form-control mb-2" rows={8} maxLength={8000} value={draft} onChange={(e) => setDraft(e.target.value)} aria-label="The note" placeholder={"What is it, where does it sit, what was done to it last time?\n\n- lists, **bold**, `code` and links work"} autoFocus />
|
||||
<div className="d-flex gap-2">
|
||||
<button type="button" className="btn btn-primary" disabled={busy} onClick={() => void save()}>
|
||||
{busy ? "Saving…" : "Save"}
|
||||
</button>
|
||||
<button type="button" className="btn" disabled={busy} onClick={() => setDraft(null)}>
|
||||
Cancel
|
||||
</button>
|
||||
</div>
|
||||
</>
|
||||
) : note ? (
|
||||
<>
|
||||
<RunbookText text={note.body} />
|
||||
<div className="text-secondary small mt-2">
|
||||
{note.updatedBy ? `${note.updatedBy}, ` : ""}
|
||||
<TimeAgo value={note.updatedAt} />
|
||||
</div>
|
||||
</>
|
||||
) : (
|
||||
<div className="text-secondary">Nothing written down yet.{canEdit ? " Add what is worth knowing about it." : ""}</div>
|
||||
);
|
||||
|
||||
const edit =
|
||||
canEdit && draft === null && note !== undefined && !failed ? (
|
||||
<button type="button" className="btn btn-sm btn-primary" onClick={() => setDraft(note?.body ?? "")}>
|
||||
{note ? "Edit" : "Write a note"}
|
||||
</button>
|
||||
) : null;
|
||||
|
||||
if (bare) {
|
||||
return (
|
||||
<>
|
||||
{body}
|
||||
{edit && <div className="mt-3">{edit}</div>}
|
||||
</>
|
||||
);
|
||||
}
|
||||
return (
|
||||
<div className="card mb-3">
|
||||
<div className="card-header">
|
||||
<h3 className="card-title">Notes</h3>
|
||||
{edit && <div className="card-actions">{edit}</div>}
|
||||
</div>
|
||||
<div className="card-body">{body}</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -57,7 +57,8 @@ export default function TimeChart({
|
||||
to: number;
|
||||
/** Read out by screen readers in place of the picture. */
|
||||
label: string;
|
||||
unit?: "%" | "ms";
|
||||
/** "%" is a 0–100 scale; "ms" starts at zero and fits the data; "value" fits the data from its lowest to its highest, so negatives (a freezer's temperature) are drawn too. */
|
||||
unit?: "%" | "ms" | "value";
|
||||
compact?: boolean;
|
||||
/** One bar per point instead of a line (daily availability). */
|
||||
bars?: boolean;
|
||||
@@ -70,19 +71,30 @@ export default function TimeChart({
|
||||
// Where the cursor is, as a time (null: nowhere), and whether the keyboard put it there.
|
||||
const [cursor, setCursor] = useState<number | null>(null);
|
||||
|
||||
const fmt = format ?? ((v: number) => (unit === "%" ? `${Math.round(v * 10) / 10}%` : `${Math.round(v)} ms`));
|
||||
const fmt = format ?? ((v: number) => (unit === "%" ? `${Math.round(v * 10) / 10}%` : unit === "value" ? `${Math.round(v * 10) / 10}` : `${Math.round(v)} ms`));
|
||||
const parsed = useMemo(() => series.map((s) => ({ ...s, pts: s.points.map((p) => ({ ...p, x: Date.parse(p.t) })).filter((p) => Number.isFinite(p.x)) })), [series]);
|
||||
const any = parsed.some((s) => s.pts.length > 0);
|
||||
|
||||
const top = unit === "%" ? 100 : niceCeil(Math.max(0, ...parsed.flatMap((s) => s.pts.map((p) => p.hi ?? p.v))) * 1.05);
|
||||
const [bottom, top] = (() => {
|
||||
if (unit === "%") return [0, 100];
|
||||
const values = parsed.flatMap((s) => s.pts.flatMap((p) => [p.lo ?? p.v, p.hi ?? p.v]));
|
||||
if (unit !== "value") return [0, niceCeil(Math.max(0, ...values) * 1.05)];
|
||||
if (values.length === 0) return [0, 1];
|
||||
const lo = Math.min(...values);
|
||||
const hi = Math.max(...values);
|
||||
const span = hi - lo || Math.abs(hi) * 0.2 || 1;
|
||||
const step = niceCeil((span * 1.1) / 4);
|
||||
const low = Math.floor((lo - span * 0.05) / step) * step;
|
||||
return [low, Math.max(Math.ceil((hi + span * 0.05) / step) * step, low + step)];
|
||||
})();
|
||||
const x = (t: number) => PAD.left + ((t - from) / Math.max(to - from, 1)) * (W - PAD.left - PAD.right);
|
||||
const y = (v: number) => PAD.top + (1 - Math.min(top, Math.max(0, v)) / top) * (H - PAD.top - PAD.bottom);
|
||||
const y = (v: number) => PAD.top + (1 - (Math.min(top, Math.max(bottom, v)) - bottom) / (top - bottom)) * (H - PAD.top - PAD.bottom);
|
||||
|
||||
const spanDays = (to - from) / 86_400_000;
|
||||
const tickLabel = (t: number) =>
|
||||
spanDays <= 1.5 ? formatTime(new Date(t), false) : spanDays <= 45 ? new Date(t).toLocaleDateString(undefined, { day: "numeric", month: "short" }) : new Date(t).toLocaleDateString(undefined, { month: "short", year: "2-digit" });
|
||||
const ticks = (compact ? [0, 0.5, 1] : [0, 0.25, 0.5, 0.75, 1]).map((f) => from + f * (to - from));
|
||||
const grid = compact ? [0, top / 2, top] : [0, top / 4, top / 2, (3 * top) / 4, top];
|
||||
const grid = (compact ? [0, 0.5, 1] : [0, 0.25, 0.5, 0.75, 1]).map((f) => bottom + f * (top - bottom));
|
||||
|
||||
// Every moment any series has a point, for the arrow keys to step through.
|
||||
const moments = useMemo(() => [...new Set(parsed.flatMap((s) => s.pts.map((p) => p.x)))].sort((a, b) => a - b), [parsed]);
|
||||
@@ -149,7 +161,7 @@ export default function TimeChart({
|
||||
<g key={v}>
|
||||
<line x1={PAD.left} x2={W - PAD.right} y1={y(v)} y2={y(v)} stroke="currentColor" strokeOpacity={v === 0 ? 0.4 : 0.15} />
|
||||
<text x={PAD.left - 6} y={y(v) + 4} textAnchor="end" fontSize="11" fill="currentColor" fillOpacity="0.7">
|
||||
{unit === "%" ? `${v}%` : `${Math.round(v)}`}
|
||||
{unit === "%" ? `${v}%` : unit === "value" ? `${Math.round(v * 10) / 10}` : `${Math.round(v)}`}
|
||||
</text>
|
||||
</g>
|
||||
))}
|
||||
|
||||
@@ -16,6 +16,8 @@ const TYPE_LABELS: Record<string, string> = {
|
||||
pbs: "Proxmox Backup",
|
||||
osticket: "osTicket",
|
||||
unifi: "UniFi",
|
||||
npm: "Nginx Proxy Manager",
|
||||
homeassistant: "Home Assistant",
|
||||
};
|
||||
|
||||
export type WallLevel = "critical" | "warning" | "ok" | "unsure";
|
||||
|
||||
@@ -5,7 +5,10 @@ import {
|
||||
IconWorld,
|
||||
IconNetwork,
|
||||
IconRouter,
|
||||
IconArrowsSplit2,
|
||||
IconHome2,
|
||||
IconChecklist,
|
||||
IconListDetails,
|
||||
IconKey,
|
||||
IconPlugConnected,
|
||||
IconBrandDocker,
|
||||
@@ -96,6 +99,7 @@ export const NAV: NavEntry[] = [
|
||||
items: [
|
||||
{ to: "/dns", label: "DNS", icon: <IconWorld size={20} /> },
|
||||
{ to: "/network/devices", label: "Network devices", icon: <IconRouter size={20} /> },
|
||||
{ to: "/proxy-hosts", label: "Proxy hosts", icon: <IconArrowsSplit2 size={20} /> },
|
||||
{ to: "/ipam", label: "IP Addresses", icon: <IconNetwork size={20} /> },
|
||||
{ to: "/ports", label: "Ports", icon: <IconPlug size={20} /> },
|
||||
{ to: "/consistency", label: "Consistency", icon: <IconListCheck size={20} /> },
|
||||
@@ -109,6 +113,7 @@ export const NAV: NavEntry[] = [
|
||||
{ to: "/endpoints", label: "Endpoints", icon: <IconHeartbeat size={20} /> },
|
||||
{ to: "/heartbeats", label: "Heartbeats", icon: <IconHeartRateMonitor size={20} /> },
|
||||
{ to: "/uptime-kuma", label: "Uptime Kuma", icon: <IconActivityHeartbeat size={20} /> },
|
||||
{ to: "/home-assistant", label: "Home Assistant", icon: <IconHome2 size={20} /> },
|
||||
],
|
||||
},
|
||||
{
|
||||
@@ -131,6 +136,7 @@ export const NAV: NavEntry[] = [
|
||||
{ to: "/maintenance", label: "Maintenance", icon: <IconTool size={20} /> },
|
||||
{ to: "/updates", label: "Updates", icon: <IconDownload size={20} /> },
|
||||
{ to: "/coming-up", label: "Coming up", icon: <IconCalendarEvent size={20} /> },
|
||||
{ to: "/todo", label: "To do", icon: <IconListDetails size={20} /> },
|
||||
{ to: "/reports", label: "Monthly report", icon: <IconReportAnalytics size={20} /> },
|
||||
{ to: "/changes", label: "Change log", icon: <IconGitCommit size={20} />, minRole: "operator" },
|
||||
],
|
||||
|
||||
@@ -18,6 +18,7 @@ const KINDS: { id: AgendaKind; label: string }[] = [
|
||||
{ id: "maintenance", label: "Maintenance" },
|
||||
{ id: "restore", label: "Restore tests" },
|
||||
{ id: "updates", label: "Updates waiting" },
|
||||
{ id: "todo", label: "To do" },
|
||||
];
|
||||
const KIND_LABEL = Object.fromEntries(KINDS.map((k) => [k.id, k.label])) as Record<AgendaKind, string>;
|
||||
|
||||
|
||||
@@ -332,7 +332,7 @@ function MyWorkStrip() {
|
||||
return (
|
||||
<div className="alert alert-info d-flex flex-wrap align-items-center gap-2 py-2" role="status">
|
||||
<span>
|
||||
<strong>{work.needsAttention}</strong> {work.needsAttention === 1 ? "thing" : "things"} of yours {work.needsAttention === 1 ? "wants" : "want"} a look — alerts handed to you or about to run out, maintenance about to end, update runs still going.
|
||||
<strong>{work.needsAttention}</strong> {work.needsAttention === 1 ? "thing" : "things"} of yours {work.needsAttention === 1 ? "wants" : "want"} a look — alerts handed to you or about to run out, maintenance about to end, update runs still going, jobs that are due.
|
||||
</span>
|
||||
<Link to="/my-work" className="btn btn-sm btn-primary ms-auto">
|
||||
My work
|
||||
|
||||
@@ -0,0 +1,400 @@
|
||||
import { useCallback, useEffect, useMemo, useState } from "react";
|
||||
import { api, type CurrentUser, type HaEntityChoice, type HaOverview, type HaWatchedEntity } from "../api/client";
|
||||
import DetailDrawer from "../components/DetailDrawer";
|
||||
import RangeSelect from "../components/RangeSelect";
|
||||
import TimeAgo from "../components/TimeAgo";
|
||||
import TimeChart from "../components/TimeChart";
|
||||
import EmptyState from "../components/EmptyState";
|
||||
import { BlockSkeleton } from "../components/Skeleton";
|
||||
import { ErrorNotice } from "../components/Notice";
|
||||
import { useChartRange } from "../utils/chartRanges";
|
||||
import { readableError } from "../utils/errors";
|
||||
import { toast } from "../utils/toast";
|
||||
|
||||
type Integration = HaOverview["integrations"][number];
|
||||
|
||||
const shownName = (w: HaWatchedEntity) => w.name ?? w.entity?.friendlyName ?? w.entityId;
|
||||
const numberWords = (v: number, unit: string | null) => `${Math.round(v * 100) / 100}${unit ? ` ${unit}` : ""}`;
|
||||
|
||||
function StateCell({ w, problem }: { w: HaWatchedEntity; problem: boolean }) {
|
||||
const e = w.entity;
|
||||
if (!e) return <span className="text-secondary">not read yet</span>;
|
||||
if (e.unavailableSince !== null || e.state === "missing") {
|
||||
return (
|
||||
<span className="badge bg-yellow-lt text-yellow" title={`Since ${e.unavailableSince ?? "?"}`}>
|
||||
{e.state === "missing" ? "Not in Home Assistant" : e.state}
|
||||
{e.unavailableSince ? <> · <TimeAgo value={e.unavailableSince} /></> : null}
|
||||
</span>
|
||||
);
|
||||
}
|
||||
return <strong className={problem ? "text-red" : ""}>{e.value !== null ? numberWords(e.value, e.unit) : e.state}</strong>;
|
||||
}
|
||||
|
||||
function limitsWords(w: HaWatchedEntity): string {
|
||||
const unit = w.entity?.unit ? ` ${w.entity.unit}` : "";
|
||||
if (w.below !== null && w.above !== null) return `${w.below}–${w.above}${unit}`;
|
||||
if (w.below !== null) return `not under ${w.below}${unit}`;
|
||||
if (w.above !== null) return `not over ${w.above}${unit}`;
|
||||
return "";
|
||||
}
|
||||
|
||||
/** A watched number over time, from what this app has kept (up to 35 days). */
|
||||
function HistoryDrawer({ integrationId, w, onClose }: { integrationId: number; w: HaWatchedEntity; onClose: () => void }) {
|
||||
const [days, setDays] = useChartRange(7);
|
||||
const shown = days > 30 ? 30 : days;
|
||||
const [points, setPoints] = useState<{ t: string; v: number }[] | null>(null);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [to, setTo] = useState(() => Date.now());
|
||||
|
||||
useEffect(() => {
|
||||
let alive = true;
|
||||
api.homeAssistant
|
||||
.history(integrationId, w.entityId, shown)
|
||||
.then((res) => {
|
||||
if (!alive) return;
|
||||
setPoints(res.points);
|
||||
setTo(Date.now());
|
||||
setError(null);
|
||||
})
|
||||
.catch((err) => alive && setError(readableError(err)));
|
||||
return () => {
|
||||
alive = false;
|
||||
};
|
||||
}, [integrationId, w.entityId, shown]);
|
||||
|
||||
const unit = w.entity?.unit ?? "";
|
||||
const series = useMemo(() => [{ name: shownName(w), color: "#4299e1", points: points ?? [] }], [points, w]);
|
||||
return (
|
||||
<DetailDrawer title={shownName(w)} subtitle={w.entityId} onClose={onClose}>
|
||||
<div className="mb-3">
|
||||
<RangeSelect value={shown} onChange={setDays} only={[1, 7, 30]} />
|
||||
</div>
|
||||
<ErrorNotice error={error} />
|
||||
{!points && !error && <BlockSkeleton lines={5} />}
|
||||
{points && <TimeChart series={series} unit="value" from={to - shown * 86_400_000} to={to} label={`${shownName(w)} over time`} format={(v) => numberWords(v, unit)} empty="Nothing kept for this in this stretch yet — a reading is added every few minutes." />}
|
||||
<div className="text-secondary small mt-2">Readings are kept for 35 days.</div>
|
||||
</DetailDrawer>
|
||||
);
|
||||
}
|
||||
|
||||
/** Change what is said about an entity already watched: its name and its limits. */
|
||||
function EditDrawer({ integrationId, w, onClose, onSaved }: { integrationId: number; w: HaWatchedEntity; onClose: () => void; onSaved: () => void }) {
|
||||
const numeric = w.entity?.value !== null && w.entity?.value !== undefined;
|
||||
const [name, setName] = useState(w.name ?? "");
|
||||
const [below, setBelow] = useState(w.below === null ? "" : String(w.below));
|
||||
const [above, setAbove] = useState(w.above === null ? "" : String(w.above));
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
async function save() {
|
||||
const parse = (s: string) => (s.trim() === "" ? null : Number(s));
|
||||
const b = parse(below);
|
||||
const a = parse(above);
|
||||
if ((b !== null && !Number.isFinite(b)) || (a !== null && !Number.isFinite(a))) return setError("The limits have to be numbers.");
|
||||
setBusy(true);
|
||||
try {
|
||||
await api.homeAssistant.watch(integrationId, { entityId: w.entityId, name: name.trim() || null, below: b, above: a });
|
||||
onSaved();
|
||||
onClose();
|
||||
} catch (err) {
|
||||
setError(readableError(err));
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<DetailDrawer title={shownName(w)} subtitle={w.entityId} onClose={onClose}>
|
||||
<ErrorNotice error={error} />
|
||||
<div className="mb-3">
|
||||
<label className="form-label" htmlFor="ha-name">
|
||||
Name shown here
|
||||
</label>
|
||||
<input id="ha-name" className="form-control" value={name} maxLength={80} placeholder={w.entity?.friendlyName ?? w.entityId} onChange={(e) => setName(e.target.value)} />
|
||||
</div>
|
||||
{numeric ? (
|
||||
<div className="row g-2 mb-3">
|
||||
<div className="col-6">
|
||||
<label className="form-label" htmlFor="ha-below">
|
||||
Alert when under{w.entity?.unit ? ` (${w.entity.unit})` : ""}
|
||||
</label>
|
||||
<input id="ha-below" className="form-control" inputMode="decimal" value={below} onChange={(e) => setBelow(e.target.value)} placeholder="no limit" />
|
||||
</div>
|
||||
<div className="col-6">
|
||||
<label className="form-label" htmlFor="ha-above">
|
||||
Alert when over{w.entity?.unit ? ` (${w.entity.unit})` : ""}
|
||||
</label>
|
||||
<input id="ha-above" className="form-control" inputMode="decimal" value={above} onChange={(e) => setAbove(e.target.value)} placeholder="no limit" />
|
||||
</div>
|
||||
</div>
|
||||
) : (
|
||||
<div className="text-secondary mb-3">This one isn’t a number, so there are no limits to set. It is an alert when it has been unavailable for 15 minutes.</div>
|
||||
)}
|
||||
<button className="btn btn-primary" onClick={() => void save()} disabled={busy}>
|
||||
{busy ? "Saving…" : "Save"}
|
||||
</button>
|
||||
</DetailDrawer>
|
||||
);
|
||||
}
|
||||
|
||||
/** Pick entities to watch: search what Home Assistant has, read live. */
|
||||
function PickDrawer({ integration, onClose, onChanged }: { integration: Integration; onClose: () => void; onChanged: () => void }) {
|
||||
const [q, setQ] = useState("");
|
||||
const [found, setFound] = useState<{ total: number; entities: HaEntityChoice[] } | null>(null);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [busy, setBusy] = useState<string | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
let alive = true;
|
||||
const timer = setTimeout(() => {
|
||||
api.homeAssistant
|
||||
.entities(integration.id, q)
|
||||
.then((res) => {
|
||||
if (!alive) return;
|
||||
setFound(res);
|
||||
setError(null);
|
||||
})
|
||||
.catch((err) => alive && setError(readableError(err)));
|
||||
}, 250);
|
||||
return () => {
|
||||
alive = false;
|
||||
clearTimeout(timer);
|
||||
};
|
||||
}, [integration.id, q]);
|
||||
|
||||
async function watch(e: HaEntityChoice) {
|
||||
setBusy(e.entityId);
|
||||
try {
|
||||
await api.homeAssistant.watch(integration.id, { entityId: e.entityId });
|
||||
setFound((f) => (f ? { ...f, entities: f.entities.map((x) => (x.entityId === e.entityId ? { ...x, watched: true } : x)) } : f));
|
||||
onChanged();
|
||||
} catch (err) {
|
||||
toast.error(readableError(err));
|
||||
} finally {
|
||||
setBusy(null);
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<DetailDrawer title="Watch something" subtitle={integration.name} onClose={onClose}>
|
||||
<div className="text-secondary mb-2">Search by name or id, like “battery” or “freezer”. Limits are set afterwards, from the list.</div>
|
||||
<input className="form-control mb-3" type="search" value={q} onChange={(e) => setQ(e.target.value)} placeholder="Search Home Assistant…" aria-label="Search entities" autoFocus />
|
||||
<ErrorNotice error={error} />
|
||||
{!found && !error && <BlockSkeleton lines={4} />}
|
||||
{found && (
|
||||
<>
|
||||
<div className="list-group list-group-flush">
|
||||
{found.entities.map((e) => (
|
||||
<div className="list-group-item d-flex align-items-center gap-2 px-0" key={e.entityId}>
|
||||
<div className="flex-fill" style={{ minWidth: 0 }}>
|
||||
<div className="text-truncate">{e.name}</div>
|
||||
<div className="text-secondary small font-monospace text-truncate">
|
||||
{e.entityId} · {e.state}
|
||||
{e.unit ? ` ${e.unit}` : ""}
|
||||
</div>
|
||||
</div>
|
||||
{e.watched ? (
|
||||
<span className="badge bg-green-lt text-green">Watching</span>
|
||||
) : (
|
||||
<button className="btn btn-sm btn-outline-primary" disabled={busy === e.entityId} onClick={() => void watch(e)}>
|
||||
Watch
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
{found.entities.length === 0 && <div className="text-secondary">Nothing matches.</div>}
|
||||
<div className="text-secondary small mt-2">
|
||||
{found.entities.length >= 60 ? "The first 60 of " : ""}
|
||||
{found.total} in Home Assistant.
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
</DetailDrawer>
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* The few things worth watching in Home Assistant — a UPS battery, a freezer, a door sensor, a pump — chosen here, not all several hundred entities. Each is read every
|
||||
* few minutes and kept with its history; it is an alert when it has said nothing for 15 minutes, or a number goes past a limit that was set. Nothing is ever written
|
||||
* to Home Assistant.
|
||||
*/
|
||||
export default function HomeAssistant({ user }: { user: CurrentUser }) {
|
||||
const canAct = user.role === "admin" || user.role === "operator";
|
||||
const [data, setData] = useState<HaOverview | null>(null);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [reading, setReading] = useState(false);
|
||||
const [picking, setPicking] = useState<Integration | null>(null);
|
||||
const [editing, setEditing] = useState<{ id: number; w: HaWatchedEntity } | null>(null);
|
||||
const [charting, setCharting] = useState<{ id: number; w: HaWatchedEntity } | null>(null);
|
||||
|
||||
const load = useCallback(
|
||||
() =>
|
||||
api.homeAssistant
|
||||
.overview()
|
||||
.then((res) => {
|
||||
setData(res);
|
||||
setError(null);
|
||||
})
|
||||
.catch((err) => setError(readableError(err))),
|
||||
[],
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
void load();
|
||||
const timer = setInterval(() => void load(), 60_000);
|
||||
return () => clearInterval(timer);
|
||||
}, [load]);
|
||||
|
||||
async function readNow() {
|
||||
setReading(true);
|
||||
try {
|
||||
const res = await api.homeAssistant.sync();
|
||||
const failed = Object.values(res.statuses).filter((s) => !s.ok);
|
||||
if (failed.length > 0) toast.error(`Couldn't read Home Assistant: ${failed[0].error ?? "no answer"}`);
|
||||
else toast.success("Read from Home Assistant.");
|
||||
await load();
|
||||
} catch (err) {
|
||||
toast.error(readableError(err));
|
||||
} finally {
|
||||
setReading(false);
|
||||
}
|
||||
}
|
||||
|
||||
async function stop(id: number, w: HaWatchedEntity) {
|
||||
try {
|
||||
await api.homeAssistant.unwatch(id, w.entityId);
|
||||
toast.success(`No longer watching ${shownName(w)}.`);
|
||||
await load();
|
||||
} catch (err) {
|
||||
toast.error(readableError(err));
|
||||
}
|
||||
}
|
||||
|
||||
const live = data?.integrations ?? [];
|
||||
return (
|
||||
<>
|
||||
<div className="d-flex flex-wrap align-items-center gap-2 mb-1">
|
||||
<h2 className="page-title mb-0">Home Assistant</h2>
|
||||
{canAct && live.length > 0 && (
|
||||
<div className="ms-auto btn-list">
|
||||
<button className="btn btn-outline-secondary" onClick={() => void readNow()} disabled={reading}>
|
||||
{reading ? "Reading…" : "Read now"}
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<div className="text-secondary mb-3">
|
||||
The few things worth an alert in Home Assistant — a UPS battery, a freezer, a pump — chosen here rather than every entity it has. Each is read every five minutes and its history is kept for 35 days. It only
|
||||
reads; nothing is ever sent to Home Assistant.
|
||||
</div>
|
||||
<ErrorNotice error={error} />
|
||||
{data === null && !error && <BlockSkeleton lines={4} />}
|
||||
{data && live.length === 0 && (
|
||||
<div className="card">
|
||||
<EmptyState
|
||||
title="No Home Assistant connected"
|
||||
hint="Add a Home Assistant integration with a long-lived access token (made at the bottom of your profile page in Home Assistant), then choose what to watch."
|
||||
action={user.role === "admin" ? { label: "Add Home Assistant", to: "/integrations" } : undefined}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{live.map((i) => (
|
||||
<div className="card mb-3" key={i.id}>
|
||||
<div className="card-header">
|
||||
<div>
|
||||
<h3 className="card-title">{i.name}</h3>
|
||||
<div className={`small ${i.status && !i.status.ok ? "text-red" : "text-secondary"}`}>
|
||||
{!i.enabled ? (
|
||||
"Switched off"
|
||||
) : !i.status ? (
|
||||
"Not read yet"
|
||||
) : i.status.ok ? (
|
||||
<>
|
||||
Read <TimeAgo value={i.status.at} /> · watching {i.status.entities}
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
Couldn’t be read: {i.status.error}. Showing what was read <TimeAgo value={i.status.at} />; its alerts are held until it can be read again.
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
{canAct && i.enabled && (
|
||||
<div className="card-actions">
|
||||
<button className="btn btn-primary btn-sm" onClick={() => setPicking(i)}>
|
||||
Watch something
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
{i.watched.length === 0 ? (
|
||||
<EmptyState title="Nothing is being watched yet" hint="Pick the entities that matter — a battery level, a temperature, a switch — and set the limits that should raise an alert." action={canAct && i.enabled ? { label: "Watch something", onClick: () => setPicking(i) } : undefined} />
|
||||
) : (
|
||||
<div className="table-responsive">
|
||||
<table className="table table-vcenter card-table table-stack">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Name</th>
|
||||
<th>Now</th>
|
||||
<th>Alert when</th>
|
||||
<th>Last changed</th>
|
||||
<th className="w-1">
|
||||
<span className="visually-hidden">Actions</span>
|
||||
</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{i.watched.map((w) => {
|
||||
const problem = i.problems.some((p) => p.key.endsWith(`:${w.entityId}`));
|
||||
return (
|
||||
<tr key={w.entityId}>
|
||||
<td className="stack-wide">
|
||||
<strong>{shownName(w)}</strong>
|
||||
<div className="text-secondary small font-monospace">{w.entityId}</div>
|
||||
</td>
|
||||
<td className="stack-label" data-label="Now">
|
||||
<StateCell w={w} problem={problem} />
|
||||
</td>
|
||||
<td className="stack-label" data-label="Alert when">
|
||||
{limitsWords(w) || <span className="text-secondary">unavailable for 15 min</span>}
|
||||
</td>
|
||||
<td className="stack-label" data-label="Last changed">
|
||||
{w.entity?.lastChanged ? <TimeAgo value={w.entity.lastChanged} /> : <span className="text-secondary">—</span>}
|
||||
</td>
|
||||
<td className="text-nowrap stack-wide">
|
||||
<div className="btn-list flex-nowrap">
|
||||
{w.entity?.value !== null && w.entity?.value !== undefined && (
|
||||
<button className="btn btn-sm btn-outline-secondary" onClick={() => setCharting({ id: i.id, w })}>
|
||||
History
|
||||
</button>
|
||||
)}
|
||||
{canAct && (
|
||||
<>
|
||||
<button className="btn btn-sm btn-outline-secondary" onClick={() => setEditing({ id: i.id, w })}>
|
||||
Limits
|
||||
</button>
|
||||
<button className="btn btn-sm btn-outline-danger" onClick={() => void stop(i.id, w)} aria-label={`Stop watching ${shownName(w)}`}>
|
||||
Stop
|
||||
</button>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
);
|
||||
})}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
))}
|
||||
|
||||
{picking && <PickDrawer integration={picking} onClose={() => setPicking(null)} onChanged={() => void load()} />}
|
||||
{editing && <EditDrawer integrationId={editing.id} w={editing.w} onClose={() => setEditing(null)} onSaved={() => void load()} />}
|
||||
{charting && <HistoryDrawer integrationId={charting.id} w={charting.w} onClose={() => setCharting(null)} />}
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -26,6 +26,8 @@ const TYPE_LABELS: Record<IntegrationType, string> = {
|
||||
pbs: "Proxmox Backup Server",
|
||||
osticket: "osTicket",
|
||||
unifi: "UniFi",
|
||||
npm: "Nginx Proxy Manager",
|
||||
homeassistant: "Home Assistant",
|
||||
};
|
||||
|
||||
/** The page each kind of integration gets once it is connected. */
|
||||
@@ -41,6 +43,8 @@ const TYPE_PAGES: Record<IntegrationType, string> = {
|
||||
pbs: "/pbs",
|
||||
osticket: "/osticket",
|
||||
unifi: "/network/devices",
|
||||
npm: "/proxy-hosts",
|
||||
homeassistant: "/home-assistant",
|
||||
};
|
||||
|
||||
/** "pve.example.lan" out of "https://pve.example.lan:8006/", or the text itself when it isn't an address. */
|
||||
|
||||
@@ -268,6 +268,9 @@ export default function Inventory({ user }: { user: CurrentUser }) {
|
||||
<div className="d-flex align-items-center mb-3">
|
||||
<h2 className="page-title mb-0">Inventory</h2>
|
||||
<div className="ms-auto d-flex gap-2">
|
||||
<Link to="/inventory/racks" className="btn">
|
||||
Racks
|
||||
</Link>
|
||||
<Link to="/inventory/lifecycle" className="btn">
|
||||
Lifecycle
|
||||
</Link>
|
||||
|
||||
@@ -0,0 +1,140 @@
|
||||
import { useEffect, useState } from "react";
|
||||
import { Link } from "react-router-dom";
|
||||
import { api } from "../api/client";
|
||||
import EmptyState from "../components/EmptyState";
|
||||
import { ErrorNotice } from "../components/Notice";
|
||||
import { BlockSkeleton } from "../components/Skeleton";
|
||||
import { readableError } from "../utils/errors";
|
||||
import { KIND_LABELS } from "../utils/inventory";
|
||||
|
||||
type Rack = Awaited<ReturnType<typeof api.inventory.racks>>["racks"][number];
|
||||
|
||||
const UNIT_H = 24;
|
||||
const KIND_COLOR: Record<string, string> = { server: "#4299e1", storage: "#38b2ac", network: "#48bb78", power: "#f59f00", client: "#a78bfa", other: "#8896ab" };
|
||||
|
||||
/** One rack, from the bottom (unit 1) up, as it stands: each piece where it says it sits, empty units between, a clash in red. */
|
||||
function RackDrawing({ rack }: { rack: Rack }) {
|
||||
const height = rack.units * UNIT_H;
|
||||
const yOf = (unit: number) => height - unit * UNIT_H; // top edge of a unit
|
||||
return (
|
||||
<div className="rack-drawing">
|
||||
<svg viewBox={`0 0 260 ${height + 4}`} width="100%" role="img" aria-label={`${rack.name}: ${rack.slots.length} pieces in ${rack.units} units${rack.slots.some((s) => s.clash) ? ", with a clash" : ""}`} style={{ maxWidth: 300 }}>
|
||||
<rect x="26" y="1" width="228" height={height + 2} fill="none" stroke="currentColor" strokeOpacity="0.5" strokeWidth="2" rx="3" />
|
||||
{Array.from({ length: rack.units }, (_, i) => i + 1).map((u) => (
|
||||
<g key={u}>
|
||||
<line x1="26" x2="254" y1={yOf(u) + 1} y2={yOf(u) + 1} stroke="currentColor" strokeOpacity="0.1" />
|
||||
<text x="22" y={yOf(u) + UNIT_H / 2 + 4} textAnchor="end" fontSize="10" fill="currentColor" fillOpacity="0.55">
|
||||
{u}
|
||||
</text>
|
||||
</g>
|
||||
))}
|
||||
{rack.slots.map((s) => {
|
||||
const h = (s.to - s.from + 1) * UNIT_H;
|
||||
const y = yOf(s.to);
|
||||
return (
|
||||
<g key={s.assetId}>
|
||||
<title>{`${s.name}${s.model ? ` — ${s.model}` : ""} (${KIND_LABELS[s.kind as keyof typeof KIND_LABELS] ?? s.kind}, ${s.from === s.to ? `unit ${s.from}` : `units ${s.from}–${s.to}`}${s.powerWatts !== null ? `, ${s.powerWatts} W` : ""})${s.clash ? " — another piece is in the same unit" : ""}`}</title>
|
||||
<rect x="29" y={y + 2} width="222" height={h - 3} rx="3" fill={KIND_COLOR[s.kind] ?? KIND_COLOR.other} fillOpacity="0.28" stroke={s.clash ? "#f03e3e" : KIND_COLOR[s.kind] ?? KIND_COLOR.other} strokeWidth={s.clash ? 2.5 : 1.5} />
|
||||
<text x="38" y={y + Math.min(h, UNIT_H) / 2 + 4} fontSize="12" fill="currentColor" fontWeight="600">
|
||||
{s.name.length > 30 ? `${s.name.slice(0, 29)}…` : s.name}
|
||||
</text>
|
||||
{h >= UNIT_H * 2 && s.model && (
|
||||
<text x="38" y={y + UNIT_H / 2 + 20} fontSize="10.5" fill="currentColor" fillOpacity="0.75">
|
||||
{s.model.length > 34 ? `${s.model.slice(0, 33)}…` : s.model}
|
||||
</text>
|
||||
)}
|
||||
</g>
|
||||
);
|
||||
})}
|
||||
</svg>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* The racks, drawn from the inventory: each piece of hardware that says which unit it sits in (U12, or U12-13) and where (the rack's name in its location). Edit a
|
||||
* piece's location and unit on the Inventory page and it moves here. Print it for the door of the cabinet.
|
||||
*/
|
||||
export default function InventoryRacks() {
|
||||
const [racks, setRacks] = useState<Rack[] | null>(null);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
api.inventory
|
||||
.racks()
|
||||
.then((res) => setRacks(res.racks))
|
||||
.catch((err) => setError(readableError(err)));
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<>
|
||||
<div className="d-flex flex-wrap align-items-center gap-2 mb-1">
|
||||
<h2 className="page-title mb-0">Racks</h2>
|
||||
<div className="ms-auto btn-list no-print">
|
||||
<button type="button" className="btn btn-outline-secondary" onClick={() => window.print()} disabled={!racks || racks.length === 0}>
|
||||
Print
|
||||
</button>
|
||||
<Link to="/inventory" className="btn btn-outline-secondary">
|
||||
Hardware
|
||||
</Link>
|
||||
</div>
|
||||
</div>
|
||||
<div className="text-secondary mb-3 no-print">
|
||||
Each piece of hardware that gives a unit (“U12”, or “U12-13” for a tall one) and the rack’s name as its location is drawn where it sits, from the bottom up. Two pieces in the same unit are shown in red.
|
||||
</div>
|
||||
<ErrorNotice error={error} />
|
||||
{racks === null && !error && <BlockSkeleton lines={4} />}
|
||||
{racks !== null && racks.length === 0 && (
|
||||
<div className="card">
|
||||
<EmptyState title="No racks to draw yet" hint={<>On the <Link to="/inventory">Hardware</Link> page, give a piece a location (the rack’s name) and a rack unit such as U12, and it appears here.</>} />
|
||||
</div>
|
||||
)}
|
||||
<div className="row row-cards">
|
||||
{racks?.map((rack) => (
|
||||
<div className="col-md-6 col-xl-4" key={rack.name}>
|
||||
<div className="card h-100">
|
||||
<div className="card-header">
|
||||
<h3 className="card-title">{rack.name}</h3>
|
||||
<div className="card-actions text-secondary small">
|
||||
{rack.slots.length} {rack.slots.length === 1 ? "piece" : "pieces"} · {rack.usedUnits} of {rack.units} units{rack.watts > 0 ? ` · ${rack.watts} W` : ""}
|
||||
</div>
|
||||
</div>
|
||||
<div className="card-body">
|
||||
<RackDrawing rack={rack} />
|
||||
{rack.slots.some((s) => s.clash) && (
|
||||
<div className="alert alert-danger mt-3 mb-0">
|
||||
Clash: {rack.slots.filter((s) => s.clash).map((s) => `${s.name} (${s.from === s.to ? `U${s.from}` : `U${s.from}-${s.to}`})`).join(", ")} claim the same unit. Correct one of them on the Hardware page.
|
||||
</div>
|
||||
)}
|
||||
{rack.unplaced.length > 0 && (
|
||||
<div className="mt-3 small">
|
||||
<span className="text-secondary">Named here but not in a unit: </span>
|
||||
{rack.unplaced.map((u, i) => (
|
||||
<span key={u.assetId}>
|
||||
{i > 0 && ", "}
|
||||
<Link to={`/inventory?asset=${u.assetId}`}>{u.name}</Link>
|
||||
</span>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
<ul className="list-unstyled small mt-3 mb-0 d-print-none">
|
||||
{[...rack.slots].reverse().map((s) => (
|
||||
<li key={s.assetId}>
|
||||
<span className="text-secondary">{s.from === s.to ? `U${s.from}` : `U${s.from}–${s.to}`}</span> <Link to={`/inventory?asset=${s.assetId}`}>{s.name}</Link>
|
||||
{s.serverId !== null && (
|
||||
<>
|
||||
{" "}
|
||||
· <Link to={`/servers/${s.serverId}`}>server</Link>
|
||||
</>
|
||||
)}
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -74,7 +74,28 @@ export default function MyWorkPage({ user }: { user: CurrentUser }) {
|
||||
}
|
||||
}
|
||||
|
||||
const nothing = work !== null && work.alerts.length === 0 && work.windows.length === 0 && work.updateRuns.length === 0;
|
||||
const nothing = work !== null && work.alerts.length === 0 && work.windows.length === 0 && work.updateRuns.length === 0 && work.todos.length === 0;
|
||||
|
||||
async function finish(t: MyWork["todos"][number]) {
|
||||
try {
|
||||
await api.todos.update(t.id, { done: true });
|
||||
toast.success(`Done: ${t.title}`, {
|
||||
label: "Undo",
|
||||
onClick: async () => {
|
||||
try {
|
||||
await api.todos.update(t.id, { done: false });
|
||||
} catch (err) {
|
||||
toast.error(readableError(err));
|
||||
} finally {
|
||||
reload();
|
||||
}
|
||||
},
|
||||
});
|
||||
reload();
|
||||
} catch (err) {
|
||||
toast.error(readableError(err));
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
@@ -82,7 +103,7 @@ export default function MyWorkPage({ user }: { user: CurrentUser }) {
|
||||
<h2 className="page-title mb-0">My work</h2>
|
||||
</div>
|
||||
<div className="text-secondary mb-3">
|
||||
What has your name on it: alerts you acknowledged or were handed, maintenance you started and update runs you started. Whatever is about to run out is marked, and listed first.
|
||||
What has your name on it: alerts you acknowledged or were handed, maintenance you started and update runs you started, and jobs handed to you. Whatever is about to run out is marked, and listed first.
|
||||
</div>
|
||||
|
||||
{work === null && <BlockSkeleton lines={4} />}
|
||||
@@ -146,6 +167,32 @@ export default function MyWorkPage({ user }: { user: CurrentUser }) {
|
||||
</div>
|
||||
)}
|
||||
|
||||
{work && work.todos.length > 0 && (
|
||||
<div className="card mb-3">
|
||||
<div className="card-header">
|
||||
<h3 className="card-title">Jobs handed to you</h3>
|
||||
<div className="card-actions">
|
||||
<Link to="/todo?who=me" className="small">
|
||||
To do
|
||||
</Link>
|
||||
</div>
|
||||
</div>
|
||||
<ul className="list-group list-group-flush">
|
||||
{work.todos.map((t) => (
|
||||
<li key={t.id} className="list-group-item d-flex gap-3 align-items-start">
|
||||
<input type="checkbox" className="form-check-input mt-1" aria-label={`Done: ${t.title}`} checked={false} onChange={() => void finish(t)} />
|
||||
<div className="me-auto">
|
||||
<div className="fw-bold">{t.title}</div>
|
||||
{t.detail && <div className="text-secondary small">{t.detail}</div>}
|
||||
{t.target && <div className="small text-secondary">about {t.target.name}</div>}
|
||||
</div>
|
||||
{t.dueDate && <span className={`small ${t.overdue ? "text-red" : t.dueSoon ? "text-yellow" : "text-secondary"}`}>{t.overdue ? "overdue · " : ""}{t.dueDate}</span>}
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{work && work.windows.length > 0 && (
|
||||
<div className="card mb-3">
|
||||
<div className="card-header">
|
||||
|
||||
@@ -8,6 +8,9 @@ import TimeAgo from "../components/TimeAgo";
|
||||
import ViewTabs from "../components/ViewTabs";
|
||||
import SilenceButton from "../components/SilenceButton";
|
||||
import RunbookEntry from "../components/RunbookEntry";
|
||||
import NetworkDiagram from "../components/NetworkDiagram";
|
||||
import NotesCard from "../components/NotesCard";
|
||||
import DetailDrawer from "../components/DetailDrawer";
|
||||
import CopyButton from "../components/CopyButton";
|
||||
import EmptyState, { EmptyRow } from "../components/EmptyState";
|
||||
import { BlockSkeleton } from "../components/Skeleton";
|
||||
@@ -62,12 +65,14 @@ function StateBadge({ d }: { d: NetworkDevice }) {
|
||||
export default function NetworkDevices({ user }: { user: CurrentUser }) {
|
||||
const canAct = user.role === "admin" || user.role === "operator";
|
||||
const [params] = useSearchParams();
|
||||
const view = params.get("view") === "wifi" ? "wifi" : "devices";
|
||||
const asked = params.get("view");
|
||||
const view = asked === "wifi" ? "wifi" : asked === "diagram" ? "diagram" : "devices";
|
||||
const [data, setData] = useState<NetworkOverview | null>(null);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [reading, setReading] = useState(false);
|
||||
const [kind, setKind] = useState<"all" | NetworkDeviceKind>("all");
|
||||
const [status, setStatus] = useState<"all" | "problem">("all");
|
||||
const [noting, setNoting] = useState<NetworkDevice | null>(null);
|
||||
|
||||
const load = useCallback(() => {
|
||||
return api.network
|
||||
@@ -120,6 +125,7 @@ export default function NetworkDevices({ user }: { user: CurrentUser }) {
|
||||
label="Network devices view"
|
||||
tabs={[
|
||||
{ to: "/network/devices", label: "Devices", active: view === "devices" },
|
||||
{ to: "/network/devices?view=diagram", label: "Diagram", active: view === "diagram" },
|
||||
{ to: "/network/devices?view=wifi", label: "Networks & Wi-Fi", active: view === "wifi" },
|
||||
]}
|
||||
/>
|
||||
@@ -324,6 +330,9 @@ export default function NetworkDevices({ user }: { user: CurrentUser }) {
|
||||
<div className="btn-list flex-nowrap align-items-center">
|
||||
{!d.inMaintenance && <SilenceButton target={{ type: "device", id: d.id, name: d.name }} onDone={() => void load()} />}
|
||||
<RunbookEntry targetType="device" targetId={d.id} targetName={d.name} canEdit={canAct} offerAdd />
|
||||
<button type="button" className="btn btn-link btn-sm p-0" onClick={() => setNoting(d)}>
|
||||
Notes
|
||||
</button>
|
||||
</div>
|
||||
</td>
|
||||
)}
|
||||
@@ -345,7 +354,22 @@ export default function NetworkDevices({ user }: { user: CurrentUser }) {
|
||||
</>
|
||||
)}
|
||||
|
||||
{data && data.integrations.length > 0 && view === "diagram" && (
|
||||
<>
|
||||
<div className="mb-2 no-print">
|
||||
<button type="button" className="btn btn-sm btn-outline-secondary" onClick={() => window.print()}>
|
||||
Print
|
||||
</button>
|
||||
</div>
|
||||
<NetworkDiagram devices={devices} />
|
||||
</>
|
||||
)}
|
||||
{data && data.integrations.length > 0 && view === "wifi" && <NetworksAndWifi data={data} />}
|
||||
{noting && (
|
||||
<DetailDrawer title={noting.name} subtitle="Notes" onClose={() => setNoting(null)}>
|
||||
<NotesCard targetType="device" targetId={noting.id} canEdit={canAct} bare />
|
||||
</DetailDrawer>
|
||||
)}
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,242 @@
|
||||
import { useCallback, useEffect, useMemo, useState } from "react";
|
||||
import { Link } from "react-router-dom";
|
||||
import { api, type CurrentUser, type IntegrationProblem, type NpmHost, type NpmOverview } from "../api/client";
|
||||
import SortableTh from "../components/SortableTh";
|
||||
import Pagination from "../components/Pagination";
|
||||
import TableToolbar from "../components/TableToolbar";
|
||||
import TimeAgo from "../components/TimeAgo";
|
||||
import EmptyState, { EmptyRow } from "../components/EmptyState";
|
||||
import { BlockSkeleton } from "../components/Skeleton";
|
||||
import { ErrorNotice } from "../components/Notice";
|
||||
import { useSortable } from "../hooks/useSortable";
|
||||
import { useTableSearch } from "../hooks/useTableSearch";
|
||||
import { usePagination } from "../hooks/usePagination";
|
||||
import { downloadCsv } from "../utils/csv";
|
||||
import { formatDate } from "../utils/date";
|
||||
import { readableError } from "../utils/errors";
|
||||
import { toast } from "../utils/toast";
|
||||
|
||||
type Row = NpmHost & { integrationName: string; domain: string; target: string; certDays: number | null; certName: string; statusRank: number; serverName: string };
|
||||
|
||||
const DAY_MS = 86_400_000;
|
||||
const daysLeft = (iso: string | null): number | null => (iso && Number.isFinite(Date.parse(iso)) ? Math.floor((Date.parse(iso) - Date.now()) / DAY_MS) : null);
|
||||
|
||||
function StatusBadge({ h }: { h: Row }) {
|
||||
if (!h.enabled) return <span className="badge bg-secondary-lt text-secondary">Switched off</span>;
|
||||
if (!h.online)
|
||||
return (
|
||||
<span className="badge bg-red-lt text-red" title={h.error ?? "Nginx Proxy Manager could not set this host up"}>
|
||||
Not working
|
||||
</span>
|
||||
);
|
||||
return <span className="badge bg-green-lt text-green">Working</span>;
|
||||
}
|
||||
|
||||
function CertBadge({ h }: { h: Row }) {
|
||||
if (!h.certificate) return <span className="text-secondary">{h.sslForced ? "—" : "No certificate"}</span>;
|
||||
const d = h.certDays;
|
||||
const color = d === null ? "secondary" : d < 0 ? "red" : d <= 3 ? "red" : d <= 14 ? "yellow" : "green";
|
||||
const words = d === null ? "no end date" : d < 0 ? `ran out ${-d} ${d === -1 ? "day" : "days"} ago` : d === 0 ? "ends today" : `${d} ${d === 1 ? "day" : "days"} left`;
|
||||
return (
|
||||
<span title={h.certificate.expiresAt ? `${h.certificate.name} — ends ${formatDate(new Date(h.certificate.expiresAt))}` : h.certificate.name}>
|
||||
<span className={`badge bg-${color}-lt text-${color}`}>{words}</span>
|
||||
<div className="text-secondary small">{h.certificate.name}</div>
|
||||
</span>
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* What Nginx Proxy Manager proxies: each name people use, where it forwards to (and which server that is), its certificate and whether NPM could set it up. Read
|
||||
* from NPM when the page is opened (and at most once a minute); nothing here changes anything in NPM.
|
||||
*/
|
||||
export default function ProxyHosts({ user }: { user: CurrentUser }) {
|
||||
const [data, setData] = useState<NpmOverview | null>(null);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [reading, setReading] = useState(false);
|
||||
|
||||
const load = useCallback(
|
||||
(refresh = false) =>
|
||||
api.npm
|
||||
.overview(refresh)
|
||||
.then((res) => {
|
||||
setData(res);
|
||||
setError(null);
|
||||
})
|
||||
.catch((err) => setError(readableError(err))),
|
||||
[],
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
void load();
|
||||
const timer = setInterval(() => void load(), 60_000);
|
||||
return () => clearInterval(timer);
|
||||
}, [load]);
|
||||
|
||||
async function readNow() {
|
||||
setReading(true);
|
||||
await load(true);
|
||||
setReading(false);
|
||||
toast.success("Read from Nginx Proxy Manager.");
|
||||
}
|
||||
|
||||
const rows: Row[] = useMemo(
|
||||
() =>
|
||||
(data?.integrations ?? []).flatMap((i) =>
|
||||
i.hosts.map((h) => ({
|
||||
...h,
|
||||
integrationName: i.name,
|
||||
domain: h.domains.join(", "),
|
||||
target: `${h.forwardScheme}://${h.forwardHost}${h.forwardPort !== null ? `:${h.forwardPort}` : ""}`,
|
||||
certDays: daysLeft(h.certificate?.expiresAt ?? null),
|
||||
certName: h.certificate?.name ?? "",
|
||||
statusRank: !h.enabled ? 2 : h.online ? 1 : 0,
|
||||
serverName: h.server?.name ?? "",
|
||||
})),
|
||||
),
|
||||
[data],
|
||||
);
|
||||
const tableSearch = useTableSearch(rows);
|
||||
const { sorted, sortKey, sortDir, requestSort } = useSortable(tableSearch.filtered, "statusRank", "asc", { url: true });
|
||||
const { pageItems, page, setPage, pageCount, totalCount } = usePagination(sorted);
|
||||
|
||||
const problems: (IntegrationProblem & { integration: string })[] = (data?.integrations ?? []).flatMap((i) => i.problems.map((p) => ({ ...p, integration: i.name })));
|
||||
const soon = rows.filter((h) => h.enabled && h.certDays !== null && h.certDays <= 14).length;
|
||||
const noNpm = data !== null && data.integrations.length === 0;
|
||||
|
||||
function exportCsv() {
|
||||
downloadCsv(
|
||||
"proxy-hosts.csv",
|
||||
["Names", "Forwards to", "Server", "Status", "Certificate", "Certificate ends", "Force HTTPS", "Nginx Proxy Manager"],
|
||||
(sorted ?? []).map((h) => [h.domain, h.target, h.serverName, !h.enabled ? "switched off" : h.online ? "working" : "not working", h.certName, h.certificate?.expiresAt ?? "", h.sslForced ? "yes" : "no", h.integrationName]),
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<div className="d-flex flex-wrap align-items-center gap-2 mb-1">
|
||||
<h2 className="page-title mb-0">Proxy hosts</h2>
|
||||
{(user.role === "admin" || user.role === "operator") && data && data.integrations.length > 0 && (
|
||||
<div className="ms-auto btn-list">
|
||||
<button className="btn btn-outline-secondary" onClick={() => void readNow()} disabled={reading}>
|
||||
{reading ? "Reading…" : "Read now"}
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<div className="text-secondary mb-3">
|
||||
What Nginx Proxy Manager serves: the name, where it forwards to, the certificate and whether NPM could set it up. It only reads — nothing here changes anything in NPM. A certificate in its last
|
||||
two weeks, or a host NPM can’t set up, is an alert.
|
||||
</div>
|
||||
<ErrorNotice error={error} />
|
||||
{data === null && !error && <BlockSkeleton lines={4} />}
|
||||
|
||||
{noNpm && (
|
||||
<div className="card">
|
||||
<EmptyState
|
||||
title="No Nginx Proxy Manager connected"
|
||||
hint="Add an Nginx Proxy Manager integration — a user that may only view proxy hosts and certificates is enough — and what it serves appears here."
|
||||
action={user.role === "admin" ? { label: "Add Nginx Proxy Manager", to: "/integrations" } : undefined}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{data && data.integrations.length > 0 && (
|
||||
<div className="mb-3 small">
|
||||
{data.integrations.map((i) => (
|
||||
<div key={i.id} className={i.error ? "text-red" : "text-secondary"}>
|
||||
<strong>{i.name}</strong>
|
||||
{i.error ? <> — couldn’t be read: {i.error}</> : <> — read <TimeAgo value={i.readAt} />{i.version ? ` · version ${i.version}` : ""} · {i.hosts.length} {i.hosts.length === 1 ? "host" : "hosts"}</>}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{data && data.integrations.some((i) => !i.error) && (
|
||||
<>
|
||||
<div className="row row-cards mb-3">
|
||||
{[
|
||||
{ label: "Proxy hosts", value: rows.length, color: "" },
|
||||
{ label: "Not working", value: rows.filter((h) => h.enabled && !h.online).length, color: rows.some((h) => h.enabled && !h.online) ? "text-red" : "text-green" },
|
||||
{ label: "Certificates ending within 14 days", value: soon, color: soon > 0 ? "text-yellow" : "text-green" },
|
||||
{ label: "Switched off", value: rows.filter((h) => !h.enabled).length, color: "" },
|
||||
].map((c) => (
|
||||
<div className="col-6 col-lg-3" key={c.label}>
|
||||
<div className="card card-sm">
|
||||
<div className="card-body">
|
||||
<div className={`h2 mb-0 ${c.color}`}>{c.value}</div>
|
||||
<div className="text-secondary">{c.label}</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
|
||||
{problems.length > 0 && (
|
||||
<div className="card mb-3">
|
||||
<div className="card-header">
|
||||
<h3 className="card-title">Needs attention</h3>
|
||||
</div>
|
||||
<div className="list-group list-group-flush">
|
||||
{problems.map((p) => (
|
||||
<div className="list-group-item d-flex align-items-center gap-2" key={p.key}>
|
||||
<span className={`badge ${p.severity === "critical" ? "bg-red-lt text-red" : "bg-yellow-lt text-yellow"}`}>{p.severity === "critical" ? "Critical" : "Warning"}</span>
|
||||
<span>{p.message}</span>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="card">
|
||||
<TableToolbar search={tableSearch.query} onSearch={tableSearch.setQuery} placeholder="Search names, targets, servers…" shown={sorted?.length} total={rows.length} onExport={exportCsv} exportDisabled={!sorted || sorted.length === 0} />
|
||||
<div className="table-responsive">
|
||||
<table className="table table-vcenter card-table table-stack">
|
||||
<thead>
|
||||
<tr>
|
||||
<SortableTh<Row> label="Status" sortKeyName="statusRank" activeKey={sortKey} direction={sortDir} onSort={requestSort} />
|
||||
<SortableTh<Row> label="Name" sortKeyName="domain" activeKey={sortKey} direction={sortDir} onSort={requestSort} />
|
||||
<SortableTh<Row> label="Forwards to" sortKeyName="target" activeKey={sortKey} direction={sortDir} onSort={requestSort} />
|
||||
<SortableTh<Row> label="Certificate" sortKeyName="certDays" activeKey={sortKey} direction={sortDir} onSort={requestSort} />
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{(pageItems ?? []).length === 0 && <EmptyRow colSpan={4} title="No proxy hosts" filtered={tableSearch.active ? () => tableSearch.setQuery("") : undefined} />}
|
||||
{(pageItems ?? []).map((h) => (
|
||||
<tr key={`${h.integrationName}-${h.id}`}>
|
||||
<td>
|
||||
<StatusBadge h={h} />
|
||||
{h.error && h.enabled && <div className="text-secondary small">{h.error.slice(0, 120)}</div>}
|
||||
</td>
|
||||
<td className="stack-wide">
|
||||
{h.domains.map((d, i) => (
|
||||
<div key={i}>
|
||||
<a href={`${h.sslForced || h.certificate ? "https" : "http"}://${d}`} target="_blank" rel="noopener noreferrer">
|
||||
<strong>{d}</strong>
|
||||
</a>
|
||||
</div>
|
||||
))}
|
||||
{data && data.integrations.length > 1 && <div className="text-secondary small">{h.integrationName}</div>}
|
||||
</td>
|
||||
<td className="stack-label" data-label="Forwards to">
|
||||
<span className="font-monospace">{h.target}</span>
|
||||
{h.server && (
|
||||
<div className="small">
|
||||
<Link to={`/servers/${h.server.id}`}>{h.server.name}</Link>
|
||||
</div>
|
||||
)}
|
||||
</td>
|
||||
<td className="stack-label" data-label="Certificate">
|
||||
<CertBadge h={h} />
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<Pagination page={page} pageCount={pageCount} totalCount={totalCount} onPageChange={setPage} />
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -15,6 +15,7 @@ import {
|
||||
import ServerPorts from "../components/ServerPorts";
|
||||
import ServerTrends from "../components/ServerTrends";
|
||||
import UptimeHistory from "../components/UptimeHistory";
|
||||
import NotesCard from "../components/NotesCard";
|
||||
import ServerActivity from "../components/ServerActivity";
|
||||
import TimeAgo from "../components/TimeAgo";
|
||||
import UptimeRibbon from "../components/UptimeRibbon";
|
||||
@@ -705,6 +706,17 @@ export default function ServerDetail({ user }: { user: CurrentUser }) {
|
||||
<dd className="col-7">
|
||||
<UptimeRibbon kind="servers" id={server.id} name={server.name} />
|
||||
</dd>
|
||||
{detail.os && (
|
||||
<>
|
||||
<dt className="col-5">System</dt>
|
||||
<dd className="col-7">
|
||||
{detail.os.product}
|
||||
{detail.os.state === "ended" && <span className="badge bg-red-lt text-red ms-2" title={`Support ended on ${detail.os.endsOn}`}>support ended</span>}
|
||||
{detail.os.state === "soon" && <span className="badge bg-yellow-lt text-yellow ms-2" title={`Support ends on ${detail.os.endsOn}`}>support ends in {detail.os.daysLeft} {detail.os.daysLeft === 1 ? "day" : "days"}</span>}
|
||||
{detail.os.state === "ok" && detail.os.endsOn && <div className="text-secondary small">supported until {detail.os.endsOn}</div>}
|
||||
</dd>
|
||||
</>
|
||||
)}
|
||||
<dt className="col-5">Runs on</dt>
|
||||
<dd className="col-7">
|
||||
<RunsOn serverId={server.id} parent={detail.relations.parent} noParent={server.noParent} canEdit={isAdmin} onChanged={() => void loadDetail()} />
|
||||
@@ -752,6 +764,7 @@ export default function ServerDetail({ user }: { user: CurrentUser }) {
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<NotesCard targetType="server" targetId={server.id} canEdit={canEditTasks} />
|
||||
<div className="card mb-3">
|
||||
<div className="card-header">
|
||||
<h3 className="card-title">Runbooks</h3>
|
||||
|
||||
@@ -0,0 +1,320 @@
|
||||
import { useEffect, useMemo, useState } from "react";
|
||||
import { Link, useSearchParams } from "react-router-dom";
|
||||
import { api, type CurrentUser, type TodoInput, type TodoItem } from "../api/client";
|
||||
import DetailDrawer from "../components/DetailDrawer";
|
||||
import EmptyState from "../components/EmptyState";
|
||||
import { ErrorNotice } from "../components/Notice";
|
||||
import { BlockSkeleton } from "../components/Skeleton";
|
||||
import TimeAgo from "../components/TimeAgo";
|
||||
import { refreshMyWork } from "../hooks/useMyWork";
|
||||
import { formatDate } from "../utils/date";
|
||||
import { readableError } from "../utils/errors";
|
||||
import { toast } from "../utils/toast";
|
||||
import { removeWithUndo } from "../utils/undoable";
|
||||
|
||||
type Person = { id: number; name: string; role: string };
|
||||
type Place = { type: "server" | "device"; id: number; name: string };
|
||||
|
||||
const today = () => new Date().toISOString().slice(0, 10);
|
||||
|
||||
function dueWords(t: TodoItem): { text: string; className: string } | null {
|
||||
if (!t.dueDate) return null;
|
||||
const days = Math.round((Date.parse(`${t.dueDate}T00:00:00Z`) - Date.parse(`${today()}T00:00:00Z`)) / 86_400_000);
|
||||
if (t.doneAt) return { text: formatDate(new Date(`${t.dueDate}T12:00:00Z`)), className: "text-secondary" };
|
||||
if (days < 0) return { text: `${-days} ${days === -1 ? "day" : "days"} overdue`, className: "text-red" };
|
||||
if (days === 0) return { text: "due today", className: "text-yellow" };
|
||||
if (days === 1) return { text: "due tomorrow", className: "text-yellow" };
|
||||
return { text: `due ${formatDate(new Date(`${t.dueDate}T12:00:00Z`))}`, className: days <= 7 ? "text-yellow" : "text-secondary" };
|
||||
}
|
||||
|
||||
/** The same form to make a job and to change one. */
|
||||
function JobForm({ initial, people, places, busy, submitLabel, onSubmit, onCancel }: { initial: TodoInput & { targetKey?: string }; people: Person[]; places: Place[]; busy: boolean; submitLabel: string; onSubmit: (v: TodoInput) => void; onCancel?: () => void }) {
|
||||
const [title, setTitle] = useState(initial.title);
|
||||
const [detail, setDetail] = useState(initial.detail ?? "");
|
||||
const [due, setDue] = useState(initial.dueDate ?? "");
|
||||
const [who, setWho] = useState(initial.assigneeId ? String(initial.assigneeId) : "");
|
||||
const [about, setAbout] = useState(initial.targetType && initial.targetId ? `${initial.targetType}:${initial.targetId}` : "");
|
||||
return (
|
||||
<form
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault();
|
||||
const [type, id] = about.split(":");
|
||||
onSubmit({ title: title.trim(), detail: detail.trim() || null, dueDate: due || null, assigneeId: who ? Number(who) : null, targetType: about ? (type as "server" | "device") : null, targetId: about ? Number(id) : null });
|
||||
}}
|
||||
>
|
||||
<div className="row g-3">
|
||||
<div className="col-12">
|
||||
<label className="form-label" htmlFor="todo-title">
|
||||
What is to be done
|
||||
</label>
|
||||
<input id="todo-title" className="form-control" maxLength={160} required value={title} onChange={(e) => setTitle(e.target.value)} placeholder="e.g. Replace the UPS battery" autoFocus />
|
||||
</div>
|
||||
<div className="col-md-4">
|
||||
<label className="form-label" htmlFor="todo-due">
|
||||
By <span className="text-secondary">(optional)</span>
|
||||
</label>
|
||||
<input id="todo-due" type="date" className="form-control" value={due} onChange={(e) => setDue(e.target.value)} />
|
||||
</div>
|
||||
<div className="col-md-4">
|
||||
<label className="form-label" htmlFor="todo-who">
|
||||
For <span className="text-secondary">(optional)</span>
|
||||
</label>
|
||||
<select id="todo-who" className="form-select" value={who} onChange={(e) => setWho(e.target.value)}>
|
||||
<option value="">Nobody in particular</option>
|
||||
{people.map((p) => (
|
||||
<option key={p.id} value={p.id}>
|
||||
{p.name}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
<div className="col-md-4">
|
||||
<label className="form-label" htmlFor="todo-about">
|
||||
About <span className="text-secondary">(optional)</span>
|
||||
</label>
|
||||
<select id="todo-about" className="form-select" value={about} onChange={(e) => setAbout(e.target.value)}>
|
||||
<option value="">Nothing in particular</option>
|
||||
<optgroup label="Servers">
|
||||
{places.filter((p) => p.type === "server").map((p) => (
|
||||
<option key={`s${p.id}`} value={`server:${p.id}`}>
|
||||
{p.name}
|
||||
</option>
|
||||
))}
|
||||
</optgroup>
|
||||
{places.some((p) => p.type === "device") && (
|
||||
<optgroup label="Network devices">
|
||||
{places.filter((p) => p.type === "device").map((p) => (
|
||||
<option key={`d${p.id}`} value={`device:${p.id}`}>
|
||||
{p.name}
|
||||
</option>
|
||||
))}
|
||||
</optgroup>
|
||||
)}
|
||||
</select>
|
||||
</div>
|
||||
<div className="col-12">
|
||||
<label className="form-label" htmlFor="todo-detail">
|
||||
Details <span className="text-secondary">(optional)</span>
|
||||
</label>
|
||||
<textarea id="todo-detail" className="form-control" rows={3} maxLength={2000} value={detail} onChange={(e) => setDetail(e.target.value)} />
|
||||
</div>
|
||||
</div>
|
||||
<div className="d-flex gap-2 mt-3">
|
||||
<button type="submit" className="btn btn-primary" disabled={busy || title.trim() === ""}>
|
||||
{busy ? "Saving…" : submitLabel}
|
||||
</button>
|
||||
{onCancel && (
|
||||
<button type="button" className="btn" onClick={onCancel}>
|
||||
Cancel
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
</form>
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* The small jobs of a homelab, written down: what, by when, for whom and about which server or device. A job with a day is in Coming up; one handed to somebody is in
|
||||
* their My work. Ticking one off keeps it (under Done), and it can be brought back.
|
||||
*/
|
||||
export default function Todo({ user }: { user: CurrentUser }) {
|
||||
const canEdit = user.role === "admin" || user.role === "operator";
|
||||
const [params, setParams] = useSearchParams();
|
||||
const mineOnly = params.get("who") === "me";
|
||||
const [list, setList] = useState<TodoItem[] | null>(null);
|
||||
const [people, setPeople] = useState<Person[]>([]);
|
||||
const [places, setPlaces] = useState<Place[]>([]);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [editing, setEditing] = useState<TodoItem | null>(null);
|
||||
const [showDone, setShowDone] = useState(false);
|
||||
|
||||
function load() {
|
||||
api.todos
|
||||
.list()
|
||||
.then((res) => {
|
||||
setList(res.todos);
|
||||
setPeople(res.people);
|
||||
})
|
||||
.catch((err) => setError(readableError(err)));
|
||||
refreshMyWork();
|
||||
}
|
||||
useEffect(load, []);
|
||||
useEffect(() => {
|
||||
// What a job can be about: the servers, and the network devices when there are any.
|
||||
Promise.all([api.servers.list().catch(() => null), api.network.overview().catch(() => null)]).then(([s, n]) =>
|
||||
setPlaces([...(s?.servers ?? []).map((x) => ({ type: "server" as const, id: x.id, name: x.name })), ...(n?.devices ?? []).map((x) => ({ type: "device" as const, id: x.id, name: x.name }))]),
|
||||
);
|
||||
}, []);
|
||||
|
||||
const visible = useMemo(() => (list ?? []).filter((t) => !mineOnly || t.assignee?.id === user.id), [list, mineOnly, user.id]);
|
||||
const open = visible.filter((t) => !t.doneAt);
|
||||
const done = visible.filter((t) => t.doneAt);
|
||||
|
||||
async function create(v: TodoInput) {
|
||||
setBusy(true);
|
||||
setError(null);
|
||||
try {
|
||||
await api.todos.create(v);
|
||||
toast.success("Added.");
|
||||
load();
|
||||
} catch (err) {
|
||||
setError(readableError(err));
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
}
|
||||
|
||||
async function save(t: TodoItem, v: TodoInput) {
|
||||
setBusy(true);
|
||||
try {
|
||||
await api.todos.update(t.id, v);
|
||||
setEditing(null);
|
||||
toast.success("Saved.");
|
||||
load();
|
||||
} catch (err) {
|
||||
toast.error(readableError(err));
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
}
|
||||
|
||||
async function tick(t: TodoItem, finished: boolean) {
|
||||
try {
|
||||
await api.todos.update(t.id, { done: finished });
|
||||
if (finished) {
|
||||
toast.success(`Done: ${t.title}`, {
|
||||
label: "Undo",
|
||||
onClick: async () => {
|
||||
try {
|
||||
await api.todos.update(t.id, { done: false });
|
||||
} catch (err) {
|
||||
toast.error(readableError(err));
|
||||
} finally {
|
||||
load();
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
load();
|
||||
} catch (err) {
|
||||
toast.error(readableError(err));
|
||||
}
|
||||
}
|
||||
|
||||
async function remove(t: TodoItem) {
|
||||
setEditing(null);
|
||||
await removeWithUndo({
|
||||
remove: () => api.todos.remove(t.id),
|
||||
restore: () => api.todos.create({ title: t.title, detail: t.detail, dueDate: t.dueDate, assigneeId: t.assignee?.id ?? null, targetType: t.target?.type ?? null, targetId: t.target?.id ?? null }).then((r) => (t.doneAt ? api.todos.update(r.todo.id, { done: true }) : r)),
|
||||
message: `Removed "${t.title}".`,
|
||||
reload: load,
|
||||
});
|
||||
}
|
||||
|
||||
const row = (t: TodoItem) => {
|
||||
const due = dueWords(t);
|
||||
return (
|
||||
<li key={t.id} className="list-group-item d-flex gap-3 align-items-start">
|
||||
<input type="checkbox" className="form-check-input mt-1" aria-label={t.doneAt ? `Bring back: ${t.title}` : `Done: ${t.title}`} checked={!!t.doneAt} disabled={!canEdit} onChange={() => void tick(t, !t.doneAt)} />
|
||||
<div className="me-auto">
|
||||
<div className={t.doneAt ? "text-decoration-line-through text-secondary" : "fw-bold"}>{t.title}</div>
|
||||
{t.detail && <div className="text-secondary small" style={{ whiteSpace: "pre-wrap" }}>{t.detail}</div>}
|
||||
<div className="small d-flex flex-wrap gap-2 mt-1">
|
||||
{due && <span className={due.className}>{due.text}</span>}
|
||||
{t.assignee && <span className="badge bg-blue-lt text-blue">{t.assignee.name}</span>}
|
||||
{t.target && <Link to={t.target.type === "server" ? `/servers/${t.target.id}` : "/network/devices"}>{t.target.name}</Link>}
|
||||
{t.doneAt && (
|
||||
<span className="text-secondary">
|
||||
done <TimeAgo value={t.doneAt} />
|
||||
{t.doneBy ? ` by ${t.doneBy}` : ""}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
{canEdit && (
|
||||
<button type="button" className="btn btn-sm btn-outline-secondary" onClick={() => setEditing(t)}>
|
||||
Edit
|
||||
</button>
|
||||
)}
|
||||
</li>
|
||||
);
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
<div className="d-flex flex-wrap align-items-center gap-2 mb-1">
|
||||
<h2 className="page-title mb-0">To do</h2>
|
||||
<div className="ms-auto btn-group btn-group-sm" role="group" aria-label="Whose jobs">
|
||||
<button type="button" className={`btn ${!mineOnly ? "btn-primary" : "btn-outline-secondary"}`} aria-pressed={!mineOnly} onClick={() => setParams({})}>
|
||||
Everyone’s
|
||||
</button>
|
||||
<button type="button" className={`btn ${mineOnly ? "btn-primary" : "btn-outline-secondary"}`} aria-pressed={mineOnly} onClick={() => setParams({ who: "me" })}>
|
||||
Handed to me
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<div className="text-secondary mb-3">
|
||||
The small jobs: replace the battery, move the NAS to the new switch. A job with a day is in <Link to="/coming-up?kinds=todo">Coming up</Link>; one handed to somebody is in their <Link to="/my-work">My work</Link>.
|
||||
</div>
|
||||
<ErrorNotice error={error} />
|
||||
|
||||
{canEdit && (
|
||||
<div className="card mb-3">
|
||||
<div className="card-header">
|
||||
<h3 className="card-title">Add a job</h3>
|
||||
</div>
|
||||
<div className="card-body">
|
||||
<JobForm key={list?.length ?? 0} initial={{ title: "" }} people={people} places={places} busy={busy} submitLabel="Add" onSubmit={(v) => void create(v)} />
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{list === null && !error && <BlockSkeleton lines={3} />}
|
||||
{list !== null && open.length === 0 && (
|
||||
<div className="card mb-3">
|
||||
<EmptyState title={mineOnly ? "Nothing is handed to you" : "Nothing to do"} hint={canEdit ? "Add a job above, and give it a day if it has one." : "Nothing has been written down."} />
|
||||
</div>
|
||||
)}
|
||||
{open.length > 0 && (
|
||||
<div className="card mb-3">
|
||||
<div className="card-header">
|
||||
<h3 className="card-title">To do ({open.length})</h3>
|
||||
</div>
|
||||
<ul className="list-group list-group-flush">{open.map(row)}</ul>
|
||||
</div>
|
||||
)}
|
||||
{done.length > 0 && (
|
||||
<div className="card">
|
||||
<div className="card-header">
|
||||
<h3 className="card-title">Done ({done.length})</h3>
|
||||
<div className="card-actions">
|
||||
<button type="button" className="btn btn-sm" onClick={() => setShowDone((v) => !v)}>
|
||||
{showDone ? "Hide" : "Show"}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
{showDone && <ul className="list-group list-group-flush">{done.map(row)}</ul>}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{editing && (
|
||||
<DetailDrawer title="Edit the job" subtitle={editing.title} onClose={() => setEditing(null)}>
|
||||
<JobForm
|
||||
initial={{ title: editing.title, detail: editing.detail, dueDate: editing.dueDate, assigneeId: editing.assignee?.id ?? null, targetType: editing.target?.type ?? null, targetId: editing.target?.id ?? null }}
|
||||
people={people}
|
||||
places={places}
|
||||
busy={busy}
|
||||
submitLabel="Save"
|
||||
onSubmit={(v) => void save(editing, v)}
|
||||
onCancel={() => setEditing(null)}
|
||||
/>
|
||||
<hr />
|
||||
<button type="button" className="btn btn-outline-danger" onClick={() => void remove(editing)}>
|
||||
Remove it
|
||||
</button>
|
||||
</DetailDrawer>
|
||||
)}
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -24,6 +24,8 @@ const INTEGRATION_NAMES: Record<string, string> = {
|
||||
pbs: "Proxmox Backup Server",
|
||||
osticket: "osTicket",
|
||||
unifi: "UniFi",
|
||||
npm: "Nginx Proxy Manager",
|
||||
homeassistant: "Home Assistant",
|
||||
};
|
||||
|
||||
function ColorList({
|
||||
|
||||
@@ -82,7 +82,7 @@ const DIGEST_DAYS: { value: number; label: string }[] = [
|
||||
{ value: 6, label: "Saturday" },
|
||||
{ value: 0, label: "Sunday" },
|
||||
];
|
||||
const DEFAULT_HEALTH_CHECKS: HealthCheckSettings = { serverOfflineMinutes: 60, diskUsagePercent: 90, domainWarnDays: 30, diskForecastDays: 14, backupStaleHours: 48, restoreTestDays: 0, newPortAlerts: true, flapRaises: 3, deviceOfflineMinutes: 10 };
|
||||
const DEFAULT_HEALTH_CHECKS: HealthCheckSettings = { serverOfflineMinutes: 60, diskUsagePercent: 90, domainWarnDays: 30, diskForecastDays: 14, backupStaleHours: 48, restoreTestDays: 0, newPortAlerts: true, flapRaises: 3, deviceOfflineMinutes: 10, osSupportWarnDays: 90 };
|
||||
const DEFAULT_MONTHLY: MonthlyReportSettings = { enabled: false, day: 1, time: "08:00" };
|
||||
|
||||
type TestResult = { ok: boolean; message: string } | null;
|
||||
@@ -735,6 +735,15 @@ export default function NotificationSettings() {
|
||||
hint="From each agent server's own history, shown on the Alerts page and dashboard (not sent as a notification). Needs a few days of steady growth to say anything. 0 turns it off."
|
||||
onChange={(v) => setHealthChecks((h) => ({ ...h, diskForecastDays: v }))}
|
||||
/>
|
||||
<NumberField
|
||||
label="Flag an operating system whose support ends within"
|
||||
value={healthChecks.osSupportWarnDays}
|
||||
min={0}
|
||||
max={365}
|
||||
unit="days"
|
||||
hint="From what each agent says it runs (Linux agent 7, Windows agent 6, or later) and the vendors' published end-of-support dates. Shown on the Alerts page and the server's page (not sent as a notification); one whose support has ended is a warning. 0 turns it off."
|
||||
onChange={(v) => setHealthChecks((h) => ({ ...h, osSupportWarnDays: v }))}
|
||||
/>
|
||||
<NumberField
|
||||
label="Alert when a network device is offline for"
|
||||
value={healthChecks.deviceOfflineMinutes}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { useEffect, useState } from "react";
|
||||
import { Link } from "react-router-dom";
|
||||
import { api, type CurrentUser, type DependenciesResponse, type DependencyNode } from "../api/client";
|
||||
import { api, type CurrentUser, type DependenciesResponse, type DependencyNode, type DependencySuggestions } from "../api/client";
|
||||
import { ErrorNotice } from "../components/Notice";
|
||||
import { BlockSkeleton } from "../components/Skeleton";
|
||||
import { removeWithUndo } from "../utils/undoable";
|
||||
@@ -49,12 +49,48 @@ export default function DependencyEditor({ user }: { user: CurrentUser }) {
|
||||
const [from, setFrom] = useState("");
|
||||
const [to, setTo] = useState("");
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [offered, setOffered] = useState<DependencySuggestions | null>(null);
|
||||
const [showHidden, setShowHidden] = useState(false);
|
||||
|
||||
function load() {
|
||||
api.dependencies
|
||||
.list()
|
||||
.then(setData)
|
||||
.catch((err) => setError(readableError(err)));
|
||||
api.dependencies
|
||||
.suggestions()
|
||||
.then(setOffered)
|
||||
.catch(() => {});
|
||||
}
|
||||
|
||||
async function addOffered(keys: string[]) {
|
||||
setBusy(true);
|
||||
try {
|
||||
const res = await api.dependencies.addSuggestions(keys);
|
||||
toast.success(res.added.length === 1 ? `${res.added[0].from} now needs ${res.added[0].to}.` : `Added ${res.added.length} dependencies.`);
|
||||
load();
|
||||
} catch (err) {
|
||||
setError(readableError(err));
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
}
|
||||
|
||||
async function hideOffered(keys: string[]) {
|
||||
try {
|
||||
setOffered(await api.dependencies.dismissSuggestions(keys));
|
||||
} catch (err) {
|
||||
setError(readableError(err));
|
||||
}
|
||||
}
|
||||
|
||||
async function restoreOffered() {
|
||||
try {
|
||||
setOffered(await api.dependencies.restoreSuggestions());
|
||||
setShowHidden(false);
|
||||
} catch (err) {
|
||||
setError(readableError(err));
|
||||
}
|
||||
}
|
||||
useEffect(load, []);
|
||||
|
||||
@@ -101,6 +137,69 @@ export default function DependencyEditor({ user }: { user: CurrentUser }) {
|
||||
</p>
|
||||
<ErrorNotice error={error} />
|
||||
|
||||
{offered && (offered.suggestions.length > 0 || offered.dismissed.length > 0) && (
|
||||
<div className="card mb-3">
|
||||
<div className="card-header">
|
||||
<h3 className="card-title">Suggested</h3>
|
||||
{canEdit && offered.suggestions.length > 1 && (
|
||||
<div className="card-actions">
|
||||
<button type="button" className="btn btn-sm btn-primary" disabled={busy} onClick={() => void addOffered(offered.suggestions.map((s) => s.key))}>
|
||||
Add all {offered.suggestions.length}
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<div className="card-body text-secondary small pb-0">Worked out from where each service is reached: when that is a server’s own address, the service most likely runs there. Nothing is written until you add it.</div>
|
||||
{offered.suggestions.length > 0 ? (
|
||||
<ul className="list-group list-group-flush">
|
||||
{offered.suggestions.map((s) => (
|
||||
<li key={s.key} className="list-group-item d-flex flex-wrap align-items-center gap-2">
|
||||
<div className="me-auto">
|
||||
<strong>{s.from.name}</strong> <span className="text-secondary">needs</span> <strong>{s.to.name}</strong>
|
||||
<div className="text-secondary small">{s.reason}</div>
|
||||
</div>
|
||||
{canEdit && (
|
||||
<>
|
||||
<button type="button" className="btn btn-sm btn-primary" disabled={busy} onClick={() => void addOffered([s.key])}>
|
||||
Add
|
||||
</button>
|
||||
<button type="button" className="btn btn-sm" onClick={() => void hideOffered([s.key])}>
|
||||
Not that
|
||||
</button>
|
||||
</>
|
||||
)}
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
) : (
|
||||
<div className="card-body text-secondary">Nothing more to suggest.</div>
|
||||
)}
|
||||
{offered.dismissed.length > 0 && (
|
||||
<div className="card-footer small">
|
||||
<button type="button" className="btn btn-link btn-sm p-0" onClick={() => setShowHidden((v) => !v)}>
|
||||
{showHidden ? "Hide" : "Show"} the {offered.dismissed.length} you said no to
|
||||
</button>
|
||||
{showHidden && (
|
||||
<>
|
||||
<ul className="mb-2 mt-2">
|
||||
{offered.dismissed.map((s) => (
|
||||
<li key={s.key}>
|
||||
{s.from.name} needs {s.to.name}
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
{canEdit && (
|
||||
<button type="button" className="btn btn-sm" onClick={() => void restoreOffered()}>
|
||||
Offer them again
|
||||
</button>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{canEdit && (
|
||||
<form className="card mb-3" onSubmit={add}>
|
||||
<div className="card-header">
|
||||
|
||||
Reference in new issue
Block a user