Custom links in Netbox for Snipe-IT Asset Management

Netbox supports Custom Links per object (Sites, Devices, …), which can be used to link within Netbox to other web-based applications. This function comes handy if access to other information or data, like CAD drawings or PDF or Office files, is required.

If the external application has an URL scheme like https://en.wikipedia.org/wiki/Ni_Hao, then it’s easy to replace the Ni_Hao-part with an object from Netbox.

Please note that the naming of sites in Netbox and locations in Snipe-IT should be the same. Because Snipe-IT is a PHP-based web application, the code for the API call runs on the same server and is also written in PHP.

However, it gets more complex if the application uses ID’s or unique ID’s to manage data. In this case, the best method is to check out the application API if there is something like a get_id_by_name function.

In my blog post, I’m integrating as example Snipe-IT Asset Management with a Custom Link in Netbox Site view. The same method (API call) should also work with other web-based applications and the result then looks like below:

Netbox: Custom Links example
Netbox: Custom Links example

Netbox Custom links

In Netbox, Custom links can be set per object-basis. For a link from a Netbox Sites-object to a Snipe-ITlocation, the link need to be created in DCIM->Site like shown in the screenshot.

Netbox: Custom Link to Snipe-IT Location
Netbox: Custom Link to Snipe-IT Location

It’s possible to group links (for example external/internal links) by setting a Group name. By using a Button class, different link types can be visually highlighted (for example red for external links and green for internal links). By ticking the checkbox New window the link opens in a new browser window or browser tab.

The URL scheme makes usage of Jinja2 templating, which means {{ obj }} is replaced by the corresponding Netbox-object (Sites, Devices, …). In my case, the link is to a PHP script running on the Snipe-IT web server. The PHP script expects a parameter location, coming from the Netbox Sites-object.

    https://assetmanagement.tld/netbox-snipeit.php?location={{ obj }}
Netbox: URL with object in Jinja2 template format

Snipe-IT Generate API Token

In Snipe-IT, the API token can be generated as shown in the screenshot below. The API Token is used in the PHP script to authenticate against Snipe-IT.

Snipe-IT: Generate API Token
Snipe-IT: Generate API Token

The API Token is used to call Snipe-IT’s APILocations function, which gets the location-id by searching the site name passed to the PHP script.

PHP script

Because a location in Snipe-IT cannot be accessed with a site name passed in the URL, some type of script functionality is required. The algorithm of the PHP script works like this:

  • Initialize a snipeit array for an API call to the locations function

  • Take the site name from Netbox as GET parameter

  • Use PHPcurl function to authenticate to Snipe-ITAPI and pass the site name

  • If Snipe-ITAPI returns a result, decode the json data as PHPobject

  • From the PHPobject, get the location id value corresponding to the site name

  • Redirect to Snipe-IT with the location id in the URL

The PHP curl function is required. If this is not installed, PHP curl can be installed in a Debian Linux by the commands below. Please note: To activate PHP curl a restart of the web server is required.

    # apt install php7.3-curl
    # /etc/init.d/apache2 restart
Install PHP curl and restart Apache web server

Because Snipe-IT is installed on my server in /var/www/html/snipe-it/, I’m putting the PHP script into the Snipe-IT’s public folder (as: /var/www/html/snipe-it/public/netbox-snipeit.php).

    <?php
    #
    # Initialize a snipeit array with token and URL to location API function
    #  
    $snipeit = Array(
        'token' => 'Bearer API_TOKEN_GOES_HERE
        'accept' => 'application/json',
        'content-type' => 'application/json',
        'url' => 'https://SNIPEIT_FQDN/api/v1/locations?limit=1&offset=0&search=',
    );
    #
    # Verify if a location parameter is passed as HTTP GET
    #
    if(isset($_GET['location'])) {
        #
        # Set CURL options and get data
        #
        $ch = curl_init($snipeit['url'] . $_GET['location']);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_URL, $snipeit['url'] . $_GET['location']);
        curl_setopt($ch, CURLOPT_HTTPHEADER, Array('authorization: ' . $snipeit['token']));
        $data = curl_exec($ch);
        #
        # If CURL returns HTTP status code 200 (OK), then we can
        # parse the json data and redirect to Snipe-IT location
        #
        if(curl_getinfo($ch, CURLINFO_HTTP_CODE) == 200){
            #
            # use json data as object
            #
            $object = json_decode($data);
              #
              # DEBUG to verify output
              #
              #print_r($object);
              #print $object->rows[0]->id;
            #
            # Do an HTTP redirect to Snipe-IT location by the ID
            #
            $redirect = 'Location: https://SNIPEIT_FQDN/locations/' . $object->rows[0]->id;
            header($redirect);
            exit;
        }
        curl_close($ch);
    }
    ?>
PHP-Script: netbox-snipeit.php

The code of the PHP script is also available on GitHub at netbox-custom-links


Share: