Ejemplo 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
Ejemplo n.º 2
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)