Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- ##----------------------------------------------------------------------
- ## EdgeCore.ES.get_interfaces
- ##----------------------------------------------------------------------
- ## Copyright (C) 2007-2012 The NOC Project
- ## See LICENSE for details
- ##----------------------------------------------------------------------
- """
- """
- # Python modules
- import re
- from collections import defaultdict
- # NOC modules
- from noc.sa.script import Script as NOCScript
- from noc.sa.interfaces import IGetInterfaces, InterfaceTypeError, MACAddressParameter
- class Script(NOCScript):
- name = "EdgeCore.ES.get_interfaces"
- implements = [IGetInterfaces]
- TIMEOUT = 240
- types = {
- "Eth": "physical",
- "Trunk": "aggregated",
- }
- rx_if_admstat_3526 = re.compile(r"Port Admin:\s+(?P<admstatus>up|down)\n",
- re.MULTILINE | re.IGNORECASE | re.DOTALL)
- rx_if_intstat_3526 = re.compile(r".*?Name:[^\n]* (?P<descr>[^\n]*?)\n.*?Link Status:\s+(?P<intstatus>up|down)\n",
- re.MULTILINE | re.IGNORECASE | re.DOTALL)
- rx_if_3526 = re.compile(r"Information of (?P<name>[^\n]+?)\n.*?Mac Address:\s+(?P<mac>[^\n]+?)\n(?P<block>.*)",
- re.MULTILINE | re.IGNORECASE | re.DOTALL)
- def execute(self):
- ifaces = {}
- current = None
- is_bundle = False
- print "bla"
- raise
- ae_map = {} # member -> bundle
- v = self.cli("show interface status")
- for l in v.splitlines():
- match = self.rx_if_3526.match(l)
- if match:
- current = match.group("name")
- mac = MACAddressParameter().clean(match.group("mac"))
- ifaces[current] = {
- "name": current,
- "mac": mac
- }
- is_bundle = current.startswith("Trunk")
- if is_bundle:
- members = []
- type = "aggregated"
- ifaces[current] = {
- "members": members,
- "type": type
- }
- else:
- type = "physical"
- ifaces[current]["type"] = type
- elif not current:
- continue
- print "blablabla"
- raise
- l = l.strip()
- # Process description and oper_status
- match = self.rx_if_intstat_3526.match(l)
- if match:
- description = match.group("descr")
- intstatus = match.group("intstatus") == "Up"
- ifaces[current] = {
- "description": description,
- "oper_status": intstatus
- }
- continue
- # Process admin status
- match = self.rx_if_admstat_3526.match(l)
- if match:
- admstatus = match.group("admstatus") == "Up"
- ifaces[current] = {
- "admin_status": admstatus
- }
- continue
- # Process IP addresses
- # match = self.rx_ip.match(l)
- # if match:
- # ip = match.group("ip")
- # if ip.lower() != "unknown":
- # ifaces[current]["addresses"] = (
- # ifaces[current].get("addresses", []) +
- # [ip]
- # )
- # continue
- # Process hardware type and
- # match = self.rx_hw.match(l)
- # if match:
- # hw = match.group("hw").lower()
- # t = self.types.get(hw, "unknown")
- # if t == "unknown":
- # self.error("Unknown hardware type: %s" % hw)
- # ifaces[current]["type"] = t
- # mac = match.group("mac")
- # if mac:
- # ifaces[current]["mac"] = mac
- # Process VLAN id
- # match = self.rx_vlan_id.match(l)
- # if match:
- # ifaces[current]["vlan_ids"] = [int(match.group("vlan"))]
- # Process ethernet bundles
- if is_bundle:
- portchannel_members = {} # member)
- for s in self.scripts.get_portchannel():
- portchannel_members[s["interface"]] = s["members"]
- # match = self.rx_bundle_member.match(l)
- # if match:
- m = [portchannel_members.get(current, "")]
- ifaces[current]["members"] += [m]
- if "." in current:
- ae_map[m] = current.split(".", 1)[0]
- else:
- ae_map[m] = current
- # Get VRFs and "default" VRF interfaces
- r = []
- seen = set()
- # vpns = self.scripts.get_mpls_vpn()
- # for v in vpns:
- # seen.update(v["interfaces"])
- vpns = [{
- "name": "default",
- "type": "ip",
- "interfaces": set(ifaces) - seen
- }] + vpns
- # Bring result together
- for fi in vpns:
- # Forwarding instance
- rr = {
- "forwarding_instance": fi["name"],
- "type": fi["type"],
- "interfaces": []
- }
- rd = fi.get("rd")
- if rd:
- rr["rd"] = rd
- # Get interface -> subinterfaces mapping
- p_ifaces = dict((x, []) for x in
- set(i.split(".", 1)[0]
- for i in fi["interfaces"]))
- for i in fi["interfaces"]:
- p = i.split(".", 1)[0]
- p_ifaces[p] += [i]
- # Create interfaces
- for iface in p_ifaces:
- if iface not in ifaces:
- continue
- i = ifaces[iface]
- p = {
- "name": iface,
- "type": i["type"],
- "admin_status": i["status"],
- "oper_status": i["status"],
- "subinterfaces": [],
- }
- if i.get("mac"):
- p["mac"] = i["mac"]
- if i.get("description"):
- p["description"] = i["description"]
- if iface in ae_map:
- # Bundle member
- p["aggregated_interface"] = ae_map[iface]
- else:
- # Create subinterfaces
- for siface in p_ifaces[iface]:
- if siface not in ifaces:
- continue
- ii = ifaces[siface]
- sp = {
- "name": siface,
- "admin_status": ii["status"],
- "oper_status": ii["status"],
- }
- if ii.get("mac"):
- sp["mac"] = ii["mac"]
- if ii.get("description"):
- sp["description"] = ii["description"]
- # if ii.get("vlan_ids"):
- # sp["vlan_ids"] = ii["vlan_ids"]
- # Process addresses
- if "addresses" in ii:
- ipv4_addresses = [a for a in ii["addresses"] if ":" not in a]
- ipv6_addresses = [a for a in ii["addresses"] if ":" in a]
- if ipv4_addresses:
- sp["is_ipv4"] = True
- sp["ipv4_addresses"] = ipv4_addresses
- if ipv6_addresses:
- sp["is_ipv6"] = True
- sp["ipv6_addresses"] = ipv6_addresses
- p["subinterfaces"] += [sp]
- rr["interfaces"] += [p]
- r += [rr]
- # Return result
- return r
Advertisement
Add Comment
Please, Sign In to add comment