Removal of hosts and service using API

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
Locked
mejokj
Posts: 353
Joined: Mon Jul 22, 2013 10:31 pm

Removal of hosts and service using API

Post by mejokj »

Hi Team,

Can you help with the script removal of hosts and along with services from scheduled Maintenance mode Using API from nagiosxi.

Thanks
benjaminsmith
Posts: 5324
Joined: Wed Aug 22, 2018 4:39 pm
Location: saint paul

Re: Removal of hosts and service using API

Post by benjaminsmith »

Hi @mejokj,

You can use the API commands to do this. The API is fully documented in the GUI by going to Help > API Docs.

There are two parts to automating this task. In order to remove the scheduled downtime, it's necessary to retrieve the internal_id. You can get list of all the objects and their corresponding internal_id with using the following example command. Check the API docs for this by going to objects/downtime.

Code: Select all

curl -XGET "https://192.168.23.113/nagiosxi/api/v1/objects/downtime?apikey=sCWXTQ3rHtm483AgRUUtLi04v5ECCVmktCCGoU8mINpPPflWafJbRKeGO8fGjUh6&pretty=1"
Then the following command System Command will remove scheduled downtime.

Code: Select all

curl -XDELETE "https://192.168.23.113/nagiosxi/api/v1/system/scheduleddowntime/10?apikey=sCWXTQ3rHtm483AgRUUtLi04v5ECCVmktCCGoU8mINpPPflWafJbRKeGO8fGjUh6&pretty=1"
If you have a bunch of objects in schedule downtime, then you'll need to make a simple script to grab all the internal_ids and delete them.

Let me know if you have any questions about the API.

We don't really create custom scripts as part of product support. Below is an example you can use to help get you started.

Code: Select all

import requests
import json

ipaddress = '192.168.23.113'
apikey = 'sCWXTQ3rHtm483AgRUUtLi04v5ECCVmktCCGoU8mINpPPflWafJbRKeGO8fGjUh6'
data = requests.get("http://" + ipaddress + "/nagiosxi/api/v1/objects/downtime?apikey=" + apikey )
objects = json.loads(data.text)
ids = []
 
for item in objects['scheduleddowntime']:
    ids.append(item['internal_id'])

data2 = {"query": {"match_all": {}}}
 
for id in ids:
    delete_downtime = 'http://' + ipaddress + '/nagiosxi/api/v1/system/scheduleddowntime/' + id + '?apikey=' + apikey
    r= requests.delete(delete_downtime, data=data2)

print('done')
Best Regards,
Benjamin
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.

Be sure to check out our Knowledgebase for helpful articles and solutions!
mejokj
Posts: 353
Joined: Mon Jul 22, 2013 10:31 pm

Re: Removal of hosts and service using API

Post by mejokj »

Hi Benjaminsmith,

Thanks for your feedback. We have a few more queries

Do we have any other way to remove the host/services from Scheduled Maintenance mode instead of getting object ID.

The attachment is collected Internal ID using API, but how we should know that this internal ID belongs to this host. I know in output hostname is showing like "host_name": "localhost".

Attachment is sent as PM to you.

Could you please make us clear instead of selecting internal_id do we have an option like if we give hostnames in one file which we need to removed from scheduled Maintenance mode based on that list, Nagios api should remove hosts/services from Scheduled Maintenance mode.

Thanks
benjaminsmith
Posts: 5324
Joined: Wed Aug 22, 2018 4:39 pm
Location: saint paul

Re: Removal of hosts and service using API

Post by benjaminsmith »

Hi @mejokj,

For the system/scheduleddowntime endpoint, the internal id is required in order to delete the event.
api-downtime.png
However, it won't be too difficult to parse GET objects/downtime and grab all of the internal ids associated with a specific hostname by adding an extra step or check for that in your API script.

Best Regards,
Benjamin
You do not have the required permissions to view the files attached to this post.
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.

Be sure to check out our Knowledgebase for helpful articles and solutions!
Locked