Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,27 @@ recognisable path forward.

### Added

- **Streams across servers** (multi-server installs). The WebRTC signalling
WebSocket (`/api/v3/live/{id}/webrtc/ws`) for another server's monitor is
relayed: this server checks the caller, connects to the owning server, then
accepts the upgrade, so a server that is down or refuses gives an HTTP
error. `GET /api/v3/events/stream` now carries every server's `alarm_state`
messages, by following each other server's stream with the client's own
token. `GET /api/v3/monitor-status/{id}` is answered by the server that runs
the monitor. `GET /api/v3/live/sessions` and `/live/sources` take
`?server_id=<n>|all`. On a multi-server install the event stream no longer
reports other servers' monitors as `unknown`. Enables the
`rustls-tls-native-roots` feature of `tokio-tungstenite` for `wss://`.
- **System control across servers** (multi-server installs). The daemon and
system routes (`/api/v3/daemons`, `/api/v3/system/*`) take
`?server_id=<n>` to act on another server, relayed to its zm-api with the
caller's own token, or `?server_id=all` to act on every server and get a 207
with one result per server. Applying a run state
(`POST /api/v3/system/state`) now also tells every other server to restart
its daemons, and the response gains an optional `servers` list. The request
gains an optional `daemons_only` flag, which restarts daemons and writes
nothing. Without `server_id`, everything behaves as before on a
single-server install.
- **Multi-server installs** (#230). Every ZoneMinder server runs a zm-api,
and a request for a monitor another server runs is relayed to the zm-api
there with the caller's own token: live HLS, WHEP, snapshots, alarms, PTZ,
Expand All @@ -18,7 +39,14 @@ recognisable path forward.
settings name the other servers where the default (this server's scheme and
port on `Servers.Hostname`) is wrong. `GET /api/v3/servers` gains `api_url`
and `is_this_server`. See the multi-server guide for the limits: WebSocket
signalling, system-wide control and recordings are not relayed yet.
signalling is not relayed.
- **Recordings on per-server storage** (multi-server installs). A storage
area with a `ServerId` holds its files on that server only. A request for
an event's video, frames, thumbnail, synopsis or generated videos, or to
delete the event, is now relayed to the server that holds the files, with
the caller's own token. Storage with no `ServerId` is shared and is never
relayed. Export and the storage list act on what this server can read, and
say so. Media jobs wait for the server that can read the event.
- **Live event stream**, `GET /api/v3/events/stream` (Server-Sent Events):
`event_start`, `event_end` and `event_updated` (notes changed, where object
detection writes) for every monitor the caller may see, and each monitor's
Expand Down Expand Up @@ -268,6 +296,14 @@ recognisable path forward.

### Fixed

- **The retention reaper and the maintenance audit no longer act on another
server's storage.** On a multi-server install they ran against every
`Storage` row. They could not see a per-server area's files, so the audit
could take every event on it for one with no media and delete the rows, and
the reaper measured the wrong disk. Both now skip a storage whose
`ServerId` is another server, and say so once in the log. The audit's
empty-event sweep and the event audit report also leave those events alone.

- **`POST /events` works.** It never set `Events.StateId`, which is NOT NULL
with no default, so every create failed with 500. It now records the
active run state, or 0, as zmc does.
Expand Down
6 changes: 6 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ test-context = "0.5"
thiserror = "2"
tokio = { version = "1", features = ["full"] }
tokio-util = { version = "0.7", features = ["io", "compat"] }
tokio-tungstenite = "0.30"
tokio-tungstenite = { version = "0.30", features = ["rustls-tls-native-roots"] }
tracing = { version = "0.1", features = ["attributes"] }
tracing-appender = "0.2"
tracing-bunyan-formatter = "0.3"
Expand Down
31 changes: 30 additions & 1 deletion book/src/guide/live-events.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,39 @@ The stream ends when your access token expires and when you lose access
minute). Reconnect with a fresh token and `Last-Event-ID`. A comment line
arrives every 15 seconds, so a silent connection is a dead one.

## Several servers

On an install with more than one ZoneMinder server, one subscription covers
every server. Events are read from the shared database, so any server has
them all. Alarm states exist only on the server that runs a camera, so the
server you connect to also subscribes to each other server's stream and passes
on its `alarm_state` messages, for that server's own cameras only. Nothing is
sent twice.

