NagiosXI API Schedule Maintinance

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
Locked
imadc
Posts: 58
Joined: Wed Jun 14, 2017 12:10 pm

NagiosXI API Schedule Maintinance

Post by imadc »

Hello Team

We would like your help to twick the REST API call a global schedule maintenance on all hosts and associated services

curl -XPOST "https://X.X.X.X/nagiosxi/api/v1/system/ ... Y&pretty=1" -d "comment=Test downtime creation&start=1554864688&end=1554865288&hosts[]=localhost"

Then we would like to delete this scheduled maintenance downtime

curl -XGET "https://X.X.X.X/nagiosxi/api/v1/objects ... Y&pretty=1"

Get the integer then place in this command

curl -XDELETE "https://X.X.X.X/nagiosxi/api/v1/system/ ... Y&pretty=1"

Thanks
Insurity
User avatar
lmiltchev
Bugs find me
Posts: 13589
Joined: Mon May 23, 2011 12:15 pm

Re: NagiosXI API Schedule Maintinance

Post by lmiltchev »

You could place all of your hosts in a hostgroup, for example "allhosts". Then you could use a script like the one below to accomplish this:

Code: Select all

#!/bin/bash

# Define variables
ip=`ip addr | grep global | grep -m 1 'inet' | awk '/inet[^6]/{print substr($2,0)}' | sed 's|/.*||'`
apikey=`echo "select api_key from xi_users where username='nagiosadmin';" | mysql -uroot -pnagiosxi nagiosxi | tail -1`

# Schedule downtime for all hosts/services
curl -XPOST "http://$ip/nagiosxi/api/v1/system/scheduleddowntime?apikey=$apikey&pretty=1" -d "comment=Test downtime creation&start=`date +'%s'`&end=`date -d '+2 hours' +'%s'`&hostgroups[]=allhosts&all_services=1"

# Remove downtimes
for id in `curl -s -XGET "http://$ip/nagiosxi/api/v1/objects/downtime?apikey=$apikey&pretty=1" | grep "internal_id" | cut -d ':' -f2 | sed 's/[^0-9]*//g'`; do
curl -XDELETE "http://$ip/nagiosxi/api/v1/system/scheduleddowntime/$id?apikey=$apikey&pretty=1"
done
Be sure to check out our Knowledgebase for helpful articles and solutions!
imadc
Posts: 58
Joined: Wed Jun 14, 2017 12:10 pm

Re: NagiosXI API Schedule Maintinance

Post by imadc »

Thanks for the help

When I try this in power shell no action is visible or register in Nagios UI end

Could it be the quotation in the variables you sent are not passing variables through?

Any recommendation?

Thansk
Insurity
User avatar
lmiltchev
Bugs find me
Posts: 13589
Joined: Mon May 23, 2011 12:15 pm

Re: NagiosXI API Schedule Maintinance

Post by lmiltchev »

When I try this in power shell no action is visible or register in Nagios UI end
This is NOT a power shell script. It's a bash script, and it is meant to be run from the command line on the Nagios XI server (in SSH/putty session)... Why are you running it in power shell?

You can check to see if the variables resolve by using the echo command. For example:

Code: Select all

ip=`ip addr | grep global | grep -m 1 'inet' | awk '/inet[^6]/{print substr($2,0)}' | sed 's|/.*||'`
echo $ip
apikey=`echo "select api_key from xi_users where username='nagiosadmin';" | mysql -uroot -pnagiosxi nagiosxi | tail -1`
echo $apikey
Note: If you changed the default mysql root password or if your mysql is offloaded to a remote server, you would need to modify the script accordingly.

You can get a list of the "internal_id"s by running:

Code: Select all

curl -s -XGET "http://$ip/nagiosxi/api/v1/objects/downtime?apikey=$apikey&pretty=1" | grep "internal_id" | cut -d ':' -f2 | sed 's/[^0-9]*//g'
Be sure to check out our Knowledgebase for helpful articles and solutions!
Locked