diff --git a/check_reboot-required b/check_reboot-required new file mode 100644 index 0000000..8104315 --- /dev/null +++ b/check_reboot-required @@ -0,0 +1,154 @@ +#!/bin/bash +############################################################################################## +# # +# 88 # +# 88 # +# 88 # +# ,adPPYba, 8b,dPPYba, ,adPPYba, 88 ,adPPYba, ,adPPYba, 88,dPYba,,adPYba, # +# I8[ "" 88P' `"8a a8P_____88 88 a8" "" a8" "8a 88P' "88" "8a # +# `"Y8ba, 88 88 8PP""""""" 88 8b 8b d8 88 88 88 # +# aa ]8I 88 88 "8b, ,aa 88 888 "8a, ,aa "8a, ,a8" 88 88 88 # +# `"YbbdP"' 88 88 `"Ybbd8"' 88 888 `"Ybbd8"' `"YbbdP"' 88 88 88 # +# # +# # +############################################################################################## +# # +# Author: Andreas Maus / Snel.com - Yavuz Aydin # +# E-mail: support@snel.com # +# (c) Snel.com - all rights reserved # +# # +############################################################################################## +# # +# Script: check_reboot-required # +# License: This file is licensed under the GPLv3 License # +# Purpose: Check if reboot is required # +# # +############################################################################################## +# # +# Changelog: # +# # +############################################################################################## +# # +# 2021/01/07 1.0 First release # +# # +############################################################################################## + +OK=0 +WARNING=1 +CRITICAL=2 +UNKNOWN=3 + +warn_only="false" + +which lsb_release 2>/dev/null >/dev/null +if [ $? -ne 0 ]; then + echo "lsb_release is missing" + exit ${UNKNOWN} +fi + +function usage() { + echo "check_reboot-required" + echo "" + echo "Usage: $0 [-w|--warn] [-h|--help]" + echo "" + echo " -h This text" + echo " --help" + echo "" + echo " -w Report warning (instead of a critical) condition if reboot is required" + echo " --warn" + echo "" +} + +case $1 in + -w|--warn) + warn_only="true" + ;; + -h|--help) + usage + exit 0 + ;; +esac + +function check_debian() { + if [ -s /var/run/reboot-required ]; then + if [ -s /var/run/reboot-required.pkgs ]; then + packages=$(xargs < /var/run/reboot-required.pkgs) + else + packages="" + fi + if [ -z "${packages}" ]; then + if [ "${warn_only}" == "true" ]; then + echo "System reboot is required" + exit ${WARNING} + else + echo "System reboot is required" + exit ${CRITICAL} + fi + else + if [ "${warn_only}" == "true" ]; then + echo "System reboot is required by: ${packages}" + exit ${WARNING} + else + echo "System reboot is required by: ${packages}" + exit ${CRITICAL} + fi + fi + else + check_running_kernel + fi +} + +function check_redhat() { + major=$(lsb_release -rs | cut -d '.' -f 1) + + if [ ${major} -lt 7 ]; then + check_running_kernel + fi + + output=$(needs-restarting -r | xargs) + if [ $? -eq 0 ]; then + echo "${output}" + exit ${OK} + elif [ $? -eq 1 ]; then + echo "${output}" + if [ "${warn_only}" == "true" ]; then + exit ${WARNING} + else + exit ${CRITICAL} + fi + fi +} + +function check_running_kernel() { + newest_installed_kernel=`ls -t /boot/vmlinuz-* | sed "s/\/boot\/vmlinuz-//g" | head -n1` + running_kernel=`uname -r` + if [[ $newest_installed_kernel != $running_kernel ]]; then + if [ "${warn_only}" == "true" ]; then + echo "System reboot is required by a newer kernel" + exit ${WARNING} + else + echo "System reboot is required by a newer kernel" + exit ${CRITICAL} + fi + else + echo "No reboot required" + exit ${OK} + fi +} + +distro=$(lsb_release -is) +case "${distro}" in + Debian|Ubuntu) + check_debian + ;; + CentOS) + check_redhat + ;; + *) + echo "Unsupported distribution ${distro}" + exit ${UNKNOWN} + ;; +esac + +# Never reached +exit ${UNKNOWN}