kiwi-simple-metrics/kiwi_simple_metrics/metrics/disk.py

43 lines
990 B
Python

import os
from ..settings import SETTINGS
from ._report import Report
def disk() -> Report | None:
if not SETTINGS.disk.enabled:
return None
def path_to_free_percent(path: os.PathLike) -> float:
try:
sv = os.statvfs(path)
return sv.f_bavail / sv.f_blocks * 100
except ZeroDivisionError:
return 0
data = sorted([
(str(path), path_to_free_percent(path))
for path in SETTINGS.disk.paths
], key=lambda d: d[1])
reports = [Report.new(
settings=SETTINGS.disk,
name=path,
value=percent,
) for path, percent in data]
report_inner = ", ".join(
report.result
for report in reports[:SETTINGS.disk.count]
)
return Report(
SETTINGS.disk.report_outer.format(
name=SETTINGS.disk.name,
inner=report_inner,
),
failed=any(
report.failed
for report in reports
),
)