Skip to content

Instantly share code, notes, and snippets.

@rfdrake
Created October 24, 2022 14:19
Show Gist options
  • Save rfdrake/714f71992af6f2ea097f22d348efd91d to your computer and use it in GitHub Desktop.
Save rfdrake/714f71992af6f2ea097f22d348efd91d to your computer and use it in GitHub Desktop.
Netbox device export template for OpenNMS
<?xml version="1.0" encoding="UTF-8"?>
<model-import xmlns="http://xmlns.opennms.org/xsd/config/model-import"
date-stamp="2009-01-11T19:25:03.851-05:00"
foreign-source="netbox-2022">
{% for device in queryset %}
{% if device.status == 'active' and device.primary_ip4 and ( 'No Monitoring' not in device.tags.names() ) %}
{% set netbox_url = 'https://YOURNETBOXHOST/dcim/devices/' %}
{% set id = device.cf.bbsites_id or 'nb'~device.id %}
{% set url = '<a href=' ~ netbox_url ~ device.id ~ ' target="_blank">' ~ netbox_url ~ device.id ~ '</a>' %}
{% set addr = device.site.cf.address1 %}
{% set city = device.site.cf.city %}
{% set state = device.site.cf.state %}
{% set zip = device.site.cf.zip %}
{% set building = device.site.cf.site_code or device.site %}
{% set ip4, mask = ('' ~ device.primary_ip4).split("/") %}
{% set parent_foreign_id = 'if we supply this with a custom field' %}
{% set origin = device.cf.origin %}
{% if 'Non Production' in device.tags.names() %}
{% set alerts = 'NoAlerts'%}
{% else %}
{% set alerts = 'Alerts1M'%}
{% endif %}
<node node-label="{{ device }}" foreign-id="{{ id }}" building="{{ building }}" city="{{ city }}">
<interface ip-addr="{{ ip4 }}" descr="" status="1" managed="true" snmp-primary="P">
<monitored-service service-name="ICMP"/>
</interface>
<category name="{{ device.device_role }}"/>
<category name="ORIGIN_{{ origin }}"/>
{% for t in device.tags.names() %}
<category name="{{ t }}"/>
{% endfor %}
<category name="{{ alerts }}"/>
<asset name="Latitude" value="{{ device.site.latitude }}" />
<asset name="Longitude" value="{{ device.site.longitude }}" />
<asset name="ZIP" value="{{ zip }}" />
<asset name="State" value="{{ state }}" />
<asset name="Address 1" value="{{ addr }}" />
<asset name="manufacturer" value="{{ device.device_type.manufacturer }}" />
<asset name="model" value="{{ device.device_type }}" />
<asset name="serial" value="{{ device.serial }}" />
<asset name="description" value="{{ url |e }}" />
<meta-data context="requisition" key="ORIGIN" value="{{ origin }}" />
</node>
{% endif %}
{%- endfor %}
</model-import>
@rfdrake
Copy link
Author

rfdrake commented Oct 24, 2022

I've seen some hacky stuff to tie opennms to netbox. I won't claim this isn't hacky, but it's simpler than many of the other solutions.

We use several custom values like "device.site.cf.site_code" or "device.site.cf.city", so you would need to tailor this to your own system.

In order to use this you would need to go to "Other | Export Templates" then create a new template called "OpenNMS" with a content type of DCIM > device. Uncheck "as attachment".

Then on the NMS box you need something like this:

curl -sk -H "Authorization: Token $NETBOX_AUTH" --compressed -H 'Connection: keep-alive'  \
    "https://YOURNETBOXURL/api/dcim/devices/?export=OpenNMS" > $IMPORTDIR/imports-netbox.$DATE
/usr/share/opennms/bin/send-event.pl \
            -p "url file:$IMPORTDIR/imports-netbox.$DATE" \
             uei.opennms.org/internal/importer/reloadImport

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment