
#!/bin/ksh
# File  : cdot_chk_disks.sh
# By    : Maarten.deBoer@atos.net, 2014-12-09, 210503
# Subject       : Script to check for broken disks for cDOT-systems/clusters
#(0.2)	: Add ping for checking connectivity
#(0.3),210503	: Mod for cDOT, Removed IMI & SDM-tickets
#(0.4),210517	: Mod's
#(0.5),210531	: Add CNT
#(0.6),220110	: Add -r | --report
PGM="`basename $0|cut -d\. -f1`"
VER="0.6"
TMP="/tmp/${PGM}.$$"
HOSTNAME="`hostname|cut -d\. -f1`"
LOG="${HOME}/log/${PGM}.log"

CLUSTERS="${HOME}/etc/clusters"
CFILTER="[?]*"
SSH="/usr/bin/ssh -n -c 3des-cbc"
MAIL=""
MAILTO="maarten.deboer@atos.net"
REPORT=""

USAGE()
{
  echo "Usage: ${PGM} [<options>]"
  echo "  Version: ${VER}"
  echo "  options       :"
  echo "    -c          : filter Clustername (${CFILTER})"
  echo "    -h|--help   : this help"
  echo "    -m|--mail   : Mail the output to (${MAILTO})"
  echo "    -r|--report : Report all disks"
  echo "    --mailto    : Mailto (${MAILTO})"
  echo "    -V          : Version"
  echo "    -x          : set -x"
}

# Check options
while [ $# -gt 0 ]
  do
  case $1 in
    -c) CFILTER=$2; shift ;;
    -h | --help) USAGE; exit 1 ;;
    -r | --report) REPORT=1 ;;
    -V) echo "${PGM}: v${VER}"; exit 3 ;;
    -x)  set -x ;;
    --mailto) MAILTO=${2}; MAIL=1; shift ;;
    -m | --mail) MAIL=1;;
    *)  echo "Option $1 not known."; USAGE; exit 1 ;;
  esac
    shift
done

#--------------------------------------------------------------------------#
echo "`date` ${PGM}: v${VER} @${HOSTNAME} Started with FILER=${FILTER} & VOLUME=${VOLFILTER}"| tee -a ${LOG}
echo "CFILTER=${CFILTER}"
echo "MAILTO=${MAILTO}"
echo "TMP=${TMP}"
echo "PGM=${PGM}"
echo "REPORT=${REPORT}"
echo "VER=${VER}"
sleep 1

touch ${TMP} 
echo "(cDOT) DISKS:" >> ${TMP}

for CLUSTER in `cat ${CLUSTERS}|grep -v \^#|awk -F\; '{print $1}'|grep "${CFILTER}"`
do
  echo "  Cluster: ${CLUSTER}"|tee -a ${TMP}
  ping -c 1 ${CLUSTER}
  EC=${?}
  if [ ${EC} -eq 0 ]; then
    if [ ${REPORT} ]; then
      ${SSH} ${CLUSTER} "storage disk show" | tee -a ${TMP}
    else
      ${SSH} ${CLUSTER} "storage disk show -error-type diskfail" | tee -a ${TMP}
    fi
  else
    echo "    ping ERROR (${EC}) on ${CLUSTER}"|tee -a ${TMP}
  fi  # ping
done  # for CLUSTER 

if [ "${MAIL}" ]; then
  echo "" >> ${TMP}
  echo "# `date` at ${HOSTNAME} by ${PGM} v${VER}" >> ${TMP}
  cp ${TMP} ${TMP}.txt
  NRDISKS=`cat ${TMP}| grep "entries were displayed"|awk '{print $1}'`
  echo "(cDOT) DISKS: ${NRDISKS}. See attachement."|mailx -a ${TMP}.txt -s ":${HOSTNAME}: Check (cDOT) disks [${PGM} v${VER}]" "${MAILTO}"
  echo "  Mailed to ${MAILTO}"|tee -a ${LOG}
  rm ${TMP}.txt
fi  # MAIL

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

