Metadata-Version: 2.4
Name: monitoring-app
Version: 1.0.0
Summary: Monitor batch pipelines via API and email alerts — install and deploy on Linux servers from PyPI
Author-email: Inferyx DevOps <devops@inferyx.com>
License-Expression: LicenseRef-Proprietary
Keywords: batch,monitoring,pipeline,email,inferyx
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: System Administrators
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Operating System :: OS Independent
Classifier: Topic :: System :: Monitoring
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: openpyxl>=3.1.0
Requires-Dist: pandas>=2.0.0
Requires-Dist: requests>=2.31.0
Requires-Dist: python-dateutil>=2.8.2
Requires-Dist: fastapi>=0.110.0
Requires-Dist: python-multipart>=0.0.9
Requires-Dist: uvicorn[standard]>=0.27.0
Requires-Dist: pydantic>=2.0.0
Requires-Dist: cryptography>=42.0.0
Requires-Dist: lxml>=5.0.0
Requires-Dist: signxml>=3.2.0
Requires-Dist: boto3>=1.34.0
Requires-Dist: pymongo<5,>=4.6
Provides-Extra: admin
Provides-Extra: dev
Requires-Dist: build; extra == "dev"
Requires-Dist: twine; extra == "dev"
Requires-Dist: check-manifest; extra == "dev"
Dynamic: license-file

# monitoring-app

Batch monitoring for the Inferyx platform: email (and Teams / Google Chat) alerts
when a scheduled batch **fails** or **does not start**, plus a web console with a
dashboard, schedules, batch logs, settings and user management.

| | |
|---|---|
| **Install directory** | `/opt/monitoring-app` |
| **Console** | `https://<your-host>/monitoring/admin/` (behind nginx) |
| **Service** | `monitoring-app` (systemd) |
| **Guides shipped with the package** | `/opt/monitoring-app/.venv/share/monitoring-app/` — `DEPLOYMENT.md` (install, upgrade, rollback, security, MongoDB), `USER_GUIDE.md` (alerts and console), `CHANGELOG.md` |

---

## What it does

- Reads the schedules from Inferyx and checks every run against its schedule, every minute.
- **Failed** — a scheduled run ends in a failed stage → mail with the error reason.
- **Missed** — no run was created for a scheduled time: after start time + grace
  (default 5 min) the monitor checks 5 times; only then is it Missed.
- **No data** — Inferyx could not be reached → mail to DevOps only (never reported as Missed).
- One mail per occurrence; follow-ups arrive as replies in the same thread. Failures from
  before the monitor was switched on are never mailed.
- Every alert is stored and shown in the console with its mail result.

**Batch status source** (Environment Settings): `api` (Inferyx REST API, default) or
`mongodb` (reads the Inferyx MongoDB directly, read-only). With MongoDB only runs
started **by the scheduler** are alerted — runs started by hand are ignored — and
suspended, deleted and expired schedules are not monitored. Credentials are entered
in the console and stored encrypted.

---

## Prerequisites

| Requirement | Notes |
|-------------|-------|
| **OS** | Ubuntu 22.04+ (or Debian with `apt`) |
| **Python** | 3.10+ (`python3`, `python3-venv`) |
| **nginx** | Reverse proxy for `/monitoring/admin/` and `/monitoring/api/` |
| **Network** | SMTP; the Inferyx API; optional: Inferyx MongoDB, Teams / Google Chat webhooks, Google OAuth, AWS IAM Identity Center |

---

## Install

```bash
export IM_HOME=/opt/monitoring-app
sudo mkdir -p "$IM_HOME"
sudo useradd --system --home-dir "$IM_HOME" --shell /usr/sbin/nologin inferyx 2>/dev/null || true

sudo -u inferyx python3 -m venv "$IM_HOME/.venv"
sudo -u inferyx "$IM_HOME/.venv/bin/pip" install --upgrade pip
sudo -u inferyx "$IM_HOME/.venv/bin/pip" install monitoring-app

sudo bash "$IM_HOME/.venv/share/monitoring-app/release.sh" install --skip-pip
```

Pin a version with `pip install "monitoring-app==1.0.0"`, or install a local wheel:
`pip install /path/to/monitoring_app-1.0.0-py3-none-any.whl`.

The install script creates:

| Path | Purpose |
|------|---------|
| `.env` | SMTP host, Inferyx API URL, alert settings — **no secret values** |
| `auth.policy` | Console sign-in (local, Google, AWS IAM Identity Center), paths |
| `secrets/` | `master.key` (encryption key), `session_secret` — owner-only |
| `data/` | `alerts.db` (alerts), `config.db` (encrypted secrets), `console.db` (users, approvals) |
| `www/` | Console files |
| `logs/` | Monitor log |

### Configure

1. **`.env`** — `MONITORING_APP_SMTP_HOST`, `_SMTP_USERNAME`, `_MAIL_TO`, `_DEVOPS_EMAIL`,
   `_API_BASE_URL`, `_API_TOKEN_HEADER`.
