I’ve been playing with Netbox lately as a SSOT (Single Source of Truth) for some automation tasks I’m looking at. The problem is I need more data wrapped around Interfaces, ie: on anything layer 2, I’d like to know things like Storm Control settings, Portfast etc.

There’s been more than a few feature requests for this on the github site, but the devs have decided this isn’t really in the Netbox remit – which is understandable, this is their software overall.

2.8 introduced the Plugin architecture, so what I’m trying to achieve may be possible which that, but examples are fairly thin on the ground + I was more interested in how difficult this would be to apply to Interface objects.

Also note – I’m not a fan of hacking OSS to make it more difficult to then update.

*** CAVEAT *** – this is hack, not all functionality is tested and it make break many other things – use at your own risk. I am *not* responsible for any/all breakages.

So, how to do this:

Add in Custom Field functionality to Interface Objects

in netbox/dcim/models/device_components.py

Add:

from extras.models import CustomFieldModel

Change:

@extras_features('graphs', 'export_templates', 'webhooks')
class Interface(CableTermination, ComponentModel):

to:

@extras_features('graphs', 'export_templates', 'webhooks','custom_fields')
class Interface(CableTermination, ComponentModel, CustomFieldModel):

In class Interface() add the below function:

custom_field_values = GenericRelation(
    to='extras.CustomFieldValue',
    content_type_field='obj_type',
    object_id_field='obj_id'
)

Restarting Netbox, going to the admin page now should give us:

So lets add a test one:

Add to View/Edit Fields

In: netbox/netbox/templates/dcim/interface.html
insert:
{% include 'inc/custom_fields_panel.html' with obj=interface %}
above:
{% include 'extras/inc/tags_panel.html' with tags=interface.tags.all %}

In: netbox/netbox/templates/dcim/interface_edit.html

add the below below the ending /div for panel-default:

{% if form.custom_fields %}
        <div class="panel panel-default">
            <div class="panel-heading"><strong>Custom Fields</strong></div>
            <div class="panel-body">
                {% render_custom_fields form %}
            </div>
        </div>
    {% endif %}

In: /opt/netbox/netbox/dcim/forms.py
Change:

class InterfaceForm(InterfaceCommonForm, BootstrapMixin, forms.ModelForm, CustomFieldModelForm):
To:
class InterfaceForm(InterfaceCommonForm, BootstrapMixin, CustomFieldModelForm):

After restarting Netbox, we should now be able to view/edit our custom fields:

View:

Edit

Showing this in the API


In: /opt/netbox/netbox/dcim/api/views.py
Change:

class InterfaceViewSet(CableTraceMixin, ModelViewSet):

To:

class InterfaceViewSet(CableTraceMixin, CustomFieldModelViewSet):

In: /opt/netbox/netbox/dcim/api/serializers.py
Change:

class InterfaceSerializer(TaggitSerializer, ConnectedEndpointSerializer)
To:
class InterfaceSerializer(TaggitSerializer, ConnectedEndpointSerializer,CustomFieldModelSerializer):

in class meta: (under the InterfaceSerializer Class): add ‘custom_fields’ to fields = [

Now testing using the API:

*** CAVEAT *** – this is hack, not all functionality is tested and it make break many other things – use at your own risk. I am *not* responsible for any/all breakages.

It’d be nice for this to be conditional (ie: l3 interface, specific set of custom fields, but this is possible using a l2/l3 custom field…)

Categories:

Tags:

No responses yet

Leave a Reply

Your email address will not be published.

sixteen + twenty =

Adsense
About This Site

This may be a good place to introduce yourself and your site or include some credits.