- Each of your streams opens one upstream stream per other server, with your
own token. That server applies your permissions and ends its stream when your
token expires, as it would for you directly. This costs one connection per
client per server.
- A server that is down does not break your stream. Its cameras send no
`alarm_state` updates until it is back. zm-api reconnects with a wait that
grows to 30 seconds and logs one warning per outage.
- On connect you get the current state of each camera as before, from its own
server. After a server returns, only states that changed are sent.
- You see an `alarm_state` only for cameras you may see.
- `Last-Event-ID` and `?since=` replay events from the database, which covers
every server. Alarm states are never replayed.
- A request that carries `X-ZM-Forwarded-By`, which is how one zm-api asks
another, gets only that server's own alarm states and opens no further
streams, so servers never follow each other in a loop.

A single-server install opens no upstream connections and its output is the same.

## How it works

One watcher in zm-api serves every subscriber and runs only while someone is
connected. Alarm states come from each camera's shared memory, four times a
second; a camera's new event wakes the watcher at once. Events come from
ZoneMinder's `Events` table, so cameras on other ZoneMinder servers appear too,
within a second or so. Nothing is written to the database.
within a second or so. Nothing is written to the database. On a multi-server
install the watcher reads shared memory only for the cameras its own server
runs.
78 changes: 72 additions & 6 deletions book/src/guide/multi-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,56 @@ The relayed request carries the caller's own token. The owning server authentica

What is relayed:

