
#!/bin/bash
# File	: cdot_rpt_vol_idr.sh
# By	: Maarten.deBoer@atos.net, 170606,200616
# Subject	: Script to report (ad-hoc) volume usage for StorageGRID (via Fabric Pool)
# IDR need to be enabled 1st (https://docs.netapp.com/us-en/ontap/fabricpool/determine-data-inactive-reporting-task.html)
# cluster1::> storage aggregate modify -aggregate aggr1 -is-inactive-data-reporting-enabled true
#
#(0.2),230102	: Renamed from cdot_rpt_vol_for_sg.sh. Added -field performance-tier-inactive-user-data
#(0.3),230216	: Some Mod's
#(0.4).230223	: Renamed from cdot_rpt_vol_fabricpool.sh, Mod CSV with `date`, added ${TMP}.mail
#(0.5),230223	: Added cluster total IDR
#(0.6),230301    : Added  -tiering-minimum-cooling-days AND -set priv advanced, percent-snapshot-space
#(0.7),240129	: Mod. VFILTER
PGM="`basename $0|cut -d\. -f1`"
VER="0.7"
TMP="/tmp/${PGM}.$$"
CSV="/tmp/${PGM}.csv"
LOG="${HOME}/log/${PGM}.log"
SSH="/usr/bin/ssh -n"
HOSTNAME="`hostname|cut -d\. -f1`"
MAILTO="maarten.deboer@atos.net"
MAIL=""
CLUSTERS="${HOME}/etc/clusters"
SFILTER="*"
TFILTER="*"
SCFILTER="[?]*"
VFILTER="[?]*"
CSV="/tmp/${PGM}_`date +%Y%m%d`.csv"

CLUSTERS="${HOME}/etc/clusters"
CLUSTER_IDR=0

# File	: sshcmd.func
# VER="0.1", <node>-04
# VER="0.2", <node>-06
# VER="0.3", <node>-10
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}.$$.tmperr"
  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}-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}-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}-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}-02 "${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
                  sleep 1
                  /usr/bin/ssh -n ${1}-10 "${2}" 2> ${TMPERR}
                  EC=${?}
                  grep 'Connection refused' ${TMPERR}
                  EC2=${?}
                  if [ ${EC} -ne 0 ] && [ ${EC2} -eq 0 ]; then
                    sleep 1
                    /usr/bin/ssh -n ${1}-11 "${2}" 2> ${TMPERR}
                    EC=${?}
                    grep 'Connection refused' ${TMPERR}
                    EC2=${?}
                    if [ ${EC} -ne 0 ] && [ ${EC2} -eq 0 ]; then
                      sleep 1
                      /usr/bin/ssh -n ${1}-12 "${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 .. -10 failed too."|tee -a ${LOG} 
                      fi 
                    fi
                  fi 
                fi 
              fi 
            fi  
          fi  
        fi  
      fi  
    fi  
  fi  # 
  rm ${TMPERR}
}


# MAIN
USAGE()
{
  echo "Usage: $PGM [options] "
  echo "  Version: ${VER}"
  echo "  options        :"
  echo "    -d           : report Df (-g)"
  echo "    -e|--etc     : Etc/cluster-file (${CLUSTERS})"
  echo "    -c           : Cluster filter (${CFILTER})"
  echo "    -s|--svm     : SVM filter (${SFILTER})"
  echo "    --sc         : StorageClass (${SCFILTER})"
  echo "    -t|--type    : (volume) Type filter (${TFILTER})"
  echo "    -v|--vol     : VOLume filter (${VFILTER})"
  echo "    -h|--help    : this help"
  echo "    -m|--mail    : send Mail"
  echo "    -V           : Version"
  echo "    -x           : set -x"
  echo "    --mailto     : change MAILTO address & do send mail (${MAILTO})"
}
# Check options
while [ $# -gt 0 ]
  do
  case ${1} in
    -e | --etc) CLUSTERS=${2}; shift ;;
    -m | --mail) MAIL=1 ;;
    --mailto) MAILTO=$2; MAIL=1; shift ;;
    -h | --help) USAGE; exit 1 ;;
    -c) CFILTER=${2}; shift ;;
    --sc) SCFILTER=${2}; shift ;;
    -s | --svm) SFILTER=${2}; shift ;;
    -t | --type) TFILTER=${2}; shift ;;
    -v | --vol) VFILTER=${2}; shift ;;
    -V) echo "${PGM}: v${VER}"; exit 3 ;;
    -x)  set -x ;;
    *)  echo "Option ${1} not known."; USAGE; exit 1 ;;
  esac
    shift
done

echo "`date` ${PGM} v${VER} started."|tee -a ${LOG} ${TMP}.mail
echo "CLUSTERS=${CLUSTERS}"|tee -a ${TMP}.mail
echo "CSV=${CSV}"|tee -a ${TMP}.mail
echo "CFILTER=${CFILTER}"|tee -a ${TMP}.mail
echo "HOSTNAME=${HOSTNAME}"|tee -a ${TMP}.mail
echo "SCFILTER=${SCFILTER}"|tee -a ${TMP}.mail
echo "SFILTER=${SFILTER}"|tee -a ${TMP}.mail
echo "TFILTER=${TFILTER}"|tee -a ${TMP}.mail
echo "VFILTER=${VFILTER}"|tee -a ${TMP}.mail
echo "MAIL=${MAIL}"|tee -a ${TMP}.mail
echo "MAILTO=${MAILTO}"|tee -a ${TMP}.mail

sleep 1

echo "# vserver;volume;aggregate;size (GB);total (GB);used (GB);type;node;size-used-by-snapshots (GB);qos-policy-group;qos-adaptive-policy-group;tiering-policy;tiering-minimum-cooling-days;performance-tier-inactive-user-data (GB);" > ${TMP}
# vserver;volume;aggregate;size;total;used;type;node;size-used-by-snapshots;qos-policy-group;qos-adaptive-policy-group;tiering-policy;tiering-minimum-cooling-days;performance-tier-inactive-user-data;

touch ${TMP}.idr 

for CLUSTER in `cat ${CLUSTERS}|grep -v ^#|grep ${CFILTER}`
do
  echo "#;" >>  ${TMP}
  echo "# ${CLUSTER} ;" | tee -a ${TMP}
  CLUSTER_IDR=0
  SSHCMD ${CLUSTER} "set -priv advanced -units GB -showseparator \";\" ;volume show -vserver ${SFILTER} -volume ${VFILTER} -state online -type ${TFILTER} -field aggregate,used,type,tiering-policy,node,size,total,qos-policy-group,qos-adaptive-policy-group,performance-tier-inactive-user-data,tiering-minimum-cooling-days,size-used-by-snapshots"|grep "${CLUSTER}"| while read LINE
  do
# vserver;volume;aggregate;size;total;used;type;node;size-used-by-snapshots;qos-policy-group;qos-adaptive-policy-group;tiering-policy;tiering-minimum-cooling-days;performance-tier-inactive-user-data;


    PTIUD=`echo ${LINE}|awk -F\; '{print $14}'|sed 's/GB//g'|grep -v "-"|grep "[0-9]*$"`
    if [ "${PTIUD}" != "" ]; then
      CLUSTER_IDR=`expr ${CLUSTER_IDR} + ${PTIUD}`
    fi
    echo "${LINE}"| sed 's/GB//g'|tee -a ${TMP}
#    echo "    CLUSTER_IDR=${CLUSTER_IDR}"
    echo "CLUSTER=${CLUSTER}" > ${TMP}.idr
    echo "CLUSTER_IDR=${CLUSTER_IDR}" >> ${TMP}.idr
  done  # LINE

  echo "  TOTAL IDR for `grep 'CLUSTER=' ${TMP}.idr|cut -d\= -f2` = `grep 'CLUSTER_IDR=' ${TMP}.idr|cut -d\= -f2` GB" >> ${TMP}.mail

done  # for CLUSTER

if [ ${MAIL} ] && [ -s ${TMP} ] ; then
  cp ${TMP} ${CSV}
  cat ${TMP}.mail |mailx -a ${CSV} -s ":${HOSTNAME}: cDOT Volume IDR (Inactive Data Reporting) [${PGM} v${VER}]" ${MAILTO}
  echo "  Mailed ${CSV} to ${MAILTO}"|tee -a ${LOG}
fi  # MAIL

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

