templates-zabbix-itguys/clean_hosts_export.py

69 lines
2.5 KiB
Python
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import json
import sys
INPUT_FILE = "zabbix_hosts_export.json"
OUTPUT_FILE = "zabbix_hosts_export_NUCLEAR_CLEAN.json"
def main():
print("="*60)
print(" 🧹 LIMPADOR EXTREMO (DEEP CLEANER)")
print("="*60)
print(" Modo Deep Clean: Removendo templates E itens/triggers/painéis.")
print(" O objetivo é importar apenas os HOSTS (Casca vazia).")
print(f"📂 Lendo: {INPUT_FILE}...", end=" ")
try:
with open(INPUT_FILE, 'r', encoding='utf-8') as f:
data = json.load(f)
print("✅ OK")
except FileNotFoundError:
print("❌ Arquivo não encontrado!")
sys.exit(1)
# Detecta onde estão os hosts
hosts_list = []
if 'zabbix_export' in data and 'hosts' in data['zabbix_export']:
hosts_list = data['zabbix_export']['hosts']
elif 'hosts' in data:
hosts_list = data['hosts']
count = 0
# Limpeza dentro dos hosts
if hosts_list:
keys_to_remove = ['templates', 'items', 'discovery_rules', 'httptests', 'triggers', 'graphs', 'dashboards', 'macros']
# Adicionei 'macros' só por garantia, embora as vezes seja útil manter.
# O erro atual é sobre trigger.
for host in hosts_list:
for key in keys_to_remove:
if key in host:
# REMOVER A CHAVE COMPLETAMENTE em vez de deixar lista vazia []
# Algumas versões do Zabbix não gostam de chaves vazias se não houver nada.
del host[key]
# Limpeza extra: Remover tags vazias ou inventory se necessário
# Mas o foco é o erro de trigger.
# Limpeza GLOBAL (Top Level)
# Zabbix export pode ter 'triggers' globais fora dos hosts
if 'zabbix_export' in data:
if 'triggers' in data['zabbix_export']:
print("⚠️ Encontrado bloco global de 'triggers'. Removendo...")
del data['zabbix_export']['triggers']
if 'graphs' in data['zabbix_export']:
del data['zabbix_export']['graphs']
print(f"✨ Hosts limpos profundamente: {count}")
print(" (Templates, Itens, Triggers e LLDs removidos TOTALMENTE)")
print(f"💾 Salvando: {OUTPUT_FILE}...", end=" ")
with open(OUTPUT_FILE, 'w', encoding='utf-8') as f:
json.dump(data, f, indent=4, ensure_ascii=False)
print("✅ Sucesso!")
print("\n👉 Use este arquivo CLEAN no novo Zabbix para importar APENAS os hosts.")
if __name__ == "__main__":
main()