Exemple #1
0
def set_interface_values(account, interface, request):
    """Use snmp to set the values in the request on the netbox"""
    try:
        fac = SNMPFactory.get_instance(interface.netbox)
    except SnmpError, error:
        _logger.error('Error getting snmpfactory instance %s: %s',
                      interface.netbox, error)
        messages.info(request, 'Could not connect to netbox')
Exemple #2
0
def set_interface_values(account, interface, request):
    """Use snmp to set the values in the request on the netbox"""
    try:
        fac = SNMPFactory.get_instance(interface.netbox)
    except SnmpError, error:
        _logger.error('Error getting snmpfactory instance %s: %s',
                      interface.netbox, error)
        messages.info(request, 'Could not connect to netbox')
Exemple #3
0
def change_port_vlan(identity, vlan):
    """
    Change switchport access vlan. Returns vlan on port before change.

    Reasons for not successful change may be:
    - Wrong community, use rw-community
    - rw-community not set on netbox
    - port is a trunk

    """
    interface = identity.interface
    netbox = interface.netbox

    agent = SNMPFactory().get_instance(netbox)
    try:
        fromvlan = agent.get_vlan(interface)
    except Exception as error:
        raise ChangePortVlanError(error)
    else:
        LOGGER.info('Setting vlan %s on interface %s', vlan, interface)
        try:
            agent.set_vlan(interface, vlan)
            agent.restart_if(interface.ifindex)
        except Exception as error:
            raise ChangePortVlanError(error)
        else:
            return fromvlan
Exemple #4
0
def get_and_populate_livedata(netbox, interfaces):
    """Fetch live data from netbox"""
    handler = SNMPFactory.get_instance(netbox)
    live_ifaliases = create_dict_from_tuplelist(handler.get_all_if_alias())
    live_vlans = create_dict_from_tuplelist(handler.get_all_vlans())
    live_operstatus = dict(handler.get_netbox_oper_status())
    live_adminstatus = dict(handler.get_netbox_admin_status())
    update_interfaces_with_snmpdata(interfaces, live_ifaliases, live_vlans,
                                    live_operstatus, live_adminstatus)

    return handler
Exemple #5
0
def find_vlans_on_netbox(netbox, factory=None):
    """Find all the vlans on this netbox

    fac: already instantiated factory instance. Use this if possible
    to enable use of cached values

    :returns: list of FantasyVlans
    :rtype: list
    """
    if not factory:
        factory = SNMPFactory.get_instance(netbox)
    return factory.get_netbox_vlans()
Exemple #6
0
def render_trunk_edit(request, interfaceid):
    """Controller for rendering trunk edit view"""

    interface = Interface.objects.get(pk=interfaceid)
    agent = SNMPFactory().get_instance(interface.netbox)
    if request.method == 'POST':
        try:
            handle_trunk_edit(request, agent, interface)
        except SnmpError, error:
            messages.error(request, 'Error editing trunk: %s' % error)
        else:
            messages.success(request, 'Trunk edit successful')
Exemple #7
0
def get_factory(netbox):
    """Get a SNMP factory instance"""
    config = read_config()
    timeout = get_config_value(config, 'general', 'timeout', fallback=3)
    retries = get_config_value(config, 'general', 'retries', fallback=3)

    try:
        return SNMPFactory.get_instance(netbox, timeout=timeout,
                                        retries=retries)
    except SnmpError as error:
        _logger.error('Error getting snmpfactory instance %s: %s',
                      netbox, error)
Exemple #8
0
def change_port_vlan(identity, vlan):
    """
    Change switchport access vlan. Returns vlan on port before change.

    Reasons for not successful change may be:
    - Wrong community, use rw-community
    - rw-community not set on netbox
    - port is a trunk

    """
    interface = identity.interface
    netbox = interface.netbox

    agent = SNMPFactory().get_instance(netbox)
    try:
        fromvlan = agent.get_vlan(interface.ifindex)
    except Exception as error:
        raise ChangePortVlanError(error)
    else:
        LOGGER.info('Setting vlan %s on interface %s' % (vlan, interface))
        try:
            agent.set_vlan(interface.ifindex, vlan)
            agent.restart_if(interface.ifindex)
        except Exception as error:
            raise ChangePortVlanError(error)
        else:
            return fromvlan
Exemple #9
0
def restart_interface(request):
    """Restart the interface by setting admin status to down and up"""
    if request.method == 'POST':
        try:
            interface = Interface.objects.get(
                pk=request.POST.get('interfaceid'))
        except Interface.DoesNotExist:
            return HttpResponse(status=404)

        try:
            fac = SNMPFactory.get_instance(interface.netbox)
        except SnmpError, error:
            _logger.error('Error getting snmpfactory instance when '
                          'restarting interface %s: %s',
                          interface.netbox, error)
            return HttpResponse(status=500)

        # Restart interface so that client fetches new address
        fac.restart_if(interface.ifindex)
        return HttpResponse()
Exemple #10
0
def restart_interface(request):
    """Restart the interface by setting admin status to down and up"""
    if request.method == 'POST':
        try:
            interface = Interface.objects.get(
                pk=request.POST.get('interfaceid'))
        except Interface.DoesNotExist:
            return HttpResponse(status=404)

        try:
            fac = SNMPFactory.get_instance(interface.netbox)
        except SnmpError, error:
            _logger.error(
                'Error getting snmpfactory instance when '
                'restarting interface %s: %s', interface.netbox, error)
            return HttpResponse(status=500)

        # Restart interface so that client fetches new address
        fac.restart_if(interface.ifindex)
        return HttpResponse()