
#!/bin/sh
# File	: proc_ssh_input.sh
# By	: Maarten.deBoer@Atos.net, 180724
# Subject	: Script to receive info from SSH
#(0.2),180724	: Added heartbeat
#(0.3),180725	: Added, ${TMP}, FILTERS
#(0.4),180725	: Added CLUSTERS to find the SVM
PGM=`basename $0|cut -d\. -f1`
VER="0.4"
TMP="/tmp/${PGM}.$$"
LOG="${HOME}/log/${PGM}.log"
HBFILE="${HOME}/data/heartbeat"
PROCFILE="${HOME}/data/procfile"
CLUSTERS="${HOME}/etc/clusters"
SSH="/usr/bin/ssh -n"
OUT="${TMP}.out"
LAST="${HOME}/data/${PGM}.last"

#SVM_FILTER="nlnafs[a-z][a-z][a-z][0-9][0-9]"
SVM_FILTER="nlnafsmss[0-9][0-9]"
AGGR_FILTER="n[0-9][0-9][ace][0-9][0-9][0-9][0-9]"
VOL_FILTER="[a-z][a-z][a-z][0-9][0-9]_vol[0-9][0-9][0-9][0-9][0-9]"

# empty ${LAST} before start
rm ${LAST}
touch ${LAST}
echo "`date` ${PGM} v${VER} started."|tee -a ${LOG} ${LAST}
touch ${TMP} ${OUT}

# Layout:
# <cmd> ; <svm> ; <aggr> ; <volume> ; <qtree> ; <new_size> ;
# - heartbeat 
# - list
# - remove
# - unremove;


while read LINE
do
  echo "--LINE:${LINE}" >> ${LAST}
  IN_CMD=`echo ${LINE}|grep -v ^#|awk -F\; '{print $1}'`
  IN_SVM=`echo ${LINE}|grep -v ^#|awk -F\; '{print $2}'`
  IN_AGGR=`echo ${LINE}|grep -v ^#|awk -F\; '{print $3}'`
  IN_VOL=`echo ${LINE}|grep -v ^#|awk -F\; '{print $4}'`
  IN_QTREE=`echo ${LINE}|grep -v ^#|awk -F\; '{print $5}'`
  IN_NEW_SIZE=`echo ${LINE}|grep -v ^#|awk -F\; '{print $6}'`

  # do, when IN_CMD is not empty

  if [ "${IN_CMD}" != "" ]; then
    case ${IN_CMD} in
      heartbeat)  echo "  INFO: heartbeat = OK (`date +%Y-%m-%d-%H-%M-%S`)"|tee ${HBFILE} ${LAST} ;;
      help)  echo "  INFO: NO help available"|tee -a ${LAST} ;;
      list)  
        # Checks against FILTERs
        SVM=`echo ${IN_SVM}|grep "${SVM_FILTER}"`
        AGGR=`echo ${IN_AGGR}|grep "${AGGR_FILTER}"`
        # if AGGR is empty, then use wildcard (*)
        if [ "${AGGR}" = "" ]; then
          AGGR="*"
        fi
        VOL=`echo ${IN_VOL}|grep "${VOL_FILTER}"`
        # if VOL is empty, then use wildcard (*)
        if [ "${VOL}" = "" ]; then
          VOL="*"
        fi
     
        echo "  INFO: list of SVM=${SVM}  AGGR=${AGGR}  VOL=${VOL}"|tee -a ${LAST}
        # find the SVM at a cluster
        cat ${CLUSTERS}|while read CLUSTER REST
        do
#          echo "${CLUSTER} ..."
          ${SSH} ${CLUSTER} "vserver show -vserver ${SVM}" 1> ${TMP} 2>&1
          EC="${?}"
          # if EC=0 then SVM found
          if [ ${EC} -eq 0 ]; then
            # grep ${AGGR_FILTER} is needed to select only the aggr-data (no headers, etc)
            ${SSH} ${CLUSTER} "set -showseparator \";\" ; aggr show -aggregate ${AGGR} -field node,aggregate" | grep ${AGGR_FILTER} 1> ${TMP} 2>&1
            EC="${?}"
            if [ ${EC} -ne 0 ]; then
              echo "  ERROR: ${CLUSTER}"|tee -a ${LAST} ${LOG}
              cat ${TMP}| grep -i error|head -1|tee -a ${LAST} ${LOG}
            else
              echo "  INFO: Aggr list;"|tee -a ${LAST}
              cat ${TMP}|tee -a ${LAST}
            fi
        
            # grep ${SVM} is needed to select only the data (and not headers, etc)
            ${SSH} ${CLUSTER} "set -showseparator \";\" ; volume show -vserver ${SVM} -volume ${VOL} -field volume"|grep ${SVM} 1> ${TMP} 2>&1
            EC="${?}"
            if [ ${EC} -ne 0 ]; then
              echo "  ERROR: ${CLUSTER}"|tee -a ${LAST} ${LOG}
              cat ${TMP}| grep -i error|head -1|tee -a ${LAST} ${LOG}
            else
              echo "  INFO: Volume list;"|tee -a ${LAST}
              cat ${TMP}|tee -a ${LAST}
            fi

            # grep ${SVM} is needed to select only the data (and not headers, etc)
            # grep -v ';\"\";' is needed for deselecting empty (with no Qtree) line
            ${SSH} ${CLUSTER} "set -showseparator \";\" ; qtree show -vserver ${SVM} -volume ${VOL} -field qtree,volume"|grep ${SVM}|grep -v ';\"\";' 1> ${TMP} 2>&1
            EC="${?}"
            if [ ${EC} -ne 0 ]; then
              echo "  ERROR: ${CLUSTER}"|tee -a ${LAST} ${LOG}
              cat ${TMP}| grep -i error|head -1|tee -a ${LAST} ${LOG}
            else
              echo "  INFO: Qtree list;"|tee -a ${LAST}
              cat ${TMP}|tee -a ${LAST}
            fi
        

          fi

        done  # cat ${CLUSTERS}
      
      
        ;;
      *) echo "  ERROR: ${IN_CMD} = wrong command (`date +%Y-%m-%d-%H-%M-%S`)"|tee -a ${LOG} ${LAST} ;;
    esac
  fi  # if .. != ""

done

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

