From 8eb47e200da548583c27ffaba5704094931ead8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Thu, 31 Aug 2023 09:45:10 +0000 Subject: [PATCH] metrics.disk_metric ("failed" is buggy) --- kiwi_simple_metrics/main.py | 23 ++----------------- kiwi_simple_metrics/metrics.py | 39 ++++++++++++++++++++++++++++++++- kiwi_simple_metrics/settings.py | 2 +- 3 files changed, 41 insertions(+), 23 deletions(-) diff --git a/kiwi_simple_metrics/main.py b/kiwi_simple_metrics/main.py index c189eb7..9f2840b 100644 --- a/kiwi_simple_metrics/main.py +++ b/kiwi_simple_metrics/main.py @@ -1,7 +1,4 @@ -import os -import shutil - -from .metrics import cpu_metric +from .metrics import cpu_metric, disk_metric from .settings import SETTINGS @@ -15,23 +12,7 @@ def main() -> None: # MEM metric # DISK metric - # TODO test this using timeit - for path in SETTINGS.disk.paths: - try: - sv = os.statvfs(path) - percent = sv.f_bavail / sv.f_blocks * 100 - except ZeroDivisionError: - percent = 0 - - print(f"{path} Free (sv): {percent:.2f} %") - - try: - total, _, free = shutil.disk_usage(path) - percent = free / total * 100 - except ZeroDivisionError: - percent = 0 - - print(f"{path} Free (du): {percent:.2f} %") + print(disk_metric()) if __name__ == "__main__": diff --git a/kiwi_simple_metrics/metrics.py b/kiwi_simple_metrics/metrics.py index a790536..4dac2c3 100644 --- a/kiwi_simple_metrics/metrics.py +++ b/kiwi_simple_metrics/metrics.py @@ -1,3 +1,4 @@ +import os from dataclasses import dataclass, field import psutil @@ -12,6 +13,7 @@ class Report: def _report( + *, settings: MetricSettings, name: str, value: float, @@ -33,4 +35,39 @@ def _report( def cpu_metric() -> Report | None: value = psutil.cpu_percent(interval=1) - return _report(SETTINGS.cpu, "CPU", value) + return _report( + settings=SETTINGS.cpu, + name="CPU", + value=value, + ) + + +def disk_metric() -> Report | None: + if not SETTINGS.disk.enabled: + return None + + def vfs_to_percent(sv: os.statvfs_result) -> float: + try: + return sv.f_bavail / sv.f_blocks * 100 + except ZeroDivisionError: + return 0 + + data = sorted([ + (str(path), vfs_to_percent(os.statvfs(path))) + for path in SETTINGS.disk.paths + ], key=lambda d: d[1]) + + reports = [ + report + for path, percent in data + if (report := _report( + settings=SETTINGS.disk, + name=path, + value=percent, + )) is not None + ] + + return Report( + ", ".join(report.result for report in reports), + failed=any(report.failed for report in reports), + ) diff --git a/kiwi_simple_metrics/settings.py b/kiwi_simple_metrics/settings.py index a92159d..260679c 100644 --- a/kiwi_simple_metrics/settings.py +++ b/kiwi_simple_metrics/settings.py @@ -31,7 +31,7 @@ class Settings(BaseSettings): cpu: MetricSettings = MetricSettings(threshold=math.inf) memory: MetricSettings = MetricSettings(threshold=90) - disk: DiskMS = DiskMS(threshold=85) + disk: DiskMS = DiskMS(threshold=15, inverted=True) SETTINGS = Settings()