# Occupi feed from Checkmk

This is a small script that runs on your Checkmk server. It reads the Wi-Fi counts Checkmk
already holds and sends them to Occupi. It replaces the manual CSV export.

## What it reads

One Checkmk service, by default **Users per Floor**. Its performance data holds one number per
zone, for example `Library_Ground=175 Essex_Floor_1=8`.

It reads those numbers and nothing else. It never asks for client lists, MAC addresses,
usernames or device names.

## What it sends

One number per zone, once every 15 minutes. For example:

```json
{"readings": [{"apId": "Library_Ground", "ts": "2026-11-26T13:30:00Z", "deviceCount": 175}]}
```

Zone names are turned into ids the same way the CSV import does it. Readings therefore land on
the zones Space Planning has already placed.

## Which way the connections go

- The script makes **outbound HTTPS (port 443) calls to Occupi**. Nothing else.
- **Nothing connects in** to your network or to Checkmk. The script opens no port.
- **Nothing touches the Wi-Fi controllers.** Checkmk keeps polling them exactly as it does today.
- The script reads Checkmk on the same machine, through Livestatus (the `lq` command).

## Install

You need Checkmk's own Python 3 (it is already there) and the Occupi address and key.

1. In Occupi, open **Setup** and create a collector key. Copy it. It is shown once.
2. Log in to the Checkmk server and switch to the site user: `su - mysite`.
3. Copy `occupi-checkmk-push.py` to `~/local/bin/` and make it executable:
   `chmod 755 ~/local/bin/occupi-checkmk-push.py`
4. Save the key in a file only the site user can read:
   `umask 077; printf '%s\n' 'PASTE-THE-KEY' > ~/etc/occupi-key`
5. Create `~/etc/occupi-push.ini`:

   ```ini
   [occupi]
   url = https://your-occupi-address
   key_file = ~/etc/occupi-key
   service = Users per Floor
   ; host = monitor.example.ac.uk   (optional: only this host)
   ```

6. Check it (see the next section).
7. Add one line to the site user's crontab (`crontab -e`):

   ```
   */15 * * * * ~/local/bin/occupi-checkmk-push.py --once
   ```

   For a reading every 5 minutes use `*/5` instead. Occupi keeps one value per zone per
   15 minutes, and the latest one wins. It does not need 5 minutes, but it does no harm.

If the Livestatus `lq` command is not on the path, the script reads the site's socket,
`~/tmp/run/live`. You can set `lq_command` or `socket` in a `[livestatus]` section.

## Check it works

```
~/local/bin/occupi-checkmk-push.py --dry-run
```

This prints what would be sent. It sends nothing and keeps nothing. Check the zone names and
numbers match what you see in Checkmk. Then wait for the next cron run and look at the Wi-Fi
page in Occupi.

## Send history

Occupi can start from months of past counts, such as a busy term. Checkmk already holds them.

Run this once on the Checkmk server, as the site user:

```
~/local/bin/occupi-checkmk-push.py --backfill 2026-10-01 2026-10-31
```

The two dates are the first and last day, both included. They are read in the campus time zone.
Set `timezone = Europe/London` (or your own zone) in the `[occupi]` section of
`~/etc/occupi-push.ini`. Without it the server's own zone is used. You can also give a full time,
such as `2026-10-01T08:00`.

**What it reads.** Only the stored history (the RRD data) of the one service, by default
**Users per Floor**. It asks Checkmk for the average of each zone's number. It never reads client
lists, MAC addresses, usernames or device names. It needs no Occupi address or key, and it sends
nothing to Occupi.

**What it writes.** One file in the current folder, `occupi-history-2026-10-01-2026-10-31.csv`
(use `--out FILE` to choose the name). It looks like this:

```
# occupi-checkmk-history 1
# service: Users per Floor
zone,timestamp,count,step_seconds
Library_Ground,2026-10-01T08:00:00+00:00,175,900
```

