# Alerts

# Alerts

Drillmark evaluates the configured rules per tenant on every alerts-generation run (part of the scheduled job set). Detected conditions are written to `alerts_sent` and dispatched to every enabled channel for the tenant.

## Alert kinds

Seven detectors are implemented in the backend.

| Kind | Fires when |
|---|---|
| `HighAuthFailureRate` | DMARC authentication failure rate over the evaluation window exceeds `ALERT_AUTH_FAILURE_RATE` percent (default 10.0). |
| `EmailVolumeSpike` | Volume grows by more than `ALERT_VOLUME_SPIKE_RATE` percent (default 50.0) vs. the previous period. |
| `DmarcPolicyViolation` | A record was handled under a policy stricter than expected. |
| `NewUnauthorizedSender` | A new source IP sends mail for a monitored domain without passing authentication. |
| `PolicyDowngrade` | A domain's published DMARC policy weakened (e.g. `reject` -> `quarantine` -> `none`). |
| `DkimSigningChange` | A monitored domain's DKIM signing posture changed in a way worth flagging. |
| `BlockedCountryTraffic` | Traffic appears from a country the tenant has blocked under their country-policy rules. |

## Detection modes

- **Legacy** (no `alert_rules` rows for a tenant): the three original detectors run with thresholds from the tenant row, dispatching to every enabled channel. Preserves pre-rule behaviour verbatim.
- **Rule-driven** (any `alert_rules` row): only the detectors named by enabled rules run, each with its rule-specific threshold, and each dispatches to its rule's `channel_ids`.

## Channels

SMTP is the one channel implemented today. Create a channel on the tenant's alert settings screen (or let Drillmark auto-bootstrap one from the tenant's `smtp_*` columns on the first run, so an existing deployment keeps alerting without manual setup).

Delivery uses the SMTP settings from `.env` (`SMTP_HOST`, `SMTP_PORT`, `SMTP_SECURITY`, `SMTP_USERNAME`, `SMTP_PASSWORD`, `SMTP_FROM_ADDRESS`). The `ADMIN_EMAIL` recipient is the default; per-channel overrides live on the channel row.

## Non-routable recipients

An alert addressed to a non-routable domain (`example.com`, `.invalid`, `.localhost`, `.test`) is not sent; the rendered message is written to `TRANSACTIONAL_OUTBOX_DIR` (default `tmp/outbox`) instead. Useful in dev and CI.

## Deduplication

Drillmark runs `deduplicate_alerts` before dispatch, so the same condition does not re-fire on every run; it only resurfaces once the underlying state changes.

## Turning alerts off

- Disable individual alert channels on the tenant (do not dispatch).
- Delete or disable `alert_rules` rows to stop the rule-driven detectors.
- There is no global `ALERTS_ENABLED=false` switch today; the job always runs but produces nothing when no tenant has an enabled channel.
