mirror of
https://github.com/yavook/kiwi-simple-metrics.git
synced 2024-11-24 08:23:01 +00:00
allow per-report settings for external metrics
This commit is contained in:
parent
4efd957972
commit
cc45e8d02a
5 changed files with 47 additions and 14 deletions
|
@ -8,23 +8,50 @@ from ..settings import SETTINGS, MetricSettings
|
||||||
class ReportData:
|
class ReportData:
|
||||||
name: str
|
name: str
|
||||||
value: float
|
value: float
|
||||||
|
threshold: float
|
||||||
|
inverted: bool
|
||||||
|
format: str
|
||||||
|
|
||||||
@classmethod
|
@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(
|
return cls(
|
||||||
name=name,
|
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(
|
return Report(
|
||||||
result=settings.report.format(
|
result=self.format.format(
|
||||||
name=self.name,
|
name=self.name,
|
||||||
value=self.value,
|
value=self.value,
|
||||||
),
|
),
|
||||||
failed=(
|
failed=(
|
||||||
self.value > settings.threshold and not settings.inverted
|
self.value > self.threshold and not self.inverted
|
||||||
or self.value < settings.threshold and settings.inverted
|
or self.value < self.threshold and self.inverted
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -70,10 +97,7 @@ class Report:
|
||||||
if not settings.enabled:
|
if not settings.enabled:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
reports = [
|
reports = [data.report for data in get_data()]
|
||||||
data.report(settings)
|
|
||||||
for data in get_data()
|
|
||||||
]
|
|
||||||
|
|
||||||
return cls(
|
return cls(
|
||||||
result=settings.report_outer.format(
|
result=settings.report_outer.format(
|
||||||
|
|
|
@ -7,9 +7,10 @@ from ._report import Report, ReportData
|
||||||
|
|
||||||
|
|
||||||
def _hwdata() -> Iterator[ReportData]:
|
def _hwdata() -> Iterator[ReportData]:
|
||||||
yield ReportData(
|
yield ReportData.from_settings(
|
||||||
name=SETTINGS.cpu.name,
|
name=SETTINGS.cpu.name,
|
||||||
value=psutil.cpu_percent(interval=1),
|
value=psutil.cpu_percent(interval=1),
|
||||||
|
settings=SETTINGS.cpu,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -17,6 +17,7 @@ def _hwdata() -> Iterator[ReportData]:
|
||||||
ReportData.from_free_total(
|
ReportData.from_free_total(
|
||||||
name=str(path),
|
name=str(path),
|
||||||
**get_path_statvfs(path),
|
**get_path_statvfs(path),
|
||||||
|
settings=SETTINGS.disk,
|
||||||
) for path in SETTINGS.disk.paths
|
) for path in SETTINGS.disk.paths
|
||||||
], key=lambda d: d.value, reverse=True)
|
], key=lambda d: d.value, reverse=True)
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,9 @@ def _hwdata() -> Iterator[ReportData]:
|
||||||
yield ReportData(
|
yield ReportData(
|
||||||
name="Foo",
|
name="Foo",
|
||||||
value=69.42,
|
value=69.42,
|
||||||
|
threshold=80,
|
||||||
|
inverted=False,
|
||||||
|
format=SETTINGS.cpu.report,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -11,9 +11,10 @@ def _hwdata() -> Iterator[ReportData]:
|
||||||
swap = psutil.swap_memory()
|
swap = psutil.swap_memory()
|
||||||
|
|
||||||
if SETTINGS.memory.swap == "exclude":
|
if SETTINGS.memory.swap == "exclude":
|
||||||
yield ReportData(
|
yield ReportData.from_settings(
|
||||||
name=SETTINGS.memory.name_ram,
|
name=SETTINGS.memory.name_ram,
|
||||||
value=vmem.percent,
|
value=vmem.percent,
|
||||||
|
settings=SETTINGS.memory,
|
||||||
)
|
)
|
||||||
|
|
||||||
elif SETTINGS.memory.swap == "combine":
|
elif SETTINGS.memory.swap == "combine":
|
||||||
|
@ -21,16 +22,19 @@ def _hwdata() -> Iterator[ReportData]:
|
||||||
name=SETTINGS.memory.name,
|
name=SETTINGS.memory.name,
|
||||||
free=vmem.available + swap.free,
|
free=vmem.available + swap.free,
|
||||||
total=vmem.total + swap.total,
|
total=vmem.total + swap.total,
|
||||||
|
settings=SETTINGS.memory,
|
||||||
)
|
)
|
||||||
|
|
||||||
else: # SETTINGS.memory.swap == "include"
|
else: # SETTINGS.memory.swap == "include"
|
||||||
yield ReportData(
|
yield ReportData.from_settings(
|
||||||
name=SETTINGS.memory.name_ram,
|
name=SETTINGS.memory.name_ram,
|
||||||
value=vmem.percent,
|
value=vmem.percent,
|
||||||
|
settings=SETTINGS.memory,
|
||||||
)
|
)
|
||||||
yield ReportData(
|
yield ReportData.from_settings(
|
||||||
name=SETTINGS.memory.name_swap,
|
name=SETTINGS.memory.name_swap,
|
||||||
value=swap.percent,
|
value=swap.percent,
|
||||||
|
settings=SETTINGS.memory,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue