summaryrefslogtreecommitdiffstats
path: root/web/attachments/154472-nagios_plugins-check_test.patch
blob: 832c6a07693e281fc713ebba74ea1ab93a422380 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
--- ../../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 <svc> -H <host> -e <exitcode> -f <file> -ht -m <msg>
+ -E - exact entry including hostname is needed. Prevents matching
+      only on service_ name.
+ -H <hostname> - the hostname this test is running on. No default.
+ -e <exit code> - used like check dummy, always exits with this
+                  status code. Ignores the external file.
+ -f <filename> - the external file name used for determining exit
+                 code. Default: /tmp/check_test.
+ -h - This help text.
+ -s <service_name> - 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 <svc> -H <host> -e <exitcode> -f <file>"
+         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