Merge pull request 'dev-2.0.1' (#4) from dev-2.0.1 into main

Reviewed-on: #4
This commit is contained in:
sergey 2025-04-14 13:38:14 +00:00
commit 6c1b0c68e5
10 changed files with 95 additions and 7 deletions

View File

@ -5,6 +5,11 @@ CFG__ZABBIX__URL=https://example.com
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_TAG_USER=string
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

View File

@ -49,6 +49,13 @@ class ZabbixConfig(BaseModel):
token: str
min_severity: int
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
api_state: str = "UP"
class Settings(BaseSettings):

View File

@ -10,8 +10,10 @@ services:
command: [redis-server, --protected-mode yes, --port 6379, --requirepass, P@ssw0rd!]
tg-bot:
image: git.sm8255082.ru/osnova/zbx-tg-bot:2.0.0
image: git.sm8255082.ru/osnova/zbx-tg-bot:2.0.1
restart: always
volumes:
- ./logfile.log:/app/logfile.log
depends_on:
- redis
environment:
@ -22,6 +24,11 @@ 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_TAG_USER=
- 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

15
main.py
View File

@ -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 = []

View File

@ -1,6 +1,6 @@
[project]
name = "zbx-tg-bot"
version = "2.0.0"
version = "2.0.1"
description = "telegram bot for telegram-zabbix dashboard"
requires-python = ">=3.13"
dependencies = [

View File

@ -1,6 +1,7 @@
from .message import (
send_message,
del_message,
send_zabbix_api_alert,
)
from .bot import start_bot
@ -8,4 +9,5 @@ __all__ = [
"send_message",
"del_message",
"start_bot",
"send_zabbix_api_alert",
]

View File

@ -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)

View File

@ -67,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": conf.zabbix.alert_tag_user + "\n" + 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}

View File

@ -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",
]

View File

@ -3,6 +3,7 @@ import logging as log
from pyexpat.errors import messages
from zabbix_utils import ZabbixAPI
from config import conf
from datetime import datetime, timedelta
@ -80,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:
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:
log.warning(
f"Zabbix API state changed to DOWN. Count {conf.zabbix.change_state_count}"
)
return "DOWN"
else:
conf.zabbix.change_state_count += 1