Page 1 of 1

Python and Nagios XI Api

Posted: Mon Jan 30, 2017 4:10 pm
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!

Re: Python and Nagios XI Api

Posted: Mon Jan 30, 2017 5:11 pm
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.

Re: Python and Nagios XI Api

Posted: Mon Jan 30, 2017 5:54 pm
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.

Re: Python and Nagios XI Api

Posted: Tue Jan 31, 2017 11:30 am
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.

Re: Python and Nagios XI Api

Posted: Tue Jan 31, 2017 11:35 am
by bennyboy
I already try that. I have the same result.

Re: Python and Nagios XI Api

Posted: Tue Jan 31, 2017 2:45 pm
by dwhitfield
As you put in a ticket for this, I am going to lock the thread.