Compare commits

...

5 Commits

Author SHA1 Message Date
b7433f0c99 chore(config): unify timezone setting using TZ environment variable
All checks were successful
Build and Push Docker Images / build (push) Successful in 47s
Replaced multiple timezone definitions with a single environment variable `TZ` to centralize and simplify timezone configuration across the project.

Signed-off-by: UdoChudo <stream@udochudo.ru>
2025-06-17 23:50:38 +05:00
1169605e6e fix(subscription): handle missing MessageID during subscription flow
Fixed a bug where the subscription handler failed when MessageID was not properly received, causing the process to break or behave unexpectedly.

Signed-off-by: UdoChudo <stream@udochudo.ru>
2025-06-17 23:49:28 +05:00
d9df449a17 refactor(alerts): improve active problem fetching and message formatting
Reworked the logic for retrieving data from Zabbix API to make it more efficient and filter-aware. Message generation for Telegram bot was refactored and decoupled from data retrieval logic to improve structure, readability, and reuse.

Signed-off-by: UdoChudo <stream@udochudo.ru>
2025-06-17 23:46:39 +05:00
Vladislav Zverev
0169bf5d6b refactor(alerts): improve active problem fetching and message formatting
Reworked the logic for retrieving data from Zabbix API to make it more efficient and filter-aware. Message generation for Telegram bot was refactored and decoupled from data retrieval logic to improve structure, readability, and reuse.

Signed-off-by: UdoChudo <stream@udochudo.ru>
2025-06-17 23:44:30 +05:00
04d012759c Add debug command for switch logging level for telebot
Signed-off-by: UdoChudo <stream@udochudo.ru>
2025-06-17 23:34:37 +05:00
10 changed files with 236 additions and 191 deletions

View File

@ -1,12 +1,12 @@
from telebot.types import Message, CallbackQuery
from app.bot.keyboards.active_triggers import create_region_keyboard
from app.bot.utils.regions import get_sorted_regions_plain
from app.bot.processors.active_triggers_processor import (
process_region_selection,
process_group_selection,
process_all_groups_request,
)
from app.bot.utils.regions import get_sorted_regions_plain
def register_active_triggers(bot, app, state_manager):
@ -22,17 +22,28 @@ def register_callbacks_active_triggers(bot,app,state_manager):
@bot.callback_query_handler(func=lambda c: c.data.startswith("region_"))
def region_selected(callback_query: CallbackQuery):
region_id = callback_query.data.split("_")[1]
process_region_selection(callback_query.message.chat.id,bot, region_id)
chat_id = callback_query.message.chat.id
bot.answer_callback_query(callback_query.id)
bot.delete_message(chat_id, callback_query.message.message_id)
process_region_selection(bot, chat_id, region_id)
@bot.callback_query_handler(func=lambda c: c.data.startswith("group_"))
def group_selected(callback_query: CallbackQuery):
group_id = callback_query.data.split("_")[1]
process_group_selection(callback_query.message.chat.id,bot, group_id)
chat_id = callback_query.message.chat.id
bot.answer_callback_query(callback_query.id)
bot.delete_message(chat_id, callback_query.message.message_id)
bot.send_message(chat_id, f"Обработка...")
process_group_selection(bot, chat_id, group_id)
@bot.callback_query_handler(func=lambda c: c.data.startswith("all_groups_"))
def all_groups_selected(callback_query: CallbackQuery):
region_id = callback_query.data.split("_")[2]
process_all_groups_request(callback_query.message.chat.id,bot, region_id)
chat_id = callback_query.message.chat.id
bot.answer_callback_query(callback_query.id)
bot.delete_message(chat_id, callback_query.message.message_id)
bot.send_message(chat_id, f"Обработка...")
process_all_groups_request(bot, chat_id, region_id)
@bot.callback_query_handler(func=lambda c: c.data.startswith("regions_page_"))
def regions_page_selected(callback_query: CallbackQuery):

View File

