Exemplo n.º 1
0
def syncInterfaces(device, connection, iface_regex):
    ''' Get interfaces from device and add|update it on netbox
    '''
    interfaces = connection.get_interfaces()

    print("Connection complete, number of interfaces {}".format(
        len(interfaces)))

    for if_name in interfaces.keys():
        # Cheking is interface matching regex
        if re.match(iface_regex, if_name):
            if_type = getInterfaceType(if_name)
            # and state (up/down)
            state = (interfaces[if_name]['is_enabled']
                     and interfaces[if_name]['is_up'])

            intf = device.interfaces.filter(name=if_name)

            # I cannot found now a way how to do it without two code block =\
            if intf.count() == 0:
                # if no interface present in device, create new
                print("Create new interface {}".format(if_name))
                iface = Interface(name=if_name)
                iface.description = interfaces[if_name]['description']
                iface.mac_address = interfaces[if_name]['mac_address']
                iface.enabled = state
                iface.form_factor = if_type
                iface.device = device
                iface.save()
                # Try to connect interface by description
                connect_interface(iface)
            else:
                # or get current iface and update them
                print("Update interface {}".format(if_name))
                iface = intf[0]
                iface.description = interfaces[if_name]['description']
                iface.mac_address = interfaces[if_name]['mac_address']
                iface.enabled = state
                #iface.form_factor = if_type
                iface.save()
                # Try to connect interface by description
                connect_interface(iface)
        else:
            pass
Exemplo n.º 2
0
    def get_mac_address(self, switch):
        # read MAC address table from switch
        mac_table = {
            "cnos": self.get_mac_address_cnos,
            "enos": self.get_mac_address_enos
        }[self.mode]()

        # parse MAC table
        if not mac_table:
            print "mac table query has failed. abort."
            return

        for mac, vlan_id, switch_port_name, state, is_openflow in mac_table:

            # mac can be formatted as "00:af" or "000.ed5"
            # we normalize them all into "00:af:a0:98"
            mac = self._cleanse_mac(mac)

            # vlan
            vlan, created = VLAN.objects.get_or_create(vid=vlan_id)

            # create Interface and link to switch
            switch_port_name = self._cleanse_port(switch_port_name)

            # Note: there are two interfaces we are to create:
            # 1. an switch port: device=switch, name = port number
            # 2. an interface on the other end of a switch traffic. Standing
            #    from POV of a switch, we don't know which device it belongs to.

            # create switch port as Interface
            existing = Interface.objects.filter(device=switch,
                                                name=switch_port_name)
            if existing:
                m = existing[0]
                m.type = INTERFACE_TYPE_SWITCH_PORT
            else:
                m = Interface(device=switch,
                              name=switch_port_name,
                              type=INTERFACE_TYPE_SWITCH_PORT)

            m.enabled = True
            m.mode = DEVICE_STATUS_ACTIVE
            m.state = state
            m.save()
            m.tagged_vlans.add(vlan)

            # Create Interface on other side of traffic.
            # Note: we don't know the interface type.
            existing = InterfaceMacTraffic.objects.filter(interface=m,
                                                          mac_address=mac)
            if not existing:
                InterfaceMacTraffic(interface=m, mac_address=mac).save()
Exemplo n.º 3
0
    def dump_port_mac(self, switch):
        port_table = {
            "cnos": self.dump_port_mac_cnos,
            "enos": self.dump_port_mac_enos
        }[self.mode]()

        for port_number, mac, enabled in port_table:
            existing = Interface.objects.filter(device=switch,
                                                name=port_number)
            if existing:
                i = existing[0]
            else:
                i = Interface(
                    device=switch,
                    name=port_number,
                )
            i.mac_address = mac
            i.type = INTERFACE_TYPE_SWITCH_PORT
            i.enabled = enabled
            i.save()
Exemplo n.º 4
0
def sync_interfaces(device, interfaces):
    """ Syncing interfaces

        :param device: object NetBox Device
        :param interfaces: list of lists

        interfaces:
            interface['NAME'] - Name of interface
            interface['MAC'] - Mac-Address
            interface['IP'] - List of IP-address
            interface['MTU'] - MTU
            interface['DESCR'] - Description of interfaces
            interface['TYPE'] - Physical type of interface (Default 1G-cooper - cannot get from linux)
            interface['STATE'] - UP|DOWN

        :return: status: bool, message: string
    """
    # Updated interface counter
    count = 0

    # Init interfaces filter
    iface_filter = device.cf().get('Interfaces filter')
    try:
        iface_regex = re.compile(iface_filter)
    except Exception as e:
        logger.warning("Cannot parse regex for interface filter: {}".format(e))
        iface_regex = re.compile('.*')

    for interface in interfaces:
        name = interface.get('NAME')
        mac = interface.get('MAC')
        ips = interface.get('IP')
        mtu = interface.get('MTU')
        description = interface.get('DESCR')
        iface_type = interface.get('TYPE')
        iface_state = interface.get('STATE')
        # TODO: add a bonding support
        iface_master = interface.get('BOND')

        # Check interface filter
        if not iface_regex.match(name):
            logger.debug("Iface {} not match with regex".format(name))
            continue

        # Get interface from device - for check if exist
        ifaces = device.interfaces.filter(name=name)
        if ifaces:
            logger.info(
                "Interface {} is exist on device {}, will update".format(
                    name, device.name))
            # TODO: I think, that only one item will be in filter, but need to add check for it
            iface = ifaces[0]
        else:
            logger.info(
                "Interface {} is not exist on device {}, will create new".
                format(name, device.name))
            iface = Interface(name=name)
            iface.device = device

        logger.info(
            "Will be save next parameters: Name:{name}, MAC: {mac}, MTU: {mtu}, Descr: {description}"
            .format(name=name, mac=mac, mtu=mtu, description=description))
        if description:
            iface.description = description
        else:
            iface.description = ''
        iface.mac_address = mac

        # MTU should be less 32767
        if int(mtu) < MAX_MTU:
            iface.mtu = mtu

        logger.info("Interface state is {}".format(iface_state))
        iface.enabled = 'up' in iface_state.lower()
        iface.form_factor = _get_interface_type(name)

        try:
            iface.save()
        except Exception as e:
            logger.error("Cannot save interface, error is {}".format(e))
        else:
            count += 1
            logger.info("Interface {} was succesfully saved".format(
                name, device.name))

        try:
            _connect_interface(iface)
        except:
            logger.error("Problem with connection function")

        # IP syncing
        if len(ips) > 0:
            for address in ips:
                addr = IPAddress()
                addr.interface = iface
                logger.info("Address is: {}".format(addr))
                # TODO: Need a test ipv6 addresses
                try:
                    # tries to determine is this address exist
                    if iface.ip_addresses.filter(address=address):
                        continue
                    addr.address = IPNetwork(address)
                    addr.save()
                except:
                    logger.warning(
                        "Cannot set address {} on interface".format(address))

    if count == 0:
        return False, "Can't update any interface, see a log for details"
    return True, "Successfully updated {} interfaces".format(count)