
#!/bin/sh
# File	: cdot_rpt_efficiency.sh
# By	: Maarten.deBoer@atos.net, 170919
# Subject	: Script to collect storage efficiency at NetApp
# set -x
PGM="`basename $0|cut -d\. -f1`"
VER="0.1"
TMP="/tmp/${TMP}.$$"
LOG="${HOME}/log/${PGM}.log"
SSH="/usr/bin/ssh -n"
HOSTNAME="`hostname|cut -d\. -f1`"
DATADIR="${HOME}/data/out"
MAILTO="maarten.deboer@atos.net"
CLUSTERS="${HOME}/etc/clusters"
CSV="${PGM}.csv"

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}
}


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

echo "# vserver;volume;state;progress;schedule policy;compression;inline-compression;inline-dedupe;" > ${TMP}

for CLUSTER in `cat ${CLUSTERS}|grep -v ^#`
do
#  echo "${CLUSTER} ..."
  SSHCMD ${CLUSTER} 'vserver show -operational-state running -field Vserver'|grep nl|while read VSERVER REST
  do
#    echo "${CLUSTER}/${VSERVER} ..."
    SSHCMD ${CLUSTER} "vol show -vserver ${VSERVER} -field volume"|grep nl|awk '{print $2}'|while read VOLUME REST
    do
      echo "${CLUSTER}/${VSERVER}:${VOLUME} ..."
      STATUS=`SSHCMD ${CLUSTER} "set -showseparator \";\";vol efficiency show -vserver ${VSERVER} -vol ${VOLUME} -field inline-compression,inline-dedupe,compression,schedule,state,progress"|grep ${VSERVER}` >> ${TMP}

#      echo ${STATUS}
#      STATUS=`SSHCMD ${CLUSTER} "set -showseparator \";\";sys show -vserver ${VSERVER} -vol ${VOLUME} -field inline-compression,inline-dedupe,compression,schedule,state,progress"|grep ${VSERVER}`

    done  # VOLUME
  done  # VSERVER
done  # for CLUSTER

cp ${TMP} /tmp/${CSV}
date | mailx -a /tmp/${CSV} -s ":${HOSTNAME}: Efficiency report [${PGM} v${VER}]" ${MAILTO}
echo "  ${CSV} mailed to ${MAILTO}"|tee -a ${LOG}

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

