check zabbix api
This commit is contained in:
parent
12c124863e
commit
ac78933106
|
@ -6,6 +6,9 @@ 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_TEXT_UP=Zabbix service UP
|
||||
CFG__ZABBIX__ALERT_TEXT_DOWN=Zabbix service DOWN
|
||||
|
||||
CFG__TGBOT__TOKEN=string
|
||||
CFG__TGBOT__CHAT_ID=00000000
|
||||
|
|
|
@ -50,7 +50,11 @@ class ZabbixConfig(BaseModel):
|
|||
min_severity: int
|
||||
upd_interval: int
|
||||
alert_fail_count: int
|
||||
current_fail: int = 0
|
||||
alert_tread_id: int
|
||||
alert_text_up: str
|
||||
alert_text_down: str
|
||||
change_state_count: int = 0
|
||||
api_state: str = "UP"
|
||||
|
||||
|
||||
class Settings(BaseSettings):
|
||||
|
|
|
@ -22,6 +22,10 @@ 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_TEXT_UP=Zabbix service UP
|
||||
- CFG__ZABBIX__ALERT_TEXT_DOWN=Zabbix service DOWN
|
||||
|
||||
- CFG__TGBOT__TOKEN=string
|
||||
- CFG__TGBOT__CHAT_ID=000000
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
from .message import (
|
||||
send_message,
|
||||
del_message,
|
||||
edit_message,
|
||||
send_zabbix_api_alert,
|
||||
)
|
||||
from .bot import start_bot
|
||||
|
||||
|
@ -9,5 +9,5 @@ __all__ = [
|
|||
"send_message",
|
||||
"del_message",
|
||||
"start_bot",
|
||||
"edit_message",
|
||||
"send_zabbix_api_alert",
|
||||
]
|
||||
|
|
|
@ -45,38 +45,6 @@ async def send_message(message: str, event_id: int) -> dict:
|
|||
return {"status": e}
|
||||
|
||||
|
||||
async def edit_message(
|
||||
message_id: int,
|
||||
new_message: str,
|
||||
) -> dict:
|
||||
url = f"https://api.telegram.org/bot{conf.tgbot.token}/editMessageText"
|
||||
async with aiohttp.ClientSession() as session:
|
||||
try:
|
||||
async with session.post(
|
||||
url,
|
||||
json={
|
||||
"chat_id": conf.tgbot.chat_id,
|
||||
"message_id": message_id,
|
||||
"text": new_message,
|
||||
},
|
||||
) as response:
|
||||
if response.status == 200:
|
||||
log.info(f"Message ID {message_id} edited")
|
||||
return {
|
||||
"status": response.status,
|
||||
}
|
||||
else:
|
||||
log.warning(
|
||||
f"Message ID {message_id} NOT edited. Response status: {response.status}"
|
||||
)
|
||||
return {
|
||||
"status": response.status,
|
||||
}
|
||||
except Exception as e:
|
||||
log.warning(f"Exception: {e}")
|
||||
return {"status": e}
|
||||
|
||||
|
||||
async def del_message(
|
||||
message_id: int,
|
||||
) -> dict:
|
||||
|
@ -99,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": 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}
|
||||
|
|
|
@ -3,11 +3,45 @@ 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:
|
||||
|
@ -40,16 +74,11 @@ 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"])
|
||||
if conf.zabbix.current_fail:
|
||||
conf.zabbix.current_fail = 0
|
||||
check_state(True)
|
||||
return events_dict
|
||||
except:
|
||||
log.warning("Get event from zabbix error")
|
||||
if conf.zabbix.current_fail == conf.zabbix.alert_fail_count:
|
||||
log.warning(f"Zabbix connection error. Count {conf.zabbix.current_fail}")
|
||||
else:
|
||||
conf.zabbix.current_fail += 1
|
||||
log.warning(f"Zabbix connection error. Count {conf.zabbix.current_fail}")
|
||||
check_state(False)
|
||||
|
||||
|
||||
def event_acknowledge(event_id: int, mute_time: int, muted_by: str):
|
||||
|
|
Loading…
Reference in New Issue