#!/bin/bash
#< Nagios plugin - Check status of a Solaris zone
AWK="/usr/bin/awk"
BASENAME="/usr/bin/basename"
ECHO="/bin/echo"
GREP="/usr/xpg4/bin/grep"
UNAME="/usr/bin/uname"
ZONEADM="/usr/sbin/zoneadm"
# Variable initialistion
THIS_PROG=$( ${BASENAME} $0 )
EXCLUDE_LIST="./check_zone.exclude"
# Exit Status Codes
OK=0
WARNING=1
CRITICAL=2
UNKNOWN=3
function usage {
{
${ECHO} "Usage: ${THIS_PROG} -C <check_type> -z <zone_name> [-v]"
${ECHO} " -C Check type:"
${ECHO} " status - Verify a zones running status"
${ECHO} " -v Verbose mode"
${ECHO} " -z Zone to check"
} >&2
}
function print_error {
${ECHO} "Error: $@" >&2
}
function printv {
(( VERBOSE_FLAG )) && ${ECHO} "$@"
}
function check_os {
OS=$( ${UNAME} -s )
case ${OS} in
"SunOS") : ;;
* ) (( VERBOSE_FLAG )) && {
print_error "${OS} is not supported by this plugin"
}
${ECHO} "UNKNOWN: Unsupported Operating System"
exit ${UNKNOWN}
;;
esac
}
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 check_args {
if [ -z "${CHECK_TYPE}" ]; then
(( VERBOSE_FLAG )) && {
print_error "-C option mandatory"
}
${ECHO} "UNKNOWN: -C option mandatory"
exit ${UNKNOWN}
else
case ${CHECK_TYPE} in
"status" ) : ;;
* ) usage
exit ${UNKNOWN} ;;
esac
fi
if [ -z "${ZONE_NAME}" ]; then
(( VERBOSE_FLAG )) && {
print_error "-z option mandatory"
}
${ECHO} "UNKNOWN: -z option mandatory"
exit ${UNKNOWN}
fi
}
function check_valid_zone {
${ZONEADM} list -c | ${GREP} -q "^${ZONE_NAME}$"
if [ "$?" -ne "0" ]; then
${ECHO} "CRITICAL: Zone ${ZONE_NAME} not present on system"
exit ${CRITICAL}
fi
}
function check_zone_status {
STATUS=$( ${ZONEADM} list -cv | ${AWK} '$2 ~ /^'${ZONE_NAME}'$/ {print $3}' )
if [ "${STATUS}" = "running" ]; then
${ECHO} "OK: Zone ${ZONE_NAME} is running"
exit ${OK}
else
if [ -r "${EXCLUDE_LIST}" ]; then
${GREP} -q "^${ZONE_NAME}$" ${EXCLUDE_LIST}
if [ "$?" -eq "0" ]; then
${ECHO} "OK: Zone ${ZONE_NAME} is in exclude list"
exit ${OK}
fi
fi
${ECHO} "CRITICAL: Zone ${ZONE_NAME} is ${STATUS}"
exit ${CRITICAL}
fi
}
# Nagios recommends multiple level verbosity (e.g. -vvv)
# but we will not bother with that
while getopts ":C:z:v" OPTION; do
case ${OPTION} in
"C") CHECK_TYPE="${OPTARG}"
;;
"v") VERBOSE_FLAG="1"
;;
"z") ZONE_NAME="${OPTARG}"
;;
* ) usage
exit ${UNKNOWN}
;;
esac
done
# Even though we're not expecting extra args, let's be thorough
shift $(( ${OPTIND} - 1 ))
check_os
check_remaining_args "$#"
check_args
check_valid_zone
check_zone_status
exit ${OK}