first commit

This commit is contained in:
Yavuz Aydın 2021-01-07 16:45:53 +01:00
parent d885161d6a
commit 1c34e9715f
1 changed files with 43 additions and 26 deletions

View File

@ -22,7 +22,7 @@
# Script: check_reboot-required # # Script: check_reboot-required #
# License: This file is licensed under the GPLv3 License # # License: This file is licensed under the GPLv3 License #
# Purpose: Check if reboot is required # # Purpose: Check if reboot is required #
# Tested on: Proxmox 6, CentOS 6/7/8, Debian 8/9/10 and Ubuntu 16.04/18.04/20.04 # # Tested on: Proxmox 6, CentOS 6/7/8, Debian 7/8/9/10 and Ubuntu 16.04/18.04/20.04 #
# # # #
############################################################################################## ##############################################################################################
# # # #
@ -36,17 +36,27 @@
APPNAME=`basename $0` APPNAME=`basename $0`
OK=0 # Defining standard messages and exit error levels
WARNING=1 # https://nagios-plugins.org/doc/guidelines.html
CRITICAL=2 OK_EXIT_CODE=0
UNKNOWN=3 WARNING_EXIT_CODE=1
CRITICAL_EXIT_CODE=2
UNKNOWN_EXIT_CODE=3
OK_MSG="OK"
WARNING_MSG="Warning"
CRITICAL_MSG="Critical"
UNKNOWN_MSG="Unknown"
# Using Unknown as default
EXIT_CODE=$UNKNOWN_EXIT_CODE
EXIT_MSG=$UNKNOWN_MSG
warn_only="false" warn_only="false"
which lsb_release 2>/dev/null >/dev/null which lsb_release 2>/dev/null >/dev/null
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
echo "lsb_release is missing" echo "lsb_release is missing"
exit ${UNKNOWN} exit ${UNKNOWN_EXIT_CODE}
fi fi
function usage() { function usage() {
@ -70,7 +80,7 @@ case $1 in
;; ;;
-h|--help) -h|--help)
usage usage
exit 0 exit ${OK_EXIT_CODE}
;; ;;
esac esac
@ -82,20 +92,22 @@ function check_debian() {
packages="" packages=""
fi fi
if [ -z "${packages}" ]; then if [ -z "${packages}" ]; then
EXIT_MSG_BODY="System reboot is required"
if [ "${warn_only}" == "true" ]; then if [ "${warn_only}" == "true" ]; then
echo "System reboot is required" EXIT_MSG=${WARNING_MSG}
exit ${WARNING} EXIT_CODE=${WARNING_EXIT_CODE}
else else
echo "System reboot is required" EXIT_MSG=${CRITICAL_MSG}
exit ${CRITICAL} EXIT_CODE=${CRITICAL_EXIT_CODE}
fi fi
else else
EXIT_MSG_BODY="System reboot is required by: ${packages}"
if [ "${warn_only}" == "true" ]; then if [ "${warn_only}" == "true" ]; then
echo "System reboot is required by: ${packages}" EXIT_MSG=${WARNING_MSG}
exit ${WARNING} EXIT_CODE=${WARNING_EXIT_CODE}
else else
echo "System reboot is required by: ${packages}" EXIT_MSG=${CRITICAL_MSG}
exit ${CRITICAL} EXIT_CODE=${CRITICAL_EXIT_CODE}
fi fi
fi fi
else else
@ -112,14 +124,16 @@ function check_redhat() {
output=$(needs-restarting -r | xargs) output=$(needs-restarting -r | xargs)
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
echo "${output}" echo "No reboot required: ${output}"
exit ${OK} exit ${OK_EXIT_CODE}
elif [ $? -eq 1 ]; then elif [ $? -eq 1 ]; then
echo "${output}" EXIT_MSG_BODY="${output}"
if [ "${warn_only}" == "true" ]; then if [ "${warn_only}" == "true" ]; then
exit ${WARNING} EXIT_MSG=${WARNING_MSG}
EXIT_CODE=${WARNING_EXIT_CODE}
else else
exit ${CRITICAL} EXIT_MSG=${CRITICAL_MSG}
EXIT_CODE=${CRITICAL_EXIT_CODE}
fi fi
fi fi
} }
@ -128,15 +142,17 @@ function check_running_kernel() {
newest_installed_kernel=`ls -t /boot/vmlinuz-* | sed "s/\/boot\/vmlinuz-//g" | head -n1` newest_installed_kernel=`ls -t /boot/vmlinuz-* | sed "s/\/boot\/vmlinuz-//g" | head -n1`
running_kernel=`uname -r` running_kernel=`uname -r`
if [[ $newest_installed_kernel != $running_kernel ]]; then if [[ $newest_installed_kernel != $running_kernel ]]; then
echo "System reboot is required by: newer kernel (${newest_installed_kernel}) is available, running kernel is ${running_kernel}" EXIT_MSG_BODY="System reboot is required by: newer kernel (${newest_installed_kernel}) is available, running kernel is ${running_kernel}"
if [ "${warn_only}" == "true" ]; then if [ "${warn_only}" == "true" ]; then
exit ${WARNING} EXIT_MSG=${WARNING_MSG}
EXIT_CODE=${WARNING_EXIT_CODE}
else else
exit ${CRITICAL} EXIT_MSG=${CRITICAL_MSG}
EXIT_CODE=${CRITICAL_EXIT_CODE}
fi fi
else else
echo "No reboot required" echo "No reboot required"
exit ${OK} exit ${OK_EXIT_CODE}
fi fi
} }
@ -154,5 +170,6 @@ case "${distro}" in
;; ;;
esac esac
# Never reached # Echo message and exit
exit ${UNKNOWN} echo "${EXIT_MSG}: ${EXIT_MSG_BODY}"
exit $EXIT_CODE