
#!/bin/sh
# File	: cdot_get_vservers.sh
# By	: Maarten.deBoer@Atos.net, 160111
# Subject	: Get all SVMs (vservers) from all clusters & place them in a .CSV-file
#(0.2),181015	: Added SSHCMD (because cluster address sometimes occupied
#(0.3),230421	: Updated the `echo`'s
PGM="`basename $0|cut -d\. -f1`"
VER="0.3"
TMP="/tmp/${PGM}.$$"
LOG="${HOME}/log/${PGM}.log"
MAILTO="maarten.deboer@atos.net"
HOSTNAME="`hostname`"
MAIL=""
CLUSTERS="${HOME}/etc/clusters"
CSV="fsod-clusters-vservers.csv"
LIST=""
LISTFILE="${HOME}/export/${CSV}"
SUBJECT="@${HOSTNAME}: FSOD clusters-vserver(SVM)s-list [${PGM} v${VER}]"
SSH="/usr/bin/ssh -n"

SSHCMD()
# 1: Filername 2:Command-string
# When issue with connection to cluster, try the nodes (-01 & -02)
# "There are no entries matching your query." => EC=255
# "no connection" is also EC=255
{
  TMPERR="/tmp/${PGM}.$$.err"
  touch ${TMPERR}
  /usr/bin/ssh -n ${1} "${2}" 2> ${TMPERR}
  EC=${?}
  # Check if "ssh: connect to host 10.192.109.202 port 22: Connection refused" If so (EC2=0), the 2nd
  grep 'Connection refused' ${TMPERR}
  EC2=${?}
  if [ ${EC} -ne 0 ] && [ ${EC2} -eq 0 ]; then
    sleep 1
    /usr/bin/ssh -n ${1}-01 "${2}" 2> ${TMPERR}
    EC=${?}
    grep 'Connection refused' ${TMPERR}
    EC2=${?}
    if [ ${EC} -ne 0 ] && [ ${EC2} -eq 0 ]; then
      sleep 1
      /usr/bin/ssh -n ${1}-05 "${2}" 2> ${TMPERR}
      EC=${?}
      grep 'Connection refused' ${TMPERR}
      EC2=${?}
      if [ ${EC} -ne 0 ] && [ ${EC2} -eq 0 ]; then
        sleep 1
        /usr/bin/ssh -n ${1}-06 "${2}" 2> ${TMPERR}
        EC=${?}
        grep 'Connection refused' ${TMPERR}
        EC2=${?}
        if [ ${EC} -ne 0 ] && [ ${EC2} -eq 0 ]; then
          sleep 1
          /usr/bin/ssh -n ${1}-07 "${2}" 2> ${TMPERR}
          EC=${?}
          grep 'Connection refused' ${TMPERR}
          EC2=${?}
          if [ ${EC} -ne 0 ] && [ ${EC2} -eq 0 ]; then
            sleep 1
            /usr/bin/ssh -n ${1}-08 "${2}" 2> ${TMPERR}
            EC=${?}
            grep 'Connection refused' ${TMPERR}
            EC2=${?}
            if [ ${EC} -ne 0 ] && [ ${EC2} -eq 0 ]; then
              sleep 1
              /usr/bin/ssh -n ${1}-09 "${2}" 2> ${TMPERR}
              EC=${?}
              grep 'Connection refused' ${TMPERR}
              EC2=${?}
              if [ ${EC} -ne 0 ] && [ ${EC2} -eq 0 ]; then
                echo  "`date` ${PGM} ERROR with communication to ${1}. Connection to -01 - -06 failed too."|tee -a ${LOG} 
              fi  # EC=0 & EC2=0
            fi  # 
          fi  #
        fi  #
      fi  #
    fi  #
  fi  # -01
  rm ${TMPERR}
}

USAGE()
{
  echo "Usage: ${PGM} <options> "
  echo "  Version: ${VER}"
  echo "  options       :"
  echo "    -e          : Etc-file (${ETC})"
  echo "    -h          : this help"
  echo "    -l | --list : produce/export list as ${LISTFILE}"
  echo "    --export    : produce/export list as ${LISTFILE}"
  echo "    -m | --mail : do send mail"
  echo "    -x          : set -x"
  echo "    -V          : Version"
  echo "    --listfile  : change LISTFILE $(LISTFILE})"
  echo "    --mailto    : do send mail to (${MAILTO}) "
  echo "    --help      : this help"
}

# Check options
while [ $# -gt 0 ]
  do
  case $1 in
    -e) ETC=${2}; shift ;;
    -l | --list | --export) LIST=1 ;;
    -m | --mail) MAIL=1 ;;
    --mailto) MAIL=1; MAILTO=${2}; shift ;;
    --listfile) LISTFILE=${2}; shift ;;
    -h | --help) USAGE; exit 1 ;;
    -V) echo "${PGM}: v${VER}"; exit 3 ;;
    -x)  set -x ;;
    *)  echo "Option ${1} not known."; USAGE; exit 1 ;;
  esac
    shift
done

touch ${TMP}

echo "`date` ${PGM} v${VER} (MAIL=${MAIL} MAILTO=${MAILTO} TOLL=${TOLL} SUBJECT=${SUBJECT}) started"|tee -a ${LOG}

echo "# cluster ; # SVM (vserver); # Status" > ${TMP}

for CLUSTER in `cat ${CLUSTERS}|grep -v \^#|sort`
do
  echo "${CLUSTER}:"
# Remove numbers and use as "pre-fix"
  PRE_FIX=`echo ${CLUSTER}|sed 's/[0-9]//g'`
# Select (grep) only usable lines (grep PRE_FIX) and no nodenames (grep -v ${CLUSTER}
  SSHCMD ${CLUSTER} "vserver show -field vserver,admin-state"|grep "${PRE_FIX}"|grep -v ${CLUSTER}|awk '{print $1,$2}'|while read VSERVER STATUS REST
  do
    echo "${CLUSTER};${VSERVER};${STATUS}; " | tee -a ${TMP}
  done  # SSH
done  # for

echo "#" >> ${TMP}
echo "# Produced ; by ${PGM} (v${VER}) at `date` at ${HOSTNAME}" >> ${TMP}

if [ ${LIST} ]; then
  cp ${TMP} ${LISTFILE}
  echo "  Copied info to ${LISTFILE}"|tee -a ${LOG}
fi

if [ ${MAIL} ]; then
  cp ${TMP} /tmp/${CSV}
  chmod 777 /tmp/${CSV}
  date|mailx -a /tmp/${CSV} -s "${SUBJECT}" ${MAILTO}
  echo "  Mailed info to ${MAILTO}."|tee -a ${LOG}
  rm /tmp/${CSV}
fi

echo "`date` ${PGM} v${VER} finished."|tee -a ${LOG}
rm ${TMP}
exit 0

