Metadata-Version: 2.4
Name: firewatcher
Version: 1.75
Summary: Watch logs for incident patterns and persist surrounding context off-box
Home-page: https://github.com/yufei-pan/firewatcher
Author: Yufei Pan
Author-email: pan@zopyr.us
License-Expression: GPL-3.0-or-later
Classifier: Programming Language :: Python :: 3
Classifier: Operating System :: POSIX :: Linux
Classifier: Topic :: System :: Logging
Classifier: Topic :: System :: Monitoring
Requires-Python: >=3.6
Description-Content-Type: text/markdown
License-File: LICENSE
Dynamic: author
Dynamic: author-email
Dynamic: classifier
Dynamic: description
Dynamic: description-content-type
Dynamic: home-page
Dynamic: license-file
Dynamic: license-expression
Dynamic: requires-python
Dynamic: summary

# firewatcher

Watch a log stream (journald, syslog, or any file) for incident patterns, then persist a window of surrounding messages to a durable location — including a network filesystem — so the record survives even if the machine later dies.

The watcher reads the journal with `journalctl --follow` when journald runs on the host (or its journal files are there, such as a host journal mounted into a container). Otherwise it follows `/var/log/syslog`, then `/var/log/messages`, even when `journalctl` is installed; `--log-file` picks a file. With no syslog file either, it falls back to `journalctl`. Running the daemon does not require systemd; only `--install-service` / `--uninstall-service` do.

