#!/bin/sh
# script to check accessibility to the reclock file on a node

[ -n "$CTDB_BASE" ] || \
    export CTDB_BASE=$(cd -P $(dirname "$0") ; dirname "$PWD")

. $CTDB_BASE/functions
loadconfig

case "$1" in 
    init)
	ctdb_counter_init
	;;
    
    monitor)
	RECLOCKFILE=$(ctdb -Y getreclock)

	ctdb_counter_incr
	(ctdb_check_counter_limit 200 >/dev/null 2>&1) || {
	    echo "Reclock file $RECLOCKFILE\" can not be accessed. Shutting down."
	    df
	    sleep 1
	    ctdb shutdown
	}

	[ -z "$RECLOCKFILE" ] && {
	    # we are not using a reclock file
	    ctdb_counter_init
	    exit 0
	}

	# try stat the reclock file as a background process
	# so that we dont block in case the cluster filesystem is unavailable
	(
	    stat $RECLOCKFILE && {
		# we could stat the file, reset the counter
		ctdb_counter_init
	    }
	) >/dev/null 2>/dev/null &

	ctdb_check_counter_limit 3 quiet
	;;

    *)
	ctdb_standard_event_handler "$@"
	;;
esac

exit 0
