February 1st, 2016

Using standard arrays in bash (Example)

Example of using standard arrays on bash.

# ELB's to check
ELBARRAY[0]="AAAA-ELB"     # dev
ELBARRAY[1]="BBBB-ELB"    #
ELBARRAY[2]="CCCC-ELB"    #

#Matching regions to the above
REGION[0]="us-east-1"
REGION[1]="us-west-1"
REGION[2]="us-east-1"

#CRITICAL LIMIT - Set to -1 for dev servers so they can't alarm critical
CRITLIMIT[0]="-1"  # dev ELB
CRITLIMIT[1]="0"
CRITLIMIT[2]="0"

     
# Test for each ELB
INDEX=0
for foofoo in "${ELBARRAY[@]}"
do   
    RESULTS=`/usr/local/bin/check_elb_health.sh -r ${REGION[$INDEX]} -c ${CRITLIMIT[$INDEX]}  ${ELBARRAY[$INDEX]}`
    RETURNCODE=$?
    # RETURNCODE=2  # set for debugging
    track_worst_result $RETURNCODE  # external function

    # Capture the results for any bad checks
    if [[ "$RETURNCODE" -gt "0" ]]; then
        FINALRESULTS="$RESULTS $FINALRESULTS"
    fi
    ((INDEX=INDEX+1))
done

Using associative arrays in bash (Example)

Example of using associative arrays in bash.

declare -A ELB_REGION=(
        ["AAAA-ELB"]="us-east-1"
        ["BBBB-ELB"]="us-west-1"
        ["CCCC-ELB"]="us-east-1"
)
declare -A ELB_CRITIALLIMIT=(
        ["AAAA-ELB"]="-1"
        ["BBBB-ELB"]="0"
        ["CCCC-ELB"]="0"
)

# Test for each ELB
for ELB in "${!ELB_REGION[@]}"
do
        # echo $ELB ${ELB_REGION[$ELB]} ${ELB_CRITIALLIMIT[$ELB]}
        RESULTS=`/usr/local/bin/check_elb_health.sh -r  ${ELB_REGION[$ELB]} -c ${ELB_CRITIALLIMIT[$ELB]}  $ELB`
        RETURNCODE=$?
        # RETURNCODE=2  # set for debugging
        track_worst_result $RETURNCODE  # External function

        # Capture the results for any bad checks
        if [[ "$RETURNCODE" -gt "0" ]]; then
                FINALRESULTS="$RESULTS $FINALRESULTS"
        fi
done