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 "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.

4 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.

2 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: