get_lldp_neighbors.py 8.6 KB
Newer Older
1
2
3
# ----------------------------------------------------------------------
# NSN.TIMOS.get_lldp_neighbors
# ----------------------------------------------------------------------
bee26's avatar
bee26 committed
4
# Copyright (C) 2007-2020 The NOC Project
5
6
7
8
9
# See LICENSE for details
# ----------------------------------------------------------------------

# Python modules
import re
bee26's avatar
bee26 committed
10
import codecs
Dmitry Volodin's avatar
Dmitry Volodin committed
11

12
13
14
# NOC modules
from noc.core.script.base import BaseScript
from noc.sa.interfaces.igetlldpneighbors import IGetLLDPNeighbors
bee26's avatar
bee26 committed
15
16
17
18
19
from noc.core.mac import MAC
from noc.core.lldp import (
    LLDP_PORT_SUBTYPE_ALIAS,
    LLDP_PORT_SUBTYPE_MAC,
    LLDP_PORT_SUBTYPE_COMPONENT,
bee26's avatar
bee26 committed
20
    LLDP_PORT_SUBTYPE_LOCAL,
bee26's avatar
bee26 committed
21
    LLDP_CHASSIS_SUBTYPE_MAC,
bee26's avatar
bee26 committed
22
23
24
25
26
27
28
29
30
    LLDP_CAP_OTHER,
    LLDP_CAP_REPEATER,
    LLDP_CAP_BRIDGE,
    LLDP_CAP_WLAN_ACCESS_POINT,
    LLDP_CAP_ROUTER,
    LLDP_CAP_TELEPHONE,
    LLDP_CAP_DOCSIS_CABLE_DEVICE,
    LLDP_CAP_STATION_ONLY,
    lldp_caps_to_bits,
bee26's avatar
bee26 committed
31
32
33
)
from noc.core.comp import smart_text
from noc.core.snmp.render import render_bin
34
35
36
37
38
39


class Script(BaseScript):
    name = "NSN.TIMOS.get_lldp_neighbors"
    interface = IGetLLDPNeighbors

bee26's avatar
bee26 committed
40
41
42
    rx_some = re.compile(r"^(?P<port>((esat-|)\d+/\d+/(c\d+|\d+)(/\d+|/u\d+|)|\w+\/\d+))\s+")

    rx_local_lldp = re.compile(r"""\s+?:\s(?P<local_interface_id>.+?)\s""")
43
    rx_remote_info = re.compile(
bee26's avatar
bee26 committed
44
45
46
47
48
49
50
51
52
53
54
55
56
57
        r"""
        Supported\sCaps\s+:\s(?P<remote_capabilities>.+?)\n
        .*?
        Chassis\sId\sSubtype\s+:\s(?P<remote_chassis_id_subtype>\d)\s
        .*?
        Chassis\sId\s+:\s(?P<remote_chassis_id>\S+)\n
        .*?
        PortId\sSubtype\s+:\s(?P<remote_port_subtype>.)\s
        .*?
        Port\sId\s+:\s(?P<remote_port>.+?)("|Port)
        .*?
        System\sName\s+:\s(?P<remote_system_name>\S+).+
        """,
        re.MULTILINE | re.DOTALL | re.VERBOSE,
58
    )
59

bee26's avatar
bee26 committed
60
61
62
63
    @staticmethod
    def fixport(port, port_type):
        # fix alcatel encode port like hex string
        if port_type == "5" and "\n " in port:
64
65
66
67
68
            # PortId Subtype        : 5 (interfaceName)
            # Port Id               : 65:73:61:74:2D:32:2F:31:2F:32:31
            #                         "esat-2/1/21"
            remote_port_name1, remote_port__name2 = port.split("\n", 1)
            return smart_text(codecs.decode(remote_port_name1.strip().replace(":", ""), "hex"))
bee26's avatar
bee26 committed
69
70
        elif port_type == "7":
            return port.replace("\n", "")
71
        return port
bee26's avatar
bee26 committed
72

73
74
75
76
77
78
79
80
    def get_port_info(self, port):
        try:
            v = self.cli("show port %s ethernet lldp remote-info" % port)
        except self.CLISyntaxError:
            raise self.NotSupportedError()
        else:
            match_obj = self.rx_remote_info.search(v)
            pri = match_obj.groupdict()
bee26's avatar
bee26 committed
81
82
83
84
85
86
87
88
89
90
91
92
93
            pri["remote_capabilities"] = lldp_caps_to_bits(
                pri["remote_capabilities"].split(),
                {
                    "other": LLDP_CAP_OTHER,
                    "repeater": LLDP_CAP_REPEATER,
                    "bridge": LLDP_CAP_BRIDGE,
                    "wlanaccesspoint": LLDP_CAP_WLAN_ACCESS_POINT,
                    "router": LLDP_CAP_ROUTER,
                    "telephone": LLDP_CAP_TELEPHONE,
                    "cvlan": LLDP_CAP_DOCSIS_CABLE_DEVICE,
                    "station": LLDP_CAP_STATION_ONLY,
                },
            )
bee26's avatar
bee26 committed
94
            pri["remote_port"] = self.fixport(pri["remote_port"], pri["remote_port_subtype"])
Dmitry Volodin's avatar
Dmitry Volodin committed
95
96
            if "n/a" in pri["remote_system_name"]:
                del pri["remote_system_name"]
97
98
99
100
101
102
103
104
105
106
107
108
            return pri

    def execute_cli(self):
        r = []
        try:
            v = self.cli("show system lldp neighbor")
        except self.CLISyntaxError:
            raise self.NotSupportedError()
        for line in v.splitlines():
            match = self.rx_some.search(line)
            if not match:
                continue
Dmitry Volodin's avatar
Dmitry Volodin committed
109
110
            port = match.group("port")
            local_lldp = self.cli("show port %s ethernet detail | match IfIndex" % port)
111
112
113
            lldp_match = self.rx_local_lldp.search(local_lldp)
            if not lldp_match:
                continue
Dmitry Volodin's avatar
Dmitry Volodin committed
114
            local_interface_id = str(lldp_match.group("local_interface_id"))
115
            pri = self.get_port_info(port)
Dmitry Volodin's avatar
Dmitry Volodin committed
116
117
118
119
120
121
122
            r += [
                {
                    "local_interface": port,
                    "local_interface_id": local_interface_id,
                    "neighbors": [pri],
                }
            ]
123
        return r
bee26's avatar
bee26 committed
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230

    # get lldp snmp
    # Lookup from different tables if port type is `Iface alias`
    # 1 - LLDP-MIB::lldpLocPortId
    # 2 - LLDP-MIB::lldpLocPortDesc
    LLDP_PORT_TABLE = 2

    def get_interface_alias(self, port_id, port_descr):
        if self.LLDP_PORT_TABLE == 1:
            return port_id
        else:
            return port_descr

    def get_local_iface(self):
        r = {}
        names = {x: y for y, x in self.scripts.get_ifindexes().items()}
        # Get LocalPort Table
        for port_num, port_subtype, port_id, port_descr in self.snmp.get_tables(
            [
                "1.3.6.1.4.1.6527.3.1.2.59.3.1.1.2",
                "1.3.6.1.4.1.6527.3.1.2.59.3.1.1.3",
                "1.3.6.1.4.1.6527.3.1.2.59.3.1.1.4",
            ],
            max_retries=1,
        ):
            port_num = port_num.split(".")[0]
            if port_subtype == LLDP_PORT_SUBTYPE_ALIAS:
                # Iface alias
                iface_name = self.get_interface_alias(port_id, port_descr)
            elif port_subtype == LLDP_PORT_SUBTYPE_MAC:
                # Iface MAC address
                raise NotImplementedError()
            elif port_subtype == LLDP_PORT_SUBTYPE_LOCAL:
                # Iface local (ifindex)
                iface_name = names[int(port_num)]
            else:
                # Iface local
                iface_name = port_id

            r[port_num] = {"local_interface": iface_name, "local_interface_subtype": port_subtype}
        if not r:
            self.logger.warning("Not getting local LLDP port mappings. Check lldp table")
            raise NotImplementedError()
        return r

    def execute_snmp(self):
        neighb = (
            "remote_chassis_id_subtype",
            "remote_chassis_id",
            "remote_port_subtype",
            "remote_port",
            "remote_port_description",
            "remote_system_name",
        )
        r = []
        local_ports = self.get_local_iface()
        for v in self.snmp.get_tables(
            [
                "1.3.6.1.4.1.6527.3.1.2.59.4.1.1.2",
                "1.3.6.1.4.1.6527.3.1.2.59.4.1.1.4",
                "1.3.6.1.4.1.6527.3.1.2.59.4.1.1.5",
                "1.3.6.1.4.1.6527.3.1.2.59.4.1.1.6",
                "1.3.6.1.4.1.6527.3.1.2.59.4.1.1.7",
                "1.3.6.1.4.1.6527.3.1.2.59.4.1.1.8",
                "1.3.6.1.4.1.6527.3.1.2.59.4.1.1.9",
            ],
            bulk=False,
            max_retries=1,
            display_hints={
                "1.3.6.1.4.1.6527.3.1.2.59.4.1.1.7": render_bin,
                "1.3.6.1.4.1.6527.3.1.2.59.4.1.1.5": render_bin,
            },
        ):
            if v:
                neigh = dict(zip(neighb, v[2:]))
                # cleaning
                if neigh["remote_port_subtype"] == LLDP_PORT_SUBTYPE_COMPONENT:
                    neigh["remote_port_subtype"] = LLDP_PORT_SUBTYPE_ALIAS
                neigh["remote_port"] = neigh["remote_port"].replace(b" \x00", b"")
                if neigh["remote_chassis_id_subtype"] == LLDP_CHASSIS_SUBTYPE_MAC:
                    neigh["remote_chassis_id"] = MAC(neigh["remote_chassis_id"])
                if neigh["remote_port_subtype"] == LLDP_PORT_SUBTYPE_MAC:
                    try:
                        neigh["remote_port"] = MAC(neigh["remote_port"])
                    except ValueError:
                        self.logger.warning(
                            "Bad MAC address on Remote Neighbor: %s", neigh["remote_port"]
                        )
                else:
                    neigh["remote_port"] = smart_text(neigh["remote_port"]).rstrip("\x00")
                if "remote_system_name" in neigh:
                    neigh["remote_system_name"] = smart_text(neigh["remote_system_name"]).rstrip(
                        "\x00"
                    )
                if "remote_port_description" in neigh:
                    neigh["remote_port_description"] = neigh["remote_port_description"].rstrip(
                        "\x00"
                    )
                r += [
                    {
                        "local_interface": local_ports[v[0].split(".")[1]]["local_interface"],
                        # @todo if local interface subtype != 5
                        # "local_interface_id": 5,
                        "neighbors": [neigh],
                    }
                ]
        return r