From a7f38335e5fed5a98bfaad64ab40544124337224 Mon Sep 17 00:00:00 2001 From: Tim Keller Date: Thu, 27 Aug 2026 10:13:13 -0500 Subject: initial commit --- collectors/zfs-metrics.py | 610 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 610 insertions(+) create mode 100644 collectors/zfs-metrics.py (limited to 'collectors/zfs-metrics.py') diff --git a/collectors/zfs-metrics.py b/collectors/zfs-metrics.py new file mode 100644 index 0000000..b313743 --- /dev/null +++ b/collectors/zfs-metrics.py @@ -0,0 +1,610 @@ +#!/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_