139 lines
4.4 KiB
Python
139 lines
4.4 KiB
Python
from typing import List, Optional
|
|
import re
|
|
|
|
def normalize_port_name(port: str) -> str:
|
|
port = port.upper()
|
|
port = port.replace("GE", "GIGABITETHERNET")
|
|
port = port.replace("XGE", "TEN-GIGABITETHERNET")
|
|
port = port.replace("BAGG", "BRIDGE-AGGREGATION")
|
|
return port
|
|
|
|
class PowerSupply:
|
|
def __init__(self, id: int, state: str, mode: Optional[str] = None):
|
|
self.id = id
|
|
self.state = state
|
|
self.mode = mode
|
|
self.summary = f"Alimentation {id} en état {state}"
|
|
|
|
def to_dict(self):
|
|
return {
|
|
"object": "power_supply",
|
|
"id": self.id,
|
|
"state": self.state,
|
|
"mode": self.mode,
|
|
"summary": self.summary,
|
|
}
|
|
|
|
class Fan:
|
|
def __init__(self, id: int, state: str):
|
|
self.id = id
|
|
self.state = state
|
|
self.summary = f"Ventilateur {id} en état {state}"
|
|
|
|
def to_dict(self):
|
|
return {
|
|
"object": "fan",
|
|
"id": self.id,
|
|
"state": self.state,
|
|
"summary": self.summary,
|
|
}
|
|
|
|
class MACEntry:
|
|
def __init__(self, mac: str, vlan: int, port: str, type_: str):
|
|
self.mac = mac
|
|
self.vlan = vlan
|
|
self.port = port
|
|
self.type = type_
|
|
|
|
def to_dict(self):
|
|
return {
|
|
"object": "mac_entry",
|
|
"mac": self.mac,
|
|
"vlan": self.vlan,
|
|
"port": self.port,
|
|
"type": self.type,
|
|
}
|
|
|
|
class Port:
|
|
def __init__(self, name: str):
|
|
self.object = "port"
|
|
self.name = name
|
|
self.status = None
|
|
self.speed = None
|
|
self.vlan = None
|
|
self.link_type = None
|
|
self.trunk_vlans: List[int] = []
|
|
self.bpdu_protection = False
|
|
self.portfast = False
|
|
self.loopback_detection = None
|
|
self.mac_addresses: List[str] = []
|
|
self.summary = ""
|
|
|
|
def to_dict(self):
|
|
mac_count = len(self.mac_addresses)
|
|
mac_info = f", {mac_count} MAC" if mac_count else ""
|
|
vlan_info = f"TRUNK ({', '.join(map(str, sorted(self.trunk_vlans)))})" if self.link_type == "trunk" else f"VLAN {self.vlan or '?'}"
|
|
status = self.status.upper() if self.status else "INCONNU"
|
|
speed = self.speed or "vitesse inconnue"
|
|
self.summary = f"Port {self.name} est {status} à {speed}, {vlan_info}{mac_info}"
|
|
return {
|
|
"object": "port",
|
|
"name": self.name,
|
|
"status": self.status,
|
|
"speed": self.speed,
|
|
"vlan": self.vlan,
|
|
"link_type": self.link_type,
|
|
"trunk_vlans": ", ".join(map(str, self.trunk_vlans)) if self.link_type == "trunk" else None,
|
|
"bpdu_protection": self.bpdu_protection,
|
|
"portfast": self.portfast,
|
|
"loopback_detection": self.loopback_detection,
|
|
"mac_addresses": self.mac_addresses,
|
|
"summary": self.summary,
|
|
}
|
|
|
|
class VLAN:
|
|
def __init__(self, vlan_id: int, name: Optional[str] = None, description: Optional[str] = None):
|
|
self.vlan_id = vlan_id
|
|
self.name = name
|
|
self.description = description
|
|
self.summary = f"VLAN {vlan_id} : {name or 'non nommé'}"
|
|
|
|
def to_dict(self):
|
|
return {
|
|
"object": "vlan",
|
|
"vlan_id": self.vlan_id,
|
|
"name": self.name,
|
|
"description": self.description,
|
|
"summary": self.summary,
|
|
}
|
|
|
|
class Switch:
|
|
def __init__(self):
|
|
self.hostname = None
|
|
self.model = None
|
|
self.firmware_version = None
|
|
self.boot_image = None
|
|
self.uptime = None
|
|
self.ports: List[Port] = []
|
|
self.vlans: List[VLAN] = []
|
|
self.fans: List[Fan] = []
|
|
self.power_supplies: List[PowerSupply] = []
|
|
self.mac_table: List[MACEntry] = []
|
|
self.summary = ""
|
|
|
|
def to_dict(self):
|
|
return {
|
|
"object": "switch",
|
|
"hostname": self.hostname,
|
|
"model": self.model,
|
|
"firmware_version": self.firmware_version,
|
|
"boot_image": self.boot_image,
|
|
"uptime": self.uptime,
|
|
"summary": self.summary,
|
|
"fans": [f.to_dict() for f in self.fans],
|
|
"power_supplies": [p.to_dict() for p in self.power_supplies],
|
|
"ports": [p.to_dict() for p in self.ports],
|
|
"vlans": [v.to_dict() for v in self.vlans],
|
|
"mac_table": [m.to_dict() for m in self.mac_table],
|
|
}
|