check files inside a folder,nagios

Bagaimana mengecek jumlah file yang ada dalam sebuah folder?, bila file yang terdapat dalam sebuah folder melebihi batas maksimum/kosong yang telah kita tentukan, maka nagios mengirimkan pesan.
#!/bin/bash
# Standard Nagios return codes
#OK       = 0
#WARNING  = 1
#CRITICAL = 2
#UNKNOWN  = 3

#SUPGEN.csv.base

usage()
{
        echo "usage: check_file [-c critical_value] [-d directory_path]"
        echo
        echo "check_file -c 0 -d /path/to/file/location"
        exit 3
}


while getopts c:d: OPTNAME; do
        case "$OPTNAME" in
        c)
                CRITVAL="$OPTARG"
                ;;
        d)
                DIRPATH="$OPTARG"
                ;;
        *)
                usage
                ;;
        esac
done

#check value
if [ "$CRITVAL" = "" ]; then
                echo "Error: critical_value must not empty"
                usage
                exit 3
fi
#check dir
if [ "$DIRPATH" = "" ]; then
                echo "Error: directory must exist do not empty"
                usage
                exit 3
fi

# get file status
NUMFILE=$(($(ls -la $DIRPATH | grep -v '^d'| wc -l)-1))
if [ $NUMFILE -gt $CRITVAL ] ; then

                echo "*** CRITICAL ***"
                echo "there is $NUMFILE files at $DIRPATH on `hostname`"
                exit 2

                else
                echo "** OK ** $NUMFILE file at $DIRPATH on `hostname`"
                exit 0
fi

Comments