Page 1 of 1

API for schedule downtime

Posted: Fri Mar 12, 2021 12:53 am
by vishal313
Hi All,

We have Nagios XI 5.6.6 and 5.6.10 in our environment. I need your help in knowing if we have any API for scheduling downtime for hostgroups in Nagios XI.
I could find information about the API for scheduling downtime for hostlist and service list but i need to know if we have anything for hostgroups and service groups and its usage.

Thank you.

Regards
Vishal

Re: API for schedule downtime

Posted: Fri Mar 12, 2021 12:00 pm
by dchurch
You could just use a shell script to form the request from a poll of the host group. Here's an example script.

Code: Select all

#!/bin/bash
# vi: et sts=4 sw=4 ts=4

# Schedule downtime for all the hosts in a host group
# For use with Nagios XI 5.8.1 or later... but it will probably work on older versions.
#
# Author: Dan Church
# Date: 2021-03-12
# License: MIT

# Fill in these values with your own configuration
API_KEY='<fill in your API key here>'
XI='http://127.0.0.1/nagiosxi'
# Unix integer timestamp, e.g. 1615353391; The --date= argument can be in the format '2021-03-09 14:00:00 UTC'
BEGIN=$(date +%s --date='now')
END=$((BEGIN + 300)) # 5 minutes of downtime
HOSTGROUP='linux-servers'
COMMENT='Test downtime creation'

if ! hash jq &>/dev/null; then
    echo 'jq is required. Install using "yum install jq" (CentOS/RHEL)' >&2
    exit 2
fi

url_encode() {
    perl -mURI::Escape -e 'print URI::Escape::uri_escape($_), "\n" foreach @ARGV' "$@"
}

DATA="comment=$(url_encode "$COMMENT")&start=$BEGIN&end=$END"
for HOSTNAME in $(
    curl -X GET \
        -s \
        "$XI/api/v1/config/hostgroup/$(url_encode "$HOSTGROUP")?apikey=$API_KEY" |
    jq -r .[0].members[]
); do
    DATA+="&hosts[]=$(url_encode "$HOSTNAME")"
done


curl -X POST \
     -s \
    -d "$DATA" \
    "$XI/api/v1/system/scheduleddowntime?apikey=$API_KEY&pretty=1"

Re: API for schedule downtime

Posted: Sun Mar 14, 2021 6:40 pm
by vishal313
Hi Dan,

Thank you very much for the script shared that covers the task I want to accomplish.
But i would like to know if there is an out of the box API available to put a host group in scheduled downtime.

Regards
Vishal Dhote

Re: API for schedule downtime

Posted: Mon Mar 15, 2021 9:23 am
by dchurch
Actually, I was mistaken, yes there is. If you look under Help (top menu) => System Reference (left menu), then under [POST] system/scheduleddowntime, it outlines it:
[POST] system/scheduleddowntime
Schedules a new scheduled downtime in Nagios XI.

Code: Select all

Required Parameters  Value Type  Values
start                integer     timestamp
end                  integer     timestamp
comment              string      comment
author               string      api key's user by default if empty or a username
Optional Parameters  Value Type  Values
hosts[]              array       list of host names (i.e. hosts[]=1&hosts[]=2, etc)
child_hosts          integer     0 or 1 for regular / 2 for children triggered by parent
all_services         integer     0 or 1 for all services on specified hosts
services[host]       array       list of host/service combos (i.e. services[localhost]=PING)
hostgroups[]         array       list of host group names
servicegroups[]      array       list of service group names
flexible             integer     0 or 1
duration             integer     flexible duration length in minutes
triggered_by         integer     ID of downtime to be triggered by
only                 string      hosts, services (make downtime for only hosts or services only applies to servicegroups[] and hostgroups[])
So an example here for a host group would be:

Code: Select all

curl -X POST \
    -d "comment=Test downtime creation&start=1615374363&end=1615381563&hostgroups[]=linux-servers&only=hosts" \
    "http://127.0.0.1/nagiosxi/api/v1/system/scheduleddowntime?apikey=<my API key>&pretty=1"