
#!/bin/bash
# File	: cdot_set_volumes.sh
# By	: Maarten.deBoer@atos.net, 141222
# Subject	: Script to set volume parameters
#set -x
#(0.2).240118	: Mod's [options]
PGM="`basename $0|cut -d\. -f1`"
VER="0.2"
TMP="/tmp/${PGM}.$$"
ASC="/tmp/${PGM}.asc"
LOG="${HOME}/log/${PGM}.log"
SSH="/usr/bin/ssh -n"
HOSTNAME="`hostname|cut -d\. -f1`"
CLUSTERS="${HOME}/etc/clusters"
PREFIX="nlnaf"
SETVOL=""
MAIL=""
MAILTO="maarten.deboer@atos.net"
CFILTER="[?]*"
SFILTER="[?]*"
VFILTER="[?]*"


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"
  /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 2
    /usr/bin/ssh -n ${1}-01 "${2}" 2> ${TMPERR}
    EC=${?}
    grep 'Connection refused' ${TMPERR}
    EC2=${?}
    if [ ${EC} -ne 0 ] && [ ${EC2} -eq 0 ]; then
      sleep 2
      /usr/bin/ssh -n ${1}-02 "${2}" 2> ${TMPERR}
      EC=${?}
      grep 'Connection refused' ${TMPERR}
      EC2=${?}
      if [ ${EC} -ne 0 ] && [ ${EC2} -eq 0 ]; then
        sleep 2
        /usr/bin/ssh -n ${1}-03 "${2}" 2> ${TMPERR}
        EC=${?}
        grep 'Connection refused' ${TMPERR}
        EC2=${?}
        if [ ${EC} -ne 0 ] && [ ${EC2} -eq 0 ]; then
          echo "  EC=${EC} sleep 2 ..."
          sleep 2
          /usr/bin/ssh -n ${1}-04 "${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 - -04 failed too."|tee -a ${LOG} 
          fi
        fi
      fi
    fi
  fi
  rm ${TMPERR}
}

USAGE()
{
  echo "Usage: ${PGM} [options]"
  echo "  Version: ${VER}"
  echo "    Setting (modifying) -space-guarantee to none (Thin Prov.) "
  echo "  options       :"
  echo "    -c          : Clustername filter (${CFILTER})"
  echo "    -e | --etc  : Etc/clusters file (${CLUSTERS})"
  echo "    -m | --mail : Mail the logging"
  echo "    -h | --help : this help"
  echo "    -s          : Svm(vserver) filter (${SFILTER})"
  echo "    -v          : Volume filter (${VFILTER})"
  echo "    -V          : Version"
  echo "    -x          : set -x"
  echo "    --mailto    : set new MAILTO (${MAILTO})"
  echo "    --setvol    : do SETVOL"
}

# Check options
while [ ${#} -gt 0 ]
  do
  case ${1} in
    -e | --etc) CLUSTERS=${2}; shift ;;
    -m | --mail) MAIL=1 ;;
    -h | --help) USAGE; exit 1 ;;
    -c) CFILTER=${2}; shift ;;
    -s) SFILTER=${2}; shift ;;
    -v) VFILTER=${2}; shift ;;
    -V) echo "${PGM}: v${VER}"; exit 3 ;;
    -x)  set -x ;;
    --mailto) MAIL=1; MAILTO=${2}; shift ;;
    --setvol) SETVOL=1 ;;
    *)  echo "Option ${1} not known."; USAGE; exit 1 ;;
  esac
    shift
done

# MAIN
touch ${TMP}
echo "`date` ${PGM} v${VER} started."|tee -a ${LOG} ${TMP}

for CLUSTER in `cat ${CLUSTERS}|grep -v ^#|grep "${CFILTER}"`
do
#  echo "${CLUSTER} ..."

  SSHCMD ${CLUSTER} 'set -showseparator " " ; vserver show -type data -fields vserver'|grep ${PREFIX}|grep "${SFILTER}"|while read VSERVER REST
 do
    echo "${CLUSTER}:${VSERVER}:"
    SSHCMD ${CLUSTER} "volume show -vserver ${VSERVER} -state online -fields volume"|egrep -v 'volume|vol0root|vol0ls'|grep ${VSERVER}|grep "${VFILTER}"|while read VSERVER VOLUME REST
    do
       echo "${VSERVER}:${VOLUME}"

      if [ ${SETVOL} ]; then
        echo "SET ${CLUSTER}:${VSERVER}:${VOLUME}: -space-guarantee none "|tee -a ${LOG} ${TMP}
        SSHCMD ${CLUSTER} "volume modify -vserver ${VSERVER} -volume ${VOLUME} -space-guarantee none "
      else
        echo "NOT ${CLUSTER}:${VSERVER}:${VOLUME}: -space-guarantee none (option --setvol is needed)"|tee -a ${LOG} ${TMP}
      fi  # ${SETVOL} 

    done   # VOLUME
  done  # vserver
done  # for CLUSTER

if [ ${MAIL} ]; then
  echo "`date` Mailing to ${MAILTO}"|tee -a ${LOG} ${TMP}
  cp ${TMP} ${ASC}
  date | mailx -a ${ASC} -s "${HOSTNAME}: Logging of cDOT set volume [${PGM} v${VER}]" ${MAILTO}
  rm ${ASC}
fi  # MAIL

# Finish

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

