Server : Apache System : Linux iad1-shared-b8-43 6.6.49-grsec-jammy+ #10 SMP Thu Sep 12 23:23:08 UTC 2024 x86_64 User : dh_edsupp ( 6597262) PHP Version : 8.2.26 Disable Function : NONE Directory : /opt/prometheus-monitoring-scripts/lib/python3.10/site-packages/custom_exporters/dpsnapshot/ |
Upload File : |
""" ZFS Snapshot Monitoring Module. Monitors the last sent ZFS snapshot timestamp and calculates age for alerting. """ from ..base import BaseExporter from ..utils import safe_file_operation import sys import re from datetime import datetime class CustomExporter(BaseExporter): """ Exporter for ZFS snapshot monitoring. Reads snapshot timestamp from /home/.zfs_last_sent_snapshot and exports metrics. """ def metric_config(self): """Define metrics for ZFS snapshot monitoring""" return { "zfs_last_sent_snapshot_timestamp": {"type": "Gauge", "labels": []}, "zfs_last_sent_snapshot_age_seconds": {"type": "Gauge", "labels": []}, } def generate(self): """Generate ZFS snapshot metrics by reading and parsing the snapshot file""" snapshot_timestamp = self.metrics.get('zfs_last_sent_snapshot_timestamp') snapshot_age = self.metrics.get('zfs_last_sent_snapshot_age_seconds') with safe_file_operation(open, '/home/.zfs_last_sent_snapshot', 'r') as f: content = f.read().strip() match = re.match(r'LAST_SENT_SNAPSHOT=(.+?)_(\d{4}-\d{2}-\d{2}_\d{2}:\d{2}:\d{2})$', content) if match: timestamp_str = match.group(2) dt = datetime.strptime(timestamp_str, '%Y-%m-%d_%H:%M:%S') epoch_time = dt.timestamp() current_time = datetime.now().timestamp() age_seconds = current_time - epoch_time snapshot_timestamp.set(epoch_time) snapshot_age.set(age_seconds) else: print(f"Failed to parse snapshot format: {content}", file=sys.stderr)