Saturday, May 29, 2010

Yum Check or Install Updates Script

SkyHi @ Saturday, May 29, 2010
Here's a quick script that will check yum updates and email you when there are new updates available.

Split into two sections, one script is for all package updates, and the other script is for letting us know if we need to reboot when a new kernel package has been installed.

Script to check for updates:

# checks for yum updates and emails if there are any available
# Eric Thern
# Zoidial Incorporated
# last update:
# Dec 30, 2008

# change this to your email

# no need to change anything below here


$yum check-update >& $yumtmp



case $yumstatus in
# no updates!
exit 0
number=$(cat $yumtmp | egrep '(.i386|.x86_64|.noarch|.src)' | wc -l)
updates=$(cat $yumtmp | egrep '(.i386|.x86_64|.noarch|.src)')
echo "
There are $number updates available on host $hostname at $date

The available updates are:
" | /bin/mail -s "UPDATE: $number updates available for $hostname" $email

# clean up

rm -f /tmp/yum-check-update.*

Script to check kernel (here we use 'ovzkernel' since we're running with an openvz kernel, if you have a stock centos kernel, change this to 'kernel')

latestkernel=$(rpm -q ovzkernel |tail -n1|sed -e 's/kernel-//')

echo "$latestkernel"

if uname -a | grep -qv "$latestkernel"; then
echo "Running Kernel is" `uname -r` "but latest installed rpm is ${latestkernel}" |\
mail -s "UPDATE: ${HOSTNAME} reboot required" $email

Crontab entries:

30 21 * * * /root/bin/ >/dev/null 2>&1
30 21 * * * /root/bin/ >/dev/null 2>&1

Run at 9:30 every night. Change times and paths to suit.