@ -1,9 +1,10 @@
import logging
from telebot import logger
from telebot.types import InlineKeyboardMarkup, InlineKeyboardButton, Message
from telebot import logger as telebot_logger
from telebot.types import InlineKeyboardMarkup, InlineKeyboardButton, Message, CallbackQuery
from config import ADMINS_LIST
LOG_LEVELS = {
"🔴 ERROR": logging.ERROR,
"🟠 WARNING": logging.WARNING,
@ -11,10 +12,26 @@ LOG_LEVELS = {
"🔵 DEBUG": logging.DEBUG
}
def set_log_level(level: int):
# Устанавливаем уровень для telebot и связанных библиотек
telebot_logger.setLevel(level)
for handler in telebot_logger.handlers:
handler.setLevel(level)
logging.getLogger("pyzabbix").setLevel(level)
logging.getLogger("requests").setLevel(level)
# logging.getLogger("urllib3").setLevel(level)
# import http.client
# if level == logging.DEBUG:
# http.client.HTTPConnection.debuglevel = 1
# else:
# http.client.HTTPConnection.debuglevel = 0
def register_handlers(bot, app, state_manager):
@bot.message_handler(commands=['debug'], func=lambda message: message.chat.id in ADMINS_LIST)
def debug_handler(message):
def debug_handler(message: Message):
chat_id = message.chat.id
markup = InlineKeyboardMarkup(row_width=1)
buttons = [InlineKeyboardButton(text=level, callback_data=f"setlog_{level}") for level in LOG_LEVELS]
@ -24,16 +41,16 @@ def register_handlers(bot,app, state_manager):
bot.send_message(chat_id, "Выберите уровень логирования", reply_markup=markup)
@bot.callback_query_handler(func=lambda call: call.data.startswith("setlog_"))
def handle_log_level_callback(call):
message_id = call.message.message_id
def handle_log_level_callback(call: CallbackQuery):
level_text = call.data.replace("setlog_", "")
chat_id = call.message.chat.id
message_id = call.message.message_id
if level_text in LOG_LEVELS:
level = LOG_LEVELS[level_text]
logger.setLevel(level)
for handler in logger.handlers:
handler.setLevel(level)
bot.answer_callback_query(call.id, f"✅ Уровень логирования: {level_text}")
bot.delete_message(call.message.chat.id, message_id)
bot.send_message(call.message.chat.id, f"📋 Логгер переведён в режим: {level_text}")
set_log_level(level)
bot.answer_callback_query(call.id, f"✅ Уровень логирования установлен: {level_text}")
bot.delete_message(chat_id, message_id)
bot.send_message(chat_id, f"📋 Логгер переведён в режим: {level_text}")
else:
bot.answer_callback_query(call.id, "❌ Неизвестный уровень логирования")

View File

@ -30,10 +30,3 @@ def create_region_keyboard(regions, page, page_size=REGIONS_PER_PAGE):
markup.add(cancel_button)
return markup
def create_group_keyboard(groups, region_id):
markup = InlineKeyboardMarkup()
for group in groups:
markup.add(InlineKeyboardButton(text=group['name'], callback_data=f"group_{group['groupid']}"))
markup.add(InlineKeyboardButton(text="Все группы региона\n(Долгое выполнение)", callback_data=f"all_groups_{region_id}"))
return markup

View File

@ -0,0 +1,23 @@
from telebot.types import InlineKeyboardMarkup, InlineKeyboardButton
def create_groups_keyboard(groups, region_id):
"""
Формирует InlineKeyboardMarkup для выбора группы хостов.
:param groups: список словарей с группами, у каждой есть 'name' и 'groupid'
:param region_id: id региона, нужен для callback_data кнопки "Все группы региона"
:return: telebot.types.InlineKeyboardMarkup
"""
markup = InlineKeyboardMarkup()
for group in groups:
markup.add(InlineKeyboardButton(
text=group['name'],
callback_data=f"group_{group['groupid']}"
))
markup.add(InlineKeyboardButton(
text="Все группы региона\n(Долгое выполнение)",
callback_data=f"all_groups_{region_id}"
))
cancel_button = InlineKeyboardButton("Отмена", callback_data="cancel_input")
markup.add(cancel_button)
return markup

View File

@ -1,19 +1,20 @@
from telebot import types
from telebot.types import InlineKeyboardMarkup, InlineKeyboardButton
from app.bot.keyboards.groups import create_groups_keyboard
from app.bot.keyboards.main_menu import get_main_menu
from app.bot.utils.zabbix import get_region_groups, get_all_groups_for_region, fetch_filtered_triggers
from app.bot.utils.tg_formatter import format_trigger_message # ⬅️ добавлено
from app.bot.utils.tg_formatter import format_trigger_for_tg
from app.bot.utils.zabbix_alt import (
get_region_groups,
get_all_groups_for_region,
fetch_triggers_data)
def process_region_selection(bot, chat_id, region_id):
try:
groups = get_region_groups(region_id)
if not groups:
return bot.send_message(chat_id, "Нет групп хостов для этого региона.")
markup = types.InlineKeyboardMarkup()
for group in groups:
markup.add(types.InlineKeyboardButton(text=group['name'], callback_data=f"group_{group['groupid']}"))
markup.add(types.InlineKeyboardButton(text="Все группы региона\n(Долгое выполнение)", callback_data=f"all_groups_{region_id}"))
markup = create_groups_keyboard(groups, region_id)
bot.send_message(chat_id, "Выберите группу хостов:", reply_markup=markup)
except Exception as e:
bot.send_message(chat_id, f"Ошибка при получении групп: {str(e)}", reply_markup=get_main_menu())
@ -21,11 +22,16 @@ def process_region_selection(bot,chat_id, region_id):
def process_group_selection(bot, chat_id, group_id):
try:
triggers = fetch_filtered_triggers(group_id)
triggers = fetch_triggers_data(group_id)
if not triggers:
bot.send_message(chat_id, "Нет активных событий.")
else:
send_trigger_messages(chat_id, triggers)
return
for trigger in triggers:
text, url = format_trigger_for_tg(trigger)
markup = InlineKeyboardMarkup()
markup.add(InlineKeyboardButton(text="Открыть график", url=url))
bot.send_message(chat_id, text, reply_markup=markup, parse_mode="HTML")
except Exception as e:
bot.send_message(chat_id, f"Ошибка при получении событий: {str(e)}")
@ -36,21 +42,21 @@ def process_all_groups_request(bot, chat_id, region_id):
groups = get_all_groups_for_region(region_id)
for group in groups:
try:
triggers = fetch_filtered_triggers(group['groupid'])
triggers = fetch_triggers_data(group['groupid'])
if triggers:
all_triggers.extend(triggers)
except Exception:
continue
if all_triggers:
send_trigger_messages(chat_id, all_triggers)
else:
if not all_triggers:
bot.send_message(chat_id, "Нет активных событий.")
return
for trigger in all_triggers:
text, url = format_trigger_for_tg(trigger)
markup = InlineKeyboardMarkup()
markup.add(InlineKeyboardButton(text="Открыть график", url=url))
bot.send_message(chat_id, text, reply_markup=markup, parse_mode="HTML")
except Exception as e:
bot.send_message(chat_id, f"Ошибка при получении данных: {str(e)}")
def send_trigger_messages(chat_id, triggers):
for trigger in triggers:
text = format_trigger_message(trigger)
bot.send_message(chat_id, text, parse_mode="MarkdownV2")

View File

@ -1,9 +1,10 @@
from telebot.types import Message, InlineKeyboardMarkup, InlineKeyboardButton, MessageID
from app.bot.keyboards.settings_menu import get_settings_menu
from app.extensions.db import db
from app import Regions, Subscriptions
from app.bot.utils.tg_audit import log_user_event
from app.bot.constants import UserStates
from app.bot.keyboards.settings_menu import get_settings_menu
from app.bot.utils.tg_audit import log_user_event
from app.extensions.db import db
def process_subscription_button(message: Message, app, bot, chat_id: int, state_manager, bot_message: MessageID):
@ -17,7 +18,8 @@ def process_subscription_button(message: Message, app, bot, chat_id: int, state_
reply_markup=markup)
def delayed_handler(msg):
process_subscription_button(msg, app, bot, chat_id, state_manager)
message_id = msg.message_id
process_subscription_button(msg, app, bot, chat_id, state_manager, message_id)
bot.register_next_step_handler(message, delayed_handler)
return

View File

@ -1,35 +1,44 @@
from datetime import datetime
from pytz import timezone
from app.bot.utils.tg_escape_chars import escape_telegram_chars
from config import ZABBIX_URL, ZABBIX_TZ
def format_trigger_message(trigger, zabbix_url: str) -> str:
tz = timezone('Europe/Moscow')
def format_trigger_for_tg(trigger):
"""
Формирует текст сообщения для одного триггера
и возвращает (text, inline_buttons_data)
"""
priority_map = {'4': 'HIGH', '5': 'DISASTER'}
event_time_epoch = int(trigger.get('lastEvent', {}).get('clock', trigger.get('lastchange', 0)))
event_time = datetime.fromtimestamp(event_time_epoch, tz=tz)
priority_map_emoji = {'4': '⚠️','5': '⛔️'}
event_time_epoch = int(trigger['lastEvent']['clock'])
event_time = datetime.fromtimestamp(event_time_epoch, tz=timezone(ZABBIX_TZ))
event_time_formatted = event_time.strftime('%Y-%m-%d %H:%M:%S Мск')
host = trigger.get('hosts', [{}])[0].get('name', 'Неизвестно')
priority = priority_map.get(str(trigger.get('priority')), 'Неизвестно')
description = escape_telegram_chars(trigger.get('description', '')).replace("{HOST.NAME}", host)
items = trigger.get('items', [])
item_ids = [item['itemid'] for item in items]
for i, item in enumerate(items):
description = escape_telegram_chars(trigger['description'])
host = trigger['hosts'][0]['name']
priority = priority_map.get(trigger['priority'], 'Неизвестно')
icon = priority_map_emoji.get(trigger['priority'], 'Неизвестно')
description = description.replace("{HOST.NAME}", host)
for i, item in enumerate(trigger['items']):
placeholder = f"{{ITEM.LASTVALUE{i + 1}}}"
if placeholder in description:
description = description.replace(placeholder, item.get('lastvalue', '?'))
description = description.replace(placeholder, item['lastvalue'])
batchgraph_link = f"{zabbix_url}/history.php?action=batchgraph&"
item_ids = [item['itemid'] for item in trigger['items']]
batchgraph_link = f"{ZABBIX_URL}/history.php?action=batchgraph&"
batchgraph_link += "&".join([f"itemids[{item_id}]={item_id}" for item_id in item_ids])
batchgraph_link += "&graphtype=0"
return (
text = (
f"<b>Host</b>: {host}\n"
f"<b>Описание</b>: {description}\n"
f"<b>Критичность</b>: {priority}\n"
f"<b>Критичность</b>: {icon} {priority}\n"
f"<b>Время создания</b>: {event_time_formatted}\n"
f'<b>URL</b>: <a href="{batchgraph_link}">Ссылка на график</a>'
# f'<b>URL</b>: <a href="{batchgraph_link}">Ссылка на график</a>'
)
# Возвращаем текст и ссылку, чтобы потом сделать inline кнопку
return text, batchgraph_link

View File

@ -1,119 +0,0 @@
import time
from datetime import datetime
from pytz import timezone
from pyzabbix import ZabbixAPI, ZabbixAPIException
from telebot import logger
from config import ZABBIX_URL, ZABBIX_API_TOKEN, ZABBIX_VERIFY_SSL, ZABBIX_TZ
from app.bot.utils.tg_escape_chars import escape_telegram_chars
TZ = timezone(ZABBIX_TZ)
verify_ssl = ZABBIX_VERIFY_SSL
def get_region_groups(region_id: str):
"""
Получает список групп, имя которых содержит регион region_id, исключая 'test'.
"""
try:
zapi = ZabbixAPI(ZABBIX_URL)
zapi.login(api_token=ZABBIX_API_TOKEN)
zapi.session.verify = verify_ssl
host_groups = zapi.hostgroup.get(output=["groupid", "name"], search={"name": region_id})
filtered_groups = [group for group in host_groups if 'test' not in group['name'].lower()]
return filtered_groups
except Exception as e:
logger.error(f"Error getting region groups for '{region_id}': {e}")
return []
def get_all_groups_for_region(region_id: str):
"""
Аналогично get_region_groups, получение всех групп по региону.
"""
return get_region_groups(region_id)
def fetch_filtered_triggers(group_id):
"""
Получение и фильтрация триггеров с severities 4 и 5,
формирование HTML сообщений для отправки в Telegram.
"""
try:
zapi = ZabbixAPI(ZABBIX_URL)
zapi.login(api_token=ZABBIX_API_TOKEN)
zapi.session.verify = verify_ssl
problems = zapi.problem.get(
severities=[4, 5],
suppressed=0,
acknowledged=0,
groupids=group_id
)
trigger_ids = [problem["objectid"] for problem in problems]
if not trigger_ids:
return []
triggers = zapi.trigger.get(
triggerids=trigger_ids,
output=["triggerid", "description", "priority"],
selectHosts=["hostid", "name"],
monitored=1,
expandDescription=1,
expandComment=1,
selectItems=["itemid", "lastvalue"],
selectLastEvent=["clock", "eventid"]
)
events = zapi.event.get(
severities=[4, 5],
objectids=trigger_ids,
select_alerts="mediatype"
)
pnet_mediatypes = {"Pnet integration JS 2025", "Pnet integration JS 2024", "Pnet integration new2"}
pnet_triggers = []
event_dict = {event["objectid"]: event for event in events}
for trigger in triggers:
event = event_dict.get(trigger["triggerid"])
if event:
for alert in event["alerts"]:
if alert["mediatypes"] and alert["mediatypes"][0]["name"] in pnet_mediatypes:
pnet_triggers.append(trigger)
break
triggers_sorted = sorted(pnet_triggers, key=lambda t: int(t['lastEvent']['clock']))
priority_map = {'4': 'HIGH', '5': 'DISASTER'}
trigger_messages = []
for trigger in triggers_sorted:
event_time_epoch = int(trigger['lastEvent']['clock'])
event_time = datetime.fromtimestamp(event_time_epoch, tz=TZ)
description = escape_telegram_chars(trigger['description'])
host = trigger['hosts'][0]['name']
priority = priority_map.get(trigger['priority'], 'Неизвестно')
item_ids = [item['itemid'] for item in trigger['items']]
batchgraph_link = f"{ZABBIX_URL}/history.php?action=batchgraph&"
batchgraph_link += "&".join([f"itemids[{item_id}]={item_id}" for item_id in item_ids])
batchgraph_link += "&graphtype=0"
description = description.replace("{HOST.NAME}", host)
for i, item in enumerate(trigger['items']):
lastvalue_placeholder = f"{{ITEM.LASTVALUE{i + 1}}}"
if lastvalue_placeholder in description:
description = description.replace(lastvalue_placeholder, item['lastvalue'])
event_time_formatted = event_time.strftime('%Y-%m-%d %H:%M:%S Мск')
message = (f"<b>Host</b>: {host}\n"
f"<b>Описание</b>: {description}\n"
f"<b>Критичность</b>: {priority}\n"
f"<b>Время создания</b>: {event_time_formatted}\n"
f'<b>URL</b>: <a href="{batchgraph_link}">Ссылка на график</a>')
trigger_messages.append(message)
logger.info(f"Fetched {len(triggers_sorted)} triggers for group {group_id}.")
return trigger_messages
except Exception as e:
logger.error(f"Error fetching triggers for group {group_id}: {e}")
return []

103
app/bot/utils/zabbix_alt.py Normal file
View File

@ -0,0 +1,103 @@
import re
import time
from pyzabbix import ZabbixAPI, ZabbixAPIException
from telebot import logger
from config import ZABBIX_URL, ZABBIX_API_TOKEN, ZABBIX_VERIFY_SSL
def get_region_groups(region_id: str):
"""
Получает список групп, имя которых содержит регион region_id, исключая 'test' и группы, не соответствующие шаблону 'имя_число'.
"""
try:
zapi = ZabbixAPI(ZABBIX_URL)
zapi.login(api_token=ZABBIX_API_TOKEN)
zapi.session.verify = ZABBIX_VERIFY_SSL
host_groups = zapi.hostgroup.get(output=["groupid", "name"], search={"name": region_id})
pattern = re.compile(r'.+_\d+$') # строка с нижним подчёркиванием перед числом в конце
filtered_groups = [
group for group in host_groups
if 'test' not in group['name'].lower() and pattern.match(group['name'])
]
return filtered_groups
except Exception as e:
logger.error(f"[Zabbix] Error getting region groups for '{region_id}': {e}")
return []
def get_all_groups_for_region(region_id: str):
"""
Аналогично get_region_groups, получение всех групп по региону.
"""
return get_region_groups(region_id)
def fetch_triggers_data(group_id):
"""
Возвращает список триггеров с необходимыми данными,
без форматирования сообщений.
"""
pnet_mediatypes = {"Pnet integration JS 2025", "Pnet integration JS 2024", "Pnet integration new2"}
start_time = time.time()
try:
zapi = ZabbixAPI(ZABBIX_URL)
zapi.login(api_token=ZABBIX_API_TOKEN)
# Получаем проблемы с высокой и критической важностью
problems = zapi.problem.get(
severities=[4, 5],
suppressed=0,
acknowledged=0,
groupids=group_id
)
trigger_ids = [problem["objectid"] for problem in problems]
if not trigger_ids:
logger.info(f"No triggers found for group {group_id}")
return []
triggers = zapi.trigger.get(
triggerids=trigger_ids,
output=["triggerid", "description", "priority"],
selectHosts=["hostid", "name"],
monitored=1,
expandDescription=1,
expandComment=1,
selectItems=["itemid", "lastvalue"],
selectLastEvent=["clock", "eventid"]
)
events = zapi.event.get(
severities=[4, 5],
objectids=trigger_ids,
select_alerts="mediatype"
)
event_dict = {event["objectid"]: event for event in events}
pnet_triggers = []
for trigger in triggers:
event = event_dict.get(trigger["triggerid"])
if event:
for alert in event["alerts"]:
if alert["mediatypes"] and alert["mediatypes"][0]["name"] in pnet_mediatypes:
pnet_triggers.append(trigger)
break
triggers_sorted = sorted(pnet_triggers, key=lambda t: int(t['lastEvent']['clock']))
logger.debug(f"Found {len(pnet_triggers)} pnet triggers for group {group_id}")
end_time = time.time()
logger.info(f"[Zabbix] Fetched {len(triggers_sorted)} triggers for group {group_id} in {end_time - start_time:.2f} seconds.")
return triggers_sorted
except ZabbixAPIException as e:
logger.error(f"[Zabbix] Zabbix API error for group {group_id}: {e}")
return []
except Exception as e:
logger.error(f"[Zabbix] Unexpected error fetching triggers for group {group_id}: {e}")
return []

View File

@ -11,7 +11,7 @@ REGIONS_PER_PAGE = os.getenv('REGIONS_PER_PAGE', 10)
ZABBIX_API_TOKEN = os.getenv('ZABBIX_API_TOKEN')
ZABBIX_URL = os.getenv('ZABBIX_URL')
ZABBIX_VERIFY_SSL = os.getenv('ZABBIX_VERIFY_SSL', True)
ZABBIX_TZ = os.getenv('ZABBIX_TZ', 'Europe/Moscow')
ZABBIX_TZ = os.getenv('TZ', 'Europe/Moscow')
#Настройки Flask и Telegram bot
basedir = os.path.abspath(os.path.dirname(__file__))
DB_ABS_PATH = os.path.join(basedir, 'db/telezab.db')