#!/bin/bash
#< Template for writing your own nagios plugins
# accepts usage such as:
# plugin_name -c critical_threshold -w warning_threshold -v
# Executables
BASENAME="/usr/bin/basename"
ECHO="/bin/echo"
EGREP="/usr/bin/egrep"
# Variable initialistion
CRITICAL_FLAG="0"
WARNING_FLAG="0"
VERBOSE_FLAG="0"
THIS_PROG=$( ${BASENAME} $0 )
# Exit Status Codes
OK=0
WARNING=1
CRITICAL=2
UNKNOWN=3
function usage {
${ECHO} "Usage: ${THIS_PROG} -w -c [-v]" >&2
}
function print_error {
${ECHO} "Error: $@" >&2
}
function check_remaining_args {
if [ "$1" -ne "0" ]; then
(( VERBOSE_FLAG )) && {
print_error "Remaining argument count not zero"
}
usage
${ECHO} "UNKNOWN: Extra arguments passed to plugin"
exit ${UNKNOWN}
fi
}
function parse_args {
ERROR_COUNT=0
if [ "${CRITICAL_FLAG}" -ne "1" ]; then
(( VERBOSE_FLAG )) && {
print_error "-c option not present"
}
(( ERROR_COUNT = ERROR_COUNT + 1 ))
else
# Let's check that the critical threshold contains a sane value
${ECHO} "${CRITICAL_THRESHOLD}" | ${EGREP} '^[0-9]+%?$' >/dev/null 2>&1
if [ "$?" -ne "0" ]; then
(( VERBOSE_FLAG )) && {
print_error "Invalid critical threshold value passed"
}
(( ERROR_COUNT = ERROR_COUNT + 1 ))
fi
# This should never be true, a usage error would be thrown by the getopts while loop
if [ -z "${CRITICAL_THRESHOLD}" ]; then
(( VERBOSE_FLAG )) && {
print_error "Critical threshold not set"
}
(( ERROR_COUNT = ERROR_COUNT + 1 ))
fi
fi
if [ "${WARNING_FLAG}" -ne "1" ]; then
(( VERBOSE_FLAG )) && {
print_error "-w option not present"
}
(( ERROR_COUNT = ERROR_COUNT + 1 ))
else
# Let's check that the warning threshold contains a sane value
${ECHO} "${WARNING_THRESHOLD}" | ${EGREP} '^[0-9]+%?$' >/dev/null 2>&1
if [ "$?" -ne "0" ]; then
(( VERBOSE_FLAG )) && {
print_error "Invalid warning threshold value passed"
}
(( ERROR_COUNT = ERROR_COUNT + 1 ))
fi
# This should never be true, a usage error would be thrown by the getopts while loop
if [ -z "${WARNING_THRESHOLD}" ]; then
(( VERBOSE_FLAG )) && {
print_error "Warning threshold not set"
}
(( ERROR_COUNT = ERROR_COUNT + 1 ))
fi
fi
if [ "${ERROR_COUNT}" -gt "0" ]; then
(( VERBOSE_FLAG )) && {
print_error "${ERROR_COUNT} error(s) found whilst parsing arguments"
}
${ECHO} "UNKNOWN: Errors encountered passing plugin arguments"
exit ${UNKNOWN}
fi
}
# Nagios recommends multiple level verbosity (e.g. -vvv)
# but we will not bother with that
while getopts ":c:w:v" OPTION; do
case ${OPTION} in
"c") if [ "${CRITICAL_FLAG}" -ne "0" ]; then
(( VERBOSE_FLAG )) && {
print_error "More than one -c option specified"
}
${ECHO} "UNKNOWN: More than one -c option passed to plugin"
exit "${UNKNOWN}"
fi
CRITICAL_FLAG="1"
CRITICAL_THRESHOLD="${OPTARG}"
;;
"w") if [ "${WARNING_FLAG}" -ne "0" ]; then
(( VERBOSE_FLAG )) && {
print_error "More than one -w option specified"
}
${ECHO} "UNKNOWN: More than one -w option passed to plugin"
exit "${UNKNOWN}"
fi
WARNING_FLAG="1"
WARNING_THRESHOLD="${OPTARG}"
;;
"v") VERBOSE_FLAG="1"
;;
* ) usage
exit ${UNKNOWN}
;;
esac
done
# Even though we're not expecting extra args, let's be thorough
shift $(( ${OPTIND} - 1 ))
check_remaining_args "$#"
parse_args
exit ${OK}