2023-08-31 12:26:50 +00:00
|
|
|
import psutil
|
|
|
|
|
|
|
|
from ..settings import SETTINGS
|
2023-08-31 15:23:00 +00:00
|
|
|
from ._report import Report, ReportData
|
2023-08-31 12:26:50 +00:00
|
|
|
|
|
|
|
|
2023-08-31 15:23:00 +00:00
|
|
|
def _hwdata() -> list[ReportData]:
|
2023-08-31 12:26:50 +00:00
|
|
|
vmem = psutil.virtual_memory()
|
|
|
|
swap = psutil.swap_memory()
|
|
|
|
|
|
|
|
if SETTINGS.memory.swap == "exclude":
|
2023-08-31 15:23:00 +00:00
|
|
|
return [ReportData(
|
|
|
|
name=SETTINGS.memory.name_ram,
|
|
|
|
value=vmem.percent,
|
|
|
|
)]
|
2023-08-31 12:26:50 +00:00
|
|
|
|
|
|
|
elif SETTINGS.memory.swap == "combine":
|
2023-08-31 15:23:00 +00:00
|
|
|
return [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"
|
2023-08-31 15:23:00 +00:00
|
|
|
return [ReportData(
|
|
|
|
name=SETTINGS.memory.name_ram,
|
|
|
|
value=vmem.percent,
|
|
|
|
), ReportData(
|
|
|
|
name=SETTINGS.memory.name_swap,
|
|
|
|
value=swap.percent,
|
|
|
|
)]
|
2023-08-31 12:26:50 +00:00
|
|
|
|
|
|
|
|
2023-08-31 15:23:00 +00:00
|
|
|
def memory() -> Report | None:
|
|
|
|
if not SETTINGS.memory.enabled:
|
|
|
|
return None
|
2023-08-31 12:26:50 +00:00
|
|
|
|
2023-08-31 15:23:00 +00:00
|
|
|
data = _hwdata()
|
|
|
|
|
|
|
|
return Report.aggregate(
|
|
|
|
settings=SETTINGS.memory,
|
|
|
|
data=data,
|
2023-08-31 12:26:50 +00:00
|
|
|
)
|