commit a36cfc9c10ef7aecb4d5541b9f645416da933134 Author: pi3c Date: Thu Apr 11 01:35:39 2024 +0300 first commit diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/mem_checker/__init__.py b/mem_checker/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/mem_checker/mem_checker.py b/mem_checker/mem_checker.py new file mode 100644 index 0000000..ff4c620 --- /dev/null +++ b/mem_checker/mem_checker.py @@ -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() diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..a2792d5 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,14 @@ +[tool.poetry] +name = "mem-checker" +version = "0.1.0" +description = "" +authors = ["pi3c "] +readme = "README.md" + +[tool.poetry.dependencies] +python = "^3.10" + + +[build-system] +requires = ["poetry-core"] +build-backend = "poetry.core.masonry.api" diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29