mirror of
https://github.com/yavook/kiwi-simple-metrics.git
synced 2024-11-21 23:32:59 +00:00
metrics.disk_metric ("failed" is buggy)
This commit is contained in:
parent
1854c4d40e
commit
8eb47e200d
3 changed files with 41 additions and 23 deletions
|
@ -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__":
|
||||
|
|
|
@ -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),
|
||||
)
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in a new issue