Compare commits

..

No commits in common. "6c1b0c68e5198afcfb28dfa9faf50ee3b2ef7ff6" and "3e34f4567c1de560e5dd1fa22c1052a75071b19b" have entirely different histories.

10 changed files with 7 additions and 95 deletions

View File

@ -5,11 +5,6 @@ CFG__ZABBIX__URL=https://example.com
CFG__ZABBIX__TOKEN=string CFG__ZABBIX__TOKEN=string
CFG__ZABBIX__MIN_SEVERITY=0 CFG__ZABBIX__MIN_SEVERITY=0
CFG__ZABBIX__UPD_INTERVAL=60 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__TOKEN=string
CFG__TGBOT__CHAT_ID=00000000 CFG__TGBOT__CHAT_ID=00000000

View File

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

View File

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

15
main.py
View File

@ -1,5 +1,5 @@
import logging as log import logging as log
from zabbix import get_active_problems, check_state from zabbix import get_active_problems
from config import conf, icon_dict from config import conf, icon_dict
from redis_db import ( from redis_db import (
get_all_keys, get_all_keys,
@ -8,24 +8,13 @@ from redis_db import (
del_value, del_value,
) )
import asyncio import asyncio
from telegram import del_message, send_message, start_bot, send_zabbix_api_alert from telegram import del_message, send_message, start_bot
async def dashboard(): async def dashboard():
active_alerts = get_active_problems() active_alerts = get_active_problems()
if active_alerts is None: 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 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() telegram_alerts = await get_all_keys()
new_alerts_id = [] new_alerts_id = []

View File

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

View File

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

View File

@ -2,8 +2,7 @@ from config import conf
from aiogram import Bot, Dispatcher, types from aiogram import Bot, Dispatcher, types
from aiogram.fsm.storage.memory import MemoryStorage from aiogram.fsm.storage.memory import MemoryStorage
from aiogram import F from aiogram import F
from zabbix import event_close from zabbix import event_acknowledge, event_close
from zabbix import event_acknowledge
import logging as log import logging as log
tg_bot = Bot(token=conf.tgbot.token) tg_bot = Bot(token=conf.tgbot.token)

View File

@ -67,36 +67,9 @@ async def del_message(
log.warning( log.warning(
f"Message ID {message_id} NOT deleted. Response status: {response.status}" f"Message ID {message_id} NOT deleted. Response status: {response.status}"
) )
return { return {
"status": response.status, "status": response.status,
} }
except Exception as e: except Exception as e:
log.warning(f"Exception: {e}") log.warning(f"Exception: {e}")
return {"status": 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_close, event_acknowledge, check_state from .zabbix_api import get_active_problems, event_acknowledge, event_close
__all__ = [ __all__ = [
"get_active_problems", "get_active_problems",
"event_acknowledge", "event_acknowledge",
"event_close", "event_close",
"check_state",
] ]

View File

@ -3,7 +3,6 @@ import logging as log
from pyexpat.errors import messages from pyexpat.errors import messages
from zabbix_utils import ZabbixAPI from zabbix_utils import ZabbixAPI
from config import conf from config import conf
from datetime import datetime, timedelta from datetime import datetime, timedelta
@ -81,30 +80,3 @@ def event_close(event_id: int, closed_by: str):
except: except:
log.warning(f"Closed event {event_id} from zabbix error") log.warning(f"Closed event {event_id} from zabbix error")
return False 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