Page 1 of 1
Removal of hosts and service using API
Posted: Thu Jan 07, 2021 12:20 am
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
Re: Removal of hosts and service using API
Posted: Thu Jan 07, 2021 5:13 pm
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
Re: Removal of hosts and service using API
Posted: Tue Jan 12, 2021 10:16 am
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
Re: Removal of hosts and service using API
Posted: Tue Jan 12, 2021 11:43 am
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