
#!/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
#(0.5),180726	: Added COMMENT in 2nd position
#(0.6),180726	: Remove ^# before while & check stdin
#set -x

PGM=`basename $0|cut -d\. -f1`
VER="0.6"
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"
STATUSENA="${HOME}/data/${PGM}.enable"
STATUSDIS="${HOME}/data/${PGM}.disable"

#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]"

USAGE()
{
  echo "  USAGE:"
  echo "    input using stdin. "
  echo "      Like (local): 'echo <text> | ${PGM}.sh' OR 'cat <file> | ${PGM}.sh'"
  echo "      Like (remote): 'echo <text> | ssh <user>@<host>' OR 'cat <file> | ssh <user>@<host>'"
  echo "    syntax: '<cmd> ; <comment> ; <svm> ; <aggr> ; <volume> ; <qtree> ; <new_size> ; '"
  echo "    cmd:"
  echo "      - help"
  echo "      - heartbeat"
  echo "      - status"
  echo "      - list"
  echo "  [${PGM} v${VER}]"
}


# If a (local)tty (interactive, 0), then exit
tty
EC=${?}
if [ ${EC} -eq 0 ]; then
# Check remote (ssh) interactive
  echo
  if [ ! -p /dev/stdin ]; then
    echo "! -p /dev/stdin"
  fi

    echo "  ERROR: ${PGM} is NOT interactive. Use stdin for input [${PGM} v${VER}]. Exiting ..."
    echo
#  USAGE
    exit 1
fi

echo "running"
echo ${PTY}

exit


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

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


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

  # do, when IN_CMD is not empty

  if [ "${IN_CMD}" != "" ]; then
    case ${IN_CMD} in
      help)
        USAGE
        ;;
      heartbeat)  echo "  HEARTBEAT: OK (`date +%Y-%m-%d-%H-%M-%S`) [${PGM} v${VER}]"|tee ${HBFILE} ${LAST} ;;
      status)  
        if [ -f ${STATUSENA} ]; then
          echo "  STATUS: ENAbled. [${PGM} v${VER}]"|tee -a ${LAST} 
        fi
        if [ -f ${STATUSDIS} ]; then
          echo "  STATUS: DISabled. [${PGM} v${VER}]"|tee -a ${LAST} 
        fi
        if [ ! -f ${STATUSENA} ] && [ ! -f ${STATUSDIS} ]; then
          echo "  STATUS: NO (file)found. [${PGM} v${VER}]"|tee -a ${LAST} 
        fi
        ;;
      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 "  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. Use 'help' for more info (`date +%Y-%m-%d-%H-%M-%S`) [${PGM} v{$VR}]"|tee -a ${LOG} ${LAST} ;;
    esac
  fi  # if .. != ""

done

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

