
#!/bin/sh
# File	: cdot_atos_accounting_v3.sh
# By	: Maarten.deBoer@atos.net, 140909
# Subject	: Script to collect accounting information from cDOT filers. 
#		  Per cDOT-cluster (not per vfiler)
#		  In the accounting-v3 way 
#		  Storage classes are determined from the QoS-policy.
#                 Therefore QoS should be set on all volumes.
#                 Lack of QoS-policy will be reported.
# 		  And send it per mail to MSS-accounting-server
#(0.2),211220	: Mod. at "Checks if .ASC-file is NOT empty" (_v3.asc added)
#(0.3),250408	: Add 2nd dest (new consolidations server)
#(0.4),250527	: Add DATE_YMD
#(0.5),250714	: Added SCPHOST3
#(0.6),260324	: Mod. disabled SCPHOST & SCPHOST2, nodes in SSHCMD
#
PGM="`basename $0|cut -d\. -f1`"
VER="0.6"
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"

SCPUSER="receiver"
SCPHOST="nlxmssp1"
SCPDIR="/home/receiver/import/inventory/cdot/"

SCPUSER2="mssbatch"
SCPHOST2="nlxmssscr01"
SCPDIR2="/home/mssbatch/data/recharging/fsod"

SCPUSER3="mssbatch"
SCPHOST3="nlxmssp3"
SCPDIR3="/home/mssbatch/data/recharging/fsod"

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

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}-07 "${2}" 2> ${TMPERR}
    EC=${?}
    grep 'Connection refused' ${TMPERR}
    EC2=${?}
    if [ ${EC} -ne 0 ] && [ ${EC2} -eq 0 ]; then
      sleep 2
      /usr/bin/ssh -n ${1}-08 "${2}" 2> ${TMPERR}
      EC=${?}
      grep 'Connection refused' ${TMPERR}
      EC2=${?}
      if [ ${EC} -ne 0 ] && [ ${EC2} -eq 0 ]; then
        sleep 2
        /usr/bin/ssh -n ${1}-09 "${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}-11 "${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}-01 "${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}

for CLUSTER in `cat ${CLUSTERS}|grep -v ^#`
do
  echo "${CLUSTER} ..."
  DATE_YMD=`date +%Y%m%d`
  echo "# `date` by ${PGM} v${VER} at ${HOSTNAME}" > ${DATADIR}/${CLUSTER}_v3.asc
# option -field selects the items to be shown. NOT the order. That is FIXED
  SSHCMD ${CLUSTER} 'set -units KB -showseparator ";" ; volume show -state online -field vserver,aggregate,volume,type,total,used,percent-used,qos-policy-group,qos-adaptive-policy-group'|grep -v 'Volume Name'|tee -a ${DATADIR}/${CLUSTER}_v3.asc

# Checks if .ASC-file is NOT empty
  LINECNT="`cat ${DATADIR}/${CLUSTER}_v3.asc|grep -v ^#|wc -l`"
  if [ ${LINECNT} -lt 5 ];then
    echo "`date` ${DATADIR}/${CLUSTER}_v3.asc has only ${LINECNT} lines. Might be a problem. Do investigate logfile (${HOSTNAME}:${LOG})."|tee -a ${LOG}|mailx -s ":${HOSTNAME}: ERROR in NetApp-accounting for ${CLUSTER} [${PGM} v${VER}]" ${MAILTO}
    echo "  Mailed ERROR to ${MAILTO}"|tee -a ${LOG}
  fi

# copy file by using SCP
#  scp ${DATADIR}/${CLUSTER}_v3.asc ${SCPUSER}@${SCPHOST}:${SCPDIR}/${CLUSTER}_v3-${DATE_YMD}.asc
#  scp ${DATADIR}/${CLUSTER}_v3.asc ${SCPUSER2}@${SCPHOST2}:${SCPDIR2}/${CLUSTER}_v3-${DATE_YMD}.asc
  scp ${DATADIR}/${CLUSTER}_v3.asc ${SCPUSER3}@${SCPHOST3}:${SCPDIR3}/${CLUSTER}_v3-${DATE_YMD}.asc
#  echo "`date` Information SCP-ed to ${SCPUSER}@${SCPHOST}:${SCPDIR} "|tee -a ${LOG}
#  echo "`date` Information SCP-ed to ${SCPUSER2}@${SCPHOST2}:${SCPDIR2} "|tee -a ${LOG}
  echo "`date` Information SCP-ed to ${SCPUSER3}@${SCPHOST3}:${SCPDIR3} "|tee -a ${LOG}
#  ${SSH} ${SCPUSER}@${SCPHOST} ls -l ${SCPDIR}|tee -a ${LOG} 2>&1
#  ${SSH} ${SCPUSER2}@${SCPHOST2} ls -l ${SCPDIR2}|tee -a ${LOG} 2>&1
  ${SSH} ${SCPUSER3}@${SCPHOST3} ls -l ${SCPDIR3}|tee -a ${LOG} 2>&1

done  # for CLUSTER

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

