allow per-report settings for external metrics

This commit is contained in:
Jörn-Michael Miehe 2023-09-01 15:18:57 +00:00
parent 4efd957972
commit cc45e8d02a
5 changed files with 47 additions and 14 deletions

View file

@ -8,23 +8,50 @@ from ..settings import SETTINGS, MetricSettings
class ReportData:
name: str
value: float
threshold: float
inverted: bool
format: str
@classmethod
def from_free_total(cls, *, name: str, free: float, total: float) -> Self:
def from_settings(
cls, *,
name: str,
value: float,
settings: MetricSettings,
) -> Self:
return cls(
name=name,
value=(total - free) / total * 100,
value=value,
threshold=settings.threshold,
inverted=settings.inverted,
format=settings.report,
)
def report(self, settings: MetricSettings) -> "Report":
@classmethod
def from_free_total(
cls, *,
name: str,
free: float,
total: float,
settings: MetricSettings,
) -> Self:
return cls.from_settings(
name=name,
value=(total - free) / total * 100,
settings=settings,
)
@property
def report(self) -> "Report":
return Report(
result=settings.report.format(
result=self.format.format(
name=self.name,
value=self.value,
),
failed=(
self.value > settings.threshold and not settings.inverted
or self.value < settings.threshold and settings.inverted
self.value > self.threshold and not self.inverted
or self.value < self.threshold and self.inverted
),
)
@ -70,10 +97,7 @@ class Report:
if not settings.enabled:
return None
reports = [
data.report(settings)
for data in get_data()
]
reports = [data.report for data in get_data()]
return cls(
result=settings.report_outer.format(

View file

@ -7,9 +7,10 @@ from ._report import Report, ReportData
def _hwdata() -> Iterator[ReportData]:
yield ReportData(
yield ReportData.from_settings(
name=SETTINGS.cpu.name,
value=psutil.cpu_percent(interval=1),
settings=SETTINGS.cpu,
)

View file

@ -17,6 +17,7 @@ def _hwdata() -> Iterator[ReportData]:
ReportData.from_free_total(
name=str(path),
**get_path_statvfs(path),
settings=SETTINGS.disk,
) for path in SETTINGS.disk.paths
], key=lambda d: d.value, reverse=True)

View file

@ -8,6 +8,9 @@ def _hwdata() -> Iterator[ReportData]:
yield ReportData(
name="Foo",
value=69.42,
threshold=80,
inverted=False,
format=SETTINGS.cpu.report,
)

View file

@ -11,9 +11,10 @@ def _hwdata() -> Iterator[ReportData]:
swap = psutil.swap_memory()
if SETTINGS.memory.swap == "exclude":
yield ReportData(
yield ReportData.from_settings(
name=SETTINGS.memory.name_ram,
value=vmem.percent,
settings=SETTINGS.memory,
)
elif SETTINGS.memory.swap == "combine":
@ -21,16 +22,19 @@ def _hwdata() -> Iterator[ReportData]:
name=SETTINGS.memory.name,
free=vmem.available + swap.free,
total=vmem.total + swap.total,
settings=SETTINGS.memory,
)
else: # SETTINGS.memory.swap == "include"
yield ReportData(
yield ReportData.from_settings(
name=SETTINGS.memory.name_ram,
value=vmem.percent,
settings=SETTINGS.memory,
)
yield ReportData(
yield ReportData.from_settings(
name=SETTINGS.memory.name_swap,
value=swap.percent,
settings=SETTINGS.memory,
)