Following workaround for raw users might be a no no to share, since recurring scheduled downtimes is an enterprise feature. If indeed not appreciated, just remove the topic.
Warning: I am a newbie regarding rest api, bash scripting & cron so following might be done better or may impose risks. But made this today, tested it and seems to be working. (-:
Using Checkmk Raw Edition 2.1.0p5 on Ubuntu 20.04.
Based on Help | Developer resources | REST API Documentation | Downtimes | Create a host related scheduled downtime, I adjusted the code example in following bash script. I named it downtime.sh
#!/bin/bash
# bash downtime.sh host "start datetime string" "stop datetime string" "comment"
DT_HOST=$1
DT_START=$2
DT_STOP=$3
DT_TXT=$4
HOST_NAME="[chkmk_host]"
SITE_NAME="[site]"
API_URL="http://$HOST_NAME/$SITE_NAME/check_mk/api/1.0"
USERNAME="[user]"
PASSWORD="[password]"
# This schema has multiple variations. Please refer to the
# 'Payload' section for details.
http --ignore-stdin POST "$API_URL/domain-types/downtime/collections/host" \
"Authorization: Bearer $USERNAME $PASSWORD" \
"Accept: application/json" \
'Content-Type:application/json' \
start_time=$(date -Is --date="$DT_START") \
end_time=$(date -Is --date="$DT_STOP") \
recur='fixed' \
duration='0' \
comment="$DT_TXT" \
downtime_type='host' \
host_name=$DT_HOST \
In above script change [chkmk_host]
, [site]
, [user]
and [password]
for the values on your server. During testing, I used the main adminstrator account. You might want to make a seperate user for it, since password is readable in script.
Package httpie
wasn’t installed on my server, so I had to do that first.
$(date -Is --date="$DT_START")
makes it possible to feed script human readable date time strings and have it converted in ISO 8601 format.
--ignore-stdin
was added to http
due to following error when I tried to run script from cron:
http: error: Request body (from stdin or a file) and request data (key=value) cannot be mixed. Pass --ignore-stdin to let key/value take priority.
Running it from command you might get errors as output, but in cron no go, so following command is used to run script and log the output:
bash /pathto/script/downtime.sh host "next Friday 06:30" "next Friday 17:30" "downtime comment" >> /pathto/log/downtime.log 2>&1
And in cron, I used crontab of root during testing, scheduled for every Sunday afternoon, calling script scheduling the downtime for coming week:
0 13 * * sun bash /pathto/script/downtime.sh host "next Friday 06:30" "next Friday 17:30" "downtime comment" >> /pathto/log/downtime.log 2>&1
Script might be expanded by also adding duration and downtime_type as arguments, but I had no use for it now, so skipped that. Adding recur has no use for raw, since enterprise is needed to use anything other than fixed value.
Enjoy!