- live HLS, live start and stop, live stats, WHEP
- live HLS, live start and stop, live stats, WHEP, and the WebRTC signalling WebSocket (see [WebSockets](#websockets))
- a monitor's status (`GET /api/v3/monitor-status/{id}`), which reads shared memory
- snapshots
- forcing and cancelling an alarm
- PTZ
- a monitor's state (start, stop, restart), its pipeline, and edits to and deletion of its row
- running a monitor action

What stays on the server the client asked: anything that only reads or writes the database, such as listing monitors, reading a monitor's row, events and users.
- an event's recording and files (see [Recordings](#recordings))

What stays on the server the client asked: anything that only reads or writes the database, such as listing monitors, reading a monitor's row, listing events, reading or editing an event's row, tags and users.

## WebSockets

`GET /api/v3/live/{monitor_id}/webrtc/ws` carries WebRTC signalling. For another server's monitor, this server checks that you may see the monitor, then opens its own WebSocket to the same path and query on the owning server, with your `Authorization` header and any `Sec-WebSocket-Protocol` you offered. Only then does it accept your upgrade, so the answers are ordinary HTTP errors:

- the owning server is down: 502
- the owning server refuses (404, 403, 503, ...): the same status and body come back to you
- the servers have different JWT keys: 502

After that, messages are copied one at a time in both directions (text, binary, ping, pong and close, with the close code and reason). When either side closes or fails, the other is closed. A failure on the owning server's side closes your socket with code 1011. The relay sets no idle timeout, since a signalling socket can sit quiet for a long time.

WebRTC media still travels direct from the owning server to the viewer over UDP. The viewer must be able to reach that server. Only the signalling goes through this one.

For `wss://` the relay trusts the system's root certificates. If `cluster.tls_ca_file` is set it trusts that file's certificates alone for WebSockets, because the WebSocket client can't add to the system roots the way the HTTP client does. Put every CA the peers need in that file.

A request that only claims to upgrade, without a valid WebSocket handshake, answers 421.

## The live event stream

`GET /api/v3/events/stream` covers every server. Events come from the shared database, so any server has them all. Alarm states come from shared memory, which only the server that runs a monitor has. So the server you connect to also follows each other server's stream for that server's alarm states, and merges them into yours. Each of your own streams opens one stream to each other server, with your own token, so the other server applies your permissions and ends the stream when your token expires. Your server shows you an alarm state only for monitors you may see and only from the server that runs the monitor. If a server is down, its monitors send no alarm states until it is back; the rest of the stream is not affected. See [Live events](live-events.md#several-servers).

## Recordings

ZoneMinder gives each storage area an optional `ServerId`:

- **No `ServerId` (or 0): shared storage.** Every server can read and delete its files, over NFS for example. Requests for these events are answered by whichever server was asked.
- **A `ServerId`: local storage.** Only that server can read its files. A request for one of its events' files goes to that server.

Relayed for an event on another server's storage: playback and HLS, the event's video, thumbnail and frame images, event info, synopsis, generated videos (list, create, download, delete), and `DELETE /api/v3/events/{id}`, so the server that has the files removes them. This server first checks that the caller may see the event's monitor.

Not relayed, because the database holds all they need: the event list, an event's row and its edits, tags, notes, and the signed link for playback (the link is valid on every server, and the requests that use it are relayed).

Background jobs leave another server's storage alone. The retention reaper, the maintenance audit (both its database and its filesystem checks) and the event audit report skip a storage area that names another server, and the log says so once. Each server's own jobs cover its own storage, so run them on every server. A media job for an event whose files are on another server stays queued until that server's worker takes it. The storage list shows no disk figures for another server's area, since the path on this server is not that disk.

Several events in one request are not split across servers:

- `POST /api/v3/events/export` packs the files of the events on storage this server can read. For an event on another server's storage it adds the details (`event.json`, `frames.json`) and a `files-unavailable.txt` naming the server. Ask that server for the files.

An event with no storage id is treated as shared.

## Setup

Expand Down Expand Up @@ -50,12 +92,36 @@ If the servers use certificates from a private CA, point `cluster.tls_ca_file` a
| 421 `belongs to server 2, which has no address` | The `Servers` row has no hostname and `cluster.peers` names none. |
| 421 `the servers disagree` | Two servers each think the other runs the monitor. Check `ZM_SERVER_ID` on both. |

## Daemons and system control

`/api/v3/daemons` and `/api/v3/system/*` act on one server's daemons. Add `?server_id=` to say which:

| `server_id` | What happens |
| --- | --- |
| absent | This server, as before. |
| a number | That server. A request for another server is relayed to its zm-api with your own token, and its answer comes back unchanged. An unknown number answers 404, and a server with no address answers 421. |
| `all` | Every server, at once. The answer is 207 with one entry per server: `server_id`, `name`, `ok`, `status`, and either `result` (what that server answered) or `error`. One server being down does not fail the request, so check each entry. This server is first. |

The permission check happens here before anything is relayed, and the other server checks it again. A request that another server relayed is always handled where it lands, whatever `server_id` says.

On an install with one server, `all` answers with a single entry whose `server_id` is `null`, and any number answers 404.

### Applying a run state

`POST /api/v3/system/state` changes monitor rows for every server in the shared database, so it reaches every server's daemons without a `server_id`. This server writes the rows once, then tells each other server to restart the daemons of its own monitors that the state names. It sends `{"state_name": ..., "daemons_only": true}`, which restarts daemons and writes nothing, so sending it twice is harmless. The response keeps its usual fields for this server and adds `servers`, one entry per other server in the same shape as above. A server that cannot be reached does not undo the change: its entry says `ok: false`, and its daemons pick up the new state at their next restart.

Run states are not aimed at one server. `server_id=all` is the same as leaving it out, and a server's number answers 400.

## Live sessions and sources

`GET /api/v3/live/sessions` and `GET /api/v3/live/sources` list one server's live sessions and sources. They take `?server_id=<n>|all` exactly as the daemon routes do (see above): a number is relayed, and `all` answers 207 with one entry per server.

## Limits

- **WebRTC media goes direct.** WHEP signalling is relayed, but the video itself travels over UDP from the server that runs the camera. The viewer must be able to reach that server.
- **WebSocket signalling is not relayed.** A WebSocket for another server's monitor answers 421 with that server's address. Use WHEP, or connect to that server.
- **System-wide control is per server.** `/api/v3/daemons` and `/api/v3/system/*` act on the server that receives them.
- **Recordings are served by the server asked.** Event video and frame images are read from that server's filesystem, so they need storage every server can reach.
- **WebRTC media goes direct.** WHEP and WebSocket signalling are relayed, but the video itself travels over UDP from the server that runs the camera. The viewer must be able to reach that server.
- **The monitor status list has no live fields for other servers' monitors.** `GET /api/v3/monitor-status` fills `alarm_state` and the other shared-memory fields only for this server's monitors. Ask for one monitor (`/monitor-status/{id}`) to have it answered by its server, or use the [live event stream](live-events.md#several-servers).
- **The event stream follows the servers it sees when it opens.** A server added later is picked up by the next connection.
- **Whole-database views are not relayed.** The media job list (`/api/v3/jobs`) shows the queue this server sees: all jobs when the database holds the queue, only this server's when a file does. The range synopsis (`/api/v3/events/synopsis`) names no single event, so this server answers it.
- **A logout takes up to ten seconds to reach the other servers.** Each server re-reads the token floors from the database on that interval.
- **Rate limits see the relaying server.** A relayed request arrives from another server's address, with the client's own appended to `X-Forwarded-For`. The global limiter is off by default. If you turn it on, set `trust_proxy_headers = true` and list the other servers in `trusted_proxies`, so each client is counted on its own.

Expand Down
Loading
Loading