Logo

NetBox Data Provider

The NetBox Data Provider connects NetBox and Matrix42 in both directions. Computers and network devices, including manufacturers and SKUs, are imported from NetBox into Matrix42 asset management – controlled by a role mapping that defines which devices are created as which asset type. In the other direction, the export transfers regions, sites, locations, device types and assets from Matrix42 to NetBox, with freely definable filters, field and status mappings. Both directions run on a schedule, so asset management and network documentation stay consistent.

abstract background

Description

The NetBox Data Provider connects a NetBox instance with Matrix42 – in both directions:

  • Import (NetBox → Matrix42): Computers and network devices, including manufacturers, SKUs and roles, are imported from NetBox into Matrix42 and kept in sync. Role mapping controls which NetBox roles are imported as which asset type.
  • Export (Matrix42 → NetBox): Regions, sites, locations, SKUs (device types) and assets (devices) are created and updated in NetBox from Matrix42. Which objects and fields are transferred is configured entirely in Matrix42.

1 Installation

1.1 Prerequisites

Component Requirement
Matrix42 version 26.1.0 or later
Worker On-premise worker (data provider pool) with network access to NetBox – required for the import
NetBox NetBox instance reachable via HTTP(S) with an API token; NetBox 4.x for the export
Export additional prerequisites, see 3.2 Prerequisites

1.2 Installing the extension

The NetBox Data Provider extension is installed via the Extension Gallery (Administration > Extension Gallery).

The installation provides, among other things:

  • the NetBox data provider (generic connector) with the NetBox Configuration dialog,
  • the workflows for import and export,
  • the engine activations Netbox - Import and Netbox - Export (locked, without schedule),
  • the web service for the export and the NetBox script for download.

1.3 Configuring the data provider

  1. Open Administration > Integration > Data Providers.Data provider list
  2. Select the NetBox data provider (in the example “NetBox DEMO”) and choose Edit.Data provider preview
  3. In the Configurations area, add a new configuration with + or open an existing configuration by double-clicking it.Generic connector with configurations
  4. In the NetBox Configuration dialog, enter the connection to NetBox on the General tab and save with Save & Close.General tab

The NetBox Configuration dialog has four tabs:

Tab Content
General Activation of the configuration and connection to NetBox
Import Settings for the import NetBox → Matrix42, role configuration (chapter 2)
Import History Jobs, workflows and logs of the import runs
Export Settings for the export Matrix42 → NetBox (chapter 3)

General tab

Field Description
Enable Configuration Activates the configuration. Without this, the configuration is used neither for the import nor for the export.
Netbox Url URL at which NetBox can be reached, e.g. https://netbox.example.com.
Token NetBox API token. It must be a v1 token. Read permissions are required for the import. If the export is started from Matrix42 (see 3.8.3), the token must also be allowed to run NetBox scripts.
Json Dll Path Full path to the Matrix42 System.Json.dll on the worker server, e.g. C:\Program Files (x86)\Matrix42\Matrix42 Workplace Management\bin\System.Json.dll.

2 Import

The import reads devices and device roles via the NetBox REST API (/api/dcim/devices, /api/dcim/device-roles/) and creates or updates computers, network devices, manufacturers, SKUs and roles in Matrix42. It is run by the generic connector on the selected data provider pool.

2.1 Configuring the import

The import settings are on the Import tab of the NetBox Configuration.

Import tab

General

Field Description
Enable Import Activates the data import.
Data Provider Pool Select the on-premise worker that has access to NetBox.
Data Provider Select the NetBox data provider.
Description Optional description of the configuration.
Last Result, Last Run Result and time of the last import run (display only).

Import settings (inventory)

Field Description
Status for New Computer Status for newly imported devices.
Status for Deleted Computer Status for deleted computers.
Create computers Activate this option if computers are to be created.
Update computers Activate this option if computers are to be updated.
Create network devices Activate this option if network devices are to be created.
Update network devices Activate this option if network devices are to be updated.

2.2 Configuring roles

The Configure Roles area defines which NetBox roles are mapped to which asset types in Matrix42. The list shows Name, Slug and Asset Type for each role.

Configure roles

Importing NetBox roles

Before configuring the roles, run the data provider once with a valid basic configuration and without role mapping. This imports the roles from NetBox into Matrix42.

Assigning NetBox roles

Once the roles have been imported, assign the matching Asset Type to the required roles in the Configure Roles area. The next data provider run imports all devices whose NetBox role has an asset type assigned.

2.3 Automatic execution (engine activation “Netbox - Import”)

Engine activations of the extension

The engine activation supplied with the extension is preconfigured as follows:

Tab Preconfiguration
General Name Netbox - Import, the whole activation is disabled
Activated Engines Engine Generic Connector, engine activation parameters: Connectors: NetBox
Schedules no schedule

Setup:

  1. Open Administration > Services & Processes > Engine Activations, select Netbox - Import and choose Edit.
  2. Activated Engines tab: open the Generic Connector engine and check that the NetBox data provider in use is entered under Connectors. If a data provider other than “NetBox” is used, select it here.
  3. Schedules tab: add a schedule with + (name, start date, run time, time zone, frequency, recurrence, end) and confirm it.
  4. General tab: clear the option that disables the whole activation.
  5. Save with Save & Close.

2.4 Attribute mapping

The following tables show which NetBox attributes are imported into which Matrix42 fields. Fields marked (matching) are used to identify existing objects.

Computers (asset group 1)

API call: GET https://{NetBoxUrl}/api/dcim/devices

NetBox Source column Target in Matrix42
constant / set in import ManagementTypeNetbox SPSAssetClassBase.ManagementType
SKUManufacturer + SKUModel [Transform] SPSAssetClassBase.SKU
primary_ip.address ip SPSComputerClassBase.IPAddress
site.name location SPSCommonClassBase.Location
device_type.manufacturer.name manufacturer [Source] SKUManufacturer
device_type.model model [Source] SKUModel
name name SPSComputerClassBase.Name
configured in the extension ($NewDeviceState) newDeviceState SPSCommonClassBase.State
serial serial SPSAssetClassBase.SerialNumber (matching)

Network devices (asset group 8)

API call: GET https://{NetBoxUrl}/api/dcim/devices

NetBox Source column Target in Matrix42
constant / set in import ManagementTypeNetbox SPSAssetClassBase.ManagementType
SKUManufacturer + SKUModel [Transform] SPSAssetClassBase.SKU
primary_ip.address ip SPSPeripheralClassBase.IPAddress
site.name location SPSCommonClassBase.Location
device_type.manufacturer.name manufacturer [Source] SKUManufacturer
device_type.model model [Source] SKUModel
name name SPSAssetClassBase.Name
configured in the extension ($NewDeviceState) newDeviceState SPSCommonClassBase.State
serial serial SPSAssetClassBase.SerialNumber (matching)

Manufacturers

The manufacturers are the distinct values of device_type.manufacturer.name.

NetBox Source column Target in Matrix42
constant / set in import IsManufacturer SPSSupplierClassBase.IsManufacturer
device_type.manufacturer.name manufacturer SPSSupplierClassBase.Name (matching)
device_type.manufacturer.name manufacturer SPSSupplierClassBase.ShortName

Roles

API call: GET https://{NetBoxUrl}/api/dcim/device-roles/

NetBox Source column Target in Matrix42
name name MTX_NetboxDeviceRolesClassBase.MTX_Name
slug slug MTX_NetboxDeviceRolesClassBase.MTX_Slug (matching)

SKUs (device types)

The SKUs are the distinct combinations of device_type.model and device_type.manufacturer.name.

NetBox Source column Target in Matrix42
constant / set in import StateActive SPSCommonClassBase.State
configured per role in the extension (asset type) assetType SPSStockKeepingUnitClassBase.Type
device_type.manufacturer.name manufacturer SPSStockKeepingUnitClassBase.Manufacturer (matching)
device_type.model model SPSStockKeepingUnitClassBase.Model (matching)

3 Export

3.1 Overview

The export creates and updates regions, sites, locations, SKUs and assets from Matrix42 in NetBox. The Export tab of the NetBox Configuration defines which objects are transferred with which fields. The transfer into NetBox is performed by the NetBox script Matrix42 Sync, which retrieves the configuration and data from the extension’s web service.

