first commit
This commit is contained in:
@@ -0,0 +1,115 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test script para verificar la funcionalidad de mostrar todas las interfaces
|
||||
usando datos simulados cuando no hay acceso al dispositivo real.
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
|
||||
# Añadir el directorio actual al path para importar los módulos
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
from cisco_mac_monitor import CiscoMacTableParser, ZabbixLLDFormatter
|
||||
|
||||
def test_all_interfaces_functionality():
|
||||
"""Prueba que todas las interfaces se muestren, incluso sin MACs"""
|
||||
|
||||
print("=== PRUEBA DE FUNCIONALIDAD: TODAS LAS INTERFACES ===\n")
|
||||
|
||||
# Simular salida de 'show interfaces status'
|
||||
mock_interface_output = """
|
||||
Port Name Status Vlan Duplex Speed Type
|
||||
Fa0/1 connected 1 full 100 10/100BaseTX
|
||||
Fa0/2 Servidor-Core connected 10 full 100 10/100BaseTX
|
||||
Fa0/3 notconnect 1 auto auto 10/100BaseTX
|
||||
Fa0/4 notconnect 1 auto auto 10/100BaseTX
|
||||
Fa0/5 notconnect 1 auto auto 10/100BaseTX
|
||||
Gi0/1 Uplink-Router connected trunk full 1000 1000BaseTX
|
||||
Gi0/2 notconnect 1 auto auto 1000BaseTX
|
||||
"""
|
||||
|
||||
# Simular salida de tabla MAC (solo algunas interfaces tienen MACs)
|
||||
mock_mac_output = """
|
||||
1 0021.9b5a.4f10 DYNAMIC Fa0/1
|
||||
1 001b.2f89.3c21 DYNAMIC Fa0/1
|
||||
10 00e0.4c36.7f89 DYNAMIC Fa0/2
|
||||
10 0050.5682.1c45 DYNAMIC Fa0/2
|
||||
1 0019.bbf7.4a21 DYNAMIC Gi0/1
|
||||
1 0025.90e4.b832 DYNAMIC Gi0/1
|
||||
1 0018.8b99.5f71 DYNAMIC Gi0/1
|
||||
"""
|
||||
|
||||
# Crear instancias de las clases
|
||||
parser = CiscoMacTableParser()
|
||||
formatter = ZabbixLLDFormatter("cisco-test-device")
|
||||
|
||||
print("1. Parseando interfaces detectadas:")
|
||||
print(" - Simulando 'show interfaces status'")
|
||||
|
||||
# Simular el parseo de interfaces
|
||||
interfaces = parser._parse_interface_list(mock_interface_output)
|
||||
print(f" - Interfaces detectadas: {len(interfaces)}")
|
||||
for interface in interfaces:
|
||||
print(f" * {interface}")
|
||||
|
||||
print(f"\n2. Parseando tabla MAC:")
|
||||
print(" - Simulando 'show mac address-table'")
|
||||
|
||||
# Parsear tabla MAC usando el método privado
|
||||
mac_entries = parser._parse_mac_table_output(mock_mac_output)
|
||||
print(f" - Entradas MAC encontradas: {len(mac_entries)}")
|
||||
for entry in mac_entries:
|
||||
print(f" * VLAN {entry.vlan} - {entry.mac_address} en {entry.port}")
|
||||
|
||||
print(f"\n3. Formateando para Zabbix (con todas las interfaces):")
|
||||
|
||||
# Formatear para Zabbix con todas las interfaces
|
||||
zabbix_data = formatter.format_for_zabbix(mac_entries, interfaces)
|
||||
|
||||
print(f" - Estructura de datos Zabbix: {type(zabbix_data)}")
|
||||
print(f" - Claves disponibles: {list(zabbix_data.keys()) if isinstance(zabbix_data, dict) else 'No es dict'}")
|
||||
|
||||
if 'data' in zabbix_data and isinstance(zabbix_data['data'], list) and len(zabbix_data['data']) > 0:
|
||||
print(f" - Primer elemento: {zabbix_data['data'][0]}")
|
||||
print(f" - Claves del primer elemento: {list(zabbix_data['data'][0].keys())}")
|
||||
|
||||
# Analizar resultados
|
||||
ports_with_macs = 0
|
||||
ports_without_macs = 0
|
||||
|
||||
for item in zabbix_data['data']:
|
||||
interface = item['{#PORT_NAME}']
|
||||
mac_count = item['{#MAC_COUNT}']
|
||||
|
||||
if mac_count > 0:
|
||||
ports_with_macs += 1
|
||||
print(f" ✅ {interface}: {mac_count} MACs conectadas")
|
||||
else:
|
||||
ports_without_macs += 1
|
||||
print(f" ⚪ {interface}: 0 MACs (puerto vacío)")
|
||||
|
||||
print(f"\n=== RESUMEN DE RESULTADOS ===")
|
||||
print(f"Total de puertos: {len(zabbix_data['data'])}")
|
||||
print(f"Puertos con MACs: {ports_with_macs}")
|
||||
print(f"Puertos vacíos: {ports_without_macs}")
|
||||
|
||||
# Mostrar algunos ejemplos del JSON Zabbix
|
||||
print(f"\n=== EJEMPLO DE SALIDA ZABBIX LLD ===")
|
||||
import json
|
||||
print(json.dumps(zabbix_data, indent=2)[:1000] + "..." if len(str(zabbix_data)) > 1000 else json.dumps(zabbix_data, indent=2))
|
||||
|
||||
# Verificar que la funcionalidad funciona
|
||||
if ports_without_macs > 0:
|
||||
print(f"\n🎉 ¡ÉXITO! La funcionalidad funciona correctamente:")
|
||||
print(f" - Se detectaron {ports_without_macs} puertos vacíos con MAC_COUNT=0")
|
||||
print(f" - Se detectaron {ports_with_macs} puertos con MACs conectadas")
|
||||
print(f" - Total de puertos mostrados: {len(zabbix_data['data'])}")
|
||||
return True
|
||||
else:
|
||||
print(f"\n❌ Error: No se detectaron puertos vacíos")
|
||||
return False
|
||||
|
||||
if __name__ == "__main__":
|
||||
success = test_all_interfaces_functionality()
|
||||
sys.exit(0 if success else 1)
|
||||
Reference in New Issue
Block a user