Ejemplo n.º 1
0
    def convert_link(cls, kb_entry, link, text=None):
        """
        Convert extracted link to URL
        Following link types are supported:
        KB<n> - Link to Knowledge Base article <n>
        TT<n> - Link to Trouble Ticket <n>
        attach:<name> - Link to attachment <name>

        :param kb_entry:
        :param link:
        :param text:
        :return:
        """
        if text is None:
            text = link
        if link.startswith("KB") and is_int(link[2:]):
            return "<a href='%s/%s/'>%s</a>" % (BASE_PATH, link[2:], text)
        elif link.startswith("TT"):
            return link[2:]
        elif link.startswith("attach:"):
            if text == link:
                text = link[7:]
            link = link[7:]
            return "<a href='%s/%d/attachment/%s/'>%s</a>" % (BASE_PATH, kb_entry.id, link, text)
        elif link.startswith("attachment:"):
            if text == link:
                text = link[11:]
            link = link[11:]
            return "<a href='/kb/kbentry/%d/attachment/%s/'>%s</a>" % (kb_entry.id, link, text)
        else:
            try:
                le = kb_entry.__class__.objects.get(subject=link)
                return "<a href='%s/%s/'>%s</a>" % (BASE_PATH, le.id, text)
            except kb_entry.__class__.DoesNotExist:
                return "<a href='%s'>%s</a>" % (link, text)
Ejemplo n.º 2
0
 def is_static(svc):
     if ":" not in svc:
         return False
     p = svc.split(":")
     if len(p) != 2:
         return False
     return is_ipv4(p[0]) and is_int(p[1])
Ejemplo n.º 3
0
 def execute_cli(self):
     r = []
     try:
         v = self.cli("show lldp remote-device | begin LocalPort")
     except self.CLISyntaxError:
         raise self.NotSupportedError()
     if v:
         for match in self.rx_lldp.finditer(v):
             local_port = match.group("local_port")
             remote_chassis_id = match.group("remote_id")
             remote_chassis_id_subtype = 4
             remote_port = match.group("remote_port")
             rn = match.group("remote_n")
             remote_port_subtype = 5
             if self.rx_mac.match(remote_port):
                 remote_port = MACAddressParameter().clean(remote_port)
                 remote_port_subtype = 3
             elif is_ipv4(remote_port):
                 remote_port_subtype = 4
             elif is_int(remote_port):
                 remote_port_subtype = 7
             if self.rx_mac.match(remote_chassis_id):
                 remote_chassis_id = remote_chassis_id.replace(" ", "-")
                 remote_chassis_id = MACAddressParameter().clean(remote_chassis_id)
                 remote_chassis_id_subtype = 3
             n = {
                 "remote_port": remote_port,
                 "remote_port_subtype": remote_port_subtype,
                 "remote_chassis_id": remote_chassis_id,
                 "remote_chassis_id_subtype": remote_chassis_id_subtype,
                 "remote_system_name": rn,
             }
             i = {"local_interface": local_port, "neighbors": [n]}
             r += [i]
     return r
Ejemplo n.º 4
0
 def execute(self):
     r = []
     for v in parse_table(self.cli("show vlan", cached=True), max_width=80):
         if not is_int(v[0]):
             continue
         r += [{"vlan_id": v[0], "name": v[1]}]
     return r
Ejemplo n.º 5
0
 def get_interface_by_local(self, port, object):
     """
     Try to guess remote port from free-form description
     :param object:
     :param port:
     :return:
     """
     self.logger.debug("Searching port by local: %s:%s", object.name, port)
     # Try ifindex
     if is_int(port):
         i = self.get_interface_by_ifindex(port, object)
         if i:
             return i
     # Try interface name
     try:
         n_port = object.get_profile().convert_interface_name(port)
         i = Interface.objects.filter(managed_object=object.id, name=n_port).first()
         if i:
             return i
         for p in object.get_profile().get_interface_names(n_port):
             i = Interface.objects.filter(managed_object=object.id, name=p).first()
             if i:
                 return i
     except InterfaceTypeError:
         pass
     # Unable to decode
     self.logger.info("Unable to decode local subtype port id %s at %s", port, object)
     return None
