Schedule and delete downtime automatically

Support forum for Nagios Core, Nagios Plugins, NCPA, NRPE, NSCA, NDOUtils and more. Engage with the community of users including those using the open source solutions.
Locked
wagnbeu0
Posts: 25
Joined: Tue Nov 03, 2015 5:28 am

Schedule and delete downtime automatically

Post by wagnbeu0 »

Hi, we plkan to schedule an downtime and delete the downtime automatically. We are using windows servers.
the best thing i was able to do was to create hoist groups, and then use the curl and date implementation from cygwin.

So my script is:

Code: Select all

echo set variables
set nagiosserver=https://servername
set apikey=gggxxxxxxxxxxxxxxxxxxxfffffffffffff
set hostgroups=%1
set comment=%2

if "%1"=="" goto :ERROR
if "%2"=="" goto :ERROR

echo set time
for /f %%i in ('D:\nagios-downtime\date1.exe +%%s') do set start_time=%%i
for /f %%j in ('D:\nagios-downtime\date1.exe +%%s -d "+2 Minutes"') do set finish_time=%%j

echo run program
echo D:\nagios-downtime\curl.exe --insecure -XPOST "%nagiosserver%/nagiosxi/api/v1/system/scheduleddowntime?apikey=%apikey%&pretty=1" -d "comment=%comment%&start=%start_time%&end=%finish_time%&hostgroups[]=%hostgroups%&all_services=1"
D:\nagios-downtime\curl.exe --insecure -XPOST "%nagiosserver%/nagiosxi/api/v1/system/scheduleddowntime?apikey=%apikey%&pretty=1" -d "comment=%comment%&start=%start_time%&end=%finish_time%&hostgroups[]=%hostgroups%&all_services=1"
Running the script is:
schedule-downtime.cmd devsystem Testdowntime

This works perfect. All hosts and services in the devsystem hostgroup are now in downtime mode for 2 minutes.

Now I want to finish the downtime for a complete hostgroup. How can I do this?
The documentation tells me that deleting a downtime requires the following info:
system/scheduleddowntime/<internal_id>
Deletes a scheduled downtime from the Nagios XI system. The downtime ID can be found in objects/downtime.
But the objects/downtime command gives me a list back with several information. How can I find out which IDs belong to a specific hostgroup?
Last edited by wagnbeu0 on Wed May 29, 2019 9:12 am, edited 1 time in total.
wagnbeu0
Posts: 25
Joined: Tue Nov 03, 2015 5:28 am

Re: Schedule and delete downtime automatically

Post by wagnbeu0 »

So, i am one step closer ...
I made the following script:

Code: Select all

echo set variables
set nagiosserver=https://servername
set apikey=gggxxxxxxxxxxxxxxxxxxxfffffffffffff


for /f "tokens=2 delims=:" %%i in ('D:\nagios-downtime\curl --insecure -XGET "%nagiosserver%/nagiosxi/api/v1/objects/downtime?apikey=%apikey%&pretty=1&host_group=TC016" ^| findstr internal_id') do (
rem echo %%i
set item=%%i
set item=!item:~2,-2!
echo Finish Downtime for internal_id !item!
D:\nagios-downtime\curl  --insecure -XDELETE "%nagiosserver%/nagiosxi/api/v1/system/scheduleddowntime/!item!?apikey=%apikey%&pretty=1"
)
Unfortunately it will delete all downtimes in the system. I can use the following command to search for a dedicated command:
...&pretty=1&comment_data=ITL_Test2"
This will search for only services or hosts with the fixed comment I used when initiating the downtime. Unfortunately two host groups can get the same comment, so it will be better if I can use the host_group for searching:
...&pretty=1&host_group=TC016"

But this query gives me all items back, also one which are not in hostgroup TC016

How can I specify a hostgroup in this query?
wagnbeu0
Posts: 25
Joined: Tue Nov 03, 2015 5:28 am

Re: Schedule and delete downtime automatically

Post by wagnbeu0 »

OK, I finally found the information i was looking for.
Here is my script for finishing an downtime by selecting hostgroups:

Code: Select all

SETLOCAL EnableDelayedExpansion

REM set variables
set PATH=%PATH%;%~dp0
set nagiosserver=https://servername
set apikey=fPMIubC0KGPSesbNki2k48OEeaTPO6X3bJKITDYTvOLMgVQTdRuAZiatr4Cfp
set hostgroup=%1

if "%1"=="" goto :ERROR

REM run program
for /f "tokens=2 delims=:" %%i in ('curl.exe --insecure -XGET "%nagiosserver%/nagiosxi/api/v1/objects/downtime?apikey=%apikey%&pretty=1&host_name=lk:%hostgroup%" ^| findstr internal_id') do (
set item=%%i
set item=!item:~2,-2!
echo Finish Downtime for internal_id !item!
curl.exe  --insecure -XDELETE "%nagiosserver%/nagiosxi/api/v1/system/scheduleddowntime/!item!?apikey=%apikey%&pretty=1"
)

GOTO :EOF
User avatar
cdienger
Support Tech
Posts: 5045
Joined: Tue Feb 07, 2017 11:26 am

Re: Schedule and delete downtime automatically

Post by cdienger »

Glad to help ;) Thanks for updating the thread with your findings!
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.
Locked