kiwi-simple-metrics/kiwi_simple_metrics/metrics/memory.py

42 lines
1,001 B
Python
Raw Normal View History

from typing import Iterator
2023-08-31 12:26:50 +00:00
import psutil
from ..settings import SETTINGS
from ._report import Report, ReportData
2023-08-31 12:26:50 +00:00
def _hwdata() -> Iterator[ReportData]:
2023-08-31 12:26:50 +00:00
vmem = psutil.virtual_memory()
swap = psutil.swap_memory()
if SETTINGS.memory.swap == "exclude":
yield ReportData(
name=SETTINGS.memory.name_ram,
value=vmem.percent,
)
2023-08-31 12:26:50 +00:00
elif SETTINGS.memory.swap == "combine":
yield ReportData.from_free_total(
name=SETTINGS.memory.name,
free=vmem.available + swap.free,
total=vmem.total + swap.total,
)
2023-08-31 12:26:50 +00:00
2023-08-31 12:31:48 +00:00
else: # SETTINGS.memory.swap == "include"
yield ReportData(
name=SETTINGS.memory.name_ram,
value=vmem.percent,
)
yield ReportData(
name=SETTINGS.memory.name_swap,
value=swap.percent,
)
2023-08-31 12:26:50 +00:00
def memory() -> Report | None:
return Report.aggregate(
settings=SETTINGS.memory,
get_data=_hwdata,
2023-08-31 12:26:50 +00:00
)