Details
-
Type: New Feature
-
Status: Resolved
-
Priority: Major
-
Resolution: Fixed
-
Affects Version/s: None
-
Fix Version/s: None
-
Component/s: SA
-
Labels:None
-
Environment:
./scripts/about
NOC 0.7(4)r7550 OS Linux monitor 3.2.0-32-generic #51-Ubuntu SMP Wed Sep 26 21:33:09 UTC 2012 x86_64 OS Brand Ubuntu 12.10 Python 2.7.3 (default, Sep 26 2012, 21:51:14) [GCC 4.7.2] PostgreSQL PostgreSQL 9.0.5 MongoDB 2.2.0 (64bit) Python Path .:..:/opt/noc/scripts:/usr/local/lib/python2.7/dist-packages/distribute-0.6.32-py2.7.egg:/usr/lib/python2.7/site-packages/mercurial-2.4.1-py2.7-linux-x86_64.egg:/usr/lib/python2.7/site-packages/pip-1.2.1-py2.7.egg:/usr/local/lib/python2.7/dist-packages:/usr/lib/python2.7/site-packages/gmpy-1.16-py2.7-linux-x86_64.egg:/usr/lib/python2.7:/usr/lib/python2.7/plat-linux2:/usr/lib/python2.7/lib-tk:/usr/lib/python2.7/lib-old:/usr/lib/python2.7/lib-dynload:/usr/local/lib/python2.7/dist-packages:/usr/local/lib/python2.7/dist-packages/setuptools-0.6c11-py2.7.egg-info:/usr/lib/python2.7/dist-packages:/usr/lib/python2.7/dist-packages/gtk-2.0 psycopg2 2.4.5 (dt dec mx pq3 ext) pymongo 2.2 Sphinx 1.0.7 protobuf 2.4.1 tornado 2.2 python-creole 0.2.4 mongoengine 0.6.20 django 1.4.1 docutils 0.7 Pygments 1.2.2 Jinja2 2.2.1 coverage 3.5.1 pysnmp 4.2.1 pyasn1 0.1.2 south 0.7.3 django-tagging 0.3.1 ./scripts/about NOC 0.7(4)r7550 OS Linux monitor 3.2.0-32-generic #51-Ubuntu SMP Wed Sep 26 21:33:09 UTC 2012 x86_64 OS Brand Ubuntu 12.10 Python 2.7.3 (default, Sep 26 2012, 21:51:14) [GCC 4.7.2] PostgreSQL PostgreSQL 9.0.5 MongoDB 2.2.0 (64bit) Python Path .:..:/opt/noc/scripts:/usr/local/lib/python2.7/dist-packages/distribute-0.6.32-py2.7.egg:/usr/lib/python2.7/site-packages/mercurial-2.4.1-py2.7-linux-x86_64.egg:/usr/lib/python2.7/site-packages/pip-1.2.1-py2.7.egg:/usr/local/lib/python2.7/dist-packages:/usr/lib/python2.7/site-packages/gmpy-1.16-py2.7-linux-x86_64.egg:/usr/lib/python2.7:/usr/lib/python2.7/plat-linux2:/usr/lib/python2.7/lib-tk:/usr/lib/python2.7/lib-old:/usr/lib/python2.7/lib-dynload:/usr/local/lib/python2.7/dist-packages:/usr/local/lib/python2.7/dist-packages/setuptools-0.6c11-py2.7.egg-info:/usr/lib/python2.7/dist-packages:/usr/lib/python2.7/dist-packages/gtk-2.0 psycopg2 2.4.5 (dt dec mx pq3 ext) pymongo 2.2 Sphinx 1.0.7 protobuf 2.4.1 tornado 2.2 python-creole 0.2.4 mongoengine 0.6.20 django 1.4.1 docutils 0.7 Pygments 1.2.2 Jinja2 2.2.1 coverage 3.5.1 pysnmp 4.2.1 pyasn1 0.1.2 south 0.7.3 django-tagging 0.3.1
-
Difficulty:Easy
Description
# -*- coding: utf-8 -*- ##---------------------------------------------------------------------- ## Juniper.JUNOS.get_lldp_neighbors ##---------------------------------------------------------------------- ## Copyright (C) 2007-2013 The NOC Project ## See LICENSE for details ##---------------------------------------------------------------------- """ """ ## Python modules import re import binascii ## NOC modules from noc.sa.script import Script as NOCScript from noc.sa.interfaces import (IGetLLDPNeighbors, IntParameter, MACAddressParameter, InterfaceTypeError) class Script(NOCScript): name = "Juniper.JUNOS.get_lldp_neighbors" implements = [IGetLLDPNeighbors] ## ## EX Series ## rx_localport = re.compile(r"^(\S+?)\s+?(\d+?)\s+?\S+?\s+?Up.+?$", re.MULTILINE | re.DOTALL) rx_neigh = re.compile(r"(?P<local_if>.e-\S+?)\s.*?$", re.MULTILINE | re.IGNORECASE) # If <p_type>=='Interface alias', then <p_id> will match 'Port description' # else it will match 'Port ID' rx_detail = re.compile( r".*Chassis ID\s+:\s(?P<id>\S+).*?" r"Port type\s+:\s(?P<p_type>[^\n]+).*?" r"Port \S+\s+:\s(?P<p_id>[^\n]+).*?" r"(?:System name\s+:\s(?P<name>\S+).*?)?" r"(?:System capabilities.+?Supported:\s(?P<capability>[^\n]+).*)?", re.MULTILINE | re.IGNORECASE | re.DOTALL) rx_detail_mx = re.compile( r".*Chassis ID\s+:\s(?P<id>\S+).*?" r"Port type\s+:\s(?P<p_type>[^\n]+).*?" r"Port \S+\s+:\s(?P<p_id>[^\n]+).*?" r"System name\s+:\s(?P<name>\S+)?" r"(?:System capabilities.+?Supported:\s(?P<capability>[^\n]+).*)?", re.MULTILINE | re.IGNORECASE | re.DOTALL) @NOCScript.match(platform__startswith="ex") def execute_ex(self): ifs = [] r = [] # Collect data local_port_ids = {} # name -> id for port, local_id in self.rx_localport.findall(self.cli("show lldp local-information")): local_port_ids[port] = IntParameter().clean(local_id) v = self.cli("show lldp neighbors") for match in self.rx_neigh.finditer(v): ifs += [{ "local_interface": match.group("local_if"), "neighbors": [], }] for i in ifs: if i["local_interface"] in local_port_ids: i["local_interface_id"] = local_port_ids[i["local_interface"]] v = self.cli("show lldp neighbors interface %s" % i["local_interface"]) match = self.re_search(self.rx_detail, v) n = {"remote_chassis_id_subtype": 4} if match: n["remote_port_subtype"] = { "Mac address": 3, "Interface alias": 5, "Locally assigned": 7 }[match.group("p_type")] if n["remote_port_subtype"] == 3: remote_port = MACAddressParameter().clean(match.group("p_id")) elif n["remote_port_subtype"] == 7: p_id = match.group("p_id") try: remote_port = IntParameter().clean(p_id) except InterfaceTypeError: remote_port = p_id else: remote_port = match.group("p_id") n["remote_chassis_id"] = match.group("id") n["remote_system_name"] = match.group("name") n["remote_port"] = remote_port # Get capability cap = 0 if match.group("capability"): for c in match.group("capability").strip().split(" "): cap |= { "Other": 1, "Repeater": 2, "Bridge": 4, "WLAN": 8, "Router": 16, "Telephone": 32, "Cable": 64, "Station": 128 }[c] n["remote_capabilities"] = cap i["neighbors"] += [n] r += [i] return r ## ## lldp on MX @NOCScript.match(platform__startswith="mx") def execute_ex(self): ifs = [] r = [] # Collect data local_port_ids = {} # name -> id for port, local_id in self.rx_localport.findall(self.cli("show lldp local-information")): local_port_ids[port] = IntParameter().clean(local_id) v = self.cli("show lldp neighbors") for match in self.rx_neigh.finditer(v): ifs += [{ "local_interface": match.group("local_if"), "neighbors": [], }] for i in ifs: if i["local_interface"] in local_port_ids: i["local_interface_id"] = local_port_ids[i["local_interface"]] v = self.cli("show lldp neighbors interface %s" % i["local_interface"]) match = self.re_search(self.rx_detail_mx, v) n = {"remote_chassis_id_subtype": 4} if match: n["remote_port_subtype"] = { "Mac address": 3, "Interface name": 5, "Locally assigned": 7 }[match.group("p_type")] if n["remote_port_subtype"] == 3: remote_port = MACAddressParameter().clean(match.group("p_id")) elif n["remote_port_subtype"] == 7: p_id = match.group("p_id") try: remote_port = IntParameter().clean(p_id) except InterfaceTypeError: remote_port = p_id else: remote_port = match.group("p_id") n["remote_chassis_id"] = match.group("id") n["remote_system_name"] = match.group("name") n["remote_port"] = remote_port # Get capability cap = 0 if match.group("capability"): for c in match.group("capability").strip().split(" "): cap |= { "Other": 1, "Repeater": 2, "Bridge": 4, "WLAN": 8, "Router": 16, "Telephone": 32, "Cable": 64, "Station": 128 }[c] n["remote_capabilities"] = cap i["neighbors"] += [n] r += [i] return r ## @NOCScript.match() def execute_other(self): raise self.NotSupportedError()