get_switchport.py 12.4 KB
Newer Older
1
# -*- coding: utf-8 -*-
Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
2
3
4
# ---------------------------------------------------------------------
# Qtech.QSW.get_switchport
# ---------------------------------------------------------------------
Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
5
# Copyright (C) 2007-2017 The NOC Project
Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
6
7
# See LICENSE for details
# ---------------------------------------------------------------------
8

Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
9
# Python modules
10
import re
Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
11
# NOC modules
Dmitry Volodin's avatar
Dmitry Volodin committed
12
13
from noc.core.script.base import BaseScript
from noc.sa.interfaces.igetswitchport import IGetSwitchport
14
15


Dmitry Volodin's avatar
Dmitry Volodin committed
16
class Script(BaseScript):
17
    name = "Qtech.QSW.get_switchport"
Dmitry Volodin's avatar
Dmitry Volodin committed
18
    interface = IGetSwitchport
19
20

    rx_interface = re.compile(
Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
21
        r"^\s*(?:Fast|Gigabit)?\s*Ethernet\s+(?P<interface>\S+)\s+(?:is|current state:)\s+(enabled|disabled),\s+port\s+link\s+is\s+(up|down)")
22
23
24
25
    rx_mode = re.compile(r"^\s*Port\s+mode\s*:\s*(?P<mode>\S+)$")
    rx_vlan_t = re.compile(r"^\s*Vlan\s+allowed\s*:\s*(?P<vlans>\S+)$")
    rx_vlan_at = re.compile(r"^\s*Tagged\s+VLAN\s+ID\s*:\s*(?P<vlans>\S+)$")
    rx_vlan_au = re.compile(r"^\s*Untagged\s+VLAN\s+ID\s*:\s*(?P<vlans>\S+)$")
MaksimSmile13's avatar
MaksimSmile13 committed
26
    rx_description = re.compile(r"^\s*(?P<interface>e\S+)(?P<description>.*)\n", re.MULTILINE)
27

MaksimSmile13's avatar
MaksimSmile13 committed
28
29
30
31
    """
    rx_channel_description = re.compile(r"^(?P<interface>Po\d+)\s+((?P<description>\S+)|)$", re.MULTILINE)
    rx_vlan_stack = re.compile(r"^(?P<interface>\S+)\s+(?P<role>\S+)\s*$", re.IGNORECASE)  # TODO
    """
32
    def execute(self):
MaksimSmile13's avatar
MaksimSmile13 committed
33
        # TODO
34
        # Get portchannels
MaksimSmile13's avatar
MaksimSmile13 committed
35
        # portchannels = self.scripts.get_portchannel()
36
37
        portchannels = []
        portchannel_members = []
MaksimSmile13's avatar
MaksimSmile13 committed
38
39
        # for p in portchannels:
        #    portchannel_members += p["members"]
40
41
42
43
44
45
46

        # Get interafces status
        interface_status = {}
        port_vlans = {}
        for s in self.scripts.get_interface_status():
            interface_status[s["interface"]] = s["status"]

MaksimSmile13's avatar
MaksimSmile13 committed
47
        # TODO
48
49
50
51
52
53
54
55
56
57
58
59
60
61
        # Get 802.1ad status if supported
        vlan_stack_status = {}
        """
        try:
            cmd = self.cli("show vlan-stacking")
            for match in self.rx_vlan_stack.finditer(cmd):
                if match.group("role").lower() == "tunnel":
                    vlan_stack_status[int(match.group("interface"))] = True
        except self.CLISyntaxError:
            pass
        """
        # Try snmp first
        """
        # SNMP not working!
Dmitry Volodin's avatar
Dmitry Volodin committed
62
        if self.has_snmp():
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
            def hex2bin(ports):
                bin = [
                    '0000', '0001', '0010', '0011',
                    '0100', '0101', '0110', '0111',
                    '1000', '1001', '1010', '1011',
                    '1100', '1101', '1110', '1111',
                    ]
                ports = ["%02x" % ord(c) for c in ports]
                p = ''
                for c in ports:
                   for i in range(len(c)):
                        p += bin[int(c[i], 16)]
                return p
            try:
                # Make a list of tags for each interface or portchannel
                port_vlans = {}
                for v in self.snmp.get_tables(
                    ["1.3.6.1.2.1.17.7.1.4.2.1.3",
                    "1.3.6.1.2.1.17.7.1.4.2.1.4",
                    "1.3.6.1.2.1.17.7.1.4.2.1.5"], bulk=True):
                    tagged = v[2]
                    untagged = v[3]

                    s = hex2bin(untagged)
                    un = []
                    for i in range(len(s)):
                        if s[i] == '1':
                            oid = "1.3.6.1.2.1.31.1.1.1.1." + str(i + 1)
                            iface = self.snmp.get(oid, cached=True)
                            if iface not in port_vlans:
                                port_vlans.update(
                                    {iface: {
                                        "tagged": [],
                                        "untagged": '',
                                        }
                                    })
                            port_vlans[iface]["untagged"] = v[1]
                            un += [str(i + 1)]

                    s = hex2bin(tagged)
                    for i in range(len(s)):
                        if s[i] == '1' and str(i + 1) not in un:
                            oid = "1.3.6.1.2.1.31.1.1.1.1." + str(i + 1)
                            iface = self.snmp.get(oid, cached=True)
                            if iface not in port_vlans:
                                port_vlans.update(
                                    {iface: {
                                        "tagged": [],
                                        "untagged": '',
                                        }
                                    })
                            port_vlans[iface]["tagged"].append(v[1])

                # Get switchport description
                port_descr = {}
                for iface, description in self.snmp.join_tables(
119
                    "1.3.6.1.2.1.31.1.1.1.1", "1.3.6.1.2.1.31.1.1.1.18"):
120
121
122
123
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
                    port_descr.update({iface: description})

                # Get switchport data and overall result
                r = []
                swp = {}
                write = False
                for name in interface_status:
                    if name in portchannel_members:
                        for p in portchannels:
                            if name in p["members"]:
                                name = p["interface"]
                                status = False
                                for interface in p["members"]:
                                    if interface_status.get(interface):
                                        status = True
                                description = port_descr[name]
                                if not description:
                                    description = ''
                                members = p["members"]
                                portchannels.remove(p)
                                write = True
                                break
                    else:
                        if interface_status.get(name):
                            status = True
                        else:
                            status = False
                        description = port_descr[name]
                        if not description:
                            description = ''
                        members = []
                        write = True
                    if write:
                        if name not in port_vlans:
                            tagged = []
                        else:
                            tagged = port_vlans[name]["tagged"]
                        swp = {
                                "status": status,
                                "description": description,
                                "802.1Q Enabled": len(port_vlans.get(name,
                                                '')) > 0,
                                "802.1ad Tunnel": vlan_stack_status.get(name,
                                                False),
                                "tagged": tagged,
                                }
                        if name in port_vlans:
                            if port_vlans[name]["untagged"]:
                                swp["untagged"] = port_vlans[name]["untagged"]
                        swp["interface"] = name
                        swp["members"] = members
                        r.append(swp)
                        write = False
                return r
            except self.snmp.TimeOutError:
                pass
        """

        # Fallback to CLI
        # Make a list of tags for each interface or portchannel
        port_vlans = {}
        port_channels = portchannels

        iface_conf = self.cli("show interface")
        # Correct Qtech BUG:
MaksimSmile13's avatar
MaksimSmile13 committed
185
186
        iface_conf = iface_conf.replace(
            "\n\n                                                                          ", "\n")
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
        iface_conf = iface_conf.splitlines()
        i = 0
        L = len(iface_conf) - 2
        while i < L:
            match = self.rx_interface.match(iface_conf[i])
            if match:
                interface = match.group("interface")
            else:
                i += 1
                continue

            if interface in portchannel_members:
                for p in port_channels:
                    if interface in p["members"]:
                        interface = p["interface"]
                        port_channels.remove(p)
                        break
            if interface not in port_vlans:
                port_vlans.update({interface: {
MaksimSmile13's avatar
MaksimSmile13 committed
206
207
208
209
                    "tagged": [],
                    "untagged": '',
                }
                })
210
211
212
213
214
215
216
217
218
219
220
221
222

            i += 1
            match_mod = self.rx_mode.match(iface_conf[i])
            while not match_mod:
                i += 1
                match_mod = self.rx_mode.match(iface_conf[i])
            mode = match_mod.group("mode")

            i += 1
            if 'trunk' == mode:
                match = self.rx_vlan_t.match(iface_conf[i])
                if match:
                    vlans = match.group("vlans")
223
224
                    if vlans == "all":
                        vlans = "1-4096"
225
226
227
228
229
230
231
                    list_vlans = self.expand_rangelist(vlans)
                    port_vlans[interface]["tagged"] = list_vlans

            elif 'access' == mode or 'hybrid' == mode:
                match = self.rx_vlan_at.match(iface_conf[i])
                if match:
                    vlans = match.group("vlans")
232
233
                    if vlans == "all":
                        vlans = "1-4096"
234
235
236
237
238
239
                    list_vlans = self.expand_rangelist(vlans)
                    port_vlans[interface]["tagged"] = list_vlans

                i += 1
                match = self.rx_vlan_au.match(iface_conf[i])
                if match:
Maksim's avatar
Maksim committed
240
241
242
243
244
                    vl = match.group("vlans")
                    vlans = vl.split(',')[0]
                    if "-" in vlans:
                        vlans = vlans.split("-")[0]
                    port_vlans[interface]["untagged"] = vlans
245
246
247
248

        iface_conf = []
        # Why portchannels=[] ???????
        # Get portchannels onse more!!!
MaksimSmile13's avatar
MaksimSmile13 committed
249
        #        portchannels = self.scripts.get_portchannel()
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
        portchannels = []
        # Get switchport data and overall result
        r = []
        swp = {}
        write = False
        cmd = self.cli("show description interface")
        for match in self.rx_description.finditer(cmd):
            name = match.group("interface")
            if name in portchannel_members:
                for p in portchannels:
                    if name in p["members"]:
                        name = p["interface"]
                        status = False
                        for interface in p["members"]:
                            if interface_status.get(interface):
                                status = True
                        cmd = "show description interface %s" % name
                        desc = self.cli(cmd)
                        match = self.rx_channel_description.search(desc)
                        if match:
                            description = match.group("description")
                            if not description:
                                description = ''
                        else:
                            description = ''
                        members = p["members"]
                        portchannels.remove(p)
                        write = True
                        break
            else:
                if interface_status.get(name):
                    status = True
                else:
                    status = False
                description = match.group("description")
                if not description:
                    description = ''
                members = []
                write = True
            if write:
290
291
292
293
                if name not in port_vlans:
                    tagged = []
                else:
                    tagged = port_vlans[name]["tagged"]
294
                swp = {
MaksimSmile13's avatar
MaksimSmile13 committed
295
296
297
298
299
300
                    "status": status,
                    "description": description.strip(),
                    "802.1Q Enabled": len(port_vlans.get(name, [])) > 0,
                    "802.1ad Tunnel": vlan_stack_status.get(name, False),
                    "tagged": tagged,
                }
301
302
303
                if name in port_vlans:
                    if port_vlans[name]["untagged"]:
                        swp["untagged"] = port_vlans[name]["untagged"]
MaksimSmile13's avatar
MaksimSmile13 committed
304
                swp["interface"] = name
305
306
307
308
                swp["members"] = members
                r.append(swp)
                write = False
        return r