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

46 lines
1.1 KiB
Python
Raw Permalink Normal View History

2023-08-31 11:17:19 +00:00
import os
2023-09-02 19:06:48 +00:00
from pathlib import PurePath
from typing import Iterator
2023-08-31 11:17:19 +00:00
from ..settings import SETTINGS
from ._report import Report, ReportData
def _hwdata() -> Iterator[ReportData]:
2023-09-02 19:06:48 +00:00
def get_path_statvfs(path: os.PathLike) -> dict[str, int]:
sv = os.statvfs(path)
return {
"free": sv.f_bavail,
"total": sv.f_blocks,
}
2023-09-02 19:06:48 +00:00
def get_path_name(path: os.PathLike) -> str:
# get path and its parents
path = PurePath(path)
# if path or above is the vroot, make it a "virtual absolute" path
if SETTINGS.disk.vroot in [path, *path.parents]:
path = "/" / path.relative_to(SETTINGS.disk.vroot)
return str(path)
2023-12-30 17:23:55 +00:00
yield from sorted(
[
ReportData.from_free_total(
name=get_path_name(path),
**get_path_statvfs(path),
settings=SETTINGS.disk,
)
for path in SETTINGS.disk.paths
],
key=lambda d: d.value,
reverse=True,
)
2023-08-31 11:17:19 +00:00
def disk() -> Report | None:
return Report.aggregate(
2023-08-31 11:17:19 +00:00
settings=SETTINGS.disk,
get_data=_hwdata,
2023-08-31 11:17:19 +00:00
)