In one of my last blog posts I showed how to create
Custom links in NetBox for Snipe-IT Asset Management.
In this blog post I’m expanding the Custom Links with some additional
functionality. This time, the code is written in Python and executed as
on the NetBox server (instead of the Snipe-IT server).
Instead of running a script on each application server, I think a better
approach is to run scripts “centrally” on the NetBox server itself. This eases
also the development and handling of scripts.
Prepare Apache web server to run CGI/Python Scripts
To prepare the Apache2 web server, the CGI module needs to be activated and configured. For handling CGI executeables, I am adding after the NetBox
configuration part in /etc/apache2/sites-enabled/netbox.conf the following lines:
Please note that a ProxyPass directive is required for /cgi, otherwise the
script is executed by Gunicorn WSGI server, which uses
the NetBox web application.
To add the CGI module
mod_cgid, I’m using
the a2enmod command. Afterwards I’m restarting the web server with
the CGI module and configuration activated:
NetBox/Snipe-IT Python script for custom links
The script in Python works similar like the PHP-script in my other blog post.
However, I am checking in this script if a parameter device or location
is in the GET variable passed. If device is passed, then Snipe-IT shows
the device based on it’s hardware ID. If location is passed in the GET
request, then Snipe-IT shows the location and the hardware assigned to the
location based on the ID.
If something goes wrong, for example a site exists in NetBox but no
corresponding location exists in Snipe-IT, then all locations in Snipe-IT
are listed. For the device/hardware, I do a similar “error handling”.
If something really goes wrong, like no parameter as GET variable is passed,
then the script just returns the default page of Snipe-IT.
To use the Custom Link in NetBox, I’m setting those up in the NetBox
Administration/Extras/Custom Links with the following configuration: