#!/bin/sh
# ctdb event script for checking local file system utilization

. $CTDB_BASE/functions
loadconfig

case "$1" in 
    monitor)
        # check each specified fs to be checked
        # config format is <fs_mount>:<fs_threshold>
        for fs in $CTDB_CHECK_FS_USE
        do
            # parse fs_mount and fs_threshold
            fs_mount=`echo "$fs" | awk -F : '{print $1}'`
            fs_threshold=`echo "$fs" | awk -F : '{print $2}'`

            # check if given fs_mount is existing directory
            if [ ! -d "$fs_mount" ]; then
                echo "$0: Directory $fs_mount does not exist"
                exit 1
            fi

            # check if given fs_threshold is number
            if ! (echo "$fs_threshold" | egrep -q '^[0-9]+$')  ; then
                echo "$0: Threshold $fs_threshold is invalid number"
                exit 1
            fi

            # get utilization of given fs from df
            fs_usage=`df -kP $fs_mount | grep '%' | awk {'print $5'} | sed 's/%//g' | tail -n 1`

            # check if fs_usage is number
            if ! (echo "$fs_usage" | egrep -q '^[0-9]+$') ; then
                echo "$0: FS utilization $fs_usage is invalid number"
                exit 1
            fi

            # check if fs_usage is higher than or equal to fs_threshold
            if [ "$fs_usage" -ge "$fs_threshold" ] ; then
                echo "ERROR: Utilization of $fs_mount ($fs_usage%) is higher than threshold ($fs_threshold%)"
                exit 1
            fi
        done

	;;

    *)
	ctdb_standard_event_handler "$@"
	;;
esac

exit 0