Ejemplo n.º 6
0
    def get_psu(self):
        r = defaultdict(list)
        k_map = {"state": "state", "partinfo": "partinfo"}
        try:
            v = self.cli("show power detail")
        except self.CLISyntaxError:
            return {}
        slot = 1
        number = None
        for block in self.rx_power_split.split(v):
            if is_int(block):
                if int(block.strip()) < number:
                    slot += 1
                number = int(block.strip())
                continue
            d = parse_kv(k_map, block)
            if d.get("state") in ["Empty", "Powered Off", None
                                  ] or "partinfo" not in d:
                continue
            partinfo = d["partinfo"].split()

            r[slot] += [{
                "type": "PSU",
                "number": number,
                "description": "".join(partinfo[:-2]),
                "vendor": "EXTREME",
                "part_no": partinfo[-1],
                "serial": partinfo[-2],
            }]
        return r
Ejemplo n.º 7
0
 def convert_interface_name(self, s):
     """
     >>> Profile().convert_interface_name("19")
     'vlan 19'
     """
     if is_int(s):
         return "vlan %s" % s
     return s
Ejemplo n.º 8
0
 def get_interface_names(self, name):
     r = []
     if name.startswith("1:"):
         r += [name[2:]]
     else:
         if is_int(name):
             r += ["1:%s" % name]
     return r
Ejemplo n.º 9
0
    def execute_cli(self, **kwargs):
        res = []
        ports = []

        try:
            v = self.cli("show fiber-ports optical-transceiver")
            for i in parse_table(v, footer=r"Temp\s+- Internally measured transceiver temperature"):
                if i[1] in ["OK", "N/S"] or is_int(i[1]):
                    ports += [i[0]]
        except self.CLISyntaxError:
            pass

        if self.has_capability("Stack | Members"):
            has_unit_command = True
            for unit in self.capabilities["Stack | Member Ids"].split(" | "):
                try:
                    plat = self.cli("show system unit %s" % unit, cached=True)
                except self.CLISyntaxError:
                    # Found on MES1124M SW version 1.1.46
                    # Left for compatibility with other models
                    if unit == "1":
                        plat = self.cli("show system", cached=True)
                        has_unit_command = False
                    else:
                        raise self.NotSupportedError()
                if not self.is_has_image:
                    if has_unit_command:
                        ver = self.cli("show version unit %s" % unit, cached=True)
                    else:
                        ver = self.cli("show version", cached=True)
                else:
                    ver = ""
                if has_unit_command:
                    ser = self.cli("show system id unit %s" % unit, cached=True)
                else:
                    ser = self.cli("show system", cached=True)
                r = self.get_chassis(plat, ver, ser)
                platform = r["part_no"][0]
                res += [r]
                for match in self.rx_pwr.finditer(plat):
                    res += [self.get_pwr(match.group("type"), match.group("pwr_type"), platform)]
                for p in ports:
                    if p.startswith("gi") or p.startswith("te"):
                        if unit == p[2]:
                            res += [self.get_trans(p)]
        else:
            plat = self.cli("show system", cached=True)
            ver = self.cli("show version", cached=True)
            ser = self.cli("show system id", cached=True)
            r = self.get_chassis(plat, ver, ser)
            platform = r["part_no"][0]
            res = [r]
            for match in self.rx_pwr.finditer(plat):
                res += [self.get_pwr(match.group("type"), match.group("pwr_type"), platform)]
            for p in ports:
                res += [self.get_trans(p)]

        return res
Ejemplo n.º 10
0
 def get_type(self, name):
     name = name.upper()
     n = name.split()
     if is_int(n[-1]):
         number = n[-1]
         name = " ".join(n[:-1])
     else:
         number = None
     return self.TYPE_MAP.get(name), number