Component Location Purpose
Export tab of the NetBox Configuration Matrix42 Selection of entities, filters, status and field mappings
Web service MountX.NetBoxDataProvider Matrix42, /m42Services/api/mountx/netbox Provides the configuration and data per entity and the sync script for download
Script module netbox_sync.py NetBox (custom scripts) Contains the scripts Matrix42 Sync and Matrix42 Link Existing Objects
Workflow Netbox - Trigger Export and engine activation Netbox - Export Matrix42 Start the sync on a schedule from Matrix42 and store the result on the Export tab

Flow of an export started from Matrix42:

Export flow

3.2 Prerequisites

  • NetBox 4.x with custom scripts enabled. The Python package requests must be available in NetBox (included in the official NetBox Docker image).
  • Network: NetBox must be able to reach Matrix42 via HTTPS. For starting the export from Matrix42, Matrix42 must in turn be able to reach NetBox at the Netbox Url.
  • Matrix42 API token (Administration > Integration > Web Services Tokens) of a user who is allowed to read the exported data.
  • Environment variables M42_BASE_URL and M42_API_TOKEN in NetBox (see 3.3).
  • Device roles: the slugs delivered for role in the field mapping must exist as device roles in NetBox.
  • Custom fields: if further custom fields are filled via cf_<name>, they must be created in NetBox and assigned to the respective object type. The script creates the custom field matrix42_eoid itself.
  • NetBox API token (v1) on the General tab with permission to run scripts (only for starting the export from Matrix42).

3.3 Setup

  1. Create a Matrix42 API token under Administration > Integration > Web Services Tokens.
  2. Enable the export: on the Export tab of the NetBox Configuration, select Enable Export and save. The Configuration ID is needed later in NetBox.
  3. Download the sync script: on the Export tab, choose DOWNLOAD SCRIPT. The downloaded file netbox_sync.py always matches the installed version of the web service.
  4. Set the environment variables in NetBox and restart NetBox. For Docker installations, the variables must be set in both the NetBox container and the worker container, because scripts run as background jobs:
    environment:
      M42_BASE_URL: 'https://matrix42.example.com'   # base URL without /m42Services
      M42_API_TOKEN: '<Matrix42 API token>'
  5. Upload the script to NetBox: in NetBox, go to Customization > Scripts, choose Add and upload the file netbox_sync.py. The scripts Matrix42 Sync and Matrix42 Link Existing Objects then appear.
  6. Enter the script ID: determine the numeric ID of the Matrix42 Sync script (visible in the URL /extras/scripts/<ID>/ or via GET /api/extras/scripts/) and enter it on the Export tab under Netbox Script Id.
  7. Create the device roles in NetBox that are used in the field mapping of the assets.
  8. Configure the entities: on the Export tab, set the export checkbox, filter, status mapping and field mapping for each entity (see 3.5 to 3.7).
  9. Link existing NetBox data: if NetBox already contains objects that are to be maintained from Matrix42, link them once with Matrix42 Link Existing Objects (see 3.8.1).
  10. Test run: run Matrix42 Sync in NetBox without Commit, check the log and then run it with Commit (see 3.8.2).
  11. Automate via the engine activation Netbox - Export (see 3.8.3).

3.4 Export tab – settings

Export tab

Field Description
Enable Export Enables the export for this configuration. If the export is not enabled or the configuration is disabled on the General tab, the web service rejects all requests.
Configuration ID Unique ID (GUID) of this configuration (display only). It is entered as Configuration ID when starting the NetBox scripts.
Netbox Script Id Numeric ID of the Matrix42 Sync script in NetBox. Required for starting the export from Matrix42.
DOWNLOAD SCRIPT Downloads the script module netbox_sync.py.
Last Export Status Status of the last NetBox job started from Matrix42, e.g. completed.
Last Export Date Time of the last export started from Matrix42.
Last Export Result Summary of the run per entity (created, updated, unchanged, failed, disappeared).
Last Export Log Log of the NetBox job.

3.5 Entities

The Export tab contains a separate area for each entity. The entities are processed in the following order so that references to objects that have already been transferred can be resolved:

