mirror of
https://github.com/yavook/kiwi-simple-metrics.git
synced 2024-11-24 08:23:01 +00:00
metrics.memory
This commit is contained in:
parent
a9cf044fe1
commit
49a16f67c3
4 changed files with 70 additions and 0 deletions
|
@ -10,6 +10,7 @@ def main() -> None:
|
||||||
print(metrics.cpu())
|
print(metrics.cpu())
|
||||||
|
|
||||||
# MEM metric
|
# MEM metric
|
||||||
|
print(metrics.memory())
|
||||||
|
|
||||||
# DISK metric
|
# DISK metric
|
||||||
print(metrics.disk())
|
print(metrics.disk())
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
from .cpu import cpu
|
from .cpu import cpu
|
||||||
from .disk import disk
|
from .disk import disk
|
||||||
|
from .memory import memory
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
"cpu",
|
"cpu",
|
||||||
"disk",
|
"disk",
|
||||||
|
"memory",
|
||||||
]
|
]
|
||||||
|
|
56
kiwi_simple_metrics/metrics/memory.py
Normal file
56
kiwi_simple_metrics/metrics/memory.py
Normal 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
|
||||||
|
),
|
||||||
|
)
|
|
@ -35,6 +35,17 @@ class MultiMS(MetricSettings):
|
||||||
class MemoryMS(MultiMS):
|
class MemoryMS(MultiMS):
|
||||||
name: str = "Memory"
|
name: str = "Memory"
|
||||||
threshold: float = 90
|
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):
|
class DiskMS(MultiMS):
|
||||||
|
|
Loading…
Reference in a new issue