Everything but [resuming after a restart](#resuming-after-a-restart) works with every source: the compressed context before a match, leaving out context already captured, the free-space floor, SIGHUP reload, and LLM analysis and pushes. Resuming needs the journal; with a log file, firewatcher reads the last `--tail-lines` lines at every start, as before.

## Install

```bash
pip install firewatcher
```

From a clone of this repository:

```bash
pip install .
```

Requires **Python 3.6+**. No third-party runtime dependencies.

## Quick start

```bash
firewatcher /etc/firewatcher/patterns.d
firewatcher -t 300 -o /var/log/captured_messages/ /etc/firewatcher/patterns.d
```

A directory argument loads every non-hidden pattern file inside it. Files ending in `.regex` are compiled as regular expressions. Any other pattern file is treated as fnmatch (bash-like) substrings, with `*` added on both ends. Lines starting with `#` are comments; to match text that starts with `#`, such as the kernel's `#PF:`, write `[#]PF:` in a plain file or `\#PF:` in a `.regex` file. Matching is case-sensitive; for a case-insensitive pattern, put it in a `.regex` file with `(?i)` in front.

`firewatcher` exits with an error when no pattern loads, for example when a pattern path is mistyped or every regex is invalid, instead of running while matching nothing.

`--install-service` seeds `/etc/firewatcher/patterns.d` with example patterns when it holds no pattern file. The `examples/` directory in the source tree is not installed onto `PATH` by `pip install`; copy the pattern files from a clone, or let `--install-service` write them.

On a match, `firewatcher` writes:

- a per-incident capture under `{output-folder}/{YYYY-MM}/…`
- a line in `{output-folder}/journal.log`

Live capture is written as lines arrive, so a copy can already be on another filesystem if the host then disappears. Each captured line is flushed as it is written, and the capture file is fsynced at most once a second and when the capture closes, so a host crash loses at most about the last second of it.

A capture holds the whole `--capture-time` window before its match, so on a busy host one capture can be tens of megabytes; a lower `--capture-time` or `--buffer-max-mb` keeps captures smaller.

When a capture opens, lines of its window that an earlier capture of the same run already holds are left out, with a note naming that file and how many lines it holds. In an error storm, where each capture opens right after the last one closes, that saves writing the same context again and again; lines that no capture holds are always written. Such a capture is not complete on its own: its LLM excerpt and push see the note, not the lines in the earlier file. A capture that moved to the fallback folder, or lost lines on the way, is never pointed to: later captures write its lines again.

A capture ends `--capture-time` seconds after its last matching line, or `--capture-time-max` seconds (default 3600) after it opened, whichever comes first, so a pattern that keeps repeating still produces a finished capture, and its analysis and push, at least once an hour. A later match starts a new capture.

If the output folder cannot be written (a full disk, a read-only remount, an NFS server that returns errors), the capture continues under `--fallback-folder` (default `/var/lib/firewatcher/captured_messages/`), starting with every line written since the last successful fsync and a note naming the file it moved from. That capture is listed in the fallback folder's own `journal.log`, and the fallback folder is compressed and pruned like the output folder. If no folder can be written, `firewatcher` keeps running, logs the error, and still hands the incident to the notifier.

Put the fallback folder on a different filesystem from the output folder. The default is under `/var/lib`, which shares a disk with `/var/log` on most systems, so it covers a failed NFS output folder but not a full local disk.

The fallback needs the failure to come back as an error. On an NFS mount with the default `hard` option, writes to an unreachable server wait instead of failing, and `firewatcher` waits with them. To fall back instead, mount the output folder with `soft` (or `softerr`) and a short `timeo`.

On SIGINT or SIGTERM, `firewatcher` handles the lines it has already read from the log, finishes the open capture, and exits. A second SIGINT or SIGTERM ends it at once.

SIGHUP reloads the pattern files and the `--always-notify` files from the paths given at startup, so after editing `/etc/firewatcher/patterns.d/`, run `sudo systemctl reload firewatcher`. The new patterns apply from the next log line, and the journal says how many loaded. If no watch pattern loads, the old ones stay and the error is logged. The config file and other settings are not re-read; restart for those. In a terminal, SIGHUP keeps its usual meaning (the terminal went away) and ends `firewatcher`. Two SIGHUPs close together reload once.

### Resuming after a restart

When firewatcher follows the journal (not a log file), it saves how far it has read in `--position-file` (default `/var/lib/firewatcher/journal.position`): about once a second while lines arrive and a second after the last one (the open capture is fsynced first, so a saved position never covers lines of a capture file that are not on disk), as soon as a match opens a capture, and when it stops. After a restart it reads on from there instead of the last `--tail-lines` lines; after a crash or `kill -9`, that can be up to about a second before the last line it read. Each save is fsynced before it replaces the old one, so a hard reset or power loss leaves the last or the previous position, never an empty file. Lines logged while it was stopped are still matched, in every boot the journal holds, so after a reboot also what the old boot logged after firewatcher stopped; not what a reboot drops from a volatile journal. While the journal still holds the saved entry, the `--capture-time` seconds before it are read again only to refill the context kept before a match, and the lines after it are matched: a match is captured again only if it was read in the second before a crash, or came after a save that failed. At most `--resume-max` seconds (default 3600) are read back, so after a longer stop only the last hour is matched.

To tell where it stopped, firewatcher reads the journal with `journalctl -o export` and writes each line as `journalctl -o short-iso` does, so journal lines in captures carry the year and time zone: `2026-10-23T17:52:41+00:00 host sshd[812]: …` instead of `Oct 23 17:52:41 host sshd[812]: …`. Lines logged while firewatcher was stopped arrive all at once. A capture opened by one of them takes its context from the `--capture-time` before that line by the time each line was stored, and holds the lines after it as they are read, up to `--capture-line-count-max`.

The position is the journal cursor of the last entry read and the second journald stored it. At a restart, firewatcher reads the journal from that entry to its end without `--follow`, then follows from the last entry read. When the first entry journalctl prints is the saved one, the journal still holds it: the lines after it are matched, whatever the clock did in between, and the lines before it are read back from it into the context, newest first, as far as `--capture-time` (by the time journald stored each) or `--buffer-max-mb` reaches. When the journal no longer holds that entry (its file was rotated away while firewatcher was stopped, a volatile journal after a reboot, a crash that lost it, a position file from 1.69), every line from `--capture-time` before its second is matched, no further back than `--resume-max`: lines are read again, never skipped, and an incident open at the stop can be captured again. In that case only, a position more than a minute ahead of the clock is ignored, and the last `--tail-lines` lines are read. A journalctl that fails while firewatcher reads on from the saved entry ends the run, so the service's restart reads on from the last line handled; one slow to print its first entry is waited for, and after 30 seconds the log says so. A failed save is logged once, and a restart then reads again from the last position saved. When the journal lost the saved entry and the clock was set back by more than `--capture-time` while firewatcher was stopped (such as a boot whose clock was far behind until corrected), the lines stamped more than `--capture-time` before the saved entry are not read ([limitations](https://github.com/yufei-pan/firewatcher/blob/master/docs/limitations.md#after-a-boot-with-the-clock-far-behind-a-restart-does-not-read-the-lines-logged-before-the-clock-was-corrected)).

Only one firewatcher uses a position file at a time: another one started with the same file, such as a test run in a terminal with the default settings, reads the last `--tail-lines` lines and saves nothing. An empty `--position-file` turns resuming off, and so does a journalctl that does not accept `-o export` and `--since=@`, which firewatcher checks at start.

### Disk and memory limits

firewatcher keeps the filesystems it writes captures to above `--min-free-percent` free (default 1%, counted as `df` does, so the root-reserved blocks are left alone). It asks the filesystem when a capture opens, about once a second, and before its own writes could come within 64 KiB of the floor (at least every 8 MiB), fsyncing first so an NFS server counts every byte; a line that would cross that margin is not written there. firewatcher's own writes never take the disk past the floor; another program filling it between two checks can.

Near the floor, firewatcher may delete its own oldest months in that folder, so a new incident can be recorded, under these rules:

- **What:** only what firewatcher itself creates: `YYYY-MM` folders, `YYYY-MM[.N].tar.xz` archives, and their `.partial` files, each of the kind it should be (a folder, or a regular file). A month counts as a whole.
- **Never:** the current month, the month being written, a month with anything in it changed in the last day (another firewatcher may be writing there), a month being compressed, or a month with another filesystem mounted inside it.
- **How much:** first a `.partial` file that has not changed for a day goes (a stop left it), and the room it frees counts. Then only as many months as it takes, counted by their size on disk, and no month at all if deleting every one of them still could not make room. That happens when something else filled the disk, and then the history is kept.
- **How often:** at most once per `--purge-interval` per folder (default an hour), so a disk that something else keeps filling drains firewatcher's history slowly. A purge that could not help is not tried again for five minutes (or the interval, if shorter). Each deletion is logged.

After deleting, firewatcher syncs that filesystem alone (syncfs; none on macOS, where APFS shows freed space at once) and reads the free space again. A filesystem that shows freed space late costs a move to the fallback folder, never the floor. If there is still no room, the capture moves to the fallback folder with a note, or, with no folder left, stops being written, which is logged; the incident is still pushed. Compression keeps to the floor too: a month whose archive could cross it is left uncompressed for now. Purging and compression never wait for each other, so log reading never waits for `tar`. `--delete-after-months 0` means nothing is deleted by age; the floor can still delete the oldest months. Set `--min-free-percent 0` to turn all of this off.

Memory is bounded too. The `--capture-time` seconds before a match are kept compressed (zlib, about 3 times smaller for a busy host's varied lines) within `--buffer-max-mb` (default 16 MiB); past it the oldest lines are dropped, and the capture says so. The newest lines, up to 256 KiB before they are compressed, are always kept, so the buffer can go over the limit by about that much. A line longer than 1 MiB is read and matched in pieces, so a pattern split across a 1 MiB boundary is missed. From the journal, the first 4 MiB of a message are read.

Monthly directories are compressed on a background thread, so log reading never waits for `tar`. Each month becomes `YYYY-MM.tar.xz` holding `YYYY-MM/…`. The directory is removed only after `tar` succeeds and the archive and its folder are synced to disk; if a sync fails, the month stays and is archived again later, nothing lost. An existing archive is never overwritten: the new one becomes `YYYY-MM.1.tar.xz`, and so on. While `tar` writes, the archive is `YYYY-MM.tar.xz.N.partial`: N is the pid, or the next number free, so no two compressions write one file (a firewatcher on another host or in another container sharing the folder can have the same pid). A compression run first removes any such file that has not changed for a day: one a stop or a crash left. Compression runs at each start and then every two days.

## systemd service

```bash
sudo firewatcher --install-service
sudo firewatcher --print-unit
sudo firewatcher --uninstall-service
```

`--install-service` writes `/etc/systemd/system/firewatcher.service` (its `ExecStart` names only `--config`), creates the output directory, seeds `/etc/firewatcher/patterns.d/` with example patterns **only if it holds no pattern file**, and writes the settings given into `/etc/firewatcher/firewatcher.conf` (see [Config file](#config-file)), then `systemctl daemon-reload && systemctl enable --now firewatcher`, or restarts a running service so that it runs the new settings (a capture in progress is finished and sent first). It refuses first, and writes nothing (exit 2), what the service could not start from (see [Upgrading to 1.74](#upgrading-to-174-from-173)). The config and the unit are each written whole before the `systemctl` steps: a step that fails leaves them (the unit not loaded, where the first `daemon-reload` fails), with one line saying what to run, and the same command, run again, finishes the install, writing neither file again when it holds its text already. A restart that fails is followed by one `systemctl start`; where that fails too, the line says how to start the service. Where `enable --now`, or the restart and that start, fails over a running service whose unit's `ExecStart` names only `--config` (1.74 and later write it so), that unit is put back, reloaded and, where the restart stopped the service, started (the line says which of these failed), so a crash starts the service with it and with what its config holds, without the unit settings this run adds (a `RequiresMountsFor=`) until the same command is run again. A service that was not running keeps the new unit, and so does one whose unit has the settings on its `ExecStart` (as 1.73 and earlier wrote it): put back, they would be readable by every local user again ([limitations](https://github.com/yufei-pan/firewatcher/blob/master/docs/limitations.md#a-crash-after-a-failed-install-over-a-unit-with-settings-on-its-execstart-starts-the-new-unit)).

`systemctl reload firewatcher` reloads the pattern files without a restart; see SIGHUP above.

Capture files follow the process umask. A systemd service defaults to `UMask=0022`, so captures are readable by every local user, while the journal they copy from is normally readable only by root and the `adm`/`systemd-journal` groups. On a host with untrusted local users, add `UMask=0027` to the unit, for example with `systemctl edit firewatcher`.

If systemd is not available, `--install-service` and `--uninstall-service` print a warning and exit. `--print-unit` still works so you can copy the unit elsewhere. It writes nothing: the settings given to it go into the config only at an install, as it says on stderr.

Useful flags:

| Flag | Description |
|------|-------------|
| `--unit-name NAME` | Unit name (default: `firewatcher`) |
| `--requires-mounts-for PATH` | Add `RequiresMountsFor=` (repeat for NFS/remote output). Each path has whitespace at either end dropped, is made absolute and normalized as text (`.`, `..`, `//`, a trailing `/`; symlinks are not followed, as systemd follows none), quoted for you when it has spaces, quotes or backslashes, and refused if it has a newline inside it or is not UTF-8 (systemd cannot read either back there). On systemd 229 and older (EL7), a path with `%`, `"` or `\` is read as a different path ([limitations](https://github.com/yufei-pan/firewatcher/blob/master/docs/limitations.md#on-systemd-229-and-older-some-mount-paths-are-written-wrong)) |
| `--no-enable` | Write the unit and the settings given, and reload, but do not enable, start or restart (restart a running service yourself). A pattern file or executable the service needs that is not there yet is then a warning, not a refusal |

```bash
sudo firewatcher --install-service -o /mnt/logs/captured_messages \
  --requires-mounts-for /mnt/logs/captured_messages \
  /etc/firewatcher/patterns.d
```

## Options

| Flag | Description |
|------|-------------|
| `pattern_file …` | Pattern files or directories |
| `--log-file` | Log file to follow (default: the journal when journald runs, else `/var/log/syslog`, else `/var/log/messages`) |
| `-t`, `--capture-time` | Seconds of context after a match (default: 300) |
| `--capture-time-max` | Longest one capture may run, in seconds (default: 3600; 0 = no limit) |
| `-o`, `--output-folder` | Where to write captures (default: `/var/log/captured_messages/`) |
| `--fallback-folder` | Where a capture continues when the output folder cannot be written (default: `/var/lib/firewatcher/captured_messages/`; `''` disables) |
| `--tail-lines` | Lines to start with (default: 20; `+N` from line N, not with journalctl). Not used when the journal resumes from a saved position |
| `--filter-only` | Filter existing logs only (`cat` instead of `tail -F`, or `journalctl` without `--follow`). Windows are measured in read time, not log time, and existing logs are read far faster than they were written, so `-t` and `--capture-time-max` barely limit a capture: it usually runs to `--capture-line-count-max` lines or the end of the input |
| `--compress-after-months` | Tar.xz monthly dirs after N months (default: 3) |
| `--delete-after-months` | Delete `YYYY-MM` dirs after N months (default: 0 = never by age; the free-space floor can still delete the oldest months) |
| `--capture-line-count-max` | Max lines written after a match (default: 10000). The lines before it are bounded by `-t` and `--buffer-max-mb` |
| `--buffer-max-mb` | Memory, in MiB, for the lines kept before a match, compressed (default: 16). The oldest are dropped past it |
| `--min-free-percent` | Free-space floor, 0–50%, for the filesystems captures go to (default: 1; 0 disables). See [Disk and memory limits](#disk-and-memory-limits) |
| `--purge-interval SEC` | Least time between two deletions of old months in one folder for free space (default: 3600; 0: no wait) |
| `--position-file PATH` | Where firewatcher saves how far it read the journal, so a restart resumes there (default: `/var/lib/firewatcher/journal.position`; `''` disables). See [Resuming after a restart](#resuming-after-a-restart) |
| `--resume-max SEC` | Most seconds of journal read back at start: lines logged while stopped, and context before them (default: 3600; 0: none) |
| `--config PATH` | INI config file. Default: `/etc/firewatcher/firewatcher.conf` when that file exists. An explicit path must exist |
| `--llm` / `--no-llm` | Enable or disable LLM analysis of each finished capture |
| `--llm-base-url URL` | OpenAI-compatible API root, such as `http://127.0.0.1:11434/v1` |
| `--llm-model NAME` | Model name |
| `--llm-timeout SEC` | LLM HTTP timeout (default: 60) |
| `--llm-max-bytes N` | Capture bytes sent to the model (default: 48000) |
| `--llm-notes TEXT` | A short note about this host, sent to the model with each capture (default: none; `''` clears a note set in the config). See [LLM analysis and notifications](#llm-analysis-and-notifications) |
| `--notify` / `--no-notify` | Enable or disable ntfy pushes |
| `--ntfy-url URL` | ntfy topic URL, such as `https://ntfy.sh/mytopic` |
| `--ntfy-priority N` | ntfy priority 1–5 (default: 4) |
| `--ntfy-timeout SEC` | Worker ntfy timeout (default: 10). A full analysis queue uses 5 seconds |
| `--always-notify PATH` | Pattern file or directory (repeatable). A capture with a matched line that also matches one of these is pushed even when the LLM says not to notify |
| `--install-service` | Install and enable a systemd unit |
| `--print-unit` | Print that unit and exit |
| `--uninstall-service` | Disable and remove the unit |
| `-V`, `--version` | Show version and exit |

`--tail_lines`, `--filter_only`, `--no-filter_only` and `--capture_line_count_max` are still accepted with underscores.

## Config file

Runtime settings live in an INI file. Explicit command-line flags override the file. Built-in defaults apply when neither sets a value. A missing default file is ignored. `--uninstall-service` does not read the file.

A commented example with all supported settings is in [`examples/firewatcher.conf`](https://github.com/yufei-pan/firewatcher/blob/master/examples/firewatcher.conf). Copy it to `/etc/firewatcher/firewatcher.conf`, or use `firewatcher --config /path/to/firewatcher.conf`.

```ini
[watch]
patterns = /etc/firewatcher/patterns.d
capture_time = 300
capture_time_max = 3600
output_folder = /var/log/captured_messages/
fallback_folder = /var/lib/firewatcher/captured_messages/
filter_only = false

[llm]
enabled = false
base_url = http://127.0.0.1:11434/v1
model = llama3.1
timeout = 60
max_bytes = 48000
notes = nightly backups are expected to load the disk

[notify]
enabled = false
ntfy_url = https://ntfy.sh/firewatcher-myhost-REPLACE_ME
priority = 4
timeout = 10
events_url =
events_token =
always_notify =
```

`patterns` and `always_notify` are one path per line, or comma-separated. Positional pattern arguments replace the `patterns` list.

Out-of-range values stop `firewatcher` at startup: a negative time or count, `capture_time` over 7 days or `capture_time_max` over 30 days, `min_free_percent` outside 0–50, `purge_interval` or `resume_max` over 30 days, `buffer_max_mb` outside 1–65536, a timeout of 0 or less or over a day, or an ntfy priority outside 1–5. So does an LLM, ntfy or events URL that is not `http://` or `https://`, has spaces, or whose server part is not a host name or a bracketed IPv6 address with an optional port (0–65535), as firewatcher reads it by its own strict rule. A `user:password` in a URL is never sent; one that would make the URL read two ways is refused, with [one exception](https://github.com/yufei-pan/firewatcher/blob/master/docs/limitations.md#a-url-whose-password-starts-with-digits-and-a--shows-those-digits-as-the-port).

`api_key` under `[llm]` and `token` under `[notify]` are not command-line flags. `FIREWATCHER_LLM_API_KEY` and `FIREWATCHER_NTFY_TOKEN` override them when set. `events_token` under `[notify]` is likewise config-only, overridden by `FIREWATCHER_EVENTS_TOKEN`. `--install-service` creates `/etc/firewatcher/firewatcher.conf` with mode `0600` when it is missing, and the unit runs `firewatcher --config` that path. When the file is there, each setting given to the install (a flag, `--always-notify`, the pattern arguments) whose value differs is written into it over its own line (a relative path is written absolute, from the folder the install runs in), keeping the file's comments, other lines, owner, Windows line ends (if every line has them) and, unless it holds a secret, its mode; a new config, or one with no `patterns` line, gets `patterns = /etc/firewatcher/patterns.d` when none is given. A config that holds a secret (the ntfy URL, a token or the API key) loses group and other access, with a line saying so. A config the install cannot write (a read-only file system, a file mounted alone) stops an install given a setting that changes it (exit 1; the config and the unit as they were; set the settings in it by hand); with no setting to change, the install goes on. A unit file mounted alone cannot be replaced either: an install that has to change it stops there (exit 1; a changed setting is written to the config first); put the text `--print-unit` prints (with the same flags) into it by hand, then run the same command again.

Generated configs include explanatory comments above each setting. When no ntfy URL is supplied, a new config gets `https://ntfy.sh/firewatcher-<hostname>-<random>`, with a 12-character URL-safe base64 suffix (72 random bits, no padding). The hostname is sanitized and shortened as needed to keep the topic within 64 characters. Explicit URLs are preserved, and reinstalling keeps the topic unless `--ntfy-url` is given. Notifications still default to off; subscribe to the generated topic in ntfy, then set `[notify] enabled = true`. When copying the example above, replace its topic with your own.

## LLM analysis and notifications

Both features default off.

- Notifications on, LLM off: every finished capture is pushed. The title is `{hostname}: {pattern}`.
- LLM on, notifications off: the model writes a title, summary, and notify decision. The report is appended to the capture file and logged. Nothing is pushed.
- Both on: a push is sent only when the model sets `notify` to true.
- With `[notify] events_url` set, the events service judges instead and firewatcher's LLM is off; see [Sending captures to an events service](#sending-captures-to-an-events-service).
- If the LLM request times out, returns an HTTP error, or is not the expected JSON, and notifications are on, firewatcher still pushes, using the generated title.
- No request firewatcher makes (the LLM, ntfy or the events service) follows a redirect: a 3xx answer is a failure, logged as `HTTP 301 from <server> (redirect, not followed)` with only the server, so the request and its token never go on to another host. A failed ntfy push is logged as `ntfy failed`; a failed LLM request falls back as above.

The capture window closes even when no further log lines arrive. Analysis runs on a background worker so log following stays live. The queue holds four captures. When the queue is full, that incident skips the model and, if notifications are on, the capture thread sends the generated-title push itself.

A capture that fits in `max_bytes` (default 48000) is sent whole. A bigger one is cut around the line that opened it: the header, then the lines just before the match (a quarter of the budget), the lines just after it (half), and the end of the capture (a quarter), each cut marked `[... N bytes skipped ...]`. Room one side does not need goes to the other. The pieces are read separately, so a large capture is never loaded into memory whole.

Any user or program on the host can write log lines, and those lines reach the model. The request marks the matched line and the excerpt as untrusted data between `BEGIN`/`END` lines that carry a random token, and the system prompt tells the model never to follow instructions inside them. That makes it harder for a log line to talk the model out of notifying, but cannot rule it out. For incidents that must always reach you, list patterns under `[notify] always_notify` (or `--always-notify`): when a line that matched a watch pattern also matches one of them, the capture is pushed even if the model says not to notify. That push uses the generated title and the line that matched, followed by the model's summary, since log text can steer the summary as well. If no always_notify pattern loads, firewatcher logs an error and keeps capturing without it; `--always-notify ''` clears a list set in the config. Only the line that opened the capture and later lines that matched a watch pattern while it was open count. Other lines in the window do not, and neither do matches from an earlier capture that appear as context. The capture's color codes are removed before it is sent to the model.

`[llm] notes` (or `--llm-notes TEXT`; `note` also works as the key, and `--llm-notes ''` clears a note set in the config) is a short note about this host, for example `nightly backups are expected to load the disk`. It is sent with every capture as `Operator notes about this host: ...`, outside the untrusted `BEGIN`/`END` block, and the system prompt tells the model that notes describe what is normal there. Any run of whitespace in it, newlines included, becomes one space. Given to `--install-service`, the note is written into the config, like any setting given there.

The ntfy topic URL is not printed at startup or in error messages, since on a public ntfy server whoever knows the topic can subscribe to it. Only the server is shown. Keep the topic in the config file (mode `0600`) rather than on the command line of a running firewatcher, where `--ntfy-url` shows in the process list. `--install-service --ntfy-url` writes it into the config, not the unit, and takes group and other access off that file.

### Sending captures to an events service

Set `[notify] events_url` to an `http://` or `https://` URL (empty means off) and firewatcher POSTs every finished capture, with an excerpt of it, to that service as JSON, with `Content-Type: application/json`. If `events_token` is set (or `FIREWATCHER_EVENTS_TOKEN`), it is sent as `Authorization: Bearer <token>`. The service can be a log dashboard, a webhook relay, or anything that accepts the POST; the timeout is `[notify] timeout`.

The receiver judges. With `events_url` set, firewatcher's own LLM analysis is off whatever `[llm] enabled` says: no capture goes to the model, nothing is appended to the capture file, and `[llm] base_url` and `model` are not checked at startup. The only judgment left at the source is the rule-based floor, `always_notify`. The fields:

| Field | Meaning |
| --- | --- |
| `source` | always `firewatcher` |
| `version` | the firewatcher version |
| `host` | the host name |
| `pattern` | the pattern that opened the capture |
| `title`, `body` | what a fallback ntfy push would carry: the generated title `{hostname}: {pattern}`, and the `always_notify` line or the matched line followed by the capture path |
| `push` | whether firewatcher itself counts the capture as worth an alert: the same as `always_notify` |
| `always_notify` | an `always_notify` pattern matched |
| `excerpt` | the capture, cut the same way as for the model when it is too big, and at most 16384 characters once JSON-encoded, so the whole event stays well under 64 KiB; `""` when the capture could not be saved or is gone |
| `path` | the capture file, or `null` when it could not be saved |
| `time` | when the event was sent (ISO 8601 with the UTC offset): after the capture window, not when the line matched |

Captures are posted whatever `push` says, so the service sees them all. Any 2xx answer means the service has the capture, and then nothing goes to ntfy. Anything else (a refused connection, a timeout, a TLS error, a redirect, a 4xx or 5xx) is logged as `event post failed`, and then only `always_notify` captures are pushed to ntfy (when notifications are on), with the generated title and the line that matched. Other captures stay in the capture folder and the journal (`event post failed: ...; not pushed (not always_notify)`). While the service does not answer, each push goes out one timeout late ([limitations](https://github.com/yufei-pan/firewatcher/blob/master/docs/limitations.md#while-the-events-receiver-is-down-each-alert-waits-one-timeout-before-ntfy)). `events_url` also works without `[notify] enabled`; a failed POST then only logs. Like the ntfy topic, the URL is shown only as its server, and the token is never logged. A `user:password` in a URL (events, ntfy or LLM) is not sent: use `events_token` (`token` under `[notify]` for ntfy, `api_key` under `[llm]` for the model). firewatcher warns at startup when one is there, naming the setting (`Warning: notify.ntfy_url holds a user:password, ...`) but never its value.

Lines firewatcher writes back to the journal are a single physical line containing `firewatcher-self`, so a summary that quotes `panic` or `I/O error` does not start another capture. In journalctl and syslog output, firewatcher recognizes its own lines by the identifier field after the timestamp and host (`firewatcher[PID]:`), not by text elsewhere in the line, so a process or user name chosen to look like firewatcher cannot hide a kernel or daemon message. Lines in other formats are checked for `firewatcher-self` or `firewatcher[PID]:` anywhere. The capture files themselves stay in the log stream's original format.


## Upgrading to 1.75 from 1.74

Nothing to change. What is different:

- `--install-service` writes the config and the unit each in full and synced (the unit, and a config that is there, through a file beside it that is renamed into place) before `daemon-reload`, `enable --now` and the restart. When one of those fails, the new config and unit stay (a unit that `enable --now` enabled stays enabled, and one that the first `daemon-reload` could not load stays unloaded), and one line says what is there and what to run. The same command, run again, finishes the install and leaves a config or unit that holds its text as it is. A restart that fails is followed by one `systemctl start` with the new files: when that works, the install exits 0 with a line on stderr. The one thing put back is the old unit of a service that was running, when `enable --now`, or the restart and that start, fails over it and its `ExecStart` names only `--config` (one 1.74 or 1.75 wrote): it is reloaded and, where the restart stopped the service, started (the line says which of these failed), so a crash starts the service with it, as with 1.74, and with what its own config holds, without this run's unit settings until the same command is run again. A unit with the settings on its `ExecStart` (as 1.73 and earlier wrote it) stays replaced. 1.74 removed a config or unit it had made, put back the old config and unit, whatever the unit held, and disabled what it had enabled.
- The unit keeps the owner and mode of the file it replaces (through a link, its target); a new unit is `0644` whatever the umask. A unit file mounted alone cannot be renamed over: an install that has to change it exits 1 (a changed setting is written to the config first); put the text `--print-unit` prints (with the same flags) into it by hand, then run the same command again ([limitations](https://github.com/yufei-pan/firewatcher/blob/master/docs/limitations.md#--install-service-given-settings-for-a-config-it-cannot-write-stops-with-exit-1)).
- A relative `-o`, `--fallback-folder`, `--position-file` or `--log-file` given to `--install-service` is written into the config absolute, from the folder the install runs in, and the output folder is made there; one the config already holds is read and made from `/`, where the service starts. 1.74 wrote them as given, and the service wrote under `/`. `--print-unit` with such a path from a folder that was removed is a message (exit 2), as for a pattern path.
- A pattern file given with a trailing slash (`p.txt/`) where `p.txt` is a file is no longer taken for a folder to make; the line says to drop the slash, and the install refuses it (exit 2, nothing written) only where no other pattern loads, and not under `--no-enable`, where it is a warning. 1.74 took it for a folder to make and stopped with a traceback. An output folder the install cannot make is a warning, and the install goes on (each capture tries again); 1.74 stopped with a traceback.

## Upgrading to 1.74 from 1.73

- A unit installed with setting flags by 1.73 or earlier while the config was there (or by 1.57 and earlier, which wrote no config) holds them on its `ExecStart`, the ntfy URL among them, where any local user can read them, and so does the running service's process list. `systemctl cat firewatcher` shows them when `ExecStart=` names more than `--config PATH`. Run the same `--install-service` command again with 1.74: it writes them into the config, rewrites the unit with `--config` only and restarts the running service, so neither holds a setting. Run without those flags, the install rewrites the unit without them, and only what the config holds stays: give them again to move them into it. The old unit showed the topic: if it must stay private, take a new one.
- `--install-service` takes group and other access off a config that holds a secret (the ntfy URL, a token or the API key) and says so. 1.73 left its mode as it was.
- If the config has notifications on and names no topic, an install without `--ntfy-url` is refused (exit 2, nothing written) and the 1.73 unit stays: run it again with `--ntfy-url`. Afterwards `systemctl cat firewatcher` shows `--config` only on `ExecStart`.
- `--print-unit` given setting flags prints a unit without them, and says on stderr that `--install-service` would write the settings into the config. It runs the config checks only, not the pattern and executable checks below, and writes nothing.
- `--install-service` refuses (exit 2, nothing written) a setting given as a flag or a pattern argument whose value the install cannot write: a newline, or in a list of paths a comma. 1.73 wrote a new config without the newline, or with the comma, which the config's reader splits, so the service read another path. Whitespace at either end of a path or text is dropped, as the config's reader drops it.
- `--install-service` also refuses (exit 2, before it writes anything, with one line on stderr) what the service could not start from: a pattern source it would exit on at start, such as a path that does not exist or a file with no usable pattern (`No usable patterns in [...]`; a missing path also prints `Pattern file not found: PATH` on stdout; a source that is missing next to one that loads is no reason to refuse), and a unit executable that does not exist (no hint is printed) or has no execute bit (`chmod +x` it). 1.73 installed both, and the service then exited at once and was started again every 5 seconds; with 1.74's restart that would have stopped a service that worked. Under `--no-enable`, which starts nothing, each is a warning and the install goes on. A reinstall can now stop on these where 1.73 did not: fix the path or the bit, or add `--no-enable`.
- `--install-service` also refuses (exit 2, nothing written, one line on stderr) a unit path that leads to something that is not a file. A masked unit (`systemctl mask` makes it a link to `/dev/null`) says so and names `systemctl unmask -- UNIT`; run the install again after it. 1.70 to 1.73 wrote the unit into the link, and when `enable --now` then failed, removed `/dev/null`. A link to `/dev/null` is a masked unit also where `/dev/null` is missing (a reboot makes it again); a link to a file, or to any other path that is not there, is written through as before.
- A pattern or `--always-notify` path given as a flag to `--install-service` is written into the config absolute, from the folder the install runs in, as `--config` and `--requires-mounts-for` are, and checked there. The service starts in `/`, so 1.73's bare `p.txt` was read from `/`, where the install had not looked. A relative path the config already holds is checked, and read by the service, from `/`. 1.74 did not make a relative `-o`, `--fallback-folder`, `--position-file` or `--log-file` absolute; 1.75 does.
- A config file that is not UTF-8 stops firewatcher with a message (exit 2); 1.73 stopped with a traceback.
- A section header with text after it is the section on every Python. Python 3.10 and later read `[notify] # see [docs]`, with a `]` later in the line, as a section named `notify] # see [docs`, so its settings did not apply there (with `enabled = true` under it, notifications stayed off); they do now, from the restart of the upgrade. Look at such a line in a config you wrote by hand.

## Upgrading to 1.71 from 1.70

Nothing to change. What is different:

- A restart reads the journal from the saved entry to its end without `--follow`, then follows from the last entry read. On systemd 252 and later, what the old boot logged after firewatcher stopped is now matched after a reboot (within `--resume-max`); 1.70 followed from the saved entry, and journalctl's follow shows only the newest boot there.
- The context before the saved entry is read back from it, so it holds the newest lines of the window up to `--buffer-max-mb`, also after a clock that was ahead was set back, and never a line logged after the stop. The note says how many lines (`Resuming the journal after …, with N lines of context …`) and comes after the `Monitoring` line.
- A journalctl that fails while firewatcher reads on from the saved entry ends the run, so the service's restart reads on from the last line handled; 1.70 matched the window again as if the saved entry were gone when it failed before its first entry. Run by hand, firewatcher then exits with one line saying so.
- The start-up check and the restart's reads are waited for however long journalctl takes; 1.70 gave up after 30 seconds, and a late answer to the check turned resuming off for that run.

## Upgrading to 1.70 from 1.69

Nothing to change. What is different:

- The position file holds the journal cursor of the last entry read, so a restart resumes exactly where the journal still holds that entry, even when the clock was set back: no line is skipped, except after a reboot on systemd 252 and later, and a match is captured again only in the cases named in [Resuming after a restart](#resuming-after-a-restart). A 1.69 position file is read by its time, as when the journal lost the saved line: the lines from `--capture-time` before it are matched once more at the upgrade. A 1.69 started on a 1.70 file says it cannot read the journal position and reads the last `--tail-lines` lines.
- firewatcher writes the journal lines in captures itself (from `journalctl -o export`): the time zone offset is always written `+HH:MM` (systemd 219, 239 and 252, as on CentOS 7, EL8 and Debian 12, print it without the colon, `+0000`), and journalctl's `-- Boot <id> --` lines no longer appear among them.
- A pattern file saved with a UTF-8 byte-order mark now matches its first pattern; 1.69 kept the mark in it.
- `--print-unit` runs the install's config checks: a unit name or config text that `--install-service` refuses is refused there too (exit 2); 1.69 printed the unit.
- A failed `--install-service` through a dangling symlink at the unit path removes the file it wrote at the link's target and leaves the link; 1.69 kept that file.

## Upgrading to 1.69 from 1.68

Nothing to change under a UTF-8 or C locale. What is different:

- After deleting old months to keep the free-space floor, firewatcher syncs only that filesystem (none on macOS), so a hard-mounted NFS share that is not answering, other than the one the captures are on, no longer stalls log reading. 1.68 synced every filesystem.
- When writing the unit, `daemon-reload` or `enable --now` fails, `--install-service` removes the config and the unit it created, puts back a unit it replaced as it was, and disables a unit it enabled, so the same command can run again. 1.68 left them, and the rerun put the command-line flags, the ntfy URL among them, on the unit's `ExecStart`. A config that does not fit on the disk is the same kind of failure; 1.68 wrote part of it and went on.
- At a restart, firewatcher first finds the saved line in the journal as it is now. When the journal lost the start of that second, the lines after the saved one are still matched; when it lost the line itself, every line from `--capture-time` before it is matched (no further back than `--resume-max`). 1.68 replayed them as context only, and with the clock set back by up to a minute, every line stamped before the saved second.
- `--tail-lines ''` is as if the flag were not given (the config's `tail_lines`, else 20), as an empty `tail_lines` in the config is. 1.68 passed it on, and `tail` or `journalctl` exited at once.
- Under a locale that is neither UTF-8 nor C (such as `en_US.ISO-8859-1`), a non-ASCII path in the config now names the file by its UTF-8 bytes, and a pattern file is read as UTF-8, where 1.68 used the locale's encoding for both: rename such a folder or file to its UTF-8 name, and save a pattern file as UTF-8 (one that is not is skipped, with a message). `--install-service` refuses a path or note typed there in the locale's own encoding (exit 2); 1.68 wrote it.
- On Python 3.6 under a C locale, non-ASCII paths (from the config, the command line or the unit), matched log lines and pattern files no longer stop firewatcher with `UnicodeEncodeError` or go unread, and `--install-service` writes a non-ASCII path or note given there as UTF-8, where 1.68 refused it. `--print-unit` writes the unit as UTF-8 in every locale (1.68 stopped on a non-ASCII `--requires-mounts-for` path) and refuses a value that is not UTF-8 (exit 2), which 1.68 printed or stopped on. On any Python, a path that is not UTF-8 no longer stops it at the first capture.

## Upgrading to 1.68 from 1.67

Nothing to change. What is different:

- `--install-service` refuses a unit name systemd cannot load, and a path or value that is not UTF-8, before it writes anything (exit 2). 1.67 could write the config first and then stop with a traceback, or install a unit under a name systemd skips. A blank `--unit-name` now means `firewatcher`, as an empty one did.
- `systemctl enable --now` and `disable --now` get `--` before the unit name, so a name starting with `-` works.
- On Python 3.6 under a C locale, `--install-service` refuses a non-ASCII path or note given on the command line (exit 2, nothing written) and says to run under a UTF-8 locale; 1.67 stopped there with `UnicodeEncodeError`. 1.69 installs it.
- The package declares its license as the SPDX expression `GPL-3.0-or-later` when built with setuptools 77 or later.
- `--uninstall-service --unit-name` with a path (`a/b`) now prints a message and exits 2; 1.67 stopped with a traceback. It still takes any other name an older firewatcher may have installed.
- Dead code and duplicated rules are gone; nothing else an operator sees changed.

## Development

```bash
pip install -r requirements-dev.txt
pytest
pytest --cov=firewatcher --cov-fail-under=95 --cov-report=term-missing
```

The restarts on a real journalctl (`tests/test_real_journal.py`) need `journalctl` and `systemd-journal-remote` (the Debian and Ubuntu package of that name); without them they are skipped, unless `FIREWATCHER_REQUIRE_JOURNALCTL` is set, as CI does.

## Author

Yufei Pan (pan@zopyr.us)

## License

GPL-3.0-or-later
