
#! /usr/bin/ksh
#
#--------------------------------------------------------------------------#
#                                                                          #
# Name: check_ndmp_backups.sh                                              #
#                                                                          #
# Description: Check tape backup logs to find missing backups		   #
#                                                                          #
# Version: 1.1                                                             #
#                                                                          #
# References: 								   #
#                                                                          #
# Documentation:                                                           #
#                                                                          #
# Parameters: day of the week to check (f.e. "Mon"),                       #
#             weekly log to check (f.e. ".0")                              #
#                                                                          #
# Usage:  check_ndmp_backups.sh <day> <lognr>                              #
# Example: check_ndmp_backups.sh Wed .4					   #
#                                                                          #
# Global Description:                                                      #
#                                                                          #
# This script searches for backup results of every volume on the FSOD      #
# filers and compares it with a list that describes the expected results.  #
# The logfiles are found under /filers/<filer>/vol0/etc/log/ with name     #
# 'backup', with an extention '.<digit>' starting with zero, after weekly  #
# log rotation.								   #
# So last weeks logname will be named backup.0                             #
#                                                                          #
#                                                                          #
#                                                                          #
#                                                                          #
#                                                                          #
#                                                                          #
#                                                                          #
#                                                                          #
#                                                                          #
# Author(s): Atos Origin                                                   #
#                                                                          #
# Change log:                                                              #
# aut   date            vers    comments                                   #
# AH    27-08-2010      1.0     Initial version Andre Hilgersom            #
# RL    21-09-2010      1.1     Add date of yesterday and today            #
# AH    08-11-2010      1.2     Added drive check, Andre Hilgersom         #
#                                                                          #
#--------------------------------------------------------------------------#
#--------------------------------------------------------------------------#
# Initialize variables                                                     #
#--------------------------------------------------------------------------#
HOSTNAME="`hostname`"
ETCDIR="${HOME}/etc"
DATADIR="${HOME}/data/out"
WEEKDAY=`date '+%A'`
TODAY=`date '+%a %h %e'`

#--------------------------------------------------------------------------#
# Define yesterday                                                         #
#--------------------------------------------------------------------------#
Save_TZ=${TZ}
export ORIGINAL_TZ=${ORIGINAL_TZ:-$TZ}

# Total of hours to reset
RESETTIME=24

#--------------------------------------------------------------------------#
# Reset first call                                                         #
#--------------------------------------------------------------------------#
export TZ=${ORIGINAL_TZ}

if [ ${RESETTIME} -ne 0 ]
then
  #------------------------------------------------------------------------#
  # Determine the difference between UTC and local time                    #
  #------------------------------------------------------------------------#
  date '+%Y +%j +%H' | read year day hour
  date -u '+%Y +%j +%H' | read year_u day_u hour_u
  hours=$((year * 365 * 24 + day * 24 + hour))
  hours_u=$((year_u * 365 * 24 + day_u * 24 + hour_u))
  DIFFERENCE=$((hours_u - hours))
  #------------------------------------------------------------------------#
  # RESETTIME is relative to UTC, not to local time                        #
  #------------------------------------------------------------------------#
  RESETTIME=$((DIFFERENCE + RESETTIME))
  #------------------------------------------------------------------------#
  # When RESETTIME >= 0, then sign plus                                    #
  #------------------------------------------------------------------------#
  [ $RESETTIME -ge 0 ] && RESETTIME="+${RESETTIME}"

  TZ=${TZ}${RESETTIME}
fi

export YESTERDAY=`date '+%a %h %e'`
TZ=${Save_TZ}

#--------------------------------------------------------------------------#

for FILER in `cut -c1-8 $HOME/etc/filers`
 do
#	ssh ${FILER} sysconfig -a|egrep "Changer|Tape"|sort  > ${DATADIR}/${FILER}_tapedrives.n
#	if ! diff ${ETCDIR}/${FILER}_tapedrives.ok ${DATADIR}/${FILER}_tapedrives.n > /dev/null
#	    then
#		echo "Tape drives changed on ${FILER} !: check availability." | mailx -s ":${HOSTNAME}: daily tapedrive check" andre.hilgersom@atosorigin.com fsod@atosorigin.com
#	fi
	for VOLUME in `ssh ${FILER} df | cut -d/ -f3 | sort -u | egrep -v "Filesystem|reserve|^vol0|na_vol0"`
	do
  		echo "${FILER}:${VOLUME} : \c"
  		grep -sh /vol/${VOLUME} /filers/${FILER}/vol0/etc/log/backup${2} |
		        egrep "^dmp ${1:-$YESTERDAY}|^dmp ${1:-$TODAY}" | grep -v ndmpcopy |
                             grep End >/dev/null && echo Done || echo NOT done
	done
 done > ${DATADIR}/check_ndmp_backups.out.n

if diff ${ETCDIR}/check_ndmp_backups.out.ok ${DATADIR}/check_ndmp_backups.out.n > /dev/null
  then
    if `tty -s`
     then
      echo "No ndmp backup problems."
    fi
elif `tty -s`
      then
        diff ${ETCDIR}/check_ndmp_backups.out.ok ${DATADIR}/check_ndmp_backups.out.n | grep NOT
else
        diff ${ETCDIR}/check_ndmp_backups.out.ok ${DATADIR}/check_ndmp_backups.out.n | grep NOT |
             mailx -s ":${HOSTNAME}: daily backup check for ${WEEKDAY}" andre.hilgersom@atosorigin.com fsod@atosorigin.com
fi

