automate maintenance mode in nagios xi
automate maintenance mode in nagios xi
Hey is there a way i could write a script in Nagios that can automate maintenance mode at a particular tie for a group of servers? we have our Jenkins developers doing some maintenance work at certain periods of time and i was wondering if there is a way they could communicate with nagios and put these servers in maintenance mode without having to reach out to us to physically login into nagios and put it into maintenance mode, i know nagios has API it can communicate through. your help will be appreciated
Re: automate maintenance mode in nagios xi
You could schedule (add) and remove downtime via the REST API in Nagios XI.
Examples:
# Schedule downtime
# Find the internal_id for a scheduled downtime
# Delete a scheduled downtime (with internal_id=517)
I am sure, these API calls could be scripted, and the script could be run on a cron. So, you have lots of flexibility here. You can view the Nagios XI REST API documentation under the "Help" menu.
Examples:
# Schedule downtime
Code: Select all
[root@main-nagios-xi nagiosxi]# curl -XPOST "http://x.x.x.x/nagiosxi/api/v1/system/scheduleddowntime?apikey=xxx&pretty=1" -d "comment=Test downtime creation&start=1569513558&end=1569514158&hosts[]=localhost"
{
"success": "Schedule downtime command(s) sent successfully.",
"scheduled": {
"hosts": [
"localhost"
]
}
}Code: Select all
[root@main-nagios-xi nagiosxi]# curl -XGET "http://x.x.x.x/nagiosxi/api/v1/objects/downtime?apikey=xxx&pretty=1"
{
"recordcount": "1",
"scheduleddowntime": {
"@attributes": {
"id": "49380"
},
"instance_id": "1",
"downtime_type": "2",
"object_id": "144",
"objecttype_id": "1",
"host_name": "localhost",
"service_description": {
},
"entry_time": "2019-09-26 11:01:31",
"author_name": "nagiosadmin",
"comment_data": "Test downtime creation",
"internal_id": "517",
"triggered_by": "0",
"fixed": "1",
"duration": "600",
"scheduled_start_time": "2019-09-26 10:59:18",
"scheduled_end_time": "2019-09-26 11:09:18",
"was_started": "1",
"actual_start_time": "2019-09-26 11:01:31",
"actual_start_time_usec": "482383"
}
}Code: Select all
[root@main-nagios-xi nagiosxi]# curl -XDELETE "http://x.x.x.x/nagiosxi/api/v1/system/scheduleddowntime/517?apikey=xxx&pretty=1"
{
"success": "Remove downtime command(s) sent successfully."
}Be sure to check out our Knowledgebase for helpful articles and solutions!
Re: automate maintenance mode in nagios xi
thank you for your response, i will need you to define the commands you pasted earlier
for example please define what am to replace from the follwing
curl -XPOST "http://x.x.x.x/nagiosxi/api/v1/system/s ... x&pretty=1" -d "comment=Test downtime creation&start=1569513558&end=1569514158&hosts[]=localhost"
{
x.x.x.x= nagios server
156951= is that a time format or what
can you please define each command. thanks
for example please define what am to replace from the follwing
curl -XPOST "http://x.x.x.x/nagiosxi/api/v1/system/s ... x&pretty=1" -d "comment=Test downtime creation&start=1569513558&end=1569514158&hosts[]=localhost"
{
x.x.x.x= nagios server
156951= is that a time format or what
can you please define each command. thanks
Re: automate maintenance mode in nagios xi
This is correct - this is the IP address of your Nagios XI server.x.x.x.x= nagios server
This is a timestamp, indicating when your scheduled downtime is supposed to start and end. You can convert "human-readable" date/time to a unix style timestamps here:156951= is that a time format or what
https://www.epochconverter.com/
Be sure to check out our Knowledgebase for helpful articles and solutions!
Re: automate maintenance mode in nagios xi
thank you, will i have to use just one time format? do i have to convert the time eachtime i want to schedule downtime or is there an easier way to just input human readable time on the command line? for example to schedule downtime for today i used the format YYMMDDHMS (2019111911500) but it scheduled it for 03-18-65953 thats way in the future when we wont even exist anymore.
Re: automate maintenance mode in nagios xi
API is a programming interface, so it is expected that you would be doing some programming, e.g. writing custom scripts, etc. Writing such scripts is out of scope of Nagios support.
Having said that, here's a script that can get you started.
where "x.x.x.x" is the IP address of your Nagios XI server, and "y.y.y.y" is the API key. The "start" time would be your current time, and the "end" time would be in 2 hours. So, in this example, the curl command will schedule downtime on localhost for 2 hours, starting now.
You can place the script in the /tmp directory, make it executable:
and run it:
You can modify this script, and expand on it if needed.
Hope this helps.
Having said that, here's a script that can get you started.
Code: Select all
#!/bin/bash
# Define variables
ip="x.x.x.x"
apikey="y.y.y.y"
start=`date +%s`
end=`date -d '+2 hour' +%s`
# Schedule downtime on localhost
curl -XPOST "http://$ip/nagiosxi/api/v1/system/scheduleddowntime?apikey=$apikey&pretty=1" -d "comment=Test downtime creation&start=$start&end=$end&hosts[]=localhost"You can place the script in the /tmp directory, make it executable:
Code: Select all
cd /tmp
chmod +x myscript.shCode: Select all
./myscript.shHope this helps.
Be sure to check out our Knowledgebase for helpful articles and solutions!
Re: automate maintenance mode in nagios xi
thank you that worked, one last question, i am trying to delete the schedules downtime with the command you provided about, is there another way to delete the scheduled downtime without using "internal id" whe you schedule downtime for hostgroups, each host from that group has different internal id's and that will take time deleting each, so is there a way to delete a scheduled down time that was scheduled for a hostsgroup? if there isnt another way, how do i end the downtime
Re: automate maintenance mode in nagios xi
Unfortunately, there is no easy way to do that. You will need to come up with a custom script, in which you would be searching for the "internal_id" in "objects/downtime", then deleting the downtime for each object (host, that is a member of your hostgroup).
Again, there is lots of programming involved with the REST API. Writing such scripts is out of scope of Nagios support.
Again, there is lots of programming involved with the REST API. Writing such scripts is out of scope of Nagios support.
Be sure to check out our Knowledgebase for helpful articles and solutions!