truenas-burnin/app/notifier.py
Brandon Walter 2dff58bd52 Stage 7: SSH architecture, SMART attribute monitoring, drive reset, and polish
SSH (app/ssh_client.py — new):
- asyncssh-based client: start_smart_test, poll_smart_progress, abort_smart_test,
  get_smart_attributes, run_badblocks with streaming progress callbacks
- SMART attribute table: monitors attrs 5/10/188/197/198/199 for warn/fail thresholds
- Falls back to REST API / mock simulation when ssh_host is not configured

Burn-in stages updated (burnin.py):
- _stage_smart_test: SSH path polls smartctl -a, stores raw output + parsed attributes
- _stage_surface_validate: SSH path streams badblocks, counts bad blocks vs configurable threshold
- _stage_final_check: SSH path checks smartctl attributes; DB fallback for mock mode
- New DB helpers: _append_stage_log, _update_stage_bad_blocks, _store_smart_attrs,
  _store_smart_raw_output

Database (database.py):
- Migrations: burnin_stages.log_text, burnin_stages.bad_blocks,
  drives.smart_attrs (JSON), smart_tests.raw_output

Settings (config.py + settings_store.py):
- ssh_host, ssh_port, ssh_user, ssh_password, ssh_key — all runtime-editable
- SSH section in Settings UI with Test SSH Connection button

Webhook (notifier.py):
- Added bad_blocks and timestamp fields to payload per SPEC

Drive reset (routes.py + drives_table.html):
- POST /api/v1/drives/{id}/reset — clears SMART state, smart_attrs; audit logged
- Reset button visible on drives with completed test state (no active burn-in)

Log drawer (app.js):
- Burn-In tab: shows raw stage log_text (SSH output) with bad block highlighting
- SMART tab: shows SMART attribute table with warn/fail colouring + raw smartctl output

Polish:
- Version badge (v1.0.0-6d) in header via Jinja2 global
- Parallel burn-in warning when max_parallel_burnins > 8 in Settings
- Stats page: avg duration by drive size + failure breakdown by stage
- settings.html: SSH section with key textarea, parallel warn div

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-24 08:09:30 -05:00

84 lines
2.5 KiB
Python

"""
Notification dispatcher — webhooks and immediate email alerts.
Called from burnin.py when a job reaches a terminal state (passed/failed).
Webhook fires unconditionally when WEBHOOK_URL is set.
Email alerts fire based on smtp_alert_on_fail / smtp_alert_on_pass settings.
"""
import asyncio
import logging
from app.config import settings
log = logging.getLogger(__name__)
async def notify_job_complete(
job_id: int,
devname: str,
serial: str | None,
model: str | None,
state: str,
profile: str,
operator: str,
error_text: str | None,
bad_blocks: int = 0,
) -> None:
"""Fire all configured notifications for a completed burn-in job."""
from datetime import datetime, timezone
tasks = []
if settings.webhook_url:
tasks.append(_send_webhook({
"event": f"burnin_{state}",
"job_id": job_id,
"devname": devname,
"serial": serial,
"model": model,
"state": state,
"profile": profile,
"operator": operator,
"error_text": error_text,
"bad_blocks": bad_blocks,
"timestamp": datetime.now(timezone.utc).isoformat(),
}))
if settings.smtp_host:
should_alert = (
(state == "failed" and settings.smtp_alert_on_fail) or
(state == "passed" and settings.smtp_alert_on_pass)
)
if should_alert:
tasks.append(_send_alert_email(job_id, devname, serial, model, state, error_text))
if not tasks:
return
results = await asyncio.gather(*tasks, return_exceptions=True)
for r in results:
if isinstance(r, Exception):
log.error("Notification failed: %s", r, extra={"job_id": job_id, "devname": devname})
async def _send_webhook(payload: dict) -> None:
import httpx
async with httpx.AsyncClient(timeout=10.0) as client:
r = await client.post(settings.webhook_url, json=payload)
r.raise_for_status()
log.info(
"Webhook sent",
extra={"event": payload.get("event"), "job_id": payload.get("job_id"), "url": settings.webhook_url},
)
async def _send_alert_email(
job_id: int,
devname: str,
serial: str | None,
model: str | None,
state: str,
error_text: str | None,
) -> None:
from app import mailer
await mailer.send_job_alert(job_id, devname, serial, model, state, error_text)