Order Area on the Export tab NetBox object Data definition Status mapping
1 Region Region selectable: SPSOrgUnitClassBase, SPSLocationClassBase, SPSCostCenterClassBase
2 Site Site selectable: SPSOrgUnitClassBase, SPSLocationClassBase, SPSCostCenterClassBase yes
3 Location Location selectable: SPSOrgUnitClassBase, SPSLocationClassBase, SPSCostCenterClassBase yes
4 SKUs Device Type SPSStockKeepingUnitClassBase
5 Assets Device SPSAssetClassBase yes

Each area contains the following fields:

Field Description
Export Regions / Sites / Locations / Skus / Assets Export the entity. If the checkbox is cleared, the web service returns no data and the script skips the entity.
… Data Definition Matrix42 data definition from which the objects are read.
… Filter Optional ASQL condition that restricts the exported objects, e.g. SerialNumber IS NOT NULL. Without a filter, all objects of the data definition are exported.
Status Mapping Mapping of Matrix42 statuses to NetBox statuses (site, location and assets only), see 3.7.
Field Mapping Mapping of Matrix42 values to NetBox fields, see 3.6.

3.6 Field mapping

Each row of the field mapping assigns a Matrix42 value to a NetBox field.

Column Description
Columns Expression ASQL expression evaluated on the data definition: an attribute (Name), a path across relations (Manufacturer.Name), a function (ISNULL(…)) or a constant ('default-role', 1).
Netbox Target Name of the NetBox field, e.g. name, serial or cf_<name> for custom fields.
Field Policy Defines when the value is written to NetBox (see below).

Rows without a columns expression or NetBox target are ignored. Each exported entity needs at least one complete row.

Required row: Every entity needs the row [Expression-ObjectID]cf_matrix42_eoid. It links the Matrix42 object to the NetBox object. Records without this value are not transferred.

Field policy

Field Policy The value is written …
Always on every run as soon as it differs from the value in NetBox
On Create only when the object is created in NetBox
If Empty only if the field in NetBox is empty
Never never (the value is only delivered, e.g. as match field for linking)

If no field policy is specified, Always applies.

NetBox fields with special handling

Netbox Target Entities Expected value Behavior
cf_matrix42_eoid all [Expression-ObjectID] Link between Matrix42 and NetBox (required)
status Site, Location, Assets Matrix42 status, e.g. State.Value Translated via the status mapping
region Site ID ([Expression-ObjectID]) of an exported region Reference to the region transferred to NetBox
site Location, Assets ID of an exported site Reference to the site transferred to NetBox
device_type Assets ID of an exported SKU Reference to the device type transferred to NetBox
manufacturer SKUs Manufacturer name The manufacturer is created in NetBox if it is missing
role Assets Slug of a NetBox device role The role must exist in NetBox
tenant Site, Location, Assets Tenant name The tenant is created in NetBox if it is missing
tags all comma-separated tag names Missing tags are created and added; existing tags are kept
cf_<name> all any Written to the custom field <name>

All other NetBox targets are written directly to the NetBox field of the same name (e.g. name, serial, asset_tag, description, comments, model, u_height, part_number, facility). Other reference fields such as location, parent or platform are not supported.

Which fields NetBox requires when creating an object depends on the object. The slug of regions, sites, locations and device types is set automatically to m42-<ID>.

NetBox object Minimum mapping (in addition to cf_matrix42_eoid)
Region name
Site name
Location name, site
Device Type manufacturer, model
Device device_type, role, site

Example: field mapping of the assets

Columns Expression Netbox Target Field Policy
ISNULL(T(SPSComputerClassBase).Name, Name) name Always
SerialNumber serial Always
[Expression-ObjectID] cf_matrix42_eoid On Create
SKU.[Expression-ObjectID] device_type Always
'default-role' role On Create
T(SPSCommonClassBase).Location.[Expression-ObjectID] site Always
T(SPSCommonClassBase).State.Value status Always

Example: field mapping of the SKUs

Columns Expression Netbox Target Field Policy
Model model Always
Manufacturer.Name manufacturer Always
[Expression-ObjectID] cf_matrix42_eoid On Create
1 u_height On Create

3.7 Status mapping

The status mapping assigns a NetBox status to each Matrix42 status. It is available for site, location and assets and is used when the field mapping delivers the status field.

  • Statuses without an assigned NetBox status are ignored. Objects with such a status are not transferred and are reported as errors in the log.
  • The NetBox status must be valid for the respective object:
NetBox object Valid statuses
Device Offline, Active, Planned, Staged, Failed, Inventory, Decommissioning
Site, Location Planned, Staging, Active, Decommissioning, Retired

3.8 Running the export

3.8.1 One-time: linking existing NetBox objects

If NetBox already contains regions, sites, locations, device types or devices that are to be maintained from Matrix42 in the future, they must be linked before the first sync. Otherwise the sync creates duplicates or fails because names are already taken.

The Matrix42 Link Existing Objects script assigns the records delivered by Matrix42 to NetBox objects that are not yet linked. For each entity it compares a selectable field and, if the values match, writes the custom field matrix42_eoid and the tag m42-managed. It does not create objects and does not change any other fields.

Section Field Default Description
Matrix42 connection Configuration ID Configuration ID from the Export tab
Matrix42 connection Verify TLS enabled Verify the Matrix42 TLS certificate. Disable only for test systems with self-signed certificates.
Regions, Sites, Locations, Device types, Devices Enabled enabled Link the entity in this run
Regions, Sites, Locations, Device types, Devices Match field name, name, name, model, serial NetBox field used for the comparison, e.g. serial, asset_tag, name or cf_<custom field>
Commit disabled Without commit, all changes are rolled back at the end (simulation).

Rules:

  • The comparison ignores upper and lower case as well as leading and trailing spaces.
  • The match field must be delivered as a NetBox target in the field mapping of the entity. If the value should not be written during the sync, use the field policy Never.
  • If a value occurs on several unlinked NetBox objects, none of them is linked.
  • Objects that are already linked remain unchanged. Differences in the match field are reported as warnings.

Recommended procedure:

  1. Run the script without Commit.
  2. Correct the reported differences in Matrix42 or NetBox and repeat step 1 until the log is clean.
  3. Run the script with Commit.
  4. Then run Matrix42 Sync.

3.8.2 Manually in NetBox

In NetBox, open the Matrix42 Sync script under Customization > Scripts, enter the parameters and run it:

Field Default Description
Configuration ID Configuration ID from the Export tab
Verify TLS enabled Verify the Matrix42 TLS certificate. Disable only for test systems with self-signed certificates.
Commit disabled Without commit, all changes are rolled back at the end (simulation).

The result contains, per entity, the number of created, updated, unchanged, failed and no longer delivered (disappeared) objects, e.g.:

regions: 0 created, 0 updated, 1 unchanged, 0 failed, 0 disappeared | sites: … | assets: 0 created, 0 updated, 1 unchanged, 0 failed, 0 disappeared

Alternatively, Matrix42 Sync can be scheduled directly in NetBox (options Schedule at and Recurs every when running the script).

3.8.3 Automatically from Matrix42 (engine activation “Netbox - Export”)

The extension supplies the engine activation Netbox - Export. It starts the workflow Netbox - Trigger Export, which starts the script stored under Netbox Script Id in NetBox for this configuration, waits for the NetBox job to finish and stores the status, time, result and log in the Last Export … fields on the Export tab.

Tab Preconfiguration
General Name Netbox - Export, the whole activation is disabled
Activated Engines Engine Start Workflow, engine activation parameters: Workflow ID: Netbox - Trigger Export
Schedules no schedule

Setup is the same as for the import (see 2.3): add a schedule on the Schedules tab, clear the option that disables the whole activation on the General tab and save.

Prerequisites for starting from Matrix42:

  • Netbox Url and Token (v1) are set on the General tab; the token is allowed to run scripts.
  • Netbox Script Id on the Export tab contains the ID of the Matrix42 Sync script.
  • Enable Export is selected.

3.9 Sync behavior

  • Linking: Matrix42 objects and NetBox objects are matched exclusively via the custom field matrix42_eoid (label “Matrix42 EOID”). The script creates the custom field on the first run and assigns it to Region, Site, Location, Device Type and Device.
  • Tagging: All objects created or changed by the sync get the tag m42-managed. Automatically created manufacturers, tenants and tags are tagged as well.
  • New objects: Objects without a matching matrix42_eoid are created. Regions, sites, locations and device types get the slug m42-<ID>.
  • Unchanged objects are not saved and do not create a changelog entry.
  • Objects no longer delivered (e.g. because of a changed filter or objects deleted in Matrix42) are only reported in the log. They are neither changed nor deleted in NetBox.
  • Errors: Connection, HTTP or format errors while retrieving data abort the entire run, so that the sync never works with incomplete data. Errors in individual records are logged; the remaining records are still processed.
  • Simulation: Without commit, all changes are rolled back at the end. The result then ends with SIMULATION - nothing was saved.

