
#!/bin/sh
# File	: cdot_eff_start.sh
# By	: Maarten.deBoer@atos.net, 211104
# Subject	: Script to start Efficiency at NetApp
#
# Run background scanner to compress existing data in the volumes (Advanced mode command)
#
#:>priv set advanced
#
#::>volume efficiency start -vserver $SVM_NAME -volume $VOL_NAME -scan-old-data true - compression true –dedupe true
#
#This will compress non-shared data in active filesystem. To compress shared blocks in active filesystem as well as blocks locked in snapshots, use the below command.
#Note: Uncompressed blocks trapped in the snapshots are locked and cannot be changed. So, compressed snapshot blocks will consume additional space in the filesystem. Space savings from the locked blocks will be realized once the snapshots are deleted/expired. 
#
#::>volume efficiency start -vserver $SVM_NAME -volume $VOL_NAME -scan-old-data true - compression true -snapshot-blocks true -shared-blocks true –dedupe true
#
#Best Practice: It is not recommended to take any snapshots while the background scanner is running. If snapshots are taken while the background scanner is running, data trapped in snapshots will remain uncompressed (even after the snapshots are deleted/expired).
#
PGM="`basename $0|cut -d\. -f1`"
VER="0.1"
TMP="/tmp/${TMP}.$$"
LOG="${HOME}/log/${PGM}.log"
RCFILE="${HOME}/etc/${PGM}.rc"
DATADIR="${HOME}/data/${PGM}"
SSH="/usr/bin/ssh -n"
HOSTNAME="`hostname|cut -d\. -f1`"
CLUSTERS="${HOME}/etc/clusters"
CFILTER="[?]*"
SFILTER="[?]*"
#VFILTER="[?]*"
VFILTER="nl"

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 "  EC=${EC} sleep 2 ..."
            sleep 2
            /usr/bin/ssh -n ${1}-05 "${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}-05 "${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  # -06
          fi  # -05
        fi  # -04
      fi  # -03
    fi  # -02
  fi  # -01
  rm ${TMPERR}
}

USAGE()
{
  echo "Usage: ${PGM} [options]"
  echo "  Version: ${VER}"
  echo "  options          :"
  echo "    -c             : Cluster filter (${CFILTER})"
  echo "    -s             : Svm filter (${SFILTER})"
  echo "    -v             : Volume filter (${VFILTER})"
  echo "    -h | --help    : this help"
  echo "    -V             : Version"
  echo "    -x             : set -x"
  echo "    --mailto       : set new MAILTO (${MAILTO})"
}
# Check options
while [ ${#} -gt 0 ]
  do
  case ${1} in
    -c) CFILTER=${2}; shift ;;
    -s) SFILTER=${2}; shift ;;
    -v) VFILTER=${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

if [ -f ${RCFILE} ]; then
  echo "  Reading RCFILE (${RCFILE}) ..."|tee -a ${LOG}
  . ${RCFILE}
fi
if [ ! -d ${DATADIR} ]; then
  echo "  NO DATADIR (${DATADIR}) found. Creating dir..."|tee -a ${LOG}
  mkdir -p ${DATADIR}
fi


# MAIN
echo "`date` ${PGM} v${VER} started."|tee -a ${LOG}
echo "CLUSTERS=${CLUSTERS}"
echo "DATADIR=${DATADIR}"
echo "CFILTER=${CFILTER}"
echo "SFILTER=${SFILTER}"
echo "VFILTER=${VFILTER}"
sleep 1
touch ${TMP}

for CLUSTER in `cat ${CLUSTERS}|grep -v ^#|grep "${CFILTER}"`
do
#  echo "${CLUSTER} ..."
  SSHCMD ${CLUSTER} 'vserver show -operational-state running -field vserver'|grep nl|grep "${SFILTER}"|while read VSERVER REST
  do
    echo "  ${CLUSTER}/${VSERVER} ..."|tee -a ${LOG}
# Volume
    SSHCMD ${CLUSTER} "set -showseparator \";\" ;volume show -vserver ${VSERVER} -field volume"|grep ${VFILTER}|awk -F\; '{print $2}'|sort|while read VOLUME REST
    do
      echo "    ${CLUSTER}/${VSERVER}:${VOLUME} ..."
      echo "    Saving volume (${CLUSTER}/${VSERVER}:${VOLUME}) info in ${DATADIR}/${PGM} "|tee -a ${LOG}
      echo "===== `date +%Y-%m-%d-%H-%M-%S` =====" >> ${DATADIR}/${CLUSTER}.${VSERVER}.${VOLUME}.instance
      echo "::> volume show -vserver ${VSERVER} -volume ${VOLUME} -instance :" >> ${DATADIR}/${CLUSTER}.${VSERVER}.${VOLUME}.instance
      SSHCMD ${CLUSTER} "volume show -vserver ${VSERVER} -volume ${VOLUME} -instance" >> ${DATADIR}/${CLUSTER}.${VSERVER}.${VOLUME}.instance

      echo "::> volume efficiency show -vserver ${VSERVER} -volume ${VOLUME} -instance :" >> ${DATADIR}/${CLUSTER}.${VSERVER}.${VOLUME}.instance
      SSHCMD ${CLUSTER} "volume efficiency show -vserver ${VSERVER} -volume ${VOLUME} -instance" >> ${DATADIR}/${CLUSTER}.${VSERVER}.${VOLUME}.instance

      echo "::> volume efficiency stat -vserver ${VSERVER} -volume ${VOLUME} -instance :" >> ${DATADIR}/${CLUSTER}.${VSERVER}.${VOLUME}.instance
      SSHCMD ${CLUSTER} "volume efficiency stat -vserver ${VSERVER} -volume ${VOLUME} -instance" >> ${DATADIR}/${CLUSTER}.${VSERVER}.${VOLUME}.instance


      SSHCMD ${CLUSTER} "set -privilege advanced; volume efficiency start -vserver ${VSERVER} -volume ${VOLUME} -scan-old-data true -compression true -snapshot-blocks true -shared-blocks true"


    done  # VOLUME

  done  # VSERVER
done  # for CLUSTER


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

