Metadata-Version: 2.1
Name: brainframe-sys-tools
Version: 0.4.5.7
Summary: This is the package including tools for BrainFrame developers to monitor and debug the system.
License: BSD License
Author: Stephen Li
Author-email: stephen@aotu.ai
Requires-Python: >=3.8,<4.0
Classifier: License :: OSI Approved :: BSD License
Classifier: License :: Other/Proprietary License
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Requires-Dist: PyYAML (>=6.0.2,<7.0.0)
Requires-Dist: apscheduler (>=3.10.4,<4.0.0)
Requires-Dist: brainframe-api (>=0.30.4,<0.31.0)
Requires-Dist: brainframe_apps (>=0.4.5.4,<0.5.0.0)
Requires-Dist: psutil (>=5.8.0,<6.0.0)
Requires-Dist: python_i18n (>=0.3.9,<0.4.0)
Requires-Dist: requests (>=2.32.3,<3.0.0)
Requires-Dist: tabulate (>=0.8.9,<0.9.0)
Description-Content-Type: text/markdown

# brainframe-sys-tools

Python CLI tools for monitoring and debugging BrainFrame system deployment.

## Installation

```bash
pip install brainframe-sys-tools
```

## Usage

```bash
brainframe-sys-tools <command> [options]
```

### Commands

- `perf-monitor` - Monitor CPU, GPU, memory, storage, and temperatures
- `fps-monitor` - Monitor BrainFrame stream FPS and throughput
- `service-monitor` - Run in the foreground, or use `install`, `status`,
  and `uninstall` to manage the daemon and WDAT watchdog
- `sys-info` - Display system information
- `ssh-tunnel` - Create SSH tunnels

### Example

```bash
brainframe-sys-tools perf-monitor
brainframe-sys-tools fps-monitor --server-url http://localhost
brainframe-sys-tools service-monitor install --dry-run
```

## Help

```bash
brainframe-sys-tools <command> --help
```

## BrainFrame Service Monitoring & Recovery Design

The target NUC setup has two independent recovery layers. Follow the setup
steps below to activate them; neither layer is active just because this
repository has been updated.

1. **BrainFrame service monitor:** every 30 seconds, request the local
   BrainFrame version with a 10-second timeout. After three consecutive
   failures, and no sooner than 10 minutes after the previous automatic
   recovery, run `brainframe compose down` and `brainframe compose up -d`.
   On startup, allow BrainFrame up to five minutes to initialize before
   counting failed checks. Zone status is logged after a successful version
   check but never triggers a restart. This layer never reboots the host.
   systemd also restarts the monitor itself if it exits or stops sending
   its service heartbeat for 10 minutes.
2. **Hardware watchdog:** the kernel's `wdat_wdt` driver exposes the NUC's
   WDAT device as `/dev/watchdog0`. systemd (PID 1) feeds the device. If
   systemd cannot run long enough to feed it, the hardware timer resets the
   host. The BrainFrame monitor does not feed this timer: an application
   failure should restart the application, not the whole machine.

The old `watchdog.service` used a load-average check that rebooted this NUC
when its 15-minute load average exceeded 12 for 60 seconds. Keep that
service disabled. It did not detect out-of-memory events. The previous
BrainFrame monitor would restart the full Compose stack after a single
failed API check; the NUC had no monitor service installed.

### Install the system watchdog and service monitor

On a WDAT-capable BrainFrame server, run the installed sys-tools command
as the account that runs BrainFrame:

```bash
brainframe-sys-tools service-monitor install
```

Run the command as the login account, **not** with `sudo`. It prompts for
sudo when changing system settings. Use `--dry-run` to inspect the generated
unit and configuration without changing the machine.

`service-monitor install` checks for the WDAT firmware device and BrainFrame CLI,
then performs these steps in order:

1. Mask the old watchdog's keepalive helper, then disable the load-based
   `watchdog.service` before loading WDAT. Neither can then take ownership
   of the hardware timer.