Ejemplo n.º 11
0
 def execute(self):
     interfaces = []
     lldp = self.get_lldp()
     ctp = self.get_ctp()
     for i in parse_table(self.cli("show interface  switchport")):
         iface = {
             "name":
             i[0],
             "type":
             "physical",
             "enabled_protocols": [],
             "subinterfaces": [{
                 "name": i[0],
                 "enabled_afi": ["BRIDGE"],
                 "untagged_vlan": i[1]
             }],
         }
         if i[0] in lldp:
             iface["enabled_protocols"] += ["LLDP"]
         if i[0] in ctp:
             iface["enabled_protocols"] += ["CTP"]
         interfaces += [iface]
     for v in parse_table(self.cli("show vlan"), max_width=80):
         if not is_int(v[0]):
             continue
         vlan_id = v[0]
         ports = self.expand_rangelist(v[2])
         for i in interfaces:
             if i["subinterfaces"][0]["untagged_vlan"] == vlan_id:
                 continue
             if int(i["name"]) in ports:
                 if "tagged_vlans" in i["subinterfaces"][0]:
                     i["subinterfaces"][0]["tagged_vlans"] += [vlan_id]
                 else:
                     i["subinterfaces"][0]["tagged_vlans"] = [vlan_id]
     mac = self.scripts.get_chassis_id()[0]["first_chassis_mac"]
     match = self.rx_ipif.search(self.cli("show ip interface"))
     ip = match.group("ip")
     mask = match.group("mask")
     ip_address = "%s/%s" % (ip, IPv4.netmask_to_len(mask))
     iface = {
         "name":
         "mgmt",
         "type":
         "SVI",
         "mac":
         mac,
         "subinterfaces": [{
             "name": "mgmt",
             "vlan_ids": match.group("vlan_id"),
             "ipv4_addresses": [ip_address],
             "enabled_afi": ["IPv4"],
             "mac": mac,
         }],
     }
     interfaces += [iface]
     return [{"interfaces": interfaces}]
Ejemplo n.º 12
0
 def has_bfd_snmp(self):
     """
     Check box has bfd enabled
     """
     # bfdAdminStatus
     bfd = self.snmp.get("1.3.6.1.4.1.2636.5.3.1.1.1.1.0")
     if is_int(bfd) and int(bfd) == 1:  # enabled(1)
         return True
     return False
Ejemplo n.º 13
0
 def execute_platform_snmp(self, caps):
     np = 0
     # jnxRpmResSumSent
     for v, r in self.snmp.getnext("1.3.6.1.4.1.2636.3.50.1.2.1.2",
                                   bulk=False):
         tests = v.split(".")
         if tests[-1] == "1":  # currentTest(1)
             np += 1
     if np > 0:
         caps["Juniper | RPM | Probes"] = np
     # jnxPPPoEMajorInterfaceCount
     pppoe = self.snmp.get("1.3.6.1.4.1.2636.3.67.1.1.3.1.0")
     if is_int(pppoe) and int(pppoe) > 0:
         caps["BRAS | PPPoE"] = True
     # jnxL2tpStatsTotalTunnels
     l2tp = self.snmp.get("1.3.6.1.4.1.2636.3.49.1.1.1.1.1.0")
     if is_int(l2tp) and int(l2tp) > 0:
         caps["BRAS | L2TP"] = True
Ejemplo n.º 14
0
 def parse_create_vlan(self, tokens):
     """
     create vlan 306 tag 306
     create vlan tag tag 306
     """
     tag = self.next_item(tokens, "tag")
     if tag and is_int(tag):
         name = self.next_item(tokens, "vlan")
         vid = int(tag)
         self.get_vlan_fact(vid).name = name
         self.vlan_ids[tag] = vid
         self.vlan_ids[name] = vid
     else:
         if len(tokens) == 5 and tokens[3] == "tag" and is_int(tokens[4]):
             name = self.next_item(tokens, "vlan")
             vid = int(tokens[4])
             self.get_vlan_fact(vid).name = name
             self.vlan_ids[tag] = vid
             self.vlan_ids[name] = vid
Ejemplo n.º 15
0
 def table_search_Q(cls, table, query):
     q = []
     for f in CustomField.objects.filter(is_active=True, table=table, is_searchable=True):
         if f.type == "str":
             q += [{"%s__icontains" % f.name: query}]
         elif f.type == "int":
             if is_int(query):
                 q += [{f.name: int(query)}]
     if q:
         return reduce(lambda x, y: x | models.Q(**y), q, models.Q(**q[0]))
     else:
         return None
