Skip to content

Instantly share code, notes, and snippets.

@guisea
Last active May 30, 2024 18:18
Show Gist options
  • Save guisea/b16431029c9b78d5c8b692effe7f6e48 to your computer and use it in GitHub Desktop.
Save guisea/b16431029c9b78d5c8b692effe7f6e48 to your computer and use it in GitHub Desktop.
A Custom Check for CheckMK - Checks RHEL/Debian and derived OS's for pending reboots
#!/bin/bash
# Filename: /local/share/check_mk/agents/custom/linux-reboot/lib/local/600/needs-restarted
source /etc/os-release
VERSION=$(echo ${VERSION} | awk -F . '{ print $1 }')
# RHEL and Derivatives
if ([[ $ID_LIKE =~ 'fedora' ]] || [[ $ID =~ 'rhel' ]]) && [[ $VERSION =~ '8' ]];
then
# RHEL 8
REBOOT=`dnf needs-restarting -r >/dev/null 2>&1; echo $?;`
elif ([[ $ID_LIKE =~ 'fedora' ]] || [[ $ID =~ 'rhel' ]]) && [[ $VERSION =~ '7' ]]
then
# RHEL 7
REBOOT=`needs-restarting -r >/dev/null 2>&1; echo $?;`
fi
# Debian and Derivatives
if ([[ $ID_LIKE =~ 'debian' ]] || [[ $ID =~ 'debian' ]]);
then
# Nice and easy, check if /var/run/reboot-required is present
if [ -f /var/run/reboot-required ];
then
REBOOT=1
else
REBOOT=0
fi
fi
if [[ $REBOOT -eq 0 ]]
then
echo "0 reboot-required - OK: Reboot not required"
elif [[ $REBOOT -ge 1 ]]
then
echo "1 reboot-required - WARN: Updated packages indicate reboot is required"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment