脚本源自:exchange.nagios.org,由ewayfly修改完成。
#!/bin/bash
# Script to check number of items in a directory and alert appropriately
# 7/8/2011
# Usage: check_dir_filecount.sh [directory] [warn value] [critical value]
#
# Return codes:
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
# Arguments:
DIRECTORY=$1
WARNLEVEL=$2
CRITLEVEL=$3
# Right number of args?
if [ $# -ne 3 ]
then
echo "Usage: dir_filecount [directory] [warnlevel] [critlevel]"
exitstatus=$STATE_UNKNOWN
exit $exitstatus
fi
# Does $DIRECTORY exist?
if [ ! -d $DIRECTORY ]; then
echo "Error, $DIRECTORY does not exist."
exitstatus=$STATE_UNKNOWN
exit exitstatus
fi
# Is the crit level less than the warn level?
if [ $CRITLEVEL -lt $WARNLEVEL -o $CRITLEVEL -eq $WARNLEVEL ]; then
echo "Error, warning value should be less than critical value."
exitstatus=$STATE_UNKNOWN
exit $exitstatus
fi
# Do we have grep/wc installed?
grepproc=`which grep`
wcproc=`which wc`
if [ $grepproc == "" ]; then
echo "Error, /bin/grep doesn't exist."
exitstatus=$STATE_UNKNOWN
exit $exitstatus
fi
if [ $wcproc == "" ]; then
echo "Error, /usr/bin/wc doesn't exist."
exitstatus=$STATE_UNKNOWN
exit $exitstatus
fi
# Get current file count:
FCOUNT=`ls -l $DIRECTORY |grep -v tot* |wc -l`
if [ $FCOUNT -lt $WARNLEVEL ]
then
echo "Ok, $FCOUNT items in $DIRECTORY"
exitstatus=$STATE_OK
exit $exitstatus
elif [ $FCOUNT -lt $CRITLEVEL -a $FCOUNT -gt $WARNLEVEL ]
then
echo "WARNING: $FCOUNT items in $DIRECTORY"
exitstatus=$STATE_WARNING
exit $exitstatus
elif [ $FCOUNT -gt $CRITLEVEL ]
then
echo "CRITICAL: $FCOUNT items in $DIRECTORY"
exitstatus=$STATE_CRITICAL
exit $exitstatus
fi
应用举例:
1、被监控端
创建脚本及添加命令
#chown nagios.nagios check_dir_filecount.sh && chmod +x check_dir_filecout.sh
编辑nrpe.cfg,添加如下命令
command[check_dir_count]=/usr/loca/nagios/libexec/check_dir_filecount.sh /home 20 100
监测/home目录下的文件与目录数量,大于20个则告警,大于100个为严重警告。
注:本脚本只监测当前目录,不会遍历子目录。
2、监控端