Python and Nagios XI Api

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
Locked
bennyboy
Posts: 157
Joined: Thu Oct 29, 2015 9:42 am

Python and Nagios XI Api

Post by bennyboy »

Hi,

I try to build a script to automate the creation of host and services in NagiosXI. I use ServiceNOW as primary source (CMDB) + NCPA + Nagios XI.
With Python I generate a payload to requests.post to Nagios XI to create the host.
Sample of my code (You don't have all)

Code: Select all

def create_hosts(response_snow):
    # For each server in the ServiceNOW List
    for server in response_snow['result']:
        # For specific OS Type
        if server['os'] == operating_systems:
            # For specific environments
            if server['used_for'] in environments:
                # Make sure the host not already exist in Nagios XI
                if not check_monitoring_exist(server['name']):
                    # Create the host
                    a = socket.gethostbyname(server['name'])
                    print "Create the host " + server['name']
                    payload = {'hostname': str(server['name']), 'address': a, 'use': 'xi_nonprod_linuxserver_host', 'applyconfig': '1', 'force': '1'}
                    print payload
                    post_json(nagiosxi_api_url, payload)
The result :

Code: Select all

Create the host servera
{'use': 'xi_nonprod_linuxserver_host', 'hostname': 'servera', 'force': '1', 'applyconfig': '1', 'address': '172.17.7.98'}
{
    "success": "Successfully added  to the system. Config applied, Nagios Core was restarted."
}

Create the host serverb
{'use': 'xi_nonprod_linuxserver_host', 'hostname': 'serverb', 'force': '1', 'applyconfig': '1', 'address': '172.17.7.114'}
{
    "success": "Successfully added  to the system. Config applied, Nagios Core was restarted."
}

Create the host serverc
{'use': 'xi_nonprod_linuxserver_host', 'hostname': 'serverc', 'force': '1', 'applyconfig': '1', 'address': '172.26.14.201'}
{
    "success": "Successfully added  to the system. Config applied, Nagios Core was restarted."
}

Create the host serverd
{'use': 'xi_nonprod_linuxserver_host', 'hostname': 'serverd', 'force': '1', 'applyconfig': '1', 'address': '172.16.7.50'}
{
    "success": "Successfully added  to the system. Config applied, Nagios Core was restarted."
}
If I go to CCM in Nagios XI I only see 1 new host empty. In /use/local/nagios/etc/hosts/ I see a file .cfg with 172.17.7.98 and the template. Can you help me!

Thank you!
User avatar
mcapra
Posts: 3739
Joined: Thu May 05, 2016 3:54 pm

Re: Python and Nagios XI Api

Post by mcapra »

Is initiating a separate API call at the end of the sequence to apply the configuration an option? Something to the effect of:

Code: Select all

if (end of list)
    # send payload with applyconfig: 1
Rather than running an applyconfig at each step. See if that produces a different effect.
Former Nagios employee
https://www.mcapra.com/
bennyboy
Posts: 157
Joined: Thu Oct 29, 2015 9:42 am

Re: Python and Nagios XI Api

Post by bennyboy »

I remove the applyconfig. I have the same result. Do I have to slow down the post ?
I will modify my list to do it only for 1 server. For the moment I have only 4 servers ... I don't understand.
User avatar
mcapra
Posts: 3739
Joined: Thu May 05, 2016 3:54 pm

Re: Python and Nagios XI Api

Post by mcapra »

Well, you'll need an applyconfig in general to submit the changes to the back-end. Otherwise nothing will change. My suggestion was to feed all your objects into Nagios XI via the API, then as the last step run an applyconfig.
Former Nagios employee
https://www.mcapra.com/
bennyboy
Posts: 157
Joined: Thu Oct 29, 2015 9:42 am

Re: Python and Nagios XI Api

Post by bennyboy »

I already try that. I have the same result.
dwhitfield
Former Nagios Staff
Posts: 4583
Joined: Wed Sep 21, 2016 10:29 am
Location: NoLo, Minneapolis, MN
Contact:

Re: Python and Nagios XI Api

Post by dwhitfield »

As you put in a ticket for this, I am going to lock the thread.
Locked