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

44 lines
1,010 B
Python
Raw Normal View History

2023-08-31 11:17:19 +00:00
import os
from ..settings import SETTINGS
from ._report import Report
def disk() -> Report | None:
if not SETTINGS.disk.enabled:
return None
2023-08-31 11:44:41 +00:00
def path_to_used_percent(path: os.PathLike) -> float:
2023-08-31 11:17:19 +00:00
try:
sv = os.statvfs(path)
2023-08-31 11:44:41 +00:00
return (1 - sv.f_bavail / sv.f_blocks) * 100
2023-08-31 11:17:19 +00:00
except ZeroDivisionError:
return 0
data = sorted([
2023-08-31 11:44:41 +00:00
(str(path), path_to_used_percent(path))
2023-08-31 11:17:19 +00:00
for path in SETTINGS.disk.paths
2023-08-31 11:44:41 +00:00
], key=lambda d: d[1], reverse=True)
2023-08-31 11:17:19 +00:00
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
),
)