API for schedule downtime

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
Locked
vishal313
Posts: 50
Joined: Wed Dec 18, 2019 10:23 pm

API for schedule downtime

Post 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
dchurch
Posts: 858
Joined: Wed Oct 07, 2020 12:46 pm
Location: Yo mama

Re: API for schedule downtime

Post 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"
If you didn't get an 8% raise over the course of the pandemic, you took a pay cut.

Discussion of wages is protected speech under the National Labor Relations Act, and no employer can tell you you can't disclose your pay with your fellow employees.
vishal313
Posts: 50
Joined: Wed Dec 18, 2019 10:23 pm

Re: API for schedule downtime

Post 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
dchurch
Posts: 858
Joined: Wed Oct 07, 2020 12:46 pm
Location: Yo mama

Re: API for schedule downtime

Post 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"
If you didn't get an 8% raise over the course of the pandemic, you took a pay cut.

Discussion of wages is protected speech under the National Labor Relations Act, and no employer can tell you you can't disclose your pay with your fellow employees.
Locked