#!/bin/bash

#< Grep through BIGIP pools for specific members

# Executables
BASENAME="/bin/basename"
BIGPIPE="/bin/bigpipe"
ECHO="/bin/echo"
GREP="/bin/grep"
SED="/bin/sed"

# Bigpipe commands
B_POOL="${BIGPIPE} pool"
B_POOLLIST="${B_POOL} list"

# Exit codes
SUCCESS=0
ERROR=1

# Others
THIS_PROG=$( ${BASENAME} $0 )

function usage {
   ${ECHO} "Usage: ${THIS_PROG} \"pattern\""
}

if [ "$#" -ne "1" ]; then
   usage && exit ${ERROR}
fi

${B_POOLLIST} | ${SED} -n -e '/^pool .*{/ { s/^pool \(.*\) {/\1/p }' | while read POOL; do
   CURRENTPOOL=$( ${B_POOL} ${POOL} list )
   ${ECHO} "${CURRENTPOOL}" | ${GREP} "$1" >/dev/null 2>&1
   if [ "$?" -eq "0" ]; then
      ${ECHO} "${CURRENTPOOL}" | ${GREP} "$1" | while read MATCH; do
         ${ECHO} ${POOL}:${MATCH}
      done
   fi
done

exit ${SUCCESS}