← Back to Bayforge

Pi software: bayforge-monitor and bayforge-fan

What it is

Two small, free programs for the Raspberry Pis in your rack, from the same signed apt repo.

Each is one Python 3 file with no other dependencies, plus a config file and a systemd unit. They need Raspberry Pi OS Bullseye or newer.

Everything runs on your own Pis. The dashboard loads no fonts or scripts from elsewhere, and neither program checks for updates of its own (apt does that). Only the hub connects to anything: the Pis in the rack, the services you list under checks, and an ntfy server if you set one. ntfy is off by default, and it is the only connection that can leave your network. bayforge-fan runs with no network access at all.

Quick install

On every Pi you want on the dashboard, add the repo and install the monitor:

sudo install -d -m 0755 /etc/apt/keyrings
sudo curl -fsSLo /etc/apt/keyrings/bayforge.gpg https://bayforge.app/apt/bayforge.gpg
sudo curl -fsSLo /etc/apt/sources.list.d/bayforge.sources https://bayforge.app/apt/bayforge.sources
sudo apt update && sudo apt install bayforge-monitor

It starts at once: open http://<pi>.local:8780, for example http://pi1.local:8780. Then pick one Pi as the hub and give the others its token.

On a Pi that drives fans, also:

sudo apt install bayforge-fan

and follow Setup before you reboot.

The repo is signed with the key 44A8 3547 E213 ED4E 86B8 9C85 D3A1 824F A6D5 6E22 (ed25519, “Bayforge apt signing”). To compare it with the one you downloaded: gpg --show-keys /etc/apt/keyrings/bayforge.gpg. The sources file trusts that key for this repo only (Signed-By:).

Which Pi needs which package:

bayforge-monitor

Every Pi runs the same package. The one with hub = yes finds the others, tracks when each is up or down, watches your services and sends the alerts; that is the one you open. It also sets bayforge-fan's curve.

Token

Reading is open on your network; changing a fan curve needs the token. Each Pi makes its own on install.

sudo bayforge-monitor token          # print it; the dashboard asks for it the first time you save

Every Pi in the rack must have the hub's token. It is how a Pi proves it belongs to your rack:

sudo bayforge-monitor token VALUE    # on each peer, with the hub's token (32 to 512 hex characters)

The right token always works. A wrong one less than a second after another wrong one from the same address gets 429. With private = yes, reading needs the token too.

The hub

On one Pi, set this in /etc/bayforge-monitor.conf, then run sudo systemctl restart bayforge-monitor:

hub = yes

With the default peers = auto, the hub looks for the rest of the rack every 5 minutes:

Add any it can't find by name: peers = auto pi9.example pi3.local:8781. Leave out auto to use only the ones you list.

One Pi is one card, however many ways the hub finds it. Each Pi has an id (see /api/self in the API), and the hub groups the names it answers at by it: one card, or one Ignored row, with its other names as “also at”. The hub polls a Pi at one name, the LAN one first (192.168.x, 10.x, 172.16–31.x, 169.254.x, .local), then its tailnet one (100.64.0.0/10, .ts.net), then any other. It stays on that name while it answers; when it doesn't, the hub tries the next in the same round, so a Pi goes down only when none of its names answers for two polls in a row. A Pi older than 0.2.4 has no id, so it is one card per name until you upgrade it. Two Pis cloned from one SD card share a machine-id, so one id, and show as one: on the copy, run sudo rm /etc/machine-id && sudo systemd-machine-id-setup && sudo reboot.

A Pi joins only after it proves it holds the token. The hub sends it a random challenge, and it answers with an HMAC of the challenge, the ip:port the hub reached and its id, keyed with the token, so a stranger can't pass on a real Pi's answer as its own. After that the hub signs each request that needs the token (reading a private Pi, saving a curve through ?host=) with it, over the request, the time and that ip:port. The token never travels between Pis, so a stranger announcing _bayforge._tcp gets nothing it can use. The signature is good for a minute, so the Pis' clocks must agree to within one (NTP, which Pi OS has on by default). A Pi with a different token, or none, is listed under Ignored with the reason. ?host= reaches only Pis that have proved themselves, so it can't be pointed anywhere else.

The hub polls every Pi every 30 s. A Pi is down after 2 missed polls in a row (60 s) and up again on its first answer. Each change goes into /var/lib/bayforge-monitor/events.jsonl, which keeps 30 days, so the availability figures and the event list survive a restart. A Pi with private = yes shows only as up or down, unless the hub has private = yes too.

Service checks

The hub can watch other things in the rack too, with the same 2-poll rule, the same events and the same alerts:

checks = tcp://nas.local:445 http://pihole.local/admin/ https://proxmox.local:8006/

A tcp:// check connects. An http:// or https:// check sends one GET, follows no redirects, and passes on any status below 400. https:// doesn't check the certificate, because homelab services are often self-signed. No check is ever sent the token.

Names. Put [Name] right before a check to name it on the dashboard, in the events and in the alerts: checks = [My NAS]tcp://nas.local:445 [Audiobookshelf]http://abs.local:13378/. A name is 1 to 40 characters, spaces allowed, with no brackets, no # and no control or invisible characters; anything else is refused (a 400 from Settings, a start-up error from the file). Without a name, an http(s) check takes its page's <title> (from the first 16 KB of a 2xx answer, with entities decoded, whitespace collapsed and cut to 40), unless it is empty or just the host name. The hub keeps that in memory, picks up a change at the next answer, and falls back to the URL until it has one. A tcp check is named host:port. A title is cleaned before use: control characters (C0, DEL, C1) and bidi controls are dropped, so a page can't break a log line, an ntfy header or the dashboard's order. The dashboard shows the name with the URL beside it.

Settings takes up to 32 checks, each at most 200 characters.

Alerts

The hub can push alerts to your phone through ntfy. Install the ntfy app, subscribe to a topic, and set it on the hub:

ntfy = https://ntfy.sh/your-long-random-topic
ntfy_token =             # only for a protected topic on your own server

Anyone who knows an ntfy.sh topic name can read it. Use a long random one (openssl rand -hex 16), or run your own ntfy server on the tailnet.

ntfy is off until you set it, and it is the only connection the monitor ever makes outside your rack. An https:// topic is sent with its certificate checked. The dashboard's Send test notification button checks the setup.

You get one message when something starts, and nothing more until it ends and starts again. The hub watches its own readings as well as its peers'.

On your phone

Over Tailscale, on the hub:

sudo tailscale serve --bg 8780       # https://<hub>.<tailnet>.ts.net, on your tailnet only

Never use tailscale funnel: that puts the dashboard on the public internet.

Behind serve you can set bind = 127.0.0.1. In Safari or Chrome, use Share → Add to Home Screen. It then opens full screen, like an app.

Warnings

Each Pi's card shows its warnings as pills: undervoltage and throttling, failed units, pending and security updates, a reboot needed, stale apt lists and fan trouble. On a Pi's page they are in Status, and the ones that need explaining are buttons. Each opens a short note under it; the button, Esc or Close shuts it again, and a tap does the same on the rack screen.

Rack screen

