#!/usr/bin/env python3 """Collect ZFS metrics for the Prometheus node_exporter textfile collector. node_exporter's built-in zfs collector reads /proc/spl/kstat/zfs, which covers ARC statistics, per-dataset objset counters and pool state -- but nothing about capacity, fragmentation, vdev errors, scrub history, compression, encryption or snapshots. All of that has to come from the zpool/zfs CLIs, which is what this script does. Design notes: * Each collection stage is independent. A stage that fails logs to stderr and simply contributes no samples, rather than taking the whole file down with it -- a broken snapshot listing should not cost you your pool capacity graphs. * Output is written to a temporary file and renamed into place, so node_exporter never reads a half-written file. * Samples are grouped into metric families by a registry rather than printed inline, so the exposition format is correct no matter what order stages emit things in. Run it with --output PATH. Optionally pass --retention label=count (repeatable) to publish the configured auto-snapshot retention targets alongside the observed counts. """ from __future__ import annotations import argparse import os import re import subprocess import sys import time from collections import defaultdict # zpool/zfs report state as a string; dashboards want something orderable. STATE_CODES = { "ONLINE": 0, "DEGRADED": 1, "FAULTED": 2, "OFFLINE": 3, "UNAVAIL": 4, "REMOVED": 5, "SUSPENDED": 6, "AVAIL": 7, "INUSE": 7, } STATE_UNKNOWN = 8 # zfs-auto-snapshot (zfstools) names snapshots "zfs-auto-snap_