Automatically Set Host Downtime on Reboots

Admins often forget to set downtimes, when they “just reboot” a server, so I created a simple systemd service that makes a host set itself into a short downtime on reboots.
Maybe this is also useful for others…

[Unit]
Description=Set CMK downtime on reboot
After=network.target

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/true
ExecStartPre=/bin/bash -c "/bin/systemctl set-environment hostname_short=$(/bin/hostname -s)"
ExecStop=/bin/curl -m 30 "https://<CMK URL>/<INSTANCE>/check_mk/view.py?_username=<AUTOMATION USER>&_secret=<SECRET>&_transid=-1&_do_confirm=yes&_do_actions=yes&host=${hostname_short}&_down_from_now=yes&_down_minutes=<DOWNTIME LENGTH>&_down_comment=<COMMENT>&view_name=hoststatus"
StandardOutput=null
StandardError=null

[Install]
WantedBy=multi-user.target

Edit this file with your CMK enviroment settings for URL, instance, user and secret. Add a downtime comment and set the desired length of the downtime.
Then save it to /etc/systemd/system/set_cmk_downtime.service.
Reload systemd “systemctl daemon-reload” and enable/start the service “systemctl enable --now set_cmk_downtime.service

The service assumes the system’s short hostname (without domain) is the same as the hostname in CMK. If not you have to manually configure the hostname.

8 Likes

Thanks for this. Since we use automatic reboots quite a lot when installing updates over night, this is a most welcome addition.

Regards,
Louis

Good idea - I suppose in CEE environments using the bakery, one could also pull the name that the host has in checkmk from the cmk-update-agent.state
something like:
grep -Po “(?<=host_name’: ‘)([^’]*)” /etc/cmk-update-agent.state
suggestions for easier regexes are welcome :slight_smile:

did you build a limited role for the automation_user?

'host_name'\s*:\s*'([^']*)' could be a bit more explicit and white-space robust

You could also get the registered server, site and protocol from the /etc/check_mk/cmk-update-agent.cfg to build the URI for the API call.

3 Likes

May I offer another suggestion here: Our brand new Ansible Collection features a downtime module, which enables you to easily schedule downtimes during update runs.
I see your use case and situation, just want to offer another angle here. :slight_smile:

4 Likes

Hi,
did anyone sucsessfully transfer this system service for working with checkmk 2.3?
API changes are breaking these usfull service.

This is my current solution for 2.3. Split into a systemd service and a simple script for setting the downtime.

Systemd Service

[Unit]
Description=Set CMK downtime on reboot
Requires=network-online.target
After=network-online.target

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/bin/true
ExecStop=/usr/local/sbin/set_cmk_downtime.sh

[Install]
WantedBy=multi-user.target

set_cmk_downtime.sh

#!/bin/bash

hostname_short=$(/bin/hostname -s)
starttime=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
endtime=$(date -u +"%Y-%m-%dT%H:%M:%SZ" --date="now +<DOWNTIME LENGTH> minutes")

/bin/curl -k -m 15 \
  --request POST \
  --header "Authorization: Bearer <USER> <SECRET>" \
  --header "Accept: application/json" \
  --header "Content-Type: application/json" \
  -s \
  --data '{ "comment": "<COMMENT>", "downtime_type": "host", "end_time": "'"$endtime"'", "host_name": "'"$hostname_short"'", "start_time": "'"$starttime"'" }' \
  "https:///<CMK HOST>/<INSTANCE>/check_mk/api/1.0/domain-types/downtime/collections/host"
2 Likes