A small screen on the hub can show the rack full time (the #kiosk view, 480×320 to 1280×800):

sudo apt install cage chromium
sudo systemctl enable --now bayforge-monitor-kiosk

On Bullseye the browser package is chromium-browser. The unit takes over tty1 and always opens port 8780. It hasn't been tried on a real screen yet; if the screen stays black, see journalctl -u bayforge-monitor-kiosk.

Settings

The settings live in /etc/bayforge-monitor.conf; after a change, run sudo systemctl restart bayforge-monitor.

Or use Settings at the top of the dashboard (it needs the token, and isn't on the rack screen). It saves to /var/lib/bayforge-monitor/settings.conf (0600), and each key there wins over the same key in /etc/bayforge-monitor.conf. The page shows where each value comes from, and Reset to file drops that key from the override. Saving restarts the monitor (the 24 h graphs start again), and the log says at start-up which keys are overridden. It only changes the Pi whose dashboard you opened, never a peer's through ?host=. A bad hand-edit of settings.conf is moved aside to settings.conf.bad and the monitor starts on /etc alone.

port, bind and private are set only in /etc/bayforge-monitor.conf: a wrong value there would lock you out of the page you'd need to fix it.

The fan curve isn't set here. The dashboard writes it to /var/lib/bayforge-monitor/fan-curve, which bayforge-fan reads in place of its own curve =. “Use config curve” deletes that file. On a Pi 5 whose fans are on the fan header, the kernel owns the curve, so the dashboard only shows it. On the hub, the chart marks every Pi's CPU temperature in that Pi's colour, with its name and the fans of the map that follow it (“pi2 · fan 2”); a Pi that is down or has no reading is left off. The name is always there, so the colour is never the only cue. Other Pis mark only their own CPU. With a fan shield on the hub, the overview shows the same chart, read-only.

Fan shield: when bayforge-fan ships newer shield firmware than the shield runs, the Pi's card and Fans panel say so in one line: “Shield firmware 1, 2 available: sudo bayforge-fan update”. Nothing flashes on its own: you run that on the Pi.

The fan map (fan shield)

On the hub, each fan of its own fan shield can follow other Pis' temperatures. fan_map = auto (the default, also when the key is left out) works out which fan cools which Pi; see Auto-assign. Or set it by hand: fan_map = 1:self 2:pi2 3:pi2,pi3 (in Settings or the conf). A manual map always wins over auto, and an explicit empty fan_map = turns the map off. Fans are 1 to 4, each at most once, with up to 8 names: self (the hub) or hostnames as the dashboard shows them, case aside. After each poll round (every 30 s, and never before the first), each fan takes the hottest CPU of the Pis it follows and runs it through the fan curve the dashboard set, else bayforge-fan.conf's, with bayforge-fan's own maths. A fan left out follows self.

Fail-safe: a fan whose Pi is down, isn't in the rack or has no temperature reading runs at 100 %, and the Fans panel says why (“pi2 is down: 100 %”). That raises no alert of its own: the down alert covers it.

The hub writes the four duties to /var/lib/bayforge-monitor/fan-targets (60 100 100 45, one line, 0644, atomically). bayforge-fan runs the shield on it while it is under 90 s old; missing, stale or garbled, it goes back to its own curve, so a stopped monitor can't hold the fans low. An empty map deletes the file at once; so does a hub without bayforge-fan or a curve, and the panel gives the reason. With hub = no the map is ignored and nothing is written (a file left from when it was the hub goes stale in 90 s).

Per-fan duty needs shield firmware 2. On firmware 1 all four fans share one duty: the firmware line above asks for sudo bayforge-fan update, or, with a bayforge-fan too old to ship firmware 2, the panel asks to upgrade it first (sudo apt update && sudo apt upgrade). If the map is set and bayforge-fan's status still doesn't say curve=targets a minute after the first write, the panel asks for bayforge-fan 0.4.0 or later.

Limitation: the map drives only a shield wired to the hub. A shield on another Pi would need the hub to push signed targets to it.

Auto-assign (fan shield)

With fan_map = auto and a fan shield on the hub, the hub finds out which fan cools which Pi by running each fan flat out in turn. It runs once by itself, when the shield answers on firmware 2, at least one other Pi is up with a temperature and there is no saved result. It runs again only from Re-run auto-assign in Settings. On firmware 1 all four fans share one duty, so there is nothing to tell apart: the panel says so and asks for sudo bayforge-fan update.

Until a run finishes, every fan follows every Pi (the hottest wins, and a Pi down means 100 %). After a run, the result is used exactly like the same map by hand.

The run takes about 28 minutes, and the overview and Settings show its phase, which fan and the time left:

  1. All four fans at the baseline for 3 min.
  2. Fan 1 at 100 % for 4 min.
  3. All at the baseline for 3 min.
  4. Fan 2, and so on to fan 4.

The baseline is the fan curve at the hottest Pi, but never under 50 %. If the curve takes it over 50 %, the result says so, since the fan at 100 % then stands out less.

Each Pi's reading is the mean of the last two polls of a step. A fan's drop for a Pi is the settle before it minus its own step. The fan goes to the Pi whose drop is furthest over the average drop, so a Pi that cools whatever runs doesn't take every fan. If that Pi cooled by under 1 °C, the fan follows self. It is one reading per fan and greedy, so a load change mid-run skews it, and two fans can land on one Pi. Re-run it if the result looks wrong, or set the map by hand.

The run stops, logs an event and runs all four fans at 100 % for 60 s if:

It doesn't retry by itself. The fans move only through fan-targets, like a map by hand. The result is kept in /var/lib/bayforge-monitor/fan-auto.json, written atomically. A bad file is logged and ignored. A re-run keeps the old result until the new one finishes.

API

Every response is JSON. Writes need Authorization: Bearer <token>, and so does everything with private = yes.

GET /
the dashboard
GET /api/self
this Pi's latest reading, with instance (this run's; new at each restart) and id (this Pi's, for good). updates.list is up to 200 {name, from, to, repo, security, seen_age}, security first; from is null for a new package, seen_age is seconds since it was first seen pending.
GET /api/self/history
the last 24 h, one point a minute: {t[], cpu[], temp[], mem[], rx[], tx[], duty[]}. In memory: a restart clears it.
GET /api/rack
{hub, now, ntfy, hosts[], checks[], ignored[]} from the hub's last poll; now is the hub's clock, which the dashboard counts “down 12 min” from. hosts is this Pi, then the peers, each with state (up, down or null before the first answer), since, uptime_pct: {24h, 7d} and bar (the last 24 h as [t, state] changes); a peer also has at (the name the hub polls it at) and also (its other names); a down peer is {host, peer, error, at, also, ...}. checks are {target, name, title, state, since, uptime_pct, bar, detail} (name: the [Name], the page title, or the URL or host:port; title: the page's cleaned <title>, or null); ignored are {host, why, also}. ntfy is only true or false. The top level also has colors, the saved pi_colors as {name: "#rrggbb"}.
On the hub, its own entry has fan_auto: {on, state, phase, fan, started, left, total, result, why, note, pis, blocked}. state is idle, running, done or aborted. result is four {fan, pi, drop} or null. blocked is why a run can't start now, or null. It also has fan_map: null with no map, else {plan, why, written}: plan is four {fan, follows[], temp, duty, reason} (reason set when it is forced to 100 %), or null with why when nothing is written; written is when it first wrote fan-targets. Each fan has duty (the highest), duties (the four, on shield firmware 2, else null), fw, fw_bundled (the firmware bayforge-fan ships, or null) and rpm[].
GET /api/events?days=N
the hub's up/down/alert log, oldest first: [{t, host, event, detail, key}], N from 1 to 30 (default 30)
POST /api/fan/auto
on the hub: a new auto-assign run from the next poll, 202 {ok, detail}, or 409 with the reason it can't run (see fan_auto.blocked)
POST /api/ntfy/test
sends a test message: {ok, detail}, or 409 when ntfy isn't set
GET /api/fan/curve
{controllable, why, source, curve, conf_curve}
PUT /api/fan/curve
{"curve": "45:30 60:60 70:100"}, checked by bayforge-fan's own rules, and the last point must be 100 % at 85 °C or lower, so the fans are flat out before the Pi throttles
DELETE /api/fan/curve
back to the curve in bayforge-fan.conf
GET /api/settings
always needs the token: {host, instance, conf, override, file_only, settings: {key: {value, source}}}, source being file or dashboard. ntfy_token's value is only true or false.
PUT /api/settings
{"hub": "yes", "checks": "tcp://nas:445"}: a string sets a key, null goes back to the file's value, a key left out stays (so ntfy_token left out keeps it, "" clears it). 400 names the field. Then the monitor restarts.

On the hub, ?host=<peer> sends any of the /api/self and /api/fan/curve requests on to that peer (its peer in /api/rack), signed when it is a write or the rack is private. Only a peer that is up and has proved it holds the token can be reached this way.

GET /api/self?challenge=<32 hex>&v=2 adds proof, even to a 401: HMAC-SHA256, keyed with the token, of "<challenge> <ip:port the request reached> <id>" (without v=2, as a hub older than 0.2.4 asks, "<challenge> <ip:port>"). The 401 carries id and instance too.

id is 32 hex, the same at every address and across restarts: HMAC-SHA256, keyed with /etc/machine-id, of bayforge-monitor, cut to 32 hex, the way systemd makes an app-specific machine id. It tells nothing about the machine-id itself, which never leaves the Pi. Without a machine-id it is a random one kept in /var/lib/bayforge-monitor/id. The hub trusts a Pi's id only once the proof covers it; an ignored Pi's id is unproven, and only groups its names into one row.

In place of the Bearer token, the hub sends X-Bayforge-Sig: <unix time>.<32 hex nonce>.<HMAC-SHA256, keyed with the token, of method, path, time, nonce, ip:port the request reached and the SHA-256 hex of the body, one per line>. A peer takes it within 60 s of its own clock, and each nonce once.

Errors: 400 (a bad value; the message names the field), 401 (token needed or wrong), 429 (a wrong token again within a second), 409 (this fan's curve can't be set, or no ntfy), 503 (bayforge-fan isn't installed or its config can't be read, or this Pi has no token), 502 (the peer is down, rejected the hub, or ntfy didn't answer), 404 (?host= isn't a proven peer). Also 405 (a method the path doesn't take), 411, 413 and 415 (a body with no length, too big, or not application/json), and 500 (the settings or curve file couldn't be written).

bayforge-fan

Runs the rack's fans from the Pi's CPU temperature. It has two backends, set with backend = in /etc/bayforge-fan.conf:

Which Pi needs it

Install

From the apt repo (skip the first three lines if the Pi already has bayforge-monitor):

sudo install -d -m 0755 /etc/apt/keyrings
sudo curl -fsSLo /etc/apt/keyrings/bayforge.gpg https://bayforge.app/apt/bayforge.gpg
sudo curl -fsSLo /etc/apt/sources.list.d/bayforge.sources https://bayforge.app/apt/bayforge.sources
sudo apt update && sudo apt install bayforge-fan

This needs Raspberry Pi OS Bullseye or newer, on any Pi. A fresh install enables the service but doesn't start it: run setup, then reboot, and it starts with the Pi.

Setup

For the gpio backend, first set backend = gpio in /etc/bayforge-fan.conf (for example with sudo nano /etc/bayforge-fan.conf). Do this before setup: with the default shield, setup turns on the UART and takes the serial console out of cmdline.txt instead. Then run:

sudo bayforge-fan setup     # prints what it will change in config.txt / cmdline.txt, asks, keeps *.bayforge-bak copies
sudo reboot

What setup does (in /boot/firmware/, or /boot/ on older systems; --yes skips the question):

BackendPi 3/4/Zero 2Pi 5
gpiodtoverlay=pwm,pin=18,func=2dtparam=cooling_fan=on
shieldenable_uart=1; takes console=serial0,… out of cmdline.txtdtparam=uart0=on (GPIO14/15 is /dev/ttyAMA0); takes the console out of cmdline.txt

On a Pi 3, the analogue audio jack uses the same PWM block. Don't use the jack together with backend = gpio.

Fan shield: on a Pi 3 or Zero 2, Bluetooth uses the full UART, so the shield gets the mini UART. That is fine at 115200.

Check it

bayforge-fan status
sudo bayforge-fan test      # steps the fans 0 to 100 % in 10 % steps of 5 s, then a verdict

test pauses the daemon while it runs (that is what it needs sudo for) and starts it again after. Ctrl-C stops it early.

Fan shield: an example of bayforge-fan status:

backend=shield device=/dev/serial0 cpu=52.5 sent=30,30,80,100 curve=targets ok
shield: t=27.4 duty=30,30,80,100 mode=pi rpm=1230,1200,2400,0 fw=2
(2s ago)
fan  duty  rpm
1     30 %  1230
2     30 %  1200
3     80 %  2400
4    100 %  0

On fw=1 the shield has one duty for all four fans: the table shows it on every row and says sudo bayforge-fan update brings per-fan control.

mode=pi means the shield is following the Pi. Some cases to know about:

If none of that helps, run journalctl -u bayforge-fan.

Settings

The settings live in /etc/bayforge-fan.conf. After changing them, run sudo systemctl restart bayforge-fan.

On a Pi 5 with the gpio backend, the curve is the kernel's. Set it in config.txt with dtparam=fan_temp0=50000,fan_temp0_speed=75, and so on up to fan_temp3.

gpio wiring (no shield)

Noctua's 5 V PWM fans have their own pull-up on the PWM wire, to 3.3 V or 5 V depending on the model. Noctua's numbers:

That pull-up rules out wiring the PWM wire to the GPIO directly:

A series resistor doesn't fix this either. To still read below 0.8 V against the fan's pull-up, it would have to be under about 200 Ω.

Sink the line with a transistor instead. The fan only needs the line pulled low; its own pull-up makes the high.

GPIO18 (pin 12) ──1 kΩ──┬── B  2N3904 / BC547 (NPN)
                      100 kΩ   C ── every fan's PWM wire (blue)
GND (pin 14) ───────────┴── E ── GND, shared with the fans' 5 V supply

With Noctua's USB cable and PC fan splitters

This setup uses three things:

The 4-pin fan pinout:

PinSignalNoctua wire colour
1GNDblack
2+5 Vyellow
3tachgreen
4PWMblue

Splitter wires are often all black. To find the pins, plug a fan into one output and see where its blue and black wires land; the same positions on the other output are PWM and GND. On a splitter output with no tach wire, pin 3 is the empty slot.

Chain (up to 2 fans plus the PWM tap, using both splitters from the 2-pack):

NA-AC7 (USB, ideally a Pi USB port) ─► splitter A in
  splitter A out 1 ─► fan 1
  splitter A out 2 ─► splitter B in
    splitter B out 1 ─► fan 2
    splitter B out 2 ─► tap: pin 4 (PWM) to the transistor's collector, pin 1 (GND) to the Pi's GND (pin 14)

How the tap works:

Tach: a splitter or hub passes PWM to every fan, but returns tach from only one fan (the first output here), or from none. The gpio backend reads no RPM at all, so with this setup nothing notices a stalled fan. For RPM per fan you need the fan shield, which has its own tach input for each of its four fans. If a tach line is ever read on a Pi GPIO, pull it up to 3.3 V, never 5 V. It is open collector.

Generic “1 to 5” 3/4-pin hubs: most of them take SATA power and feed the fans its 12 V rail. 12 V destroys these 5 V fans, whose maximum is 5.5 V. Never connect such a hub's SATA or Molex lead with 5 V fans.

A SATA-fed hub's fan lead usually doesn't carry power to the ports either. Before using one, check it with a meter:

Parts beyond the fans, the NA-AC7 and the splitters:

Update the shield's firmware (fan shield)

sudo bayforge-fan update            # flashes /usr/share/bayforge-fan/fanshield.bin, the firmware this package ships
sudo bayforge-fan update FILE.bin   # or a fanshield.bin you built; --yes skips the question

It pauses the daemon, sends the shield boot, writes and verifies the firmware through the CH32V003's factory bootloader over the same UART, and waits for a status line with the new fw=. The fans run at full meanwhile.

The updater passes its tests against a fake bootloader but hasn't been tried on a real shield yet.

apt upgrade and bayforge-fan status say when the shield's firmware is older than the one the package ships (Shield firmware 1, 2 available: sudo bayforge-fan update). Nothing flashes by itself: an upgrade can run unattended, so flashing stays manual.

Per-fan control (fan shield)

bayforge-monitor on the hub can tie each shield fan to a bay and set it from that bay's temperature. It writes targets_file (default /var/lib/bayforge-monitor/fan-targets) every 30 s: one line of four duties, 0 to 100, for fans 1–4, separated by single spaces, such as 30 30 80 100.

Updates and removal

Upgrade

sudo apt update && sudo apt upgrade

Both packages restart themselves on upgrade, and keep their settings. An edited /etc/bayforge-monitor.conf or /etc/bayforge-fan.conf is a config file to apt, so it asks before replacing it. Fan shield: if the new bayforge-fan ships newer shield firmware, it says so; run sudo bayforge-fan update when you are ready.

Remove

sudo apt remove bayforge-monitor bayforge-fan    # stop and remove the programs, keep the settings
sudo apt purge bayforge-monitor bayforge-fan     # also delete the settings and saved state

Questions or trouble: support@bayforge.app.