add check zabbix api
This commit is contained in:
sergey 2025-04-14 15:49:47 +03:00
parent ac78933106
commit e9ee7bb8b0
8 changed files with 50 additions and 42 deletions

View File

@ -7,6 +7,7 @@ CFG__ZABBIX__MIN_SEVERITY=0
CFG__ZABBIX__UPD_INTERVAL=60 CFG__ZABBIX__UPD_INTERVAL=60
CFG__ZABBIX__ALERT_FAIL_COUNT=3 CFG__ZABBIX__ALERT_FAIL_COUNT=3
CFG__ZABBIX__ALERT_TREAD_ID=0 CFG__ZABBIX__ALERT_TREAD_ID=0
CFG__ZABBIX__ALERT_TAG_USER=string
CFG__ZABBIX__ALERT_TEXT_UP=Zabbix service UP CFG__ZABBIX__ALERT_TEXT_UP=Zabbix service UP
CFG__ZABBIX__ALERT_TEXT_DOWN=Zabbix service DOWN CFG__ZABBIX__ALERT_TEXT_DOWN=Zabbix service DOWN

View File

@ -51,6 +51,7 @@ class ZabbixConfig(BaseModel):
upd_interval: int upd_interval: int
alert_fail_count: int alert_fail_count: int
alert_tread_id: int alert_tread_id: int
alert_tag_user: str = ""
alert_text_up: str alert_text_up: str
alert_text_down: str alert_text_down: str
change_state_count: int = 0 change_state_count: int = 0

View File

@ -12,6 +12,8 @@ services:
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.1
restart: always restart: always
volumes:
- ./logfile.log:/app/logfile.log
depends_on: depends_on:
- redis - redis
environment: environment:

15
main.py
View File

@ -1,5 +1,5 @@
import logging as log 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 config import conf, icon_dict
from redis_db import ( from redis_db import (
get_all_keys, get_all_keys,
@ -8,13 +8,24 @@ from redis_db import (
del_value, del_value,
) )
import asyncio 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(): 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

@ -2,7 +2,8 @@ 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_acknowledge, event_close from zabbix import 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

@ -81,7 +81,7 @@ async def send_zabbix_api_alert(message):
params = { params = {
"chat_id": conf.tgbot.chat_id, "chat_id": conf.tgbot.chat_id,
"message_thread_id": conf.zabbix.alert_tread_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: async with aiohttp.ClientSession() as session:
try: try:

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__ = [ __all__ = [
"get_active_problems", "get_active_problems",
"event_acknowledge", "event_acknowledge",
"event_close", "event_close",
"check_state",
] ]

View File

@ -3,45 +3,12 @@ import logging as log
from pyexpat.errors import messages from pyexpat.errors import messages
from zabbix_utils import ZabbixAPI from zabbix_utils import ZabbixAPI
import telegram
from config import conf from config import conf
from datetime import datetime, timedelta 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: def get_active_problems() -> dict:
api = ZabbixAPI(url=conf.zabbix.url, token=conf.zabbix.token) api = ZabbixAPI(url=conf.zabbix.url, token=conf.zabbix.token)
try: try:
@ -74,11 +41,9 @@ def get_active_problems() -> dict:
event["host"] = event.pop("hosts", None)[0]["host"] event["host"] = event.pop("hosts", None)[0]["host"]
events_dict[event["eventid"]] = event events_dict[event["eventid"]] = event
events_dict["event_ids"].append(event["eventid"]) events_dict["event_ids"].append(event["eventid"])
check_state(True)
return events_dict return events_dict
except: except:
log.warning("Get event from zabbix error") log.warning("Get event from zabbix error")
check_state(False)
def event_acknowledge(event_id: int, mute_time: int, muted_by: str): 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: 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 - 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