iac/nixos/modules/restic-alert.py
Sergei Poljanski 71047d179d
Some checks failed
nixos / asxpio-services (push) Failing after 54s
ci: run Renovate from asxpi/renovate
Renovate now runs from its own repository for every repo, with the
shared rules in its preset. Extend that preset here and keep only what
is specific to this repo (the Ansible var manager, the Paper JRE, the
runner and nix notes); drop the workflow that ran it for iac alone.

Assisted-by: Claude:opus-5-5
2026-11-13 00:00:06 +04:00

20 lines
784 B
Python

# Mail the tail of a failed unit's journal.
# Usage: restic-alert <unit> <env file>
import os, smtplib, socket, subprocess, sys
from email.message import EmailMessage
unit = sys.argv[1]
env = dict(l.rstrip("\n").split("=", 1) for l in open(sys.argv[2]) if "=" in l)
log = subprocess.run(["journalctl", "-u", unit, "-n", "60", "--no-pager", "-o", "cat"],
capture_output=True, text=True).stdout
msg = EmailMessage()
msg["Subject"] = f"[{socket.gethostname()}] {unit} failed"
msg["From"] = env["ALERT_SMTP_USER"]
msg["To"] = env["ALERT_RECIPIENT"]
msg.set_content(log)
with smtplib.SMTP(env["ALERT_SMTP_HOST"], int(env["ALERT_SMTP_PORT"]), timeout=30) as s:
s.starttls()
s.login(env["ALERT_SMTP_USER"], env["ALERT_SMTP_PASSWORD"])
s.send_message(msg)