get_interfaces.py 5.99 KB
Newer Older
1
2
3
# ---------------------------------------------------------------------
# NAG.SNR.get_interfaces
# ---------------------------------------------------------------------
4
# Copyright (C) 2007-2021 The NOC Project
5
6
# See LICENSE for details
# ---------------------------------------------------------------------
7

8
9
# Python modules
import re
Dmitry Volodin's avatar
Dmitry Volodin committed
10

11
12
13
# NOC modules
from noc.core.script.base import BaseScript
from noc.sa.interfaces.igetinterfaces import IGetInterfaces
Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
14
from noc.core.ip import IPv4
15
16
17
18
19
20
21


class Script(BaseScript):
    name = "NAG.SNR.get_interfaces"
    cache = True
    interface = IGetInterfaces

MaksimSmile13's avatar
MaksimSmile13 committed
22
23
    rx_lldp_en = re.compile(r"LLDP has been enabled globally?")
    rx_lldp = re.compile(r"LLDP enabled port : (?P<local_if>\S*.+)$", re.MULTILINE)
24
    rx_sh_int = re.compile(
Dmitry Lukhtionov's avatar
Fix    
Dmitry Lukhtionov committed
25
        r"^\s*(?P<interface>\S+)\s+is\s+(?P<admin_status>up|down|administratively down)(?:\s*\(\d\))?,\s+"
Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
26
        r"line protocol is\s+(?P<oper_status>up|down)"
Dmitry Lukhtionov's avatar
Fix    
Dmitry Lukhtionov committed
27
        r"(,(?: dev)? index is (?P<snmp_ifindex>\d+))?\s*\n"
Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
28
        r"(?P<other>(?:^\s+.+\n)+?)"
Dmitry Lukhtionov's avatar
Fix    
Dmitry Lukhtionov committed
29
        r"(?:^\s+Encapsulation |^\s+Output packets statistics:)",
Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
30
        re.MULTILINE,
Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
31
32
    )
    rx_hw = re.compile(
33
        r"^\s+Hardware is (?P<hw_type>\S+)(\(card not installed\))?(, active is \S+)?"
34
        r"(,\s+address is (?P<mac>\S+))?\s*\n",
Dmitry Volodin's avatar
Dmitry Volodin committed
35
        re.MULTILINE,
Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
36
    )
Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
37
    rx_alias = re.compile(r"\s+alias name is (?P<alias>\S+)\s", re.MULTILINE)
Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
38
    rx_index = re.compile(r", index is (?P<ifindex>\d+)")
39
    rx_alias_and_index = re.compile(r" alias name is (?P<alias>.+), index is (?P<ifindex>\d+)")
Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
40
41
42
    rx_mtu = re.compile(r"MTU (?P<mtu>\d+) bytes")
    rx_pvid = re.compile(r"PVID is (?P<pvid>\d+)")
    rx_ip = re.compile(
Dmitry Volodin's avatar
Dmitry Volodin committed
43
        r"^\s+IPv4 address is:\s*\n" r"^\s+(?P<ip>\S+)\s+(?P<mask>\S+)", re.MULTILINE
Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
44
45
46
47
48
49
50
    )
    rx_vlan = re.compile(
        r"^(?P<ifname>Ethernet\S+)\s*\n"
        r"^Type.+\s*\n"
        r"^Mode\s*:\s*(?P<mode>\S+)\s*\n"
        r"^Port VID\s*:\s*(?P<untagged_vlan>\d+)\s*\n"
        r"(^Trunk allowed Vlan\s*:\s*(?P<tagged_vlans>\S+)\s*\n)?",
Dmitry Volodin's avatar
Dmitry Volodin committed
51
        re.MULTILINE,
Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
52
    )
Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
53
    rx_lag_port = re.compile(r"\s*\S+ is LAG member port, LAG port:(?P<lag_port>\S+)\n")
Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
54

55
    def execute_cli(self):
56
        interfaces = []
MaksimSmile13's avatar
MaksimSmile13 committed
57
58
59
60
        # Get LLDP interfaces
        lldp = []
        c = self.cli("show lldp", ignore_errors=True)
        if self.rx_lldp_en.search(c):
Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
61
            ll = self.rx_lldp.search(c)
62
63
            if ll:
                lldp = ll.group("local_if").split()
