
#!/bin/sh
# File	: chk_filer_connects.sh
# By	: Maarten.deBoer@AtosOrigin.com, 091208, 091214
# Subject	: Check filer connections (ping, hostname, ROOTVOL mount) (from ${ETC})
# (0.2)	: At FSOD; Added ping to RLM/BMC/console (<filername>-rlm)
# (0.3)	: ${PING} added
#set -x

PGM="`basename $0|cut -d\. -f1`"
VERSION="0.2"
TMP="/tmp/${PGM}.$$"
LOG="${HOME}/log/${PGM}.log"
FILERS="${HOME}/etc/filers"
MAILTO="maarten.deboer@atosorigin.com"
MAILTO="fsod@atos.net maarten.deboer@atos.net"
MAILTO="fsod_mgt_status@elink.atos.net maarten.deboer@atos.net"
PING="/bin/ping"
HR="`date +%H`"
ASC="${PGM}-${HR}h.asc"

#touch ${LOG} ${TMP}

echo "# ${PGM} `date` from `hostname` " >> ${TMP}

for FILER in `cat ${FILERS}|grep -v \^#`
do
  echo "${FILER} ..." 
  # PINGable?
  ${PING} -c 1 ${FILER} 1> /dev/null 2>&1
  EC=$?
  if [ ${EC} -gt 0 ]; then
    echo "`date +%Y-%m-%d_%H:%M:%S` ${PGM}: Ping-Error to ${FILER} "|tee -a ${LOG}|tee -a ${TMP}
  fi
  # Is RML/BMC pingable?
  ${PING} -c 1 ${FILER}-rlm 1> /dev/null 2>&1
  EC=$?
  if [ ${EC} -gt 0 ]; then
    echo "`date +%Y-%m-%d_%H:%M:%S` ${PGM}: Ping-Error to ${FILER}-rlm (RLM/BMC/console) "|tee -a ${LOG}|tee -a ${TMP}
  fi

  # Check if hostname is same
  HOSTNAME="`/usr/bin/ssh -n ${FILER} hostname`"
  if [ "${FILER}" != "${HOSTNAME}" ]; then
    echo "`date +%Y-%m-%d_%H:%M:%S` ${PGM}: Filername (${FILER}) <> hostname (${HOSTNAME})"|tee -a ${LOG}|tee -a ${TMP}
  fi

  # Check if ROOTVOL can be mounted
  ROOTVOL="`/usr/bin/ssh -n ${FILER} vol status|grep ' root'|awk '{print $1}'`" 
  ETCDIR="/filers/${FILER}/${ROOTVOL}/etc/" 
# Test is DIR exsists. Otherwise (if not) script will stop
  if [ -d ${ETCDIR} ]; then
    cd ${ETCDIR} 2> /dev/null
    EC=$?
    if [ ${EC} -gt 0 ]; then
      echo "`date +%Y-%m-%d_%H:%M:%S` ${PGM}: Error at ${FILER} changing to ${ETCDIR}"|tee -a ${LOG}|tee -a ${TMP}
    fi
  else
    echo "`date +%Y-%m-%d_%H:%M:%S` ${PGM}: Warning for ${FILER} no dir ${ETCDIR} found"|tee -a ${LOG}
  fi

done

# If ${TMP} (error msg), then mail
if [ -s ${TMP} ]; then
  cat ${TMP}|uuencode ${ASC}|mailx -s ":`hostname`: Check filer connections [${PGM} v${VERSION}]" ${MAILTO}
  echo "Mailed to ${MAILTO}"
fi

rm ${TMP}
exit 0

