main
Сергей Ванюшкин 2024-04-16 05:29:17 +03:00
parent d5426a0240
commit 6fedae703a
1 changed files with 29 additions and 12 deletions

View File

@ -18,7 +18,9 @@ The request contains the following json format:
""" """
import json import json
import logging
import subprocess import subprocess
import sys
import urllib.parse import urllib.parse
import urllib.request import urllib.request
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
@ -28,10 +30,21 @@ from time import sleep
ENDPOINT_URL = "http://127.0.0.1:8000/user" # endpoint for send alerts ENDPOINT_URL = "http://127.0.0.1:8000/user" # endpoint for send alerts
FREE_IN_MB = 500 # free: the value of the trigger in megabytes FREE_IN_MB = 500 # free: the value of the trigger in megabytes
FREE_IN_GB = 0.5 # free: the value of the trigger in gigabytes FREE_IN_GB = 0.5 # free: the value of the trigger in gigabytes
DF_IN_PERCENT = 90 # df: the value of the trigger in percents DF_IN_PERCENT = 50 # df: the value of the trigger in percents
SLEEPTIME = 5 # main_loop sleep time per seconds SLEEPTIME = 5 # main_loop sleep time per seconds
logger = logging.getLogger("mem_checker_logger")
logger.setLevel(logging.DEBUG)
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.DEBUG)
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
console_handler.setFormatter(formatter)
# Добавляем обработчик в логгер
logger.addHandler(console_handler)
@dataclass() @dataclass()
class MessageDTO: class MessageDTO:
type_msg: str type_msg: str
@ -108,6 +121,7 @@ class FreeMem(Command):
memory = memory[:-2].replace(",", ".") memory = memory[:-2].replace(",", ".")
memory_value = float(memory) if "." in memory else int(memory) memory_value = float(memory) if "." in memory else int(memory)
except Exception as e: except Exception as e:
logger.error(e)
return [ return [
MessageDTO( MessageDTO(
type_msg="ParserError", type_msg="ParserError",
@ -157,15 +171,17 @@ def send_alert(alerts: list[MessageDTO]) -> None:
headers={"Content-Type": "application/json"}, headers={"Content-Type": "application/json"},
method="POST", method="POST",
) )
try:
with urllib.request.urlopen(req) as response: with urllib.request.urlopen(req) as response:
response.read().decode("utf-8") response.read().decode("utf-8")
except Exception as e:
logger.error(e)
def main_loop() -> None: def main_loop() -> None:
free_mem = FreeMem() free_mem = FreeMem()
hdd_mem = FreeHdd() hdd_mem = FreeHdd()
while True:
alerts: list[MessageDTO] = list() alerts: list[MessageDTO] = list()
alerts.extend(free_mem.get_alerts()) alerts.extend(free_mem.get_alerts())
@ -174,8 +190,9 @@ def main_loop() -> None:
if alerts: if alerts:
send_alert(alerts=alerts) send_alert(alerts=alerts)
sleep(SLEEPTIME)
if __name__ == "__main__": if __name__ == "__main__":
if "--test-mode" in sys.argv:
while True:
main_loop() main_loop()
sleep(SLEEPTIME)