#!/bin/bash # Name: report-hostgroups-empty.sh # Description: Report Host Groups that don't have any members # Last Modified: 2018-05-31 # Recent Changes:-Initial release ############################################################################################### ##### Customize These Variables ##### # IPA admin user admin_user="admin" ##### End of Customize Variables ##### #===================================== # Functions; Main starts after #===================================== function show_usage { echo -e "\n==== Report: Empty Host Groups ====" echo -e "\nDescription: Report host groups that don't have any members." echo -e "\n--Usage--" echo -e "./report-hostgroups-empty.sh [OPTIONS]" echo -e "\n-OPTIONS-" echo -e "-h => Display usage." echo -e "-v => Verbose; extra status output." echo -e "\n--Other Requirements--" echo -e "-> FreeIPA admin access." echo -e } #======================= # Get Script Arguments #======================= # Reset POSIX variable in case it has been used previously in this shell OPTIND=1 # By default, do not output verbose verbose_mode="no" while getopts "hv" opt; do case "${opt}" in h) # -h (help) argument show_usage exit 0 ;; v) # -v (verbose) argument verbose_mode="yes" ;; *) # invalid argument show_usage exit 0 ;; esac done #=================== # Pre-checks: Make sure we have good options set #=================== # See if we have a kerberos ticket, if not, prompt login /usr/bin/klist -s if [[ $? -ne 0 ]]; then echo ">>No kerberos ticket found for (${admin_user}), login as ${admin_user} now:" /usr/bin/kinit ${admin_user} echo fi #=================== # Main starts here #=================== echo -e "======================================================" echo -e "####========= Report: Empty Host Groups ==========####" echo -e "======================================================" echo echo -e "This script will report all host groups that don't have any members." if [[ ${verbose_mode} == "yes" ]]; then echo -e "Verbose: Yes" else echo -e "Verbose: No" fi echo -e "=>Continue?[y/n]:\c" read run_script if [[ ${run_script} != "y" ]]; then echo -e "\n>>Will not run the script. Exiting..." exit 1 fi # Get all host groups echo -e ">> Getting all host groups." all_hostgroups="$(ipa hostgroup-find --sizelimit=0 | awk '/Host-group/ {print $2}')" # Initialize lists to nothing hostgroup_nomembers="" hostgroup_error="" echo -e ">> Checking ($(echo ${all_hostgroups} | wc -w)) hostgroups for members\c" if [[ ${verbose_mode} == "yes" ]]; then # Extra newline required if verbose messages (due to \c above) echo fi # Loop through all hostgroups, add ones with no members to the list for hostgroup in ${all_hostgroups}; do if [[ ${verbose_mode} == "yes" ]]; then echo "--> Hostgroup: ${hostgroup}" else # Not verbose, output a single '.' per hostgroup search echo -e ".\c" fi ipa hostgroup-show ${hostgroup} | grep 'Member hosts' &> /dev/null return_code="$?" if [[ ${return_code} == "0" ]]; then # Members found if [[ ${verbose_mode} == "yes" ]]; then echo "---Members found." else # Do nothing ':' (equivalent to Python's 'pass') : fi elif [[ ${return_code} == "1" ]]; then # No members found if [[ ${verbose_mode} == "yes" ]]; then echo "---No members. (${hostgroup})" fi hostgroup_nomembers+="${hostgroup} " else # Some other error occurred if [[ ${verbose_mode} == "yes" ]]; then echo "---Warning: Some other error occurred. (${hostgroup})" fi hostgroup_error+="${hostgroup} " fi done if [[ ${verbose_mode} == "no" ]]; then # Extra space required if not verbose because of no newlines for '.' status output echo -e "[Done]" fi echo -e "\n-------------------------------------" echo -e "---- Host Groups with No Members ----" echo -e "-------------------------------------" echo -e "\n>> Host Groups with No Members: $(echo ${hostgroup_nomembers} | wc -w)" for hostgroup in ${hostgroup_nomembers}; do echo -e "${hostgroup}" done echo -e "\n>> Host Groups that had some type of grep search error: $(echo ${hostgroup_error} | wc -w)" for hostgroup in ${hostgroup_error}; do echo -e "${hostgroup}" done echo -e "\n==========================================" echo -e "=- Report: Empty Host Groups Completed. -=" echo -e "=========================================="