Ejemplo n.º 16
0
 def inner_model(qs):
     if value and not is_int(value):
         raise HTTPException(
             status_code=HTTPStatus.UNPROCESSABLE_ENTITY,
             detail=f"'{value}' is not a Integer",
         )
     vv = self.model.get_by_id(int(value)) if value else None
     if value and not vv:
         raise HTTPException(
             status_code=HTTPStatus.NOT_FOUND,
             detail=f"NotFond {str(self.model)}: {value}")
     return qs.filter(**{self.name: vv})
Ejemplo n.º 17
0
 def get_current_input(self, metrics):
     for metric in metrics:
         if self.is_lite:
             value = self.snmp.get("1.3.6.1.4.1.27514.103.0.28.0")
         else:
             value = self.snmp.get("1.3.6.1.4.1.27514.102.0.21")
         if is_float(value) or is_int(value):
             self.set_metric(
                 id=("Environment | Electric Current", metric.labels),
                 value=value,
                 scale=scale(0.01),
             )
Ejemplo n.º 18
0
 def is_ignored_interface(self, i):
     if i.lower() in self.ignored_interfaces:
         return True
     if i.startswith("flood to vlan"):
         return True
     if i.startswith("VPLS"):
         return True
     if i.startswith("seq_no:"):
         return True
     if is_int(i):
         # 10.27.0.80, 1204773146
         return True
     return False
Ejemplo n.º 19
0
 def convert_interface_name(self, s):
     """
     >>> Profile().convert_interface_name("1")
     'load'
     >>> Profile().convert_interface_name("0/1")
     'load'
     """
     match = self.rx_discrete_name.findall(s)
     if match:
         return "%s %s" % (self.SENSOR_NAME.get(int(s.split("/")[0])), s.split("/")[1])
     elif is_int(s):
         return self.SENSOR_NAME.get(int(s))
     return s
Ejemplo n.º 20
0
 def execute_cli(self):
     ifaces = []
     v = self.cli("show interface")
     for match in self.rx_int.finditer(v):
         ifname = self.profile.convert_interface_name(match.group("ifname"))
         typ = match.group("type")
         iftype = self.types[typ]
         i = {
             "name": ifname,
             "type": iftype,
             "admin_status": "up" in match.group("admin_status"),
             "oper_status": "up" in match.group("oper_status"),
             "snmp_ifindex": match.group("snmp_ifindex"),
             "mac": match.group("mac"),
         }
         sub = {
             "name": ifname,
             "admin_status": "up" in match.group("admin_status"),
             "oper_status": "up" in match.group("oper_status"),
             "mac": match.group("mac"),
             "mtu": match.group("mtu"),
         }
         if match.group("descr") and match.group("descr").strip():
             i["description"] = match.group("descr").strip()
             sub["description"] = match.group("descr").strip()
         if match.group("ip"):
             sub["enabled_afi"] = ["IPv4"]
             sub["ipv4_addresses"] = [match.group("ip")]
         if i["type"] == "physical":
             sub["enabled_afi"] = ["BRIDGE"]
             c = self.cli("show vlan interface %s" % match.group("ifname"))
             for r in parse_table(c, allow_wrap=True):
                 if is_int(r[2]):
                     untagged = int(r[2])
                     sub["untagged_vlan"] = untagged
                     tagged = self.expand_rangelist(r[3])
                     tagged = [item for item in tagged if int(item) != untagged]
                     if tagged:
                         sub["tagged_vlans"] = tagged
         if ifname.startswith("GigaEthernet"):
             cmd1 = "show lldp interface %s" % ifname
             cmd2 = self.cli(cmd1)
             for match1 in self.rx_lldp.finditer(cmd2):
                 if (
                     match1.group("lldp_rx") == "enabled"
                     or match1.groups("lldp_tx") == "enabled"
                 ):
                     i["enabled_protocols"] = ["LLDP"]
         i["subinterfaces"] = [sub]
         ifaces += [i]
     return [{"interfaces": ifaces}]