There is one row per zone per step. Times are UTC. `step_seconds` is the real step of that row.
A step with no value in Checkmk has no row. It is never written as zero. When it finishes, the
script prints the zones, the time span, the step or steps it found, the number of rows and the
number of gaps.

**How old history looks.** Checkmk keeps recent data finely and older data coarsely. With the
default settings that is 1 minute for 2 days, 5 minutes for 10 days, 30 minutes for 90 days, then
6 hours. The script asks for 15 minutes and takes what Checkmk has. So a busy period from
three months ago may come back in 6-hour steps. The script says so. Occupi can store it, but
hourly charts need steps of an hour or less. If your site keeps finer history, you get it.

**How to send it to us.** Open the **Wi-Fi** page in Occupi, find **Counts from a file**, and
choose the file. Occupi shows a preview first: the zones, the time span, the step, the rows and the
gaps. Nothing is stored until you press **Import history**. Only an Occupi admin can import
history, and each import is written to the activity log. Or email the file to us and we will
import it for you.

History is stored for zones that are already placed in a room. Zone names are turned into ids
the same way as in the one-moment import, so they land on the same zones. A zone not placed yet is
listed in the preview. Place it, then import the file again.

If Livestatus cannot be read and the REST API is set up (see below), the script uses the REST API
instead. The REST API needs an automation user that may see the service.

Names with a space, a colon or a comma cannot be asked for through Livestatus. The script lists
any it leaves out. Zone names like `Library_Ground` are fine.

A long period makes a big file. Occupi reads files up to 25 MB, so send a term at a time if the
file is bigger than that.

## If Occupi is unreachable

The script keeps the readings in a small file (`~/var/occupi-push/spool.json`, at most 20,000
readings, oldest dropped first) and sends them on the next run. Readings Occupi has accepted are
never sent again. Occupi accepts readings up to 35 days old.

Exit codes: 0 done, 2 settings problem, 3 Checkmk could not be read, 4 Occupi could not be
reached, 5 Occupi rejected the key.

## Logs

`~/var/log/occupi-push.log`. It rotates itself. Each run that sends data writes one line.

## Optional: the Checkmk REST API instead of Livestatus

```ini
[occupi]
source = rest

[rest]
rest_url = https://localhost/mysite
rest_creds_file = ~/etc/occupi-rest
; rest_verify_tls = no   (only for the site's own self-signed certificate on localhost)
```

`~/etc/occupi-rest` holds the automation user name, a space, and its secret, on one line
(`chmod 600`). The script calls
`GET <rest_url>/check_mk/api/1.0/domain-types/service/collections/all` with the columns
`host_name`, `description` and `perf_data`, filtered to the one service. It sends the header
`Authorization: Bearer <user> <secret>`. The automation user only needs permission to see
that service.

This source has been tested against a stand-in server, not a real Checkmk. Try `--dry-run`
first. Livestatus is the recommended source.

## Other settings

All can also be set as environment variables (for example `OCCUPI_URL`). In the `[occupi]`
section: `timeout` (seconds, default 20), `spool_max`, `state_dir`, `log_file`. If a service
name appears on more than one host, the zones from all of them are added together, as the CSV
import does. Set `host` to read just one.

## Stop it and remove it

- To cut off access at once, delete the key on the Occupi **Setup** page. The script then gets
  a refusal and keeps nothing it cannot send beyond its cap.
- To remove it, delete the crontab line, then `~/local/bin/occupi-checkmk-push.py`,
  `~/etc/occupi-push.ini`, `~/etc/occupi-key` and `~/var/occupi-push/`.

## For developers

`python3 -m unittest collector/checkmk/test_occupi_checkmk_push.py` runs the tests. The parsing
is a port of `src/lib/wifi/checkmk.ts`; `src/lib/wifi/checkmk-push-parity.test.ts` checks both
readers give the same zone ids and counts.
