diff --git a/config/.env-template b/config/.env-template index 5816293..cfdacf2 100755 --- a/config/.env-template +++ b/config/.env-template @@ -7,6 +7,7 @@ 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_TAG_USER=string CFG__ZABBIX__ALERT_TEXT_UP=Zabbix service UP CFG__ZABBIX__ALERT_TEXT_DOWN=Zabbix service DOWN diff --git a/config/config.py b/config/config.py index 3720822..793e476 100755 --- a/config/config.py +++ b/config/config.py @@ -51,6 +51,7 @@ class ZabbixConfig(BaseModel): upd_interval: int alert_fail_count: int alert_tread_id: int + alert_tag_user: str = "" alert_text_up: str alert_text_down: str change_state_count: int = 0 diff --git a/docker/docker-compose.yaml b/docker/docker-compose.yaml index b752209..89b00e9 100644 --- a/docker/docker-compose.yaml +++ b/docker/docker-compose.yaml @@ -12,6 +12,8 @@ services: tg-bot: image: git.sm8255082.ru/osnova/zbx-tg-bot:2.0.1 restart: always + volumes: + - ./logfile.log:/app/logfile.log depends_on: - redis environment: diff --git a/main.py b/main.py index 125ebe7..adeca18 100644 --- a/main.py +++ b/main.py @@ -1,5 +1,5 @@ import logging as log -from zabbix import get_active_problems +from zabbix import get_active_problems, check_state from config import conf, icon_dict from redis_db import ( get_all_keys, @@ -8,13 +8,24 @@ from redis_db import ( del_value, ) import asyncio -from telegram import del_message, send_message, start_bot +from telegram import del_message, send_message, start_bot, send_zabbix_api_alert async def dashboard(): active_alerts = get_active_problems() if active_alerts is None: + if check_state(False) == "DOWN": + send_state = await send_zabbix_api_alert(conf.zabbix.alert_text_down) + if send_state["status"] == 200: + conf.zabbix.api_state = "DOWN" + conf.zabbix.change_state_count = 0 return + else: + if check_state(True) == "UP": + send_state = await send_zabbix_api_alert(conf.zabbix.alert_text_up) + if send_state["status"] == 200: + conf.zabbix.api_state = "UP" + conf.zabbix.change_state_count = 0 telegram_alerts = await get_all_keys() new_alerts_id = [] diff --git a/telegram/bot.py b/telegram/bot.py index 3d911ac..a800917 100644 --- a/telegram/bot.py +++ b/telegram/bot.py @@ -2,7 +2,8 @@ from config import conf from aiogram import Bot, Dispatcher, types from aiogram.fsm.storage.memory import MemoryStorage from aiogram import F -from zabbix import event_acknowledge, event_close +from zabbix import event_close +from zabbix import event_acknowledge import logging as log tg_bot = Bot(token=conf.tgbot.token) diff --git a/telegram/message.py b/telegram/message.py index ee264da..29109c8 100644 --- a/telegram/message.py +++ b/telegram/message.py @@ -81,7 +81,7 @@ async def send_zabbix_api_alert(message): params = { "chat_id": conf.tgbot.chat_id, "message_thread_id": conf.zabbix.alert_tread_id, - "text": message, + "text": conf.zabbix.alert_tag_user + "\n" + message, } async with aiohttp.ClientSession() as session: try: diff --git a/zabbix/__init__.py b/zabbix/__init__.py index 7a4de3f..e53c8e7 100644 --- a/zabbix/__init__.py +++ b/zabbix/__init__.py @@ -1,8 +1,8 @@ -from .zabbix_api import get_active_problems, event_acknowledge, event_close - +from .zabbix_api import get_active_problems, event_close, event_acknowledge, check_state __all__ = [ "get_active_problems", "event_acknowledge", "event_close", + "check_state", ] diff --git a/zabbix/zabbix_api.py b/zabbix/zabbix_api.py index 668ed60..8e41348 100644 --- a/zabbix/zabbix_api.py +++ b/zabbix/zabbix_api.py @@ -3,45 +3,12 @@ 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: @@ -74,11 +41,9 @@ 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"]) - check_state(True) return events_dict except: log.warning("Get event from zabbix error") - check_state(False) def event_acknowledge(event_id: int, mute_time: int, muted_by: str): @@ -116,3 +81,30 @@ def event_close(event_id: int, closed_by: str): except: log.warning(f"Closed event {event_id} from zabbix error") return False + + +def check_state(success: bool) -> str | 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 - 1: + log.warning( + f"Zabbix API state changed to UP. Count {conf.zabbix.change_state_count}" + ) + return "UP" + 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 - 1: + log.warning( + f"Zabbix API state changed to DOWN. Count {conf.zabbix.change_state_count}" + ) + return "DOWN" + else: + conf.zabbix.change_state_count += 1