Using IP Fabric’s API to add devices into NAGIOS XI

6 minute read

In this blog post we are going to show you how we can use the API Clients created for IP Fabric and NAGIOS. We are going to work on a simple scenario where we want to create hosts discovered by IP Fabric into NAGIOS XI.

There are already a few blogs on how to use IP Fabric’s API, so today we’re going to focus on using the API clients, which have been created to simplify interaction and data collection.

1.    How to get the API clients

First, you need to download the code in our GitHub repository: community-fabric/integration-demos using your preferred method:

Inside the api_clients folder, you will see the existing API clients. We will have a look at ipf and nagios, to understand how to use them.

[email protected]_vDEMO$ git clone https://github.com/community-fabric/integration-demos
Cloning into 'integration-demos'…
[…]
Resolving deltas: 100% (32/32), done.
[email protected]_vDEMO$ cd integration-demos/api_clients/
[email protected]_vDEMO$

Before looking at each API client, we need to define some requirements to ensure everything will work as expected.

1.1 Pre-requisites

For this blog post, I will be using iPython for its powerful interactive shell, but you can use Python3. To use the Nagios and IPF api client, you need to install the HTTPX library, and we would recommend using the rich library as well.

How to install iPython:

[email protected]_vDEMO$ pip install ipython

How to install httpx and rich libraries:

[email protected]_vDEMO$ pip install httpx rich

1.2 IP Fabrics’s API client – Overview

To start using the API Client, you need to provide the information regarding the IP Fabric server and the token. 

To create the token on IP Fabric, go to Settings/API Tokens and create a new token:

You need to create environment variables:

[email protected]_vDEMO$ export IPF_URL="https://ipfabric.server/"
[email protected]_vDEMO$ export IPF_TOKEN="123qwe45ert67tyui"

We can now run ipython to create the IP Fabric client:

[email protected]_vDEMO$ ipython
Python 3.8.5 (default, May 27 2021, 13:30:53)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.25.0 -- An enhanced Interactive Python. Type '?' for help.

1.2.1 Class: IPFClient

Let’s explore the IPFClient class, which will interact with the IP Fabric server.

In [1]: from ipf.ipf_api_client import IPFClient, IPFDevice
In [2]: from rich import print
In [3]: ipf=IPFClient()

The Class ipf will be created using the environment variables you created above. To confirm it has worked correctly, you can look at the list of snapshots. This will confirm that we have access to the data from the IP Fabric server:

In [5]: ipf.snapshot_list()
Out[5]:
[{'index': 0,
  'id': '50ac77c2-8be4-495b-b0ef-ea2a75bfa21c',
  'name': None,
  'count': 682,
  'state': 'loaded'},
 […]
 {'index': 18,
  'id': 'e9a5504b-1592-4c74-a84f-5188f99a9283',
  'name': 'Demo day 1 - 202106 (clone)',
  'count': 678,
  'state': 'unloaded'}]
In [6]:

At the time of writing, this is the list of functions you can use within the IPFClient class:

  • 'snapshot_list' – provide the list of snapshots
  • 'site_list' – provide the list of sites
  • 'device_list' – provide the list of devices
  • 'fetch_table' – return IP Fabric data from any table as a json

1.2.2 Class: IPFDevice

You can now assign a device to a variable which will allow you to access information for that specific device (IP address, hostname, site…)

In [6]: d = IPFDevice("L38EXR1")
In [7]: print(d.hostname+"|"+d.ipaddr)
L38EXR1|10.38.255.101

This concludes the overview of the IPF API client. Before showing a more specific example on how to use it, we will look at the Nagios XI API client.

1.3 NAGIOS XI API Client – Overview

Just like we did with IP Fabric, we need to ensure we have a valid token to use the Nagios XI API. To proceed, you need to go to your account setting (1) and generate an API key (2):

You also need to create the environment variables related to Nagios:

[email protected]_vDEMO$ export NAGIOS_URL="https://nagios.server"
[email protected]_vDEMO$ export NAGIOS_TOKEN="0987lkj76uyt54fds"

We can now run ipython to create the NAGIOS XI API client:

[email protected]_vDEMO$ ipython
Python 3.8.5 (default, May 27 2021, 13:30:53)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.25.0 -- An enhanced Interactive Python. Type '?' for help.

1.3.1 Class: NAGIOSClient

Let’s explore the NAGIOSClient class, which will interact with the NAGIOS server.

In [1]: from ipf.ipf_api_client import IPFDevice, IPFClient
   ...: from nagios.nagios_api_client import NAGIOSSensor, NAGIOSClient
In [2]: nagios = NAGIOSClient()

The Class nagios will be created using the environment variables you created above. To confirm it has worked correctly, you can look at the list of existing hosts. This will confirm that we have access to Nagios’ data:

In [3]: nagios.host_list()
Out[3]:
[{'index': 0,
  'host_name': 'localhost',
  'alias': 'localhost',
  'display_name': 'localhost',
  'address': '127.0.0.1',
  'object_id': '150',
  'host_object_id': '150',
  'is_active': '1'},
[…]
]
In [4]: 

At the time of writing, this is the list of functions you can use within the NAGIOSClient class:

  • 'host_list' – provide the list of hosts
  • 'hostgroup_list' – provide the list of hostgroups
  • 'create_hostgroup' – create the hostgroup indicated, if not already present

1.3.2 Class: NAGIOSSensor

Using this class, you will be able to create a new host directly into Nagios. Here is how to proceed:

In [12]: host = NAGIOSSensor(name="L45R10", ipaddr="10.48.1.1", site="45 Ballpark")
In [13]: host.status
Out[13]: "created: {'success': 'Added L45R10 to the system. Config applied, Nagios Core was restarted.'}"

If you look at Nagios, you will now see under Hostgroup Overview the recently created host L45R10, inside the hostgroup “45 Ballpark”

This is brilliant, we can now interact with the API for IP Fabric and Nagios. In the next section we will see how we can use this to automatically populate devices in Nagios based on the information collected by IP Fabri

2. Creating Hosts in NAGIOS using the data from IP FABRIC

In this part, we are going to take, for example, the following situation: you have decided to replace your previous monitoring system with Nagios, and you want to ensure all devices will be correctly added. How can you proceed?

  • You could manually add each device, working with your CMDB, but what happens if that list is not 100% accurate, some IPs may have changed, devices were added or removed, and you are not aware.
  • Second option, execute a script to create Nagios hosts using the data from IP Fabric, so you know it’s accurate and up-to-date. Let’s look at this option in more detail.

2.1 Create Nagios hosts using a Python script

You are about to witness how short the code to execute this is!

"""
Script to show simple use of ipf_api_client and nagios_api_client
"""

from ipf.ipf_api_client import IPFClient
from nagios.nagios_api_client import NAGIOSHost
def main():
  #creation of the IPFClient
  ipf = IPFClient()
  # collect all devices from IP Fabric using the filter
  site_filter = {"siteName": ["like", "45"]}
  devices = ipf.device_list(filters=site_filter)
  # now we create in Nagios each device
  for device in devices:
    print(
      f" -- Adding device '{device['hostname']}' in site '{device['siteName']}'"
    )
    NAGIOSHost(name=device["hostname"], ipaddr=device["loginIp"], site=device["siteName"])
  print(
    f"Job completed! {len(devices)} devices were added to hostgroup '{devices[0]['siteName']}'"
  )
if __name__ == "__main__":
  main()

2.2 Important notes

Please ensure you have created the environment variable as mentioned previously in this blog:

[email protected]_vDEMO$ export IPF_URL="https://ipfabric.server/"
[email protected]_vDEMO$ export IPF_TOKEN="123qwe45ert67tyui"
[email protected]_vDEMO$ export NAGIOS_URL="https://nagios.server"
[email protected]_vDEMO$ export NAGIOS_TOKEN="0987lkj76uyt54fds"

An easy way to create the filter is to follow these steps:

1 - Go to Inventory/Devices

2 - Use any filter to get the list of devices you want to add into Nagios

3 - Click on the question mark “Table description”

4 - In the payload, find the “filters”, use this to create the variable filters in the python script. In our situation it becomes:

filters = {"siteName": ["like", "45"]}

3. Conclusion

You can learn more at www.ipfabric.io and also follow our company’s LinkedIn page or Blog, where we continually post new content.

For more information regarding existing and on-going integrations, join #CommunityFabric on the Discord server https://discord.gg/jCaEMG794P

Get IP Fabric

Request a demo and discover how to increase
your networks visibility & get better time efficiency.
Free Demo | Zero Obligation
Request a Demo
We're Hiring!
Join the Team and be part of the Future of Network Automation
Available Positions
IP Fabric, Inc.
115 BROADWAY, 5th Floor
NEW YORK NY, 10006
United States
This is a block of text. Double-click this text to edit it.
Phone : +1 (914) 752-2991
Email : [email protected]
IP Fabric s.r.o.
Kateřinská 466/40
Praha 2 - Nové Město, 120 00
Czech Republic
This is a block of text. Double-click this text to edit it.
Phone : +420 720 022 997
Email : [email protected]
IP Fabric, Inc. © 2022 All Rights Reserved