metrics.memory

This commit is contained in:
Jörn-Michael Miehe 2023-08-31 12:26:50 +00:00
parent a9cf044fe1
commit 49a16f67c3
4 changed files with 70 additions and 0 deletions

View file

@ -10,6 +10,7 @@ def main() -> None:
print(metrics.cpu())
# MEM metric
print(metrics.memory())
# DISK metric
print(metrics.disk())

View file

@ -1,7 +1,9 @@
from .cpu import cpu
from .disk import disk
from .memory import memory
__all__ = [
"cpu",
"disk",
"memory",
]

View file

@ -0,0 +1,56 @@
import psutil
from ..settings import SETTINGS
from ._report import Report
def memory() -> Report | None:
if not SETTINGS.memory.enabled:
return None
def get_used_percent(free: float, total: float) -> float:
return (total - free) / total * 100
vmem = psutil.virtual_memory()
swap = psutil.swap_memory()
if SETTINGS.memory.swap == "exclude":
data = {
SETTINGS.memory.name_ram: vmem.percent,
}
elif SETTINGS.memory.swap == "combine":
data = {
SETTINGS.memory.name: get_used_percent(
vmem.available + swap.free,
vmem.total + swap.total,
)
}
else:
data = {
SETTINGS.memory.name_ram: vmem.percent,
SETTINGS.memory.name_swap: swap.percent,
}
reports = [Report.new(
settings=SETTINGS.memory,
name=name,
value=value,
) for name, value in data.items()]
report_inner = ", ".join(
report.result
for report in reports
)
return Report(
SETTINGS.memory.report_outer.format(
name=SETTINGS.memory.name,
inner=report_inner,
),
failed=any(
report.failed
for report in reports
),
)

View file

@ -35,6 +35,17 @@ class MultiMS(MetricSettings):
class MemoryMS(MultiMS):
name: str = "Memory"
threshold: float = 90
report_outer: str = "{inner}"
# how to handle swap space
# exclude: swap space is not reported
# include: swap space is reported separately
# combine: ram and swap are combined
swap: Literal["exclude", "include", "combine"] = "include"
# names for telling apart ram and swap space
name_ram: str = "RAM"
name_swap: str = "Swap"
class DiskMS(MultiMS):