64
65
66
67
68
        v = self.cli("show interface", cached=True)
        for match in self.rx_sh_int.finditer(v):
            name = match.group("interface")
            a_stat = match.group("admin_status").lower() == "up"
            o_stat = match.group("oper_status").lower() == "up"
Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
69
70
71
            other = match.group("other")
            match1 = self.rx_hw.search(other)
            iface = {
Dmitry Lukhtionov's avatar
Fix    
Dmitry Lukhtionov committed
72
                "type": self.profile.get_interface_type(name),
Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
73
74
                "name": name,
                "admin_status": a_stat,
Dmitry Volodin's avatar
Dmitry Volodin committed
75
                "oper_status": o_stat,
Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
76
            }
Dmitry Volodin's avatar
Dmitry Volodin committed
77
            sub = {"name": name, "admin_status": a_stat, "oper_status": o_stat}
MaksimSmile13's avatar
MaksimSmile13 committed
78
79
80
            # LLDP protocol
            if name in lldp:
                iface["enabled_protocols"] = ["LLDP"]
Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
81
82
83
84
85
            if iface["type"] == "physical":
                sub["enabled_afi"] = ["BRIDGE"]
            if match1.group("mac"):
                iface["mac"] = match1.group("mac")
                sub["mac"] = match1.group("mac")
Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
86
            match1 = self.rx_alias.search(other)
Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
87
88
89
90
91
92
93
94
95
96
97
            if match1 and match1.group("alias") != "(null),":
                iface["description"] = match1.group("alias")
                sub["description"] = match1.group("alias")
            match1 = self.rx_index.search(other)
            if match1:
                iface["snmp_ifindex"] = match1.group("ifindex")
                sub["snmp_ifindex"] = match1.group("ifindex")
            else:
                if match.group("snmp_ifindex"):
                    iface["snmp_ifindex"] = match.group("snmp_ifindex")
                    sub["snmp_ifindex"] = match.group("snmp_ifindex")
Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
98
99
100
101
102
103
104
105
            # Correct alias and index on some device
            match1 = self.rx_alias_and_index.search(other)
            if match1:
                if match1.group("alias") != "(null)":
                    iface["description"] = match1.group("alias")
                    sub["description"] = match1.group("alias")
                iface["snmp_ifindex"] = match1.group("ifindex")
                sub["snmp_ifindex"] = match1.group("ifindex")
Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
106
107
108
109
110
111
112
113
            match1 = self.rx_mtu.search(other)
            if match1:
                sub["mtu"] = match1.group("mtu")
            match1 = self.rx_pvid.search(other)
            if match1:
                sub["untagged_vlan"] = match1.group("pvid")
            if name.startswith("Vlan"):
                sub["vlan_ids"] = [int(name[4:])]
Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
114
115
116
            match1 = self.rx_lag_port.search(other)
            if match1:
                iface["aggregated_interface"] = match1.group("lag_port")
Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
117
118
            match1 = self.rx_ip.search(other)
            if match1:
MaksimSmile13's avatar
MaksimSmile13 committed
119
120
                if "NULL" in match1.group("ip"):
                    continue
Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
121
122
                ip_address = "%s/%s" % (
                    match1.group("ip"),
Dmitry Volodin's avatar
Dmitry Volodin committed
123
                    IPv4.netmask_to_len(match1.group("mask")),
Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
124
125
126
127
128
129
130
131
132
133
134
135
136
                )
                sub["ipv4_addresses"] = [ip_address]
                sub["enabled_afi"] = ["IPv4"]
            iface["subinterfaces"] = [sub]
            interfaces += [iface]
        v = self.cli("show switchport interface")
        for match in self.rx_vlan.finditer(v):
            ifname = match.group("ifname")
            untagged_vlan = match.group("untagged_vlan")
            for i in interfaces:
                if ifname == i["name"]:
                    i["subinterfaces"][0]["untagged_vlan"] = untagged_vlan
                    if match.group("tagged_vlans"):
Dmitry Volodin's avatar
Dmitry Volodin committed
137
138
                        tagged_vlans = match.group("tagged_vlans").replace(";", ",")
                        i["subinterfaces"][0]["tagged_vlans"] = self.expand_rangelist(tagged_vlans)
Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
139
                    break
140
        return [{"interfaces": interfaces}]