#!/bin/bash
#==============================================================
#  Name: chk_linuxdd 
#  Abs:  A script to make sure that our disk backup utility
#        (linuxdd) runs on all our machines
#	
#	This script runs on lcls-uwd with the uwd account
#       as a cronjob
#
#  Usage: 
#
#  Facility: LCLS
#
#  Auth: Ken Brobeck
#  Rev:  dd-mmm-yyyy, First Lastname (USERNAME)
#
#        17-Jul-2009  Ken (Brobeck)
#
#--------------------------------------------------------------
#  Mod:
#        dd-mmm-yyyy, First Lastname (USERNAME)
#
#
#==============================================================
#

SLAC="@slac.stanford.edu"
MAILLIST="brobeck$SLAC, jingchen$SLAC"
MAILLIST="brobeck$SLAC"
NOT_RUN_LIST=""
#
# Location of linuxdd log files
WDIR="/nfs/slac/g/lcls/sys/log/linuxdd" 
PWDIR="/u1/lcls/sys/log/linuxdd"
#
# Server list
SLIST=`cat /afs/slac/g/lcls/tools/uwd/config/Server-list`
#
# We have two locations where we keep the linuxdd log files
# Both are $LCLS_DATA/sys/log/linuxdd; on Public and Private network
#
PUBLIST=`find $WDIR/*.log  -mtime -15 | awk -F"/" '{print $NF}'`
PUBLOG=`find $WDIR/*.log  -mtime -15 -exec ls -lh {} \;`
PRIVLIST=`ssh -x lcls-builder "find $PWDIR/*.log  -mtime -15 -exec basename {} \;"`
PRIVLOG=`ssh -x lcls-builder  "find $PWDIR/*.log  -mtime -15 -exec ls -l {} \;"`
LIST=""$PUBLIST" "$PRIVLIST""
#
# Exclude Solaris machines and Linux machines that were not 
# built with one system disk. (ex: nfs servers)
EXLIST="lcls-uwd     \
        lcls-archeng \
        mccfs2       \
        mccfs0       \
	mccfs1 	     \
	opi00gtw00   \
	opi00gtw01   \
	mccas0       \
	mccora2      \
	opi00gtw04   \
	mccelog      \
	lcls-dev"

for x in $SLIST
do
	EXCLUDED=`echo $EXLIST | grep $x >/dev/null; echo $?`
	if [ $EXCLUDED != "0" ]
	  then
            HasRun=`echo $LIST | grep $x >/dev/null; echo $?`
	    if [ "$HasRun" != "0" ]
	      then
		NOT_RUN_LIST="$NOT_RUN_LIST \n$x"
	   fi
	fi
done

if [ "$NOT_RUN_LIST" != "" ]
  then	
	printf "\n\nLinuxdd does not run on Solaris machines or "
        printf "\nLinux machines that were not built with one system disk"
	printf "\n\nlinuxdd did not run on the following nodes:\n $NOT_RUN_LIST\n\n" | \
		mailx -s "linuxdd did not run on some servers" "$MAILLIST"
fi

/bin/rm -f /tmp/outfile
if [ -a "/tmp/chk_linuxdd.log" ]
  then
        cat /tmp/chk_linuxdd.log > /tmp/outfile
	printf "\n\n$PUBLOG \n\n $PRIVLOG" >> /tmp/outfile
	cat /tmp/outfile | mailx -s "`basename $0` ran today: `date`" "$MAILLIST"
  else
	printf "\n\n$PUBLOG \n\n $PRIVLOG" > /tmp/outfile
	cat /tmp/outfile | mailx -s "`basename $0` ran today: `date`" "$MAILLIST"
fi
