Python and Nagios XI Api
Posted: Mon Jan 30, 2017 4:10 pm
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)
The result :
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!
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)
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."
}
Thank you!