automate maintenance mode in nagios xi

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
Locked
rnjie
Posts: 157
Joined: Wed Mar 20, 2019 4:59 pm

automate maintenance mode in nagios xi

Post by rnjie »

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
User avatar
lmiltchev
Bugs find me
Posts: 13589
Joined: Mon May 23, 2011 12:15 pm

Re: automate maintenance mode in nagios xi

Post by lmiltchev »

You could schedule (add) and remove downtime via the REST API in Nagios XI.

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"
        ]
    }
}
# Find the internal_id for a scheduled downtime

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"
    }
}
# Delete a scheduled downtime (with internal_id=517)

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."
}
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.
Be sure to check out our Knowledgebase for helpful articles and solutions!
rnjie
Posts: 157
Joined: Wed Mar 20, 2019 4:59 pm

Re: automate maintenance mode in nagios xi

Post by rnjie »

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
User avatar
lmiltchev
Bugs find me
Posts: 13589
Joined: Mon May 23, 2011 12:15 pm

Re: automate maintenance mode in nagios xi

Post by lmiltchev »

x.x.x.x= nagios server
This is correct - this is the IP address of your Nagios XI server.
156951= is that a time format or what
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:

https://www.epochconverter.com/
Be sure to check out our Knowledgebase for helpful articles and solutions!
rnjie
Posts: 157
Joined: Wed Mar 20, 2019 4:59 pm

Re: automate maintenance mode in nagios xi

Post by rnjie »

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.
User avatar
lmiltchev
Bugs find me
Posts: 13589
Joined: Mon May 23, 2011 12:15 pm

Re: automate maintenance mode in nagios xi

Post by lmiltchev »

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.

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"
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:

Code: Select all

cd /tmp
chmod +x myscript.sh
and run it:

Code: Select all

./myscript.sh
You can modify this script, and expand on it if needed.

Hope this helps.
Be sure to check out our Knowledgebase for helpful articles and solutions!
rnjie
Posts: 157
Joined: Wed Mar 20, 2019 4:59 pm

Re: automate maintenance mode in nagios xi

Post by rnjie »

thank you
rnjie
Posts: 157
Joined: Wed Mar 20, 2019 4:59 pm

Re: automate maintenance mode in nagios xi

Post by rnjie »

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
User avatar
lmiltchev
Bugs find me
Posts: 13589
Joined: Mon May 23, 2011 12:15 pm

Re: automate maintenance mode in nagios xi

Post by lmiltchev »

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.
Be sure to check out our Knowledgebase for helpful articles and solutions!
Locked