Ejemplo n.º 21
0
 def get_current_input(self, metrics):
     for metric in metrics:
         if self.is_lite:
             value = self.snmp.get("1.3.6.1.4.1.27514.103.0.28.0")
         else:
             value = self.snmp.get("1.3.6.1.4.1.27514.102.0.21")
         port = metric.labels[0].rsplit("::", 1)[-1]
         if is_float(value) or is_int(value):
             self.set_metric(
                 id=("Environment | Electric Current", metric.labels),
                 labels=[f"noc::sensor::{port}"],
                 value=value,
                 scale=scale(0.01),
             )
Ejemplo n.º 22
0
    def execute_cli(self, interface=None):
        # @todo without stack slot (Members | Ids)
        # show ports transceiver information
        r = []
        if interface is not None:
            ifaces = [interface.split(":")]
        elif self.has_capability("Stack | Member Ids"):
            ifaces = [(s_id, None) for s_id in
                      self.capabilities["Stack | Member Ids"].split(" | ")]
        else:
            ifaces = [(None, None)]

        for slot, port in ifaces:
            cmd = "debug hal show optic-info ddmi "
            if port is not None:
                cmd += "slot %s, port %s" % (slot, port)
            elif slot is not None:
                cmd += "slot %s" % slot
            try:
                v = self.cli(cmd)
            except self.CLISyntaxError:
                return []
            for block in self.rx_trans_split.split(v):
                if is_int(block):
                    port = block.strip()
                    continue
                if self.rx_no_trans.match(block) or not port:
                    continue
                d = parse_kv(self.k_map, block)
                if slot is not None:
                    port = "%s:%s" % (slot, port)
                if not d:
                    continue
                r += [{
                    "interface":
                    port,
                    "temp_c":
                    self.normalize_output(d.get("temp_c")),
                    "voltage_v":
                    self.normalize_output(d.get("voltage_v")),
                    "current_ma":
                    self.normalize_output(d.get("current_ma")),
                    "optical_rx_dbm":
                    self.normalize_output(d.get("optical_rx_dbm")),
                    "optical_tx_dbm":
                    self.normalize_output(d.get("optical_tx_dbm")),
                }]
        return r
Ejemplo n.º 23
0
 def convert_interface_name(self, s):
     """
     >>> Profile().convert_interface_name_cisco("gi1/0/1")
     'Gi 1/0/1'
     >>> Profile().convert_interface_name_cisco("gi1/0/1?")
     'Gi 1/0/1'
     """
     match = self.rx_eltex_interface_name.match(str(s))
     if is_int(s):
         return "Vl %s" % s
     elif s in ["oob", "stack-port"]:
         return s
     elif match:
         return "%s %s" % (match.group("type").capitalize(), match.group("number"))
     else:
         raise InterfaceTypeError("Invalid interface '%s'" % s)
Ejemplo n.º 24
0
    def get_object(self, object_name):
        """
        Resolve object by name or by id
        """
        from noc.sa.models.managedobject import ManagedObject
        from django.db.models import Q

        connect()
        if object_name.endswith(".json") and os.path.isfile(object_name):
            return JSONObject(object_name)
        q = Q(name=object_name)
        if is_int(object_name):
            q = Q(id=int(object_name)) | q
        try:
            return ManagedObject.objects.get(q)
        except ManagedObject.DoesNotExist:
            self.die("Object is not found: %s" % object_name)
Ejemplo n.º 25
0
 def __iter__(self):
     g = super(RouterOSTokenizer, self).__iter__()
     context = None
     context_fed = False
     n_item = 0
     for tokens in g:
         if tokens[0].startswith("/"):
             # New context
             if context and not context_fed:
                 yield context
             context = tokens
             context_fed = False
             n_item = 0
             continue
         if tokens[0] == "set" and len(tokens) > 1:
             # Process set instruction
             if (
                 tokens[1] == "["
                 and len(tokens) > 4
                 and tokens[2] == "find"
                 and tokens[4] == "]"
             ):
                 # set [ find key=value ] ...
                 item = tokens[3].split("=", 1)[1]
                 for ct in self.iter_context(context + (item,), tokens[5:]):
                     yield ct
             elif is_int(tokens[1]):
                 # set 0 ...
                 for ct in self.iter_context(context + (tokens[1],), tokens[2:]):
                     yield ct
             else:
                 # set XXX
                 for ct in self.iter_context(context, tokens[1:]):
                     yield ct
             context_fed = True
         if tokens[0] == "add" and len(tokens) > 1:
             # Process add instruction
             for ct in self.iter_context(context + (str(n_item),), tokens[1:]):
                 yield ct
             context_fed = True
             n_item += 1
     # Yield last context if not already yielded
     if context and not context_fed:
         yield context
Ejemplo n.º 26
0
 def execute(self, interface=None, vlan=None, mac=None):
     cmd = self.cli("show mac-address-table")
     # if mac is not None:
     #    cmd += " address %s" % mac
     if interface is not None:
         cmd += " port %s" % interface
     if vlan is not None:
         cmd += " vlan %s" % vlan
     r = []
     t = parse_table(cmd, footer="^show mac-address-table")
     for i in t:
         if is_int(i[0]):
             r += [{
                 "vlan_id": i[0],
                 "mac": i[1],
                 "interfaces": [self.profile.convert_interface_name(i[4])],
                 "type": "D" if i[2] == "learned" else "S",
             }]
     return r
Ejemplo n.º 27
0
    def get_objects_from_expression(cls, s):
        """
        Get list of Managed Object matching selector expression

        Expression must be string or list.
        Elements must be one of:
        * string starting with @ - treated as selector name
        * string containing numbers - treated as object's id
        * string - managed object name.
        * string - IPv4 or IPv6 address - management address

        Raises ManagedObject.DoesNotExists if object is not found.
        Raises ManagedObjectSelector.DoesNotExists if selector is not found
        :param cls:
        :param s:
        :return:
        """
        from .managedobject import ManagedObject

        if isinstance(s, int) or isinstance(s, str):
            s = [s]
        if not isinstance(s, list):
            raise ValueError("list required")
        objects = set()
        for so in s:
            if not isinstance(so, str):
                so = str(so)
            if so.startswith("@"):
                # Selector expression: @<selector name>
                o = ManagedObjectSelector.objects.get(name=so[1:])
                objects |= set(o.managed_objects)
            else:
                # Search by name
                q = Q(name=so)
                if is_int(so):
                    # Search by id
                    q |= Q(id=int(so))
                if is_ipv4(so) or is_ipv6(so):
                    q |= Q(address=so)
                o = ManagedObject.objects.get(q)
                objects.add(o)
        return list(objects)
Ejemplo n.º 28
0
 def convert_interface_name(self, s):
     """
     >>> Profile().convert_interface_name_cisco("gi1/0/1")
     'Gi 1/0/1'
     >>> Profile().convert_interface_name_cisco("gi1/0/1?")
     'Gi 1/0/1'
     """
     if s.startswith("Slot"):
         # @todo InterfaceType check needed
         s = s.replace("Slot", "gigabitethernet")
     match = self.rx_eltex_interface_name.match(str(s))
     if is_int(s):
         return "Vl %s" % s
     elif s in ["oob", "stack-port"]:
         return s
     elif match:
         return "%s %s" % (match.group("type").capitalize(),
                           match.group("number"))
     else:
         raise InterfaceTypeError("Invalid interface '%s'" % s)
Ejemplo n.º 29
0
    def get_managed_object(self, o_id):
        """
        Get ManagedObject by id or name
        :param o_id: Object's id or name
        :return: ManagedObject
        :rtype: ManagedObject
        """
        from noc.sa.models.managedobject import ManagedObject

        # Try to get object by id
        if is_int(o_id):
            try:
                return ManagedObject.objects.get(id=int(o_id))
            except ManagedObject.DoesNotExist:
                pass
        # Try to get object by name
        try:
            return ManagedObject.objects.get(name=o_id)
        except ManagedObject.DoesNotExist:
            return None
Ejemplo n.º 30
0
    def get_user(self, u_id):
        """
            Get User by id or name
            :param u_id: Object's id or name
            :return: ManagedObject
            :rtype: ManagedObject
            """
        from noc.aaa.models.user import User

        # Try to get object by id
        if is_int(u_id):
            try:
                return User.objects.get(id=int(u_id))
            except User.DoesNotExist:
                pass
        # Try to get object by name
        try:
            return User.objects.get(username=u_id)
        except User.DoesNotExist:
            return None