monitoring-plugins/check_reboot-required

178 lines
6.5 KiB
Bash

#!/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 <maus@ypbind.de> / 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 #
# Tested on: CentOS 6/7/8, CloudLinux 7, Debian 7/8/9/10 and Ubuntu 16.04/18.04/20.04 #
# Proxmox 6 #
# #
##############################################################################################
# #
# Changelog: #
# #
##############################################################################################
# #
# 2021/01/07 1.0 First release #
# #
##############################################################################################
APPNAME=`basename $0`
# Defining standard messages and exit error levels
# https://nagios-plugins.org/doc/guidelines.html
OK_EXIT_CODE=0
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"
which lsb_release 2>/dev/null >/dev/null
if [ $? -ne 0 ]; then
echo "lsb_release is missing"
exit ${UNKNOWN_EXIT_CODE}
fi
function usage() {
cat - <<EOF
Usage: ${APPNAME} [-w|--warn] [-h|--help]
This check command checks if a reboot are required after an upgrade. It will
also list packages that requires a reboot to ease the decision for the
sysadmin if/when a reboot needs to be carried out.
-h, --help
Show this help
-w, --warn
Report warning (instead of a critical) condition if reboot is required
EOF
}
case $1 in
-w|--warn)
warn_only="true"
;;
-h|--help)
usage
exit ${OK_EXIT_CODE}
;;
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
EXIT_MSG_BODY="System reboot is required"
if [ "${warn_only}" == "true" ]; then
EXIT_MSG=${WARNING_MSG}
EXIT_CODE=${WARNING_EXIT_CODE}
else
EXIT_MSG=${CRITICAL_MSG}
EXIT_CODE=${CRITICAL_EXIT_CODE}
fi
else
EXIT_MSG_BODY="System reboot is required by: ${packages}"
if [ "${warn_only}" == "true" ]; then
EXIT_MSG=${WARNING_MSG}
EXIT_CODE=${WARNING_EXIT_CODE}
else
EXIT_MSG=${CRITICAL_MSG}
EXIT_CODE=${CRITICAL_EXIT_CODE}
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_clean=$(needs-restarting -r | xargs)
output=$(needs-restarting -r)
if [ $? -eq 0 ]; then
echo "No reboot required: ${output_clean}"
exit ${OK_EXIT_CODE}
elif [ $? -eq 1 ]; then
EXIT_MSG_BODY="${output_clean}"
if [ "${warn_only}" == "true" ]; then
EXIT_MSG=${WARNING_MSG}
EXIT_CODE=${WARNING_EXIT_CODE}
else
EXIT_MSG=${CRITICAL_MSG}
EXIT_CODE=${CRITICAL_EXIT_CODE}
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
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
EXIT_MSG=${WARNING_MSG}
EXIT_CODE=${WARNING_EXIT_CODE}
else
EXIT_MSG=${CRITICAL_MSG}
EXIT_CODE=${CRITICAL_EXIT_CODE}
fi
else
echo "No reboot required"
exit ${OK_EXIT_CODE}
fi
}
distro=$(lsb_release -is)
case "${distro}" in
Debian|Ubuntu)
check_debian
;;
CentOS|CloudLinux)
check_redhat
;;
*)
echo "Unsupported distribution ${distro}"
exit ${UNKNOWN}
;;
esac
# Echo message and exit
echo "${EXIT_MSG}: ${EXIT_MSG_BODY}"
exit $EXIT_CODE