33 lines
1.2 KiB
Python
33 lines
1.2 KiB
Python
"""Reuse the Telegraf collector's parsing; MQTT transports measurements only."""
|
|
import os
|
|
from pathlib import Path
|
|
import subprocess
|
|
|
|
TYPES = ('replicas', 'checksum', 'erasure_code', 'compression', 'target', 'high_priority', 'pending', 'stripes')
|
|
FIELDS = tuple(f'{kind}_{part}' for part in ('data', 'metadata') for kind in TYPES)
|
|
|
|
|
|
def parse_metrics(line):
|
|
lines = line.strip().splitlines()
|
|
if len(lines) != 1 or not lines[0].startswith('bcachefs_reconcile,'):
|
|
raise ValueError('Expected one reconcile measurement')
|
|
values = {}
|
|
for field in lines[0].rsplit(' ', 1)[1].split(','):
|
|
key, value = field.split('=', 1)
|
|
if key in FIELDS:
|
|
if not value.isdecimal():
|
|
raise ValueError(f'Invalid byte count for {key}')
|
|
values[key] = int(value)
|
|
if not values:
|
|
raise ValueError('No reconcile byte measurements')
|
|
return values
|
|
|
|
|
|
def read_metrics(uuid, mount):
|
|
result = subprocess.run(
|
|
[str(Path(__file__).with_name('bcachefs_reconcile.sh')), mount],
|
|
env=dict(os.environ, BCACHEFS_UUID=uuid), capture_output=True, text=True,
|
|
check=True, timeout=15,
|
|
)
|
|
return parse_metrics(result.stdout)
|