first commit
commit
a36cfc9c10
|
@ -0,0 +1,63 @@
|
|||
import subprocess
|
||||
from time import sleep
|
||||
|
||||
|
||||
def send_alert(alerts: list) -> None:
|
||||
print(*alerts, sep="\n")
|
||||
|
||||
|
||||
def shell_command_result(command: list) -> str:
|
||||
process = subprocess.run(
|
||||
command,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE,
|
||||
check=True,
|
||||
text=True,
|
||||
)
|
||||
print("stderr", process.stderr)
|
||||
return process.stdout
|
||||
|
||||
|
||||
def get_free_mem() -> tuple[int | float, str]:
|
||||
stdout: str = shell_command_result(command=["free", "-h"])
|
||||
memory = stdout.split()[9]
|
||||
memory_units = memory[-2:]
|
||||
memory = memory[:-2].replace(",", ".")
|
||||
memory_value = float(memory) if "." in memory else int(memory)
|
||||
return memory_value, memory_units
|
||||
|
||||
|
||||
def get_hdd_free_mem() -> list[tuple[str, str]]:
|
||||
stdout: str = shell_command_result(command=["df", "-h"])
|
||||
rows = [row.split() for row in stdout.split("\n")]
|
||||
mount_points = [(row[5], row[4]) for row in rows[1:] if row]
|
||||
return mount_points
|
||||
|
||||
|
||||
def main_loop() -> None:
|
||||
while True:
|
||||
alerts = list()
|
||||
|
||||
mem_val, mem_units = get_free_mem()
|
||||
if mem_val < 500 and mem_units == "Gi":
|
||||
alerts.append(
|
||||
{
|
||||
"msg": "free memory alert",
|
||||
"val": f"{mem_val}{mem_units}",
|
||||
"units": None,
|
||||
}
|
||||
)
|
||||
hdd_mem = get_hdd_free_mem()
|
||||
for item in hdd_mem:
|
||||
if int(item[1][:-1]) > 50:
|
||||
alerts.append(
|
||||
{"msg": "Hdd memory alert", "val": item[1], "units": item[0]}
|
||||
)
|
||||
if alerts:
|
||||
send_alert(alerts)
|
||||
|
||||
sleep(1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main_loop()
|
|
@ -0,0 +1,14 @@
|
|||
[tool.poetry]
|
||||
name = "mem-checker"
|
||||
version = "0.1.0"
|
||||
description = ""
|
||||
authors = ["pi3c <pi3c@yandex.ru>"]
|
||||
readme = "README.md"
|
||||
|
||||
[tool.poetry.dependencies]
|
||||
python = "^3.10"
|
||||
|
||||
|
||||
[build-system]
|
||||
requires = ["poetry-core"]
|
||||
build-backend = "poetry.core.masonry.api"
|
Loading…
Reference in New Issue