#!/usr/bin/env python3 """Read-only resource checks for the allocated capture host.""" from __future__ import annotations import argparse import json import os from pathlib import Path import shutil import subprocess from common import atomic_json, now_ms, read_json def preflight(config: dict) -> dict: root = Path(config["run_root"]).resolve() if not root.is_dir(): raise ValueError(f"create allocated run_root before preflight: {root}") disk = shutil.disk_usage(root) root_stats = os.statvfs("/") checks = {"data_free": disk.free >= config.get("limits", {}).get("min_free_bytes", 150 * 1024**3), "data_utilization": disk.used / disk.total < 0.8, "root_inode_margin": root_stats.f_favail >= 10000, "rsync_available": shutil.which("rsync") is not None, "binary_available": Path(config["binary"]).is_file(), "all_five_tals": len(config["tals"]) == 5 and all(Path(t).is_file() for t in config["tals"])} service = subprocess.run(["systemctl", "is-active", "panda-rpki.service"], capture_output=True, text=True, timeout=10) checks["old_panda_inactive"] = service.stdout.strip() == "inactive" return {"at_ms": now_ms(), "host": os.uname().nodename, "pass": all(checks.values()), "checks": checks, "data_total": disk.total, "data_free": disk.free, "root_free_inodes": root_stats.f_favail, "cpus": os.cpu_count(), "panda_state": service.stdout.strip(), "config": config} def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--config", type=Path, required=True) parser.add_argument("--out", type=Path, required=True) args = parser.parse_args() result = preflight(read_json(args.config)) atomic_json(args.out, result) print(json.dumps(result)) return 0 if result["pass"] else 2 if __name__ == "__main__": raise SystemExit(main())