Advertisement
ss_

cat sa/profiles/Juniper/JUNOS/get_lldp_neighbors.py

ss_
Feb 20th, 2013
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.56 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. ##----------------------------------------------------------------------
  3. ## Juniper.JUNOS.get_lldp_neighbors
  4. ##----------------------------------------------------------------------
  5. ## Copyright (C) 2007-2013 The NOC Project
  6. ## See LICENSE for details
  7. ##----------------------------------------------------------------------
  8. """
  9. """
  10. ## Python modules
  11. import re
  12. import binascii
  13. ## NOC modules
  14. from noc.sa.script import Script as NOCScript
  15. from noc.sa.interfaces import (IGetLLDPNeighbors, IntParameter,
  16.                                MACAddressParameter, InterfaceTypeError)
  17.  
  18.  
  19. class Script(NOCScript):
  20.     name = "Juniper.JUNOS.get_lldp_neighbors"
  21.     implements = [IGetLLDPNeighbors]
  22.     ##
  23.     ## EX Series
  24.     ##
  25.     rx_localport = re.compile(r"^(\S+?)\s+?(\d+?)\s+?\S+?\s+?Up.+?$",
  26.         re.MULTILINE | re.DOTALL)
  27.     rx_neigh = re.compile(r"(?P<local_if>.e-\S+?)\s.*?$",
  28.         re.MULTILINE | re.IGNORECASE)
  29.     # If <p_type>=='Interface alias', then <p_id> will match 'Port description'
  30.     # else it will match 'Port ID'
  31.     rx_detail = re.compile(
  32.         r".*Chassis ID\s+:\s(?P<id>\S+).*?"
  33.         r"Port type\s+:\s(?P<p_type>[^\n]+).*?"
  34.         r"Port \S+\s+:\s(?P<p_id>[^\n]+).*?"
  35.         r"(?:System name\s+:\s(?P<name>\S+).*?)?"
  36.         r"(?:System capabilities.+?Supported:\s(?P<capability>[^\n]+).*)?",
  37.         re.MULTILINE | re.IGNORECASE | re.DOTALL)
  38.     rx_detail_mx = re.compile(
  39.         r".*Chassis ID\s+:\s(?P<id>\S+).*?"
  40.         r"Port type\s+:\s(?P<p_type>[^\n]+).*?"
  41.         r"Port \S+\s+:\s(?P<p_id>[^\n]+).*?"
  42.         r"System name\s+:\s(?P<name>\S+)?"
  43.         r"(?:System capabilities.+?Supported:\s(?P<capability>[^\n]+).*)?",
  44.         re.MULTILINE | re.IGNORECASE | re.DOTALL)
  45.  
  46.     @NOCScript.match(platform__startswith="ex")
  47.     def execute_ex(self):
  48.         ifs = []
  49.         r = []
  50.         # Collect data
  51.         local_port_ids = {}  # name -> id
  52.         for port, local_id in self.rx_localport.findall(self.cli("show lldp local-information")):
  53.             local_port_ids[port] = IntParameter().clean(local_id)
  54.         v = self.cli("show lldp neighbors")
  55.         for match in self.rx_neigh.finditer(v):
  56.             ifs += [{
  57.                 "local_interface": match.group("local_if"),
  58.                 "neighbors": [],
  59.             }]
  60.         for i in ifs:
  61.             if i["local_interface"] in local_port_ids:
  62.                 i["local_interface_id"] = local_port_ids[i["local_interface"]]
  63.             v = self.cli("show lldp neighbors interface %s" % i["local_interface"])
  64.             match = self.re_search(self.rx_detail, v)
  65.             n = {"remote_chassis_id_subtype": 4}
  66.             if match:
  67.                 n["remote_port_subtype"] = {
  68.                     "Mac address": 3,
  69.                     "Interface alias": 5,
  70.                     "Locally assigned": 7
  71.                 }[match.group("p_type")]
  72.                 if n["remote_port_subtype"] == 3:
  73.                     remote_port = MACAddressParameter().clean(match.group("p_id"))
  74.                 elif n["remote_port_subtype"] == 7:
  75.                     p_id = match.group("p_id")
  76.                     try:
  77.                         remote_port = IntParameter().clean(p_id)
  78.                     except InterfaceTypeError:
  79.                         remote_port = p_id
  80.                 else:
  81.                     remote_port = match.group("p_id")
  82.                 n["remote_chassis_id"] = match.group("id")
  83.                 n["remote_system_name"] = match.group("name")
  84.                 n["remote_port"] = remote_port
  85.                 # Get capability
  86.                 cap = 0
  87.                 if match.group("capability"):
  88.                     for c in match.group("capability").strip().split(" "):
  89.                             cap |= {
  90.                             "Other": 1, "Repeater": 2, "Bridge": 4,
  91.                             "WLAN": 8, "Router": 16, "Telephone": 32,
  92.                             "Cable": 64, "Station": 128
  93.                             }[c]
  94.                 n["remote_capabilities"] = cap
  95.             i["neighbors"] += [n]
  96.             r += [i]
  97.         return r
  98.  
  99.     ##
  100.     ## No lldp on MX
  101.     @NOCScript.match(platform__startswith="mx")
  102.     def execute_ex(self):
  103.         ifs = []
  104.         r = []
  105.         # Collect data
  106.         local_port_ids = {}  # name -> id
  107.         for port, local_id in self.rx_localport.findall(self.cli("show lldp local-information")):
  108.             local_port_ids[port] = IntParameter().clean(local_id)
  109.         v = self.cli("show lldp neighbors")
  110.         for match in self.rx_neigh.finditer(v):
  111.             ifs += [{
  112.                 "local_interface": match.group("local_if"),
  113.                 "neighbors": [],
  114.             }]
  115.         for i in ifs:
  116.             if i["local_interface"] in local_port_ids:
  117.                 i["local_interface_id"] = local_port_ids[i["local_interface"]]
  118.             v = self.cli("show lldp neighbors interface %s" % i["local_interface"])
  119.             match = self.re_search(self.rx_detail_mx, v)
  120.             n = {"remote_chassis_id_subtype": 4}
  121.             if match:
  122.                 n["remote_port_subtype"] = {
  123.                     "Mac address": 3,
  124.                     "Interface name": 5,
  125.                     "Locally assigned": 7
  126.                 }[match.group("p_type")]
  127.                 if n["remote_port_subtype"] == 3:
  128.                     remote_port = MACAddressParameter().clean(match.group("p_id"))
  129.                 elif n["remote_port_subtype"] == 7:
  130.                     p_id = match.group("p_id")
  131.                     try:
  132.                         remote_port = IntParameter().clean(p_id)
  133.                     except InterfaceTypeError:
  134.                         remote_port = p_id
  135.                 else:
  136.                     remote_port = match.group("p_id")
  137.                 n["remote_chassis_id"] = match.group("id")
  138.                 n["remote_system_name"] = match.group("name")
  139.                 n["remote_port"] = remote_port
  140.                 # Get capability
  141.                 cap = 0
  142.                 if match.group("capability"):
  143.                     for c in match.group("capability").strip().split(" "):
  144.                             cap |= {
  145.                             "Other": 1, "Repeater": 2, "Bridge": 4,
  146.                             "WLAN": 8, "Router": 16, "Telephone": 32,
  147.                             "Cable": 64, "Station": 128
  148.                             }[c]
  149.                 n["remote_capabilities"] = cap
  150.             i["neighbors"] += [n]
  151.             r += [i]
  152.         return r
  153.  
  154.  
  155.     ##
  156.     @NOCScript.match()
  157.     def execute_other(self):
  158.         raise self.NotSupportedError()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement