Page 1 of 1
NagiosXI API Schedule Maintinance
Posted: Tue Apr 09, 2019 10:05 pm
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
Re: NagiosXI API Schedule Maintinance
Posted: Wed Apr 10, 2019 11:18 am
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
Re: NagiosXI API Schedule Maintinance
Posted: Wed Apr 10, 2019 8:04 pm
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
Re: NagiosXI API Schedule Maintinance
Posted: Thu Apr 11, 2019 9:39 am
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'