get_chassis_id.py 1.47 KB
Newer Older
Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
1
2
3
# ---------------------------------------------------------------------
# Eltex.LTE.get_chassis_id
# ---------------------------------------------------------------------
Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
4
# Copyright (C) 2007-2021 The NOC Project
Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
5
6
# See LICENSE for details
# ---------------------------------------------------------------------
7

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

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


class Script(BaseScript):
    name = "Eltex.LTE.get_chassis_id"
    interface = IGetChassisID
    cache = True

Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
21
22
23
    rx_mac1 = re.compile(r"MAC address:\s*\n^\s+(?P<mac>\S+)", re.MULTILINE)
    rx_mac2 = re.compile(r"^Port \d+ MAC address: (?P<mac>\S+)", re.MULTILINE)

24
    def execute(self):
Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
25
26
        macs = []
        with self.profile.switch(self):
Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
27
28
29
30
31
32
33
34
35
            try:
                cmd = self.cli("show version")
                match = self.rx_mac1.search(cmd)
                if match:
                    macs += [match.group("mac")]
                cmd = self.cli("show interfaces mac-address")
                macs += self.rx_mac2.findall(cmd)
            except self.CLISyntaxError:
                pass
Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
36
37
38
39
40
        if not macs:
            mac_table = self.scripts.get_mac_address_table()
            for m in mac_table:
                if m["type"] == "C":
                    macs += [m["mac"]]
Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
41
        macs.sort()
Dmitry Volodin's avatar
Dmitry Volodin committed
42
43
44
        return [
            {"first_chassis_mac": f, "last_chassis_mac": t} for f, t in self.macs_to_ranges(macs)
        ]