
#!/bin/bash
# File	: cdot_rpt_space.sh
# By	: Maarten.deBoer@atos.net, 170606
# Subject	: Script to report (check) (ad-hoc) cluster space (volumes & aggr's)
#(0.2)	: Added savings (df -s)
#(0.3),170614	: Added; REPDF, REPAGGR
#(0.4),170621	: Added history
PGM="`basename $0|cut -d\. -f1`"
VER="0.4"
TMP="/tmp/${PGM}.$$"
LOG="${HOME}/log/${PGM}.log"
SSH="/usr/bin/ssh -n"
HOSTNAME="`hostname|cut -d\. -f1`"
MAILTO="maarten.deboer@atos.net"
MAIL=""
FILTER="[?]*"
MAXPERC=85
VOLTYPE=""
VOLFILTER="[?]*"
REPDF=""
REPAGGR=""
HISTORY=""
HISTFILE="${HOME}/data/${PGM}.hist"

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

# MAIN
USAGE()
{
  echo "Usage: $PGM [options] "
  echo "  Version: ${VER}"
  echo "  options        :"
  echo "    -a           : report Aggr(%)"
  echo "    -d           : report Df (-g)"
  echo "    -e|--etc     : Etc/cluster-file (${CLUSTERS})"
  echo "    -f           : clustername Filter (${FILTER})"
  echo "    -h|--help    : this help"
  echo "    --history    : add History output"
  echo "    -m|--mail    : send Mail"
  echo "    -p           : maxPerc (${MAXPERC})"
  echo "    -s           : report Savings (df -s)"
  echo "    -t|--voltype : report Savings (${VOLTYPE}) [RW | DP | LS | TMP]"
  echo "    -v           : select Volume (${VOLFILTER})"
  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
    -a) REPAGGR=1 ;;
    --history) HISTORY=1 ;;
    -d) REPDF=1 ;;
    -e | --etc) CLUSTERS=${2}; shift ;;
    -m | --mail) MAIL=1 ;;
    --mailto) MAILTO=$2; MAIL=1; shift ;;
    -h | --help) USAGE; exit 1 ;;
    -f) FILTER=${2}; shift ;;
    -v) VOLFILTER=${2}; shift ;;
    -p) MAXPERC=${2}; shift ;;
    -s) SAVINGS=1;;
    -t | --voltype) VOLTYPE="-type ${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}
touch ${TMP}

for CLUSTER in `cat ${CLUSTERS}|grep -v ^#|grep ${FILTER}`
do
  echo "${CLUSTER} ..."
  echo "= Volumes >=${MAXPERC} :" >> ${TMP}
  ${SSH} ${CLUSTER} "vol show -state online ${VOLTYPE} -field aggr,percent-used"|grep '%'|grep ${VOLFILTER}|while read LINE
  do
    VSERVER=`echo ${LINE}|awk '{print $1}'`
    VOLUME=`echo ${LINE}|awk '{print $2}'`
    AGGR=`echo ${LINE}|awk '{print $3}'`
    PCTUSED=`echo ${LINE}|awk '{print $4}'|sed 's/%//g'`
    if [ ${PCTUSED} -ge ${MAXPERC} ]; then
      AGGRPERC=`${SSH} ${CLUSTER} "aggr show ${AGGR} -state online -field percent-used"|grep '%'|awk '{print $2}'|sed 's/%//g'`
      echo "${CLUSTER}: ${VOLUME} (${AGGR}) = ${PCTUSED}%  <= aggr: ${AGGR} = ${AGGRPERC}%"|tee -a ${TMP}
      if [ ${HISTORY} ]; then
        echo "`date +%Y-%m-%d`; ${CLUSTER}: ${VOLUME} (${AGGR}) = ${PCTUSED}%  <= aggr: ${AGGR} = ${AGGRPERC}%"|tee -a ${HISTFILE}
      fi
    fi
    if [ ${REPDF} ]; then
      echo "  df -g :" >> ${TMP}
      ${SSH} ${CLUSTER} "df -g ${VOLUME}"|grep ${VOLUME}|awk '{print $1,$2,$3,$4,$5}'|tee -a ${TMP}
#      ${SSH} ${CLUSTER} "df -g ${VOLUME}"|tee -a ${TMP}
    fi
    if [ ${SAVINGS} ]; then
      echo "  Savings :" >> ${TMP}
      ${SSH} ${CLUSTER} "df -s -g ${VOLUME}"|grep ${VOLUME}|awk '{print $1,$2,$3,$4}'|tee -a ${TMP}
#      ${SSH} ${CLUSTER} "df -s -g ${VOLUME}"|tee -a ${TMP}
    fi
  done  # 'vol show '

  if [ ${HISTORY} ]; then
    echo "History:" |tee -a ${TMP}
    cat ${HISTFILE}|grep ${VOLFILTER}|sort -u|tee -a ${TMP}
  fi

  if [ ${REPAGGR} ]; then
    echo "" >> ${TMP}
    echo "= Aggrs >=${MAXPERC} :" >> ${TMP}
    ${SSH} ${CLUSTER} "aggr show -state online -field percent-used"|grep -v root|grep '%'|while read LINE
    do
      AGGR=`echo ${LINE}|awk '{print $1}'`
      PCTUSED=`echo ${LINE}|awk '{print $2}'|sed 's/%//g'`
      if [ ${PCTUSED} -ge ${MAXPERC} ]; then
        echo "${CLUSTER}: ${AGGR} = ${PCTUSED}%"|tee -a ${TMP}
      fi
    done  # 'aggr show'
  fi  # REPAGGR

done  # for CLUSTER

if [ ${MAIL} ] && [ -s ${TMP} ] ; then
  echo "" | tee -a ${TMP}
  echo "" | tee -a ${TMP}
  echo "CLUSTERS=${CLUSTERS} FILTER=${FILTER} MAIL=${MAIL} MAILTO=${MAILTO} MAXPERC=${MAXPERC} REPAGGR=${REPAGGR} REPDF=${REPDF} PGM=${PGM} VER=${VER} VOLFILTER=${VOLFILTER} VOLTYPE=${VOLTYPE}" | tee -a ${TMP}
  cat ${TMP}|mailx -s ":${HOSTNAME}: cDOT space reporting >${MAXPERC}% [${PGM} v${VER}]" ${MAILTO}
  echo "  Mailed to ${MAILTO}"|tee -a ${LOG}
fi  # MAIL

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