2. **Secrets** — stored encrypted, never in a file:

   ```bash
   CLI=/opt/monitoring-app/.venv/bin/monitoring-app
   sudo -u inferyx $CLI secrets set MONITORING_APP_SMTP_PASSWORD --work-dir /opt/monitoring-app
   sudo -u inferyx $CLI secrets set MONITORING_APP_API_TOKEN     --work-dir /opt/monitoring-app
   ```

   **Back up `secrets/master.key` separately** — stored secrets cannot be read without it.
3. **`auth.policy`** — `ui.public_base_url` and the sign-in methods. Set
   `MONITORING_APP_ADMIN_BOOTSTRAP_EMAIL` for the first admin before the console is reachable.
4. **nginx**:

   ```bash
   sudo cp /opt/monitoring-app/.venv/share/monitoring-app/nginx.conf.example /etc/nginx/sites-available/monitoring-app
   sudo ln -sf /etc/nginx/sites-available/monitoring-app /etc/nginx/sites-enabled/
   sudo nginx -t && sudo systemctl reload nginx
   ```

Afterwards, settings can be changed in the console (**Environment Settings**,
**Application Settings**); changes apply within one check, no restart needed.

### Verify

```bash
sudo systemctl status monitoring-app
curl -s http://127.0.0.1:8090/api/health
```

---

## Upgrade

```bash
sudo -u inferyx /opt/monitoring-app/.venv/bin/pip install --upgrade monitoring-app
sudo bash /opt/monitoring-app/.venv/share/monitoring-app/release.sh upgrade --skip-pip
```

The upgrade deploys the new console, adds new settings (existing values are kept),
removes retired ones, updates the service and restarts it. If the new service does not
answer its health check, it **rolls back** automatically.

### Switching from `inferyx-monitoring` (`/opt/pipeline-monitor`)

The package was renamed from `inferyx-monitoring` to `monitoring-app`. On an old server,
put the new package into the **old** virtualenv (only to get the new upgrade script) and
run the upgrade — it moves the installation to `/opt/monitoring-app` in one step:

```bash
OLD=/opt/pipeline-monitor
sudo -u inferyx "$OLD/.venv/bin/pip" install monitoring-app
sudo bash "$OLD/.venv/share/monitoring-app/release.sh" upgrade --skip-pip
```

Settings `PIPELINE_*` become `MONITORING_APP_*` (values kept), plain-text secrets move
into the encrypted store, paths in config / systemd / nginx are rewritten, and the old
folder is kept as `/opt/pipeline-monitor.migrated-<date>`. Details: `DEPLOYMENT.md`.

---

## Using MongoDB as the batch status source (optional)

1. Create a **read-only** MongoDB user with `find` on `schedule`, `scheduleexec`,
   `batchexec`, `batch` and `application`, and the two indexes — commands in `DEPLOYMENT.md`.
2. Console → **Environment Settings → MongoDB (batch status)**: connection string, user,
   password (stored encrypted), database (`framework`) → Save → **Test connection**.
3. Set **Batch Status Source** to `mongodb`. Only failures after this moment are mailed.
4. To go back, set it to `api`.

---

## Alert settings

| Setting | Default | Meaning |
|---------|---------|---------|
| `MONITORING_APP_CHECK_MODE` | `full_window` | `full_window` checks through the whole run (recommended); `schedule_windows` only at start and expected end |
| `MONITORING_APP_SCHEDULE_GRACE_MINUTES` | `5` | Minutes after the scheduled time before a run counts as missed |
| `MONITORING_APP_ALERT_FAILED` / `_MISSED` / `_NO_DATA` | `true` | Switch each alert type on or off |
| `MONITORING_APP_ALERT_ONCE_PER_DAY_ALL_SCENARIOS` | `true` | One new mail thread per issue per day |
| `MONITORING_APP_ALERT_REPLY_TO_THREAD` | `true` | Follow-ups as replies in the same thread |
| `MONITORING_APP_LOG_RETENTION_DAYS` | `30` | Keep rotated monitor logs for N days |

Test the mail setup: `sudo -u inferyx /opt/monitoring-app/.venv/bin/monitoring-app --test-mail-thread`

---

## Troubleshooting

| Symptom | Fix |
|---------|-----|
| Service restarts in a loop | `sudo journalctl -u monitoring-app -n 50` — usually a missing `.env` value |
| Console shows "Unavailable" | `curl http://127.0.0.1:8090/api/health`; check nginx with `nginx.conf.example` |
| **No data** alerts | Inferyx API unreachable or token expired — `monitoring-app secrets set MONITORING_APP_API_TOKEN` |
| **Missed** although the batch ran | The run was started by hand, not by the scheduler (MongoDB source) — check the Inferyx scheduler |
| No mails at all | Environment Settings → SMTP; the console's Alert mail column shows the error |
| MongoDB test fails | Network to MongoDB, user / password / auth database, TLS settings |

---

## Service commands

```bash
sudo systemctl restart monitoring-app
sudo systemctl status monitoring-app
sudo journalctl -u monitoring-app -f

sudo bash /opt/monitoring-app/.venv/share/monitoring-app/release.sh uninstall --skip-pip          # keep config
sudo bash /opt/monitoring-app/.venv/share/monitoring-app/release.sh uninstall --purge --skip-pip  # remove everything
```
