Refactoring get_zabbix_triggers now using problem.get instead of trigger.get
This commit is contained in:
parent
d5d0b27bed
commit
3c407b2a07
42
telezab.py
42
telezab.py
@ -1265,7 +1265,7 @@ def get_zabbix_triggers(group_id):
|
||||
|
||||
# Получение активных проблем для этих хостов
|
||||
problems = zapi.problem.get(
|
||||
output=["eventid", "name", "severity", "clock"],
|
||||
output=["eventid", "name", "severity", "clock", "objectid"],
|
||||
hostids=host_ids,
|
||||
suppressed=0,
|
||||
acknowledged=0,
|
||||
@ -1277,13 +1277,39 @@ def get_zabbix_triggers(group_id):
|
||||
telebot.logger.info(f"No active problems found for group {group_id}")
|
||||
return []
|
||||
|
||||
# Получение триггеров и их связи с хостами
|
||||
trigger_ids = [problem["objectid"] for problem in problems]
|
||||
triggers = zapi.trigger.get(
|
||||
triggerids=trigger_ids,
|
||||
output=["triggerid", "description"],
|
||||
selectHosts=["hostid"]
|
||||
)
|
||||
|
||||
# Создаем карту триггеров к хостам
|
||||
trigger_to_host_map = {}
|
||||
for trigger in triggers:
|
||||
if "hosts" in trigger and trigger["hosts"]:
|
||||
trigger_to_host_map[trigger["triggerid"]] = trigger["hosts"][0]["hostid"]
|
||||
|
||||
# Получение IP-адресов хостов
|
||||
host_interfaces = zapi.hostinterface.get(
|
||||
hostids=host_ids,
|
||||
output=["hostid", "ip"]
|
||||
)
|
||||
host_ip_map = {iface["hostid"]: iface["ip"] for iface in host_interfaces}
|
||||
print(host_ip_map)
|
||||
|
||||
# Получение itemid для триггеров на основе их описания
|
||||
item_map = {}
|
||||
for host in active_hosts:
|
||||
host_id = host["hostid"]
|
||||
items = zapi.item.get(
|
||||
hostids=host_id,
|
||||
output=["itemid", "name"]
|
||||
)
|
||||
for item in items:
|
||||
item_map[(host_id, item["name"])] = item["itemid"]
|
||||
|
||||
|
||||
moscow_tz = timezone('Europe/Moscow')
|
||||
severity_map = {'4': 'HIGH', '5': 'DISASTER'}
|
||||
priority_map = {'4': '⚠️', '5': '⛔️'}
|
||||
@ -1298,12 +1324,16 @@ def get_zabbix_triggers(group_id):
|
||||
priority = priority_map.get(problem['severity'], '')
|
||||
description = problem.get('name', 'Нет описания')
|
||||
|
||||
# Получаем хост из описания (или по-другому, если известно)
|
||||
# Получаем hostid через триггер
|
||||
trigger_id = problem.get("objectid")
|
||||
host_id = trigger_to_host_map.get(trigger_id, "Неизвестный хост")
|
||||
host_ip = host_ip_map.get(host_id, "Неизвестный IP")
|
||||
host = extract_host_from_name(description)
|
||||
host_ip = host_ip_map.get(problem.get("hostid"), "Неизвестный IP")
|
||||
description = escape_telegram_chars(description)
|
||||
|
||||
message = (f"<b>{priority} Host</b>: {host}\n"
|
||||
f"<b>IP</b>: {host_ip}\n"
|
||||
|
||||
|
||||
message = (f"{priority} {host} ({host_ip})\n"
|
||||
f"<b>Описание</b>: {description}\n"
|
||||
f"<b>Критичность</b>: {severity}\n"
|
||||
f"<b>Время создания</b>: {event_time_formatted}")
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user