--- ../../original_sources/nagios-plugins-1.4.2/contrib/check_test 2005-10-11 20:24:13.349222200 -0400 +++ nagios-plugins-1.4.2/contrib/check_test 2005-10-11 20:22:49.205014064 -0400 @@ -0,0 +1,169 @@ +#! /bin/bash + +# Author: John P. Rouillard - rouilj@cs.umb.edu, +# rouilj-check_test@renesys.com +# A quick and dirty test program similar to check_dummy, but exit +# status is contollable from an external file. It can be used for +# testing nagios service dependency and host dependency notification +# suppression. +# +# Entries in the external file are composed of a label and an exit +# code, or code list seperated by an = sign. +# +# The exit code list is any 1, 2 or 3 digit number which will exit +# with that status (0, 1, 2 and 3 are valid for nagios). The code 'S' +# or 's' will cause the process to sleep for 1 hour in the expectation +# that it will recieve a kill signal from nagios. +# +# The label has one of the following forms: +# +# hostname-service_name (if -H specified) +# service_name (if -E is not specified) +# hostname (if -H specified) +# default +# +# The labels are searched for in order. If no matching label is found, +# one is appended to the command file, with exit status 0. If duplicate +# labels are in the file, the last label takes precidence. So you can +# append a label/exit code line to the file and get a change in the +# exit code. + +# Exit code lists look like: +# +# label=value1,value2,value3,value4,...,valueN +# +# where value1 is used for this invocation, and a new label entry is +# added with value1 put after valueN so that value2 wil be used as +# exit code on the next run. This allows deterministic runs of the +# program for testing availability metrics and various senarios for +# reporting and other related activities. +# +# Note that regular poll and error poll times along with the number of +# retries to go from soft to hard have to be considered when designing +# a test. +# +# Options: +help="Usage: $0 -E -s -H -e -f -ht -m + -E - exact entry including hostname is needed. Prevents matching + only on service_ name. + -H - the hostname this test is running on. No default. + -e - used like check dummy, always exits with this + status code. Ignores the external file. + -f - the external file name used for determining exit + code. Default: /tmp/check_test. + -h - This help text. + -s - the service description for this test. Default + the basename of the progam name. + -t - enable tracing info for debugging. Note this will cause + problems is run from nagios. + -m - message to be put out when running. +" + +## standard debugging preamble +trace=true +if ! [ -z "$SHELL_DEBUG" ]; then + if echo "$SHELL_DEBUG" | grep -i xv > /dev/null 2>&1; then + set -xv + fi + if echo "$SHELL_DEBUG" | grep -i trace > /dev/null 2>&1; then + trace=echo + fi +fi + +## set variables +exact="" +exitcode=-1 +filename=/tmp/check_test +hostname="" +message="" +service_name=`basename $0` + + +## parse arguments +while getopts -- EH:e:f:hs:t arg +do + case "$arg" in + E) exact="yes";; + H) hostname="$OPTARG";; + e) exitcode="$OPTARG";; + f) filename="$OPTARG";; + h) echo "$help"; exit;; + m) message="$OPTARG";; + s) service_name="$OPTARG";; + t) trace=echo;; + *) echo "Usage: $0 -E -s -H -e -f " + exit 4;; + esac +done + +$trace "parsed arguments: $hostname, $service_name, $exitcode, $filename" + +## create command file if needed +if ! [ -r $filename ]; then + $trace Did not find readable file. Creating config file $filename + # hmm how to capture. Wrapping with `` and assigning to variable + # produces syntax error missing close ` in cygwin bash, but it works + # interactively. + touch $filename 2>&1 || \ + eval "echo \"$0: Unable to create $filename, $error\" && exit 2" +fi + +## find the exit code entry for this invocation +if [ $exitcode = "-1" ]; then + rule=`grep "^$hostname-$service_name=" $filename | tail -1` + if [ -z "$rule" ]; then + if [ -z "$exact" ]; then + rule=`grep "^$service_name=" $filename | tail -1` + fi + if [ -z "$rule" ]; then + if [ -n "$hostname" ]; then + rule=`grep "^$hostname=" $filename | tail -1` + fi + if [ -z "$rule" ]; then + rule=`grep "^default=" $filename | tail -1` + if [ -z "$rule" ]; then + $trace no matching rule found, creating new rule + rule="${hostname}${hostname:+-}${service_name}=0" + echo $rule >> $filename + fi + fi + fi + fi + $trace rule is $rule + + ## split the rule to isolate the exit code(s). + IFS="=" + set -- $rule + IFS="" + + if [ $# -gt 2 ]; then + echo "$0: Malformed rule $rule:" + exit 2 + fi + + exitcode="$2" + label=$1 +fi + +## exit with message and proper code and rotate exit code list if present +while true; do + case $exitcode in + 0) echo $0: $message exiting 0; exit 0;; + 1) echo $0: $message exiting 1; exit 1;; + 2) echo $0: $message exiting 2; exit 2;; + [0-9]|[0-9][0-9]|[0-9][0-9][0-9]) + # exit with odd exit codes + echo $0: $message exiting $exitcode; exit $exitcode;; + s|S) sleep 3600 ;; # stall forcing parent nagios to kill us + *,*) IFS="," + set -- $exitcode + exitcode=$1 + shift + exitcodes="$*,$exitcode" + echo "$label=$exitcodes" >> $filename + # not checking for bad/missing exit code here. main + # loop will do that. + ;; + *) echo "$0: Unrecognised status code $rule"; exit 2;; + esac +done