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
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ recognisable path forward.

### Added

- **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,
monitor state, pipeline, and edits to the monitor's row. A client needs one
address. Every server must share the same JWT key files. New `[cluster]`
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.
- **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
15 changes: 15 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 @@ -94,7 +94,7 @@ openssl = "0.10"
once_cell = "1"
rand = "0.10"
regex = "1"
reqwest = { version = "0.13", features = ["json"] }
reqwest = { version = "0.13", features = ["json", "stream"] }
rust_decimal = "1"
test-context = "0.5"
thiserror = "2"
Expand Down
1 change: 1 addition & 0 deletions book/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- [Configuration](guide/configuration.md)
- [TLS and certificates](guide/tls.md)
- [Passive and takeover mode](guide/takeover.md)
- [Multi-server installs](guide/multi-server.md)
- [Recording retention](guide/retention.md)
- [Replacing the Perl daemons](guide/maintenance.md)

Expand Down
62 changes: 62 additions & 0 deletions book/src/guide/multi-server.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Multi-server installs

A ZoneMinder install can spread its cameras over several servers that share one database. Each camera belongs to one server, named by `Monitors.ServerId`.

Three things a monitor needs exist only on the server that runs it: its shared memory, the stream socket zmc serves video on, and the daemon control socket. So a zm-api can serve live video, snapshots, alarms and daemon control for its own server's monitors and no others.

## How it works

Run zm-api on every server. A client can talk to any one of them.

When a request arrives for a monitor another server runs, zm-api sends the same request to the zm-api on that server and streams the answer back. The client never needs a second address.

The relayed request carries the caller's own token. The owning server authenticates it and applies that user's permissions as it would for a direct request. No server holds a credential that can act as anyone.

What is relayed:

- live HLS, live start and stop, live stats, WHEP
- 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.

## Setup

1. **Give every server the same JWT keys.** Copy the four key files named under `[secret]` from one server to the others. A token issued by one server must verify on all of them. This also makes signed media links valid on every server.
2. **Check each server knows who it is.** zm-api reads `ZM_SERVER_ID`, `ZM_SERVER_NAME` or `ZM_SERVER_HOST` from `zm.conf`, as ZoneMinder does. Its log says `This host is Servers.Id N` at startup.
3. **Check the servers can reach each other.** By default a server expects to find another one at its own scheme and port on that server's `Servers.Hostname`. If that is wrong for a server, name its address:

```toml
[cluster]
peer_port = 8443 # if the others listen on a different port than this one

[cluster.peers]
2 = "https://zm2.lan:8443"
```

`GET /api/v3/servers` shows the address each server was resolved to, as `api_url`, and marks the one that answered with `is_this_server`.

If the servers use certificates from a private CA, point `cluster.tls_ca_file` at its PEM file.

## When something is wrong

| Answer | Meaning |
|---|---|
| 502 `server 2 (zm2) did not answer` | The owning server is down or unreachable from this one. |
| 502 `refused credentials this server accepted` | The servers have different JWT keys. Copy the key files. |
| 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. |

## 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.
- **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.

Turning relaying off with `cluster.enabled = false` restores the old behaviour, where requests for another server's monitor fail locally.
45 changes: 40 additions & 5 deletions openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -3842,8 +3842,6 @@
"tags": [
"Events"
],
"summary": "Export events as a zip",
"description": "Streams a zip, built as it goes, with a `{event_id}/` folder per event\nholding the parts asked for: `details` (`event.json`), `frames`\n(`frames.json`), `images`, `video` and `misc` (the event directory's other\nfiles). Every id is checked first: one that doesn't exist or isn't\nvisible to the caller is a 404 and nothing is sent.",
"operationId": "exportEvents",
"requestBody": {
"content": {
Expand Down Expand Up @@ -21993,6 +21991,32 @@
}
}
},
{
"type": "object",
"description": "Another server of a multi-server install did not answer a relayed\nrequest, or answered it in a way that only a setup fault explains.",
"required": [
"BadGatewayError"
],
"properties": {
"BadGatewayError": {
"type": "string",
"description": "Another server of a multi-server install did not answer a relayed\nrequest, or answered it in a way that only a setup fault explains."
}
}
},
{
"type": "object",
"description": "The request is for another server's monitor and this server can't\nrelay it. The message names the server that can answer.",
"required": [
"MisdirectedError"
],
"properties": {
"MisdirectedError": {
"type": "string",
"description": "The request is for another server's monitor and this server can't\nrelay it. The message names the server that can answer."
}
}
},
{
"type": "object",
"description": "A 503 for something that will be back shortly, with a Retry-After of\nthis many seconds.",
Expand Down Expand Up @@ -24497,7 +24521,6 @@
},
"CreateFilterRequest": {
"type": "object",
"description": "Create a saved event filter. `name` and `query_json` are required; every\nother column of the `Filters` table is optional and falls back to the\nschema default when omitted (flags `0`, `execute_interval` `60`,\n`email_format` `Individual`).",
"required": [
"name"
],
Expand Down Expand Up @@ -26969,7 +26992,8 @@
"minimum": 0
},
"username": {
"type": "string"
"type": "string",
"description": "The characters login accepts. Anything else could not sign in, and a\ncontrol character would end up in the log lines that name the user."
}
}
}
Expand Down Expand Up @@ -33904,6 +33928,13 @@
"zmeventnotification"
],
"properties": {
"api_url": {
"type": [
"string",
"null"
],
"description": "Where this server's zm-api is, as the server answering works it out:\nits `cluster.peers` setting, else its own scheme and port on this\nrow's hostname. Absent when the row has no hostname. A client does not\nneed it: any server relays requests for another server's monitors."
},
"hostname": {
"type": [
"string",
Expand All @@ -33915,6 +33946,10 @@
"format": "int32",
"minimum": 0
},
"is_this_server": {
"type": "boolean",
"description": "Whether this is the server that answered."
},
"latitude": {
"type": [
"number",
Expand Down Expand Up @@ -38465,7 +38500,7 @@
"string",
"null"
],
"description": "New password (re-hashed with bcrypt; never stored in plaintext)."
"description": "New password (re-hashed with bcrypt; never stored in plaintext).\nEmpty leaves the password as it is."
},
"phone": {
"type": [
Expand Down
21 changes: 21 additions & 0 deletions settings/base.toml
Original file line number Diff line number Diff line change
Expand Up @@ -364,3 +364,24 @@ max_age_days = 0
max_bytes = 0
# Log what would be deleted without deleting (eyeball before enforcing).
dry_run = false

# Multi-server installs only. Each ZoneMinder server runs its own zm-api, and
# a request for a monitor another server runs is relayed to the zm-api there,
# with the caller's own token. Every server must use the same JWT key files
# ([secret]), or the owning server refuses the relayed token.
#
# Nothing here applies to a single-server install.
[cluster]
enabled = true
# The port zm-api listens on at the other servers. Default: this server's own.
# peer_port = 8443
connect_timeout_secs = 5
# How long another server may go without sending anything.
read_timeout_secs = 30
# Extra root certificates to trust for the other servers (a private CA).
# tls_ca_file = "/etc/zm-api/cluster-ca.pem"

# A server's zm-api is assumed to be at this server's scheme and peer_port on
# its Servers.Hostname. Name it here, by Servers.Id, when it is somewhere else.
# [cluster.peers]
# 2 = "https://zm2.lan:8443"
Loading
Loading