#!/bin/csh -f

# csh is having problems: "set: Variable name must begin with a letter."
# between H1C and H1D (probably on "/bin/df -k $STR >& $TMP"

if ( ! $?tcsh && -x /bin/tcsh) then
#    echo exec /bin/tcsh $0 $*
    exec /bin/tcsh $0 $*
#else
#    echo tcsh running $0 $*
endif

# Quick check for where NFS problems are coming from.
# All paths are absolute and timeout used so this doesn't hang.
#				daw, 10/9/00

# still getting too frequent unexplained errors
# begin extensive debugging; 3/23/07
# first, make sure we can echo output; if not, error should be distinctive
# (if we ever see this, we need to distinguish between the two)
if (! -e /bin/echo || ! -x /bin/echo) exec /dev/null # debugging

if (! -e /bin/uname || ! -x /bin/uname) exec /bin/echo {$0}: /bin/uname problem # debugging

switch (`/bin/uname -s``/bin/uname -r`)
case SunOS5*:
    set DIRNAME = /bin/dirname
    set MOUNT = /usr/sbin/mount
    set MNTTAB = /etc/mnttab
    set TIMEOUT = /usr/local/bin/timeout
    set TS9
    set WHOAMI = /usr/ucb/whoami
	set TAILPLUS = "+"
	set HEADMINUS = "-"
	breaksw
case Linux[2-5]*:
    set DIRNAME = /usr/bin/dirname
    set MOUNT = /usr/bin/mount
    if (! -e $MOUNT) set MOUNT = /bin/mount
    set KLIST = /bin/klist
    if (! -e $KLIST) set KLIST = /usr/bin/klist
    set MNTTAB = /etc/mtab
    set TIMEOUT = /usr/local/bin/timeout
    if (! -e $TIMEOUT) then
	set TIMEOUT = `which timeout |& grep .`
    endif
    set TS9 = "-s 9"
    set WHOAMI = /usr/bin/whoami
# Some Linux tails now no longer have "+n"!
# and some heads don't like -n either.
	set TAILPLUS = "--lines=+"
	set HEADMINUS = "-n"
	breaksw
default:
	exec /bin/echo ${0}: does not run on `/bin/uname -s` `/bin/uname -r`
endsw

while($#argv)
	switch ("$1")
	case -d:
		set DEBUG
		set EDEBUG
#		shift
#		breaksw
# fall through to verbose
	case -v:
		set VERBOSE
		shift
		breaksw
	case -ksw:
		set KSWITCHES = "$2"
		shift; shift
		breaksw
	case -q:
		set QUIET	# don't complain about structures we cannot read (for cron)
		shift
		breaksw
	default:
	    if (! $?OKFILE && -e "$1") then
		set OKFILE = $1
		shift
		breaksw
	    endif
		set DQ = '"'
		exec /bin/echo ${0}: "What do I do with $DQ$1$DQ?"
	endsw
end

if (-e /bin/basename) then
    set BASENAME = /bin/basename
else if (-e /usr/bin/basename) then
    set BASENAME = /usr/bin/basename
else
    exec /bin/echo {$0}: Where is basename\? # debugging
endif
if (! -e $BASENAME || ! -x $BASENAME) exec /bin/echo {$0}: $BASENAME problem # debugging
set BASE = `$BASENAME $0`
if (! -e /bin/hostname || ! -x /bin/hostname) exec /bin/echo {$0}: /bin/hostname problem # debugging
set H = `/bin/hostname`		# for DTMP
#set EDEBUG
set ESTATUS = 0
if ($?EDEBUG) echo ESTATUS = $ESTATUS "(1)"
if (! $?KSWITCHES) set KSWITCHES
if (! $?OKFILE) set OKFILE = /dev/null

set DOLLAR0 = `which $0`
set WATROUS = `/usr/bin/dirname $DOLLAR0`
if ($?DEBUG) echo WATROUS = $WATROUS

if (! -e /bin/df || ! -x /bin/df) exec /bin/echo {$0}: /bin/df problem # debugging
if (! -e /bin/grep || ! -x /bin/grep) exec /bin/echo {$0}: /bin/grep problem # debugging
if (! -e $MOUNT || ! -x $MOUNT) exec /bin/echo {$0}: $MOUNT problem
set ATMP = `/bin/df -k /tmp | /bin/grep /`
if ($ATMP[4] > 0) then
    set TMP = /tmp/$BASE.`$WHOAMI`.tmp
else
    set ATMP = `/bin/df -k /var/tmp | /bin/grep /`
    if ($ATMP[4] > 0) then
	set TMP = /var/tmp/$BASE.`$WHOAMI`.tmp
    else
	set TMP = ~/$BASE.$H.`$WHOAMI`.$$.tmp
    endif
endif
set DTMP = $TMP.`date +%H`

endif

# If running under cron, overwrite possible old debugging file
#  otherwise, append output to preserve old results

if (! -e /bin/date || ! -x /bin/date) exec /bin/echo {$0}: /bin/date problem # debugging
if ($?SHELL) then
    if ("$SHELL" == "/bin/sh"||"$SHELL" == "/usr/bin/sh") then
	/bin/date > $DTMP
    else
	if (-e $DTMP) then
	    /bin/echo "" >> $DTMP
	endif
	/bin/date >> $DTMP
    endif
endif

set DHOST1 = xstefi.rutgers.edu
set DHOST2 = xoz.rutgers.edu

if (! -e /bin/sed || ! -x /bin/sed) exec /bin/echo {$0}: /bin/sed problem # debugging
if (-e /bin/sort) then
    set SORT = /bin/sort
else if (-e /usr/bin/sort) then
    set SORT = /usr/bin/sort
else
    exec /bin/echo {$0}: Where is sort\? # debugging
endif
if (! -e $SORT || ! -x $SORT) exec /bin/echo {$0}: $SORT problem # debugging
if ("$H" == "$DHOST1" || "$H" == "$DHOST2") /bin/echo A # >> $DTMP
set HOSTS = `/bin/grep : $MNTTAB | /bin/grep nfs | /bin/grep -v "\(pid.*\)" | /bin/sed 's;:.*;;' | $SORT -u`
if ("$H" == "$DHOST1" || "$H" == "$DHOST2") /bin/echo B # >> $DTMP
if ($?DEBUG) /bin/echo HOSTS = $HOSTS

if (! -e "$TIMEOUT") then
    if (-e $WATROUS/kill-hung-process) then
	( $WATROUS/kill-hung-process -kp -p $$ -s 5 $KSWITCHES df & ) > /dev/null
    else
	/bin/echo timeout does not exist -- going verbose
	/bin/echo "   " and will hang on problems
if ("$H" == "$DHOST1" || "$H" == "$DHOST2") /bin/echo C # >> $DTMP
	set VERBOSE
if ("$H" == "$DHOST1" || "$H" == "$DHOST2") /bin/echo D # >> $DTMP
    endif
endif

# Hmm, we report NFS problems when /tmp is full.

/bin/echo Test > $TMP
set STATUS = $status
if ($STATUS) then
    /bin/echo ${0}: error $STATUS writing to $TMP
    set TMP = /var/tmp/$BASE.`$WHOAMI`.tmp
    /bin/echo "	-- switching to $TMP"
    /bin/echo Test > $TMP
    set STATUS = $status
    if ($STATUS) then
	/bin/echo ${0}: error $STATUS writing to $TMP
	set TMP = ~/$BASE.$H.`$WHOAMI`.tmp
	/bin/echo "	-- switching to $TMP"
	/bin/echo Test > $TMP
	set STATUS = $status
	if ($STATUS) then
	    /bin/echo ${0}: error $STATUS writing to $TMP
	    /bin/echo "	-- giving up"
	    @ ESTATUS += 1
	    if ($?EDEBUG) echo ESTATUS = $ESTATUS "(2)"
	    goto exit
	endif
    endif
endif

endif

# Debugging weird results from this script; 5/30/06
/bin/echo HOSTS = $HOSTS >> $DTMP

foreach HOST ( $HOSTS )
	if ($?VERBOSE) /bin/echo `/bin/date +%T` Checking $HOST
/bin/echo HOST = $HOST >> $DTMP
if ("$H" == "$DHOST1" || "$H" == "$DHOST2") /bin/echo E # >> $DTMP
    if (! -e /usr/bin/head || ! -x /usr/bin/head) exec /bin/echo {$0}: /usr/bin/head problem # debugging
    if (-e /bin/awk) then
	set AWK = /bin/awk
    else if (-e /usr/bin/awk) then
	set AWK = /usr/bin/awk
    else
	exec /bin/echo {$0}: Where is awk\? # debugging
    endif
    if (! -e $AWK || ! -x $AWK) exec /bin/echo {$0}: $AWK problem # debugging
	set STR = `/bin/grep "${HOST}:" $MNTTAB |& /usr/bin/head ${HEADMINUS}1 | $AWK '{print $2}'`
	if ("$STR" == "") then
# Avoid
#	too few arguments
#	Try `/usr/bin/dirname --help' for more information.
#	Expression Syntax.
# remove this when seen; daw, 6/23/06
#	    echo DEBUG --- structure not found for host -- $HOST -- continuing
	    continue
	endif
	grep ^$STR'$' $OKFILE > /dev/null
	if (! $status) then
	    if ($?VERBOSE) echo $STR is in $OKFILE -- continuing
	    continue
	endif
if ("$H" == "$DHOST1" || "$H" == "$DHOST2") /bin/echo F # >> $DTMP
	if ($?DEBUG) /bin/echo STR = $STR
/bin/echo STR = $STR >> $DTMP
if ("$H" == "$DHOST1" || "$H" == "$DHOST2") /bin/echo G # >> $DTMP
    if (! -e $DIRNAME || ! -x $DIRNAME) exec /bin/echo {$0}: $DIRNAME problem # debugging
	set MPT = `$DIRNAME $STR`
if ("$H" == "$DHOST1" || "$H" == "$DHOST2") /bin/echo H # >> $DTMP
#	if (! -r $STR && $?QUIET) then
/bin/echo MPT = $MPT >> $DTMP
	if (! -r $MPT && $?QUIET) then
	    if ($?VERBOSE) /bin/echo `/bin/date +%T` Skipping $STR -- cannot read $MPT
#	    /bin/echo Debugging ${0}: Would skip $STR -- cannot read $MPT
/bin/echo Debugging ${0}: Skipping $STR -- cannot read $MPT >> $DTMP
	    continue
	endif
if ("$H" == "$DHOST1" || "$H" == "$DHOST2") /bin/echo H1 # >> $DTMP
	if (-e "$TIMEOUT") then
    if (! -e "$TIMEOUT" || ! -x "$TIMEOUT") exec /bin/echo {$0}: $TIMEOUT problem # debugging
if ("$H" == "$DHOST1" || "$H" == "$DHOST2") /bin/echo H1A # >> $DTMP
#	    "$TIMEOUT" 5 /bin/df -k $STR >& $TMP
	    "$TIMEOUT" $TS9 5 /bin/df -k $STR >& $TMP
	    set STATUS = $status
if ("$H" == "$DHOST1" || "$H" == "$DHOST2") /bin/echo H1B # >> $DTMP
	else
if ("$H" == "$DHOST1" || "$H" == "$DHOST2") /bin/echo H1C # >> $DTMP
# removed blank lines from between following two lines
# and added 16 blanks after both lines
#	    /bin/df -k $STR >& $TMP                
# maybe space here?
	    /bin/df -k $STR > & $TMP                
#	    set STATUS = $status                
# try this:
	    set STATUS = ( $status )
# or here?
if ( $status ) echo set STATUS just before H1D failed
if ("$H" == "$DHOST1" || "$H" == "$DHOST2") /bin/echo H1D # >> $DTMP
	endif
if ("$H" == "$DHOST1" || "$H" == "$DHOST2") /bin/echo H2 # >> $DTMP
	if ($?DEBUG) /bin/sed 's;\(.\);    \1;' $TMP
	if ($STATUS) then
		if (-r $MPT) @ ESTATUS += $STATUS
		if ($?EDEBUG) echo ESTATUS = $ESTATUS "(3)"
#		/bin/echo Problem with $HOST
		$MOUNT | grep " on $STR .*sec=krb5"  > /dev/null
		set NOTKRB5 = $status
		$KLIST >& /dev/null
		set NOKEYS = $status
		if ($NOTKRB5 || ! $NOKEYS) then
		    /bin/echo ${0}: NFS problem with $HOST "($STR)"
		    /bin/sed 's;\(.\);    \1;' $TMP	# type any output we got
		else
		    if ($?VERBOSE) then
			if (! $NOTKRB5) echo Skipping $STR -- mounted with sec=krb5
			if ($NOKEYS) echo "   " and I have no kerberos tickets
		    endif
		    @ ESTATUS -= 1
		    if ($?EDEBUG) echo ESTATUS = $ESTATUS "(4)"
		endif
	endif

if ("$H" == "$DHOST1" || "$H" == "$DHOST2") /bin/echo I1 # >> $DTMP
end
if ("$H" == "$DHOST1" || "$H" == "$DHOST2") /bin/echo I2 # >> $DTMP

exit:
if ("$H" == "$DHOST1" || "$H" == "$DHOST2") /bin/echo I3 # >> $DTMP

if ($?VERBOSE) /bin/echo `/bin/date +%T` "[Done]"

/bin/echo Done >> $DTMP

/bin/rm -f $TMP
#/bin/rm -f $DTMP
# if DTMP looks like this:
#  Wed Jan  9 09:00:01 EST 2013
#  HOSTS =
#  Done
# it's not worth keeping.
if (-e $DTMP) then
    @ LINES = `cat $DTMP | wc -l`
    grep '^HOSTS =$' $DTMP > /dev/null
    set HSTATUS = $status
    grep '^Done$' $DTMP > /dev/null
    set DSTATUS = $status
    if ($LINES == 3 && ! ($HSTATUS || $DSTATUS)) /bin/rm -f $DTMP
endif
/bin/echo $DTMP | grep ^/tmp > /dev/null  # if $DTMP is not on /tmp
if ($status) /bin/rm -f $DTMP	# remove it

if ($?EDEBUG) echo ESTATUS = $ESTATUS "(5)"
exit $ESTATUS