2. Load `wdat_wdt`, confirm `/dev/watchdog0` identifies as `wdat_wdt`, add
   the module to the initramfs, and rebuild the initramfs for future boots.
3. Configure systemd (PID 1) to feed `/dev/watchdog0` with a three-minute
   timeout. This is within the inspected NUC's 2-613 second range.
4. Install and enable `brainframe-service-monitor.service` for the login account,
   with 30-second checks, 10-second request timeout, three failures before
   recovery, a 10-minute restart cooldown, and a 10-minute systemd service
   watchdog for a stuck monitor process.
5. Confirm that the hardware watchdog and BrainFrame monitor are active.

The command stops with an error if this host lacks WDAT or if verification
fails. It does not deliberately expire the hardware timer. A controlled
expiry test is still needed to prove that the firmware resets the machine;
do that during a maintenance window. Enabling the hardware watchdog arms a
real reset timer, so run setup while you can observe the machine.

### Run service-monitor daemon

`service-monitor install` installs `brainframe-service-monitor.service`, starts it immediately,
and enables it at boot. The daemon runs as the login account supplied during
setup. It checks the BrainFrame API and writes heartbeat logs to
`~/.local/state/brainframe-service-monitor/`.

Use systemd to start, stop, or restart daemon mode:

```bash
sudo systemctl start brainframe-service-monitor.service
sudo systemctl stop brainframe-service-monitor.service
sudo systemctl restart brainframe-service-monitor.service
```

Restarting the monitor process does not restart BrainFrame unless the monitor
later observes three consecutive API failures.

For foreground troubleshooting, stop the systemd service before running the
CLI directly. This avoids two monitors trying to recover BrainFrame at once:

```bash
sudo systemctl stop brainframe-service-monitor.service
brainframe-sys-tools service-monitor
```

Press `Ctrl+C` to stop foreground mode, then run
`sudo systemctl start brainframe-service-monitor.service` to restore daemon
mode. The local URL, log path, check interval, request timeout, failure
count, and restart cooldown shown by `service-monitor --help` have defaults;
pass options only when you need to override them.

### Check status

Check both recovery layers and show the last 30 monitor journal entries with:

```bash
brainframe-sys-tools service-monitor status
```

The summary reports whether `brainframe-service-monitor.service` is active
and enabled, whether WDAT is active and fed by systemd, and whether the old
load watchdog is inactive. It also lists machine boots from `wtmp` and
monitor-initiated BrainFrame stack restarts from the system journal for the
past 30 days. The available history may be shorter if `wtmp` or journal
records have been rotated away; manual Compose restarts are not logged by the
monitor. The command exits with an error if a recovery layer is missing. Use
`--logs 0` to omit the recent monitor log tail (the 30-day history still
appears), or `--logs N` to choose its length. Repeat the status check after
the next planned host reboot to verify both layers started automatically.

### Uninstall the system watchdog and service monitor

To stop the BrainFrame monitor and disarm systemd's hardware watchdog, run:

```bash
brainframe-sys-tools service-monitor uninstall
```

Run this as the login account; the command prompts for sudo. It removes
only configuration files marked as managed by sys-tools and the WDAT
initramfs entry if sys-tools added it. It unmasks the old `wd_keepalive`
helper without starting it, and does not re-enable the old load-based
`watchdog.service`. The WDAT driver may remain loaded until the next
reboot, but its timer is inactive. Use `service-monitor uninstall --dry-run`
to preview the changes and `service-monitor status --logs 0` to inspect the
result.

If WDAT cannot be made usable, the kernel's `softdog` is a fallback for a
stalled user-space feeder while kernel timers still work. It cannot reset a
fully frozen kernel, so it is weaker than the NUC's hardware watchdog.

See the [systemd hardware watchdog settings](https://manpages.ubuntu.com/manpages/jammy/man5/systemd-system.conf.5.html)
and the [initramfs module loading rules](https://manpages.ubuntu.com/manpages/jammy/man7/initramfs-tools.7.html).