3.10 Web service reference

Base URL: https://<Matrix42 server>/m42Services/api/mountx/netbox

Authentication

  1. POST /m42Services/api/ApiToken/GenerateAccessTokenFromApiToken with the header Authorization: Bearer <Matrix42 API token>. The response contains an access token in the RawToken field.
  2. All further calls with the header Authorization: Bearer <RawToken>.

Endpoints

Method Path Parameter Response
GET /config configurationId Export configuration: export checkboxes, data definitions, filters, field mappings and status mappings
GET /regions configurationId { "Regions": [ … ] }
GET /sites configurationId { "Sites": [ … ] }
GET /locations configurationId { "Locations": [ … ] }
GET /skus configurationId { "Skus": [ … ] }
GET /assets configurationId { "Assets": [ … ] }
GET /script Download of the script module netbox_sync.py

Each record contains the NetBox targets defined in the field mapping as keys. If an entity is not enabled for export, the list is empty.

Example:

GET https://matrix42.example.com/m42Services/api/mountx/netbox/skus?configurationId=<Configuration ID>
Authorization: Bearer <RawToken>
Accept: application/json
{
  "Skus": [
    {
      "model": "Catalyst 9300",
      "manufacturer": "Cisco",
      "cf_matrix42_eoid": "<ID of the SKU>",
      "u_height": 1
    }
  ]
}

Web service error messages

Message Cause
NetBox sync configuration '<ID>' not found The configuration ID does not exist.
NetBox sync configuration '<ID>' is disabled Enable Configuration is not selected on the General tab.
NetBox sync configuration '<ID>': export is not enabled Enable Export is not selected on the Export tab.
NetBox sync configuration '<ID>': no <entity> field mappings configured A complete field mapping is missing for an exported entity.
… MTX_StatusMapping is not valid JSON / … MTX_FieldMappingPolicy is not valid JSON The stored mappings are corrupt; check the mappings in the dialog and save them again.

3.11 Troubleshooting

The following messages appear in the log of the NetBox job or in the Last Export Log field.

Message Cause Solution
environment variable M42_API_TOKEN is not set on the NetBox container (likewise M42_BASE_URL) Environment variable missing Set the variable and restart NetBox including the worker
configuration ID is not a valid GUID Configuration ID missing or wrong Copy the configuration ID from the Export tab
…/GenerateAccessTokenFromApiToken: 401 … or response contains no RawToken Matrix42 API token invalid or expired Create a new API token and update M42_API_TOKEN
… CERTIFICATE_VERIFY_FAILED … The Matrix42 certificate is not trusted Add the certificate chain in NetBox; disable Verify TLS only in test environments
<entity>: export disabled in configuration, skipped Entity not enabled for export Information; select Export … on the Export tab if required
record has no cf_matrix42_eoid Required row missing Add the row [Expression-ObjectID]cf_matrix42_eoid
duplicate EOID in payload Object delivered more than once Check the columns expressions and filter
status: no mapping for Matrix42 status <value> Status not mapped Complete the status mapping
role: no device role with slug '<slug>' in NetBox - create it first Device role missing in NetBox Create the device role in NetBox
<field>: no synced <entity> entry for EOID '<ID>' Referenced object was not transferred to NetBox Enable the parent entity for export, check its filter and errors
<field>: cannot be applied to <object> NetBox target does not exist Correct the field name in the field mapping
unknown field policy '<policy>' Invalid field policy Correct the field policy
validation failed: … NetBox validation, e.g. required field missing or name already taken Complete the field mapping; link existing objects first (see 3.8.1)
… no longer delivered by Matrix42 (left untouched) Object is no longer delivered Information; the object is not changed in NetBox
the field mappings deliver no '<field>' column, add a mapping row for it first Match field missing in the field mapping (Link Existing Objects) Add a mapping row for the match field
Logo

Start your process improvement

Contact us and find out more.

Get started