nagios监控apache的进程数的脚本,同时学习到了一个新的命令:getopts,现把脚本分享如下。
#!/bin/bash
. /usr/local/nagios/libexec/utils.sh
#AUTHOR by wenzizone.cn
VERSION="1.0"
CHECKNAME="HTTP process"
PS=/bin/ps
GREP=/bin/grep
WC=/usr/bin/wc
FLAG_VERBOSE=FALSE
LEVEL_WARN=""
LEVEL_CRIT=""
RESULT=""
EXIT_STATUS=$STATE_OK
usage() {
echo " check_apacheprocess $VERSION - Nagios Apache concurrent HTTP process number check script"
echo ""
echo " Usage: check_apacheprocess -w <warning level> -c <critical level> [ -h ]"
echo ""
echo " -w Number of concurrent process number at which a warning is triggered"
echo " -c Number of concurrent process number at which a critical is triggered"
echo " -h Show this page"
echo ""
}
doopts() {
if ( `test 0 -lt $#` )
then
while getopts w:c:vh myarg
do
case $myarg in
h|?)
usage
exit;;
w)
LEVEL_WARN=$OPTARG;;
c)
LEVEL_CRIT=$OPTARG;;
*) # Default
usage
exit;;
esac
done
else
usage
exit
fi
}
theend() {
echo $RESULT
exit $EXIT_STATUS
}
doopts $@
OUTPUT=$($PS fax|$GREP httpd|$GREP -v grep|$WC -l)
if test -z "$OUTPUT" ; then
RESULT="$CHECKNAME WARNING - query returned no output!"
EXIT_STATUS=$STATE_WARNING
else
if test "$OUTPUT" -lt "$LEVEL_WARN" ; then
RESULT="$CHECKNAME OK - $OUTPUT concurrent process"
EXIT_STATUS=$STATE_OK
else
if test "$OUTPUT" -gt "$LEVEL_CRIT" ; then
RESULT="$CHECKNAME CRITICAL - $OUTPUT concurrent process"
EXIT_STATUS=$STATE_CRITICAL
else
if test "$OUTPUT" -gt "$LEVEL_WARN" ; then
RESULT="$CHECKNAME WARNING - $OUTPUT concurrent process"
EXIT_STATUS=$STATE_WARNING
fi
fi
fi
fi
# Quit and return information and exit status
theend