truenas-burnin/app/renderer.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

141 lines
4 KiB
Python

"""
Jinja2 template engine + filter/helper registration.
Import `templates` from here — do not create additional Jinja2 instances.
"""
from datetime import datetime, timezone
from fastapi.templating import Jinja2Templates
templates = Jinja2Templates(directory="app/templates")
# ---------------------------------------------------------------------------
# Template filters
# ---------------------------------------------------------------------------
def _format_bytes(value: int | None) -> str:
if value is None:
return ""
tb = value / 1_000_000_000_000
if tb >= 1:
return f"{tb:.0f} TB"
gb = value / 1_000_000_000
return f"{gb:.0f} GB"
def _format_eta(seconds: int | None) -> str:
if not seconds or seconds <= 0:
return ""
h = seconds // 3600
m = (seconds % 3600) // 60
if h > 0:
return f"~{h}h {m}m" if m else f"~{h}h"
return f"~{m}m" if m else "<1m"
def _temp_class(celsius: int | None) -> str:
if celsius is None:
return ""
from app.config import settings
if celsius < settings.temp_warn_c:
return "temp-cool"
if celsius < settings.temp_crit_c:
return "temp-warm"
return "temp-hot"
def _format_dt(iso: str | None) -> str:
if not iso:
return ""
try:
dt = datetime.fromisoformat(iso)
if dt.tzinfo is None:
dt = dt.replace(tzinfo=timezone.utc)
local = dt.astimezone()
return local.strftime("%H:%M:%S")
except Exception:
return iso
def _format_dt_full(iso: str | None) -> str:
"""Date + time for history tables."""
if not iso:
return ""
try:
dt = datetime.fromisoformat(iso)
if dt.tzinfo is None:
dt = dt.replace(tzinfo=timezone.utc)
local = dt.astimezone()
return local.strftime("%Y-%m-%d %H:%M:%S")
except Exception:
return iso
def _format_duration(seconds: float | int | None) -> str:
if seconds is None or seconds < 0:
return ""
seconds = int(seconds)
h = seconds // 3600
m = (seconds % 3600) // 60
s = seconds % 60
if h > 0:
return f"{h}h {m}m {s}s"
if m > 0:
return f"{m}m {s}s"
return f"{s}s"
# ---------------------------------------------------------------------------
# Template globals
# ---------------------------------------------------------------------------
def _drive_status(drive: dict) -> str:
short = (drive.get("smart_short") or {}).get("state", "idle")
long_ = (drive.get("smart_long") or {}).get("state", "idle")
health = drive.get("smart_health", "UNKNOWN")
if "running" in (short, long_):
return "running"
if short == "failed" or long_ == "failed" or health == "FAILED":
return "failed"
if "passed" in (short, long_):
return "passed"
return "idle"
def _format_elapsed(iso: str | None) -> str:
"""Human-readable elapsed time since an ISO timestamp (e.g. '2h 34m')."""
if not iso:
return ""
try:
start = datetime.fromisoformat(iso)
if start.tzinfo is None:
start = start.replace(tzinfo=timezone.utc)
elapsed = int((datetime.now(timezone.utc) - start).total_seconds())
if elapsed < 0:
return ""
h = elapsed // 3600
m = (elapsed % 3600) // 60
s = elapsed % 60
if h > 0:
return f"{h}h {m}m"
if m > 0:
return f"{m}m {s}s"
return f"{s}s"
except Exception:
return ""
# Register filters
templates.env.filters["format_bytes"] = _format_bytes
templates.env.filters["format_eta"] = _format_eta
templates.env.filters["temp_class"] = _temp_class
templates.env.filters["format_dt"] = _format_dt
templates.env.filters["format_dt_full"] = _format_dt_full
templates.env.filters["format_duration"] = _format_duration
templates.env.filters["format_elapsed"] = _format_elapsed
templates.env.globals["drive_status"] = _drive_status
from app.config import settings as _settings
templates.env.globals["app_version"] = _settings.app_version