diff --git a/config/.env-template b/config/.env-template index cf0bd90..5816293 100755 --- a/config/.env-template +++ b/config/.env-template @@ -6,6 +6,9 @@ CFG__ZABBIX__TOKEN=string CFG__ZABBIX__MIN_SEVERITY=0 CFG__ZABBIX__UPD_INTERVAL=60 CFG__ZABBIX__ALERT_FAIL_COUNT=3 +CFG__ZABBIX__ALERT_TREAD_ID=0 +CFG__ZABBIX__ALERT_TEXT_UP=Zabbix service UP +CFG__ZABBIX__ALERT_TEXT_DOWN=Zabbix service DOWN CFG__TGBOT__TOKEN=string CFG__TGBOT__CHAT_ID=00000000 diff --git a/config/config.py b/config/config.py index d41d4e4..3720822 100755 --- a/config/config.py +++ b/config/config.py @@ -50,7 +50,11 @@ class ZabbixConfig(BaseModel): min_severity: int upd_interval: int alert_fail_count: int - current_fail: int = 0 + alert_tread_id: int + alert_text_up: str + alert_text_down: str + change_state_count: int = 0 + api_state: str = "UP" class Settings(BaseSettings): diff --git a/docker/docker-compose.yaml b/docker/docker-compose.yaml index b9a6426..b752209 100644 --- a/docker/docker-compose.yaml +++ b/docker/docker-compose.yaml @@ -22,6 +22,10 @@ services: - CFG__ZABBIX__TOKEN=string - CFG__ZABBIX__MIN_SEVERITY=2 - CFG__ZABBIX__UPD_INTERVAL=30 + - CFG__ZABBIX__ALERT_FAIL_COUNT=3 + - CFG__ZABBIX__ALERT_TREAD_ID=0 + - CFG__ZABBIX__ALERT_TEXT_UP=Zabbix service UP + - CFG__ZABBIX__ALERT_TEXT_DOWN=Zabbix service DOWN - CFG__TGBOT__TOKEN=string - CFG__TGBOT__CHAT_ID=000000 diff --git a/telegram/__init__.py b/telegram/__init__.py index 3427200..0215468 100644 --- a/telegram/__init__.py +++ b/telegram/__init__.py @@ -1,7 +1,7 @@ from .message import ( send_message, del_message, - edit_message, + send_zabbix_api_alert, ) from .bot import start_bot @@ -9,5 +9,5 @@ __all__ = [ "send_message", "del_message", "start_bot", - "edit_message", + "send_zabbix_api_alert", ] diff --git a/telegram/message.py b/telegram/message.py index c6c0650..ee264da 100644 --- a/telegram/message.py +++ b/telegram/message.py @@ -45,38 +45,6 @@ async def send_message(message: str, event_id: int) -> dict: return {"status": e} -async def edit_message( - message_id: int, - new_message: str, -) -> dict: - url = f"https://api.telegram.org/bot{conf.tgbot.token}/editMessageText" - async with aiohttp.ClientSession() as session: - try: - async with session.post( - url, - json={ - "chat_id": conf.tgbot.chat_id, - "message_id": message_id, - "text": new_message, - }, - ) as response: - if response.status == 200: - log.info(f"Message ID {message_id} edited") - return { - "status": response.status, - } - else: - log.warning( - f"Message ID {message_id} NOT edited. Response status: {response.status}" - ) - return { - "status": response.status, - } - except Exception as e: - log.warning(f"Exception: {e}") - return {"status": e} - - async def del_message( message_id: int, ) -> dict: @@ -99,9 +67,36 @@ async def del_message( log.warning( f"Message ID {message_id} NOT deleted. Response status: {response.status}" ) + return { "status": response.status, } except Exception as e: log.warning(f"Exception: {e}") return {"status": e} + + +async def send_zabbix_api_alert(message): + url = f"https://api.telegram.org/bot{conf.tgbot.token}/sendMessage" + params = { + "chat_id": conf.tgbot.chat_id, + "message_thread_id": conf.zabbix.alert_tread_id, + "text": message, + } + async with aiohttp.ClientSession() as session: + try: + async with session.post( + url, + json=params, + ) as response: + log.info(f"Response status: {response.status}") + resp = await response.json() + if response.status == 200: + log.info(f"Message with ID: {resp['result']['message_id']} send") + return {"status": response.status} + else: + log.warning(f"Message not send. Response status: {response.status}") + return {"status": response.status} + except Exception as e: + log.warning(f"Exception: {e}") + return {"status": e} diff --git a/zabbix/zabbix_api.py b/zabbix/zabbix_api.py index 4f345a9..668ed60 100644 --- a/zabbix/zabbix_api.py +++ b/zabbix/zabbix_api.py @@ -3,11 +3,45 @@ import logging as log from pyexpat.errors import messages from zabbix_utils import ZabbixAPI +import telegram from config import conf from datetime import datetime, timedelta +def check_state(success: bool) -> None: + if success: + if conf.zabbix.api_state == "UP": + log.info("Zabbix API is UP") + conf.zabbix.change_state_count = 0 + else: + if conf.zabbix.change_state_count == conf.zabbix.alert_fail_count: + log.warning( + f"Zabbix API state changed to UP. Count {conf.zabbix.change_state_count}" + ) + send_result = telegram.send_zabbix_api_alert(conf.zabbix.alert_text_up) + if send_result["status"] == 200: + conf.zabbix.change_state_count = 0 + else: + conf.zabbix.change_state_count += 1 + else: + if conf.zabbix.api_state == "DOWN": + log.info("Zabbix API is DOWN") + conf.zabbix.change_state_count = 0 + else: + if conf.zabbix.change_state_count == conf.zabbix.alert_fail_count: + log.warning( + f"Zabbix API state changed to DOWN. Count {conf.zabbix.change_state_count}" + ) + send_result = telegram.send_zabbix_api_alert( + conf.zabbix.alert_text_down + ) + if send_result["status"] == 200: + conf.zabbix.change_state_count = 0 + else: + conf.zabbix.change_state_count += 1 + + def get_active_problems() -> dict: api = ZabbixAPI(url=conf.zabbix.url, token=conf.zabbix.token) try: @@ -40,16 +74,11 @@ def get_active_problems() -> dict: event["host"] = event.pop("hosts", None)[0]["host"] events_dict[event["eventid"]] = event events_dict["event_ids"].append(event["eventid"]) - if conf.zabbix.current_fail: - conf.zabbix.current_fail = 0 + check_state(True) return events_dict except: log.warning("Get event from zabbix error") - if conf.zabbix.current_fail == conf.zabbix.alert_fail_count: - log.warning(f"Zabbix connection error. Count {conf.zabbix.current_fail}") - else: - conf.zabbix.current_fail += 1 - log.warning(f"Zabbix connection error. Count {conf.zabbix.current_fail}") + check_state(False) def event_acknowledge(event_id: int, mute_time: int, muted_by: str):