Skip to content

Instantly share code, notes, and snippets.

@junaid18183
Created March 12, 2014 07:47
Show Gist options
  • Save junaid18183/9502550 to your computer and use it in GitHub Desktop.
Save junaid18183/9502550 to your computer and use it in GitHub Desktop.
#!/bin/sh
#
# ## Plugin for Nagios to monitor the count of files in directory
# ## Written by Juned Memon
# ##
# ## - 20140312 Tested on Centos
#
#
# ## You are free to use this script under the terms of the Gnu Public License.
# ## No guarantee - use at your own risc.
#
#
# Usage: ./check_file_count -d <path> -w <warn> -c <crit>
#
# ## Description:
#
# This plugin determines the number of iles inside a a directory (including sub dirs)
# and compares it with the supplied thresholds.
#PATH=""
LS="/bin/ls"
CUT="/usr/bin/cut"
WC="/usr/bin/wc"
GREP="/bin/grep"
FIND="/usr/bin/find"
PROGNAME=`/bin/basename $0`
PROGPATH=`echo $0 | /bin/sed -e 's,[\\/][^\\/][^\\/]*$,,'`
REVISION="Revision 1.1"
AUTHOR="Juned Memon"
# Exit codes
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
STATE_DEPENDENT=4
print_revision() {
echo "$REVISION $AUTHOR"
}
print_usage() {
echo "Usage: $PROGNAME -d|--dirname <path> [-w|--warning <warn>] [-c|--critical <crit>]"
echo "Usage: $PROGNAME -h|--help"
echo "Usage: $PROGNAME -V|--version"
echo ""
echo "<warn> and <crit> number of files in each sub-direcory"
}
print_help() {
print_revision $PROGNAME $REVISION
echo ""
echo "Nagios Plugin to count the files in Directory/Subdirecory"
echo ""
print_usage
echo ""
}
compare_threshold(){
if [ "$thresh_warn" != "" ]; then
if [ $count -ge $thresh_warn ]; then
exitstatus=$STATE_WARNING
fi
fi
if [ "$thresh_crit" != "" ]; then
if [ $count -ge $thresh_crit ]; then
exitstatus=$STATE_CRITICAL
fi
fi
}
# Make sure the correct number of command line
# arguments have been supplied
if [ $# -lt 1 ]; then
print_usage
exit $STATE_UNKNOWN
fi
# Grab the command line arguments
thresh_warn=""
thresh_crit=""
perfdata=""
msg=""
exitstatus=$STATE_OK #default
result=$STATE_OK
while test -n "$1"; do
case "$1" in
--help)
print_help
exit $STATE_OK
;;
-h)
print_help
exit $STATE_OK
;;
--version)
print_revision $PROGNAME $VERSION
exit $STATE_OK
;;
-V)
print_revision $PROGNAME $VERSION
exit $STATE_OK
;;
--dirname)
dirpath=$2
shift
;;
-d)
dirpath=$2
shift
;;
--warning)
thresh_warn=$2
shift
;;
-w)
thresh_warn=$2
shift
;;
--critical)
thresh_crit=$2
shift
;;
-c)
thresh_crit=$2
shift
;;
*)
echo "Unknown argument: $1"
print_usage
exit $STATE_UNKNOWN
;;
esac
shift
done
##### Get size of specified directory
error=""
for dir in $($FIND $dirpath/* -type d)
do
count=$(ls -l $dir| wc -l )
count=$(echo $count - 1 | bc ) # On my system one additional line mentioning total is presnt
compare_threshold
msg=$msg" $dir($count);"
perfdata=$perfdata" ${dir#$dirpath}=${count};${thresh_warn};${thresh_crit};"
if [ $exitstatus -eq 0 ]
then
result=$(echo $result + $exitstatus | bc)
else
result=$exitstatus
fi
done
if [ $result -eq 0 ];then
msg="File Count OK :"$msg
elif [ $result -eq 1 ];then
msg="File Count WARNING :"$msg
elif [ $result -eq 2 ];then
msg="File Count CRITICAL :"$msg
fi
echo "$msg|$perfdata"
exit $result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment