#!/bin/bash # Name: sw_force-client-checkin.sh # Description: Force Spacewalk client(s) to check in and pick up scheduled jobs # Last Updated: 2016-10-07 # Recent Changes:-Improved error checking for groups, removed spacecmd dependency # check for single system commands # -ssh connect timeout 5 seconds added #################################################################################### ##### 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==== Force Client Check In ====" echo -e "\nDescription: Force each Spacewalk client to check in and pick up scheduled jobs." echo -e "\n--Usage--" echo -e "-h => Display usage." echo -e "-s system => Single system check in." echo -e "-g system-group => System group check in." echo -e "-a => All systems check in." 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 "-> Run as a user with sudo privileges on the target server(s) for rhn_check." 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 all systems to check in 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} checkin_type="single" ;; g) # -g system-group system_group=${OPTARG} checkin_type="group" ;; a) # -a (all systems) all_systems="yes" checkin_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 in." 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 (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 (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 (single system and a system group)." show_usage exit 1 fi #=================== # Pre-checks: Ensure dependencies exist #=================== # Only check for Spacewalk dependencies if NOT sending to a single system if [[ -z ${single_system} ]]; then # 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 fi #=================== # Main starts here #=================== echo -e "======================================================" echo -e "####=== Spacewalk: Force all clients check-in ====####" echo -e "======================================================" echo echo -e "Force clients registered to Spacewalk to check in (rhn_check)." if [[ ${single_system} ]]; then echo -e "Force single client to check in: ${single_system}" elif [[ ${system_group} ]]; then echo -e "Force clients in group to check in: ${system_group}" else echo -e "Force ALL systems to check in." fi echo -e "\n=>Continue?[y/n]:\c" read run_script if [[ ${run_script} != "y" ]]; then echo -e "\n>>Will not run the force client check-in script. Exiting..." exit 1 fi case ${checkin_type} in single) ## Single system ## echo -e "\n>> Single system check in..." echo "->${single_system}" ssh -qt -o ConnectTimeout=5 ${single_system} "sudo /usr/sbin/rhn_check" ;; group) ## Group of systems (Spacewalk Group) ## echo -e "\n>> Group check in (${system_group})..." # Check to see if the Spacewalk group exists; exit if it does not ${spacecmd_cmd} group_list | grep ${system_group} > /dev/null if [[ $? -ne 0 ]]; then echo -e "-> ERROR! Could not find Spacewalk group: ${system_group}" exit 1 fi for node in $(${spacecmd_cmd} group_listsystems ${system_group}); do echo "->${node}" ssh -qt -o ConnectTimeout=5 ${node} "sudo /usr/sbin/rhn_check" done ;; all) ## All Systems ## echo -e "\n>> All systems check in..." for node in $(${spacecmd_cmd} system_list); do echo "->${node}" ssh -qt -o ConnectTimeout=5 ${node} "sudo /usr/sbin/rhn_check" done ;; esac echo -e "\n===================================" echo -e "=- Client Check In(s) Completed. -=" echo -e "==================================="