my-scripts/CheckKernel.sh

117 lines
4.1 KiB
Bash
Raw Normal View History

2021-11-01 21:47:18 +00:00
#!/bin/bash
# CheckKernel checks if Kernel was updated and not aktivated (rebooted)
#
# (c) GPL by Ulf Bartholomäus ub1x@gmx.net
VERSION="$( basename $0 ) Version 0.2.3 from 12.7.2021"
#
# Input: 1 parameter optional ("-d" = debug and "-v" = verbose / "-D" = Distro only / "-K" = running KernelVerion / "-N" = New Kernel Version)
# Output: text messages and a kdialog if this is installed
# Returnvalue: 0 if Kernel OK and 10 if Kernel is changed
#
# ToDo: Check if initrd and vmlinuz exists
# cat /etc/issue
# cat /proc/version
# Error: inxi - hangs in a script:
# MyDistro="$( eval inxi -xSz | grep Distro: | awk -F'Distro: ' '{print $2}' )"
# https://github.com/smxi/inxi/issues/181
#
# Changes:
# 0.2.3: Fix error => update in case only one Kernel is avaialble
# 0.2.2: Fix error => new Kernel update: replace initrd with vmlinuz check
# 0.2.1: added -D -K and -N option
# Fixed error and exit with more than one /boot/initrd-* file
# 0.2.0: InitKernel und VmlzKernel entfernt,
# [ $( ls -1 /boot/ | grep initrd- | wc -l ) -gt 1 ] && echo "** Error" || ls -1 /boot/ | grep initrd- | cut -d '-' -f 2,3,4
# Some small changes and simplifications
# 0.1.9: NewKernel erneut angepasstsys
# 0.1.8: "==" -> "=" sowie kleinere Shönheitskorrekturen - sowie NewKernel mit awk statt cut
# 0.1.7: remove "CheckKernel.sh: Zeile 33: lsb_release: Kommando nicht gefunden."
# and => -d = debaug
# 0.1.6: if "lsb_release" not available use "cat /etc/*-release" info
# 0.1.5: Fix trouble if NewKernel is empty
# 0.1.4: Fix some errors
# 0.1.3: Fix running and new kernel are equal but shown as different
# 0.1.2: Remove "initrd-" from InitKernel
# 0.1.1: Add Kernel Info in kdialog
# 0.1.0: exchange some direct calls against definition
# add check if new Kernel is in GRUB Config file
# 0.0.7: read Kernel version from menu.lst
# 0.0.6: add verbose mode
# 0.0.5: syslog messages
# 0.0.4: returnvalue if Kernel is changed is 10
# 0.0.3: hostname in kdialog title
# 0.0.2: kdialog output
# 0.0.1: generation
#
DEBUG=""
MyDistro=""
MyKernel=""
NewKernel=""
InitKernel=""
if [ $# -gt 0 ] && [ "$1" = "-d" ] ; then
DEBUG="1"
echo -e "\t<${VERSION}>"
shift
fi
MyDistro="$( cat /proc/version )"
MyDistro="${MyDistro/*;/}" # remove first string till ";"
MyDistro="${MyDistro/)*/}" # remove anything behind ")"
MyDistro="${MyDistro/# /}" # remove blanks in the beginning
if [ $# -gt 0 ] && [ "$1" = "-D" ] ; then
echo "${MyDistro}"
exit
fi
MyKernel="$( uname -r )"
if [ $# -gt 0 ] && [ "$1" = "-K" ] ; then
echo "${MyKernel}"
exit
fi
InitKernel="$( ls -1tr /boot/ | grep 'vmlinuz-' )"
if [ $( echo "${InitKernel}" | wc -l ) -ge 1 ] ; then
# If To much Kernels - exit
# use only the last listed kernel (hopfully the newest)
NewKernel="${InitKernel/*vmlinuz-/}"
else
# no "vmlinuz" entry
[ ${DEBUG} ] && echo "** Error: /boot/vmlinuz-* is missing <${InitKernel}>"
# exit 10
NewKernel="Missing /boot/vmlinuz-*"
fi
if [ $# -gt 0 ] && [ "$1" = "-N" ] ; then
echo "${NewKernel}"
exit
fi
if [ ${DEBUG} ] ; then
echo -e "* Debug Info *\n\t MyDistro=${MyDistro},\n\t MyKernel=${MyKernel},\n\t InitKernel=${InitKernel},\n\t NewKernel=${NewKernel}!"
[ "x${MyKernel}" = "x${NewKernel}" ] && echo -e "*\t(${MyKernel}) = (${NewKernel})" || echo -e "***\t(${MyKernel}) != ${NewKernel} !!!"
echo "*"
fi
if [ "x${MyKernel}" = "x${NewKernel}" ] ; then
if [ $# -gt 0 ] && [ "$1" = "-v" ] ; then
echo -e "\t*** Distro: ${MyDistro} \tKernel ${MyKernel} \t$( date "+%X / %d.%m.%Y" ) \t*** Kernel OK ***"
else
echo "*** Kernel OK (${MyKernel})";
fi
else
if ( [ $# -gt 0 ] && [ "$1" == "-v" ] ) ; then
echo -e "\t*** Distro: ${MyDistro} \tKernel ${MyKernel} \t$( date "+%X / %d.%m.%Y" ) \t*** NEW Kernel ${NewKernel}!!! ***"
else
MsgTxt="*** Kernel Updated from ${MyKernel} to ${NewKernel}! => please reboot"
echo "${MsgTxt}";
logger -t $( basename $0 ) "${MsgTxt}"
if [ $# -eq 0 ] && [ -e $( which kdialog 2>> /dev/null ) ] ; then
kdialog --title "*** $( hostname ) ***" --error "${MsgTxt}" >> /dev/null 2>> /dev/null
fi
fi
fi