
#!/bin/ksh
# File  : get_filer_uptime_only.sh
# By    : Maarten de Boer, 100308
# Subject       : Get filer uptime only and send (with uuencode) to msssyscol01
#	: From get_filer_uptime.ksh
# (0.2)	: split-up days
# (0.3)	: Added grep 'kern.syslog.msg:notice' /filers/??naf??/vol0/etc/messages*
#(0.4)  : # Get 'kern.syslog.msg' out /filers/${FILER}/vol0/etc/messages* & ${SSH} added.
#(0.5)  : # Get 'kern.syslog.msg:notice' out /filers/${FILER}/vol0/etc/messages* & ${SSH} added.
#(0.6)	: Mod. HOSTNAME 
#(0.7)	: Mod. for RHEL6 (mailx -a), Added ${LOG}, ${ASC}, Mod. 'rc:notice'
#(0.1)	: Only the uptime days
#(0.2)	: Added ping
# set -x
PGM="`basename $0|cut -d\. -f1`"
VER="0.2"
TMP="/tmp/${PGM}.$$"
LOG="${HOME}/log/${PGM}.log"
CSV="${PGM}.csv"
FILERS="${HOME}/etc/filers"
MAILTO="maarten.deboer@atos.net"
MSGS="${TMP}.msgs"
UNIX2DOS="/usr/bin/unix2dos"
UUENCODE="/usr/bin/uuencode"
TXT="${PGM}.txt"
ASC="${PGM}.asc"
SSH="/usr/bin/ssh"
HOSTNAME="`hostname | cut -d\. -f1`"
AFSP="mdrglob@msssyscol01.bcklan.ao-srv.com"

touch ${TMP} ${MSGS}
DAMOYR="`date +%d'-'%m'-'%Y`"
DATEHR="`date +%H`"

echo "`date` ${PGM} v${VER} started."|tee -a ${LOG}

echo "" > ${TMP}
for FILER in `cat ${FILERS}|grep -v \#`
do
  echo "  ${FILER} ..."
  ping -c 1 ${FILER}
  EC=${?}
  if [ ${EC} -eq 0 ]; then
    UPTIME="`${SSH} -n ${FILER} uptime`"
    UPTI34="`echo ${UPTIME} | awk '{print $3, $4}'| sed -e 's/,//g'`"
    UPTI5="`echo ${UPTIME} | awk '{print $5}'| sed -e 's/,//g'`"
    MIN=0
    MIN="`echo ${UPTI34}| grep min`"
    HR="`echo ${UPTI34}| grep ':' |cut -d\: -f1`"
    if [ "${HR}" = "" ]; then
      HR="`echo ${UPTI5}| grep ':' |cut -d\: -f1`"
    fi
    DAYS="`echo ${UPTI34}| grep days|cut -d' ' -f1`"

    if [ "${DAYS}" != "" ]; then
      let "HRS=${DAYS} * 24"
    else
      if [ "${HR}" != "" ]; then
        HRS=${HR}
      else
        HRS=0
      fi
    fi
    if [ "${HR}" != "" ]; then
      let "HRS=${HRS} + ${HR}"
    fi

    echo "${FILER};${DAYS};  ${UPTIME};  " | tee -a ${TMP}
  
#   Get 'kern.syslog.msg:notice' out /etc/messages onto the filer itself & NOT via /filers/${FILER}/vol0/etc/messages*
#   Because some vol0-etc's are not mountable (specialy in .CN, because of NTFS of vol0-etc :-(
    NRS="0 1 2 3 4 5"
    ${SSH} -n ${FILER} rdfile /etc/messages | grep 'rc:notice' |tee -a ${MSGS}
    for NR in ${NRS}
    do
      ${SSH} -n ${FILER} rdfile /etc/messages.${NR} | grep 'rc:notice' |tee -a ${MSGS}
    done

  fi  # ping
done  #  FILER

cp ${TMP} /tmp/${CSV}
date|mailx -a /tmp/${CSV} -s "@${HOSTNAME}: Uptime-sheet [${PGM} v${VER}]" ${MAILTO}
rm /tmp/${CSV}

cp ${MSGS} /tmp/${ASC}
date|mailx -a /tmp/${ASC}  -s "@${HOSTNAME}: Messages-files [${PGM} v${VER}]" ${MAILTO}
rm /tmp/${ASC}

cat ${TMP}|uuencode ${CSV}|mailx -s "${PGM}" ${AFSP} 

echo "  Mailed to ${MAILTO} "|tee -a ${LOG}

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

