Spacewalk Reboot Check

General Information

Check running kernel vs newest installed to see if the system needs a reboot.

These checks rely upon the information gathered via Spacewalk kernel inventory for each system. (So it is accurate as of the last system check in time)

Checklist


The Script

Execute on a system that has spacecmd installed and configured.

sw_reboot-check.sh
#!/bin/bash
# Name: sw_reboot-check.sh
# Description: Check running kernel vs newest installed to see if the system needs a reboot.
# Last Modified: 2016-10-07
# Recent Changes:-header format
#                -Minor spelling correction and added comments
####################################################################################
 
##### Customize These Variables #####
# Spacecmd command and any default arguments
spacecmd_cmd="spacecmd -q"
 
##### End of Customize Variables #####
 
#=====================================
# Functions; Main starts after
#=====================================
function show_usage
{
  echo -e "\n==== Reboot Check ===="
  echo -e "\nDescription: Check running kernel vs newest installed to see if the system needs a reboot."
  echo -e "\n--Usage--"
  echo -e "-h                    => Display usage."
  echo -e "-s system             => Check a single system's kernel."
  echo -e "-g system-group       => Check a system groups' kernel."
  echo -e "-a                    => Check all systems' kernel."
  echo -e "\n--Other Requirements--"
  echo -e "-> Spacecmd and config file setup required."
  echo -e "-> Run as a user with admin privileges on the Spacewalk server."
  echo -e
}
 
#=======================
# Get Script Arguments
#=======================
# Reset POSIX variable in case it has been used previously in this shell
OPTIND=1
 
## Default settings ##
# Do not assume we want to check all systems
all_systems="no"
 
## Get command line arguments ##
while getopts "hs:g:a" opt; do
  case "${opt}" in
    h) # -h (help) argument
      show_usage
      exit 0
    ;;
    s) # -s system
      single_system=${OPTARG}
      check_type="single"
    ;;
    g) # -g system-group
      system_group=${OPTARG}
      check_type="group"
    ;;
    a) # -a (all systems)
      all_systems="yes"
      check_type="all"
    ;;
    *) # invalid argument
      show_usage
      exit 0
    ;;
  esac
done
 
## Argument Sanity Checks ##
# Scenario: No arguments set
if [[ ${all_systems} == "no" ]]; then
  if [[ -z ${single_system} && -z ${system_group} ]]; then
    echo -e "\n>> ERROR! You must decide on something to check."
    show_usage
    exit 1
  fi
fi
 
# Scenario: All systems AND single system set
if [[ ${all_systems} == "yes" && ${single_system} ]]; then
  echo -e "\n>> ERROR! Incompatible arguments (check all systems and a single system)."
  show_usage
  exit 1
fi
 
# Scenario: All systems AND system group set
if [[ ${all_systems} == "yes" && ${system_group} ]]; then
  echo -e "\n>> ERROR! Incompatible arguments (check all systems and a system group)."
  show_usage
  exit 1
fi
 
# Scenario: Single system AND system group set
if [[ ${single_system} && ${system_group} ]]; then
  echo -e "\n>> ERROR! Incompatible arguments (check a single system and a system group)."
  show_usage
  exit 1
fi
 
#===================
# Pre-checks: Ensure dependencies exist
#===================
 
# Check for Spacecmd
which spacecmd &> /dev/null
if [[ $? -ne 0 ]]; then
  echo "\n>> Error! The command 'spacecmd' is not found or not in PATH. Exiting..."
  exit 1
fi
 
# Check to see if a spacecmd config file exists
if [[ ! -f ${HOME}/.spacecmd/config ]]; then
  echo -e "\n>> Error! No spacecmd config file found at: ${HOME}/.spacecmd/config. Exiting..."
  exit 1
fi
 
#===================
# Main starts here
#===================
 
echo -e "========================================"
echo -e "####=== Spacewalk: Reboot Check ====####"
echo -e "========================================"
echo
echo -e "Check the following systems to see if they need a reboot..."
if [[ ${single_system} ]]; then
  echo -e "Checking system: ${single_system}"
elif [[ ${system_group} ]]; then
  echo -e "Checking system group: ${system_group}"
else
  echo -e "Checking ALL systems registered with Spacewalk."
fi
echo -e "\n=>Continue?[y/n]:\c"
read run_script
 
if [[ ${run_script} != "y" ]]; then
  echo -e "\n>> Will not run the Reboot Check script. Exiting..."
  exit 1
fi
 
### Kernel Variable Intent ###
## kernel_running -> Get reported running kernel and filter output with sed to remove x86_64 
#  (leaving version number only)
## kernel_latest -> Get installed kernels, filter kernel- and x86_64 from the name 
#  (leaving version number only). Then, sort list (field sep = '.') using fields 1-5 with version format (-V)
#   tail -1 to get the highest version installed
 
## Check System Running and Installed Kernels ##
case ${check_type} in
  single)
  ## Single system ##
    echo -e "\n>> Checking the system '${single_system}'...\n"
    kernel_running="$(${spacecmd_cmd} system_details ${single_system} | awk '/Kernel/ {print $2}' | sed 's/.x86_64//')"
    kernel_latest="$(${spacecmd_cmd} system_listinstalledpackages ${single_system} | grep ^kernel-[0-9] | sed 's/kernel-//' | sed 's/.x86_64//' | sort -t '.' -k1,5 -V | tail -1)"
 
    if [[ ${kernel_running} != ${kernel_latest} ]]; then
      echo -e "--> Reboot Required: ${node} (Running kernel: ${kernel_running}, Latest Installed: ${kernel_latest})"
    fi
  ;;
 
  group)
  ## Group of systems (Spacewalk Group) ##
    echo -e "\n>> Checking the group '${system_group}'...\n"
 
    for node in $(${spacecmd_cmd} group_listsystems ${system_group}); do
      echo -e "-> Checking ${node}..."
      kernel_running="$(${spacecmd_cmd} system_details ${node} | awk '/Kernel/ {print $2}' | sed 's/.x86_64//')"
      kernel_latest="$(${spacecmd_cmd} system_listinstalledpackages ${node} | grep ^kernel-[0-9] | sed 's/kernel-//' | sed 's/.x86_64//' | sort -t '.' -k1,5 -V | tail -1)"
 
      if [[ ${kernel_running} != ${kernel_latest} ]]; then
        echo -e "--> Reboot Required: ${node} (Running kernel: ${kernel_running}, Latest Installed: ${kernel_latest})"
      fi
    done
  ;;
 
  all)
  ## All Systems ##
    echo -e "\n>> Checking all systems...\n"
 
    for node in $(${spacecmd_cmd} system_list); do
      echo -e "-> Checking ${node}..."
      kernel_running="$(${spacecmd_cmd} system_details ${node} | awk '/Kernel/ {print $2}' | sed 's/.x86_64//')"
      kernel_latest="$(${spacecmd_cmd} system_listinstalledpackages ${node} | grep ^kernel-[0-9] | sed 's/kernel-//' | sed 's/.x86_64//' | sort -t '.' -k1,5 -V | tail -1)"
 
      if [[ ${kernel_running} != ${kernel_latest} ]]; then
        echo -e "--> Reboot Required: ${node} (Running kernel: ${kernel_running}, Latest Installed: ${kernel_latest})"
      fi
    done
  ;;
esac
 
echo -e "\n============================="
echo -e "=- Reboot Check Completed. -="
echo -e "============================="