]>
Commit | Line | Data |
---|---|---|
d8f933bd IB |
1 | #!/bin/bash |
2 | # | |
3 | # check_nrdp.sh | |
4 | # | |
5 | # Copyright (c) 2010-2017 - Nagios Enterprises, LLC. | |
6 | # Written by: Scott Wilkerson (nagios@nagios.org) | |
7 | # | |
8 | # 2017-09-25 Troy Lea aka BOX293 | |
9 | # - Fixed script not working with arguments when run as a cron job | |
10 | # or if being used as a nagios command like obsessive compulsive. | |
11 | # ... "if [ ! -t 0 ]" was the reason why. | |
12 | # 2017-12-08 Jørgen van der Meulen (Conclusion Xforce) | |
13 | # - Fixed typo in NRDP abbreviation | |
14 | ||
15 | ||
16 | PROGNAME=$(basename $0) | |
17 | RELEASE="Revision 0.6.1" | |
18 | ||
19 | print_release() { | |
20 | echo "$RELEASE" | |
21 | } | |
22 | ||
23 | print_usage() { | |
24 | echo "" | |
25 | echo "$PROGNAME $RELEASE - Send NRDP script for Nagios" | |
26 | echo "" | |
27 | echo "Usage: send_nrdp.sh -u URL -t token [options]" | |
28 | echo "" | |
29 | echo "Usage: $PROGNAME -h display help" | |
30 | echo "" | |
31 | } | |
32 | ||
33 | print_help() { | |
34 | print_usage | |
35 | echo "" | |
36 | echo "This script is used to send NRDP data to a Nagios server" | |
37 | echo "" | |
38 | echo "Required:" | |
39 | echo " -u"," URL of NRDP server. Usually http://<IP_ADDRESS>/nrdp/" | |
40 | echo " -t"," Shared token. Must be the same token set in NRDP Server" | |
41 | echo "" | |
42 | echo "Options:" | |
43 | echo " Single Check:" | |
44 | echo " -H host name" | |
45 | echo " -s service name" | |
46 | echo " -S State" | |
47 | echo " -o output" | |
48 | echo "" | |
49 | echo " STDIN:" | |
50 | echo " [-d delimiter] (default -d \"\\t\")" | |
51 | echo " With only the required parameters $PROGNAME is capable of" | |
52 | echo " processing data piped to it either from a file or other" | |
53 | echo " process. By default, we use \t as the delimiter however this" | |
54 | echo " may be specified with the -d option data should be in the" | |
55 | echo " following formats one entry per line." | |
56 | echo " For Host checks:" | |
57 | echo " hostname State output" | |
58 | echo " For Service checks" | |
59 | echo " hostname servicename State output" | |
60 | echo "" | |
61 | echo " File:" | |
62 | echo " -f /full/path/to/file" | |
63 | echo " This file will be sent to the NRDP server specified in -u" | |
64 | echo " The file should be an XML file in the following format" | |
65 | echo " ##################################################" | |
66 | echo "" | |
67 | echo " <?xml version='1.0'?>" | |
68 | echo " <checkresults>" | |
69 | echo " <checkresult type=\"host\" checktype=\"1\">" | |
70 | echo " <hostname>YOUR_HOSTNAME</hostname>" | |
71 | echo " <state>0</state>" | |
72 | echo " <output>OK|perfdata=1.00;5;10;0</output>" | |
73 | echo " </checkresult>" | |
74 | echo " <checkresult type=\"service\" checktype=\"1\">" | |
75 | echo " <hostname>YOUR_HOSTNAME</hostname>" | |
76 | echo " <servicename>YOUR_SERVICENAME</servicename>" | |
77 | echo " <state>0</state>" | |
78 | echo " <output>OK|perfdata=1.00;5;10;0</output>" | |
79 | echo " </checkresult>" | |
80 | echo " </checkresults>" | |
81 | echo " ##################################################" | |
82 | echo "" | |
83 | echo " Directory:" | |
84 | echo " -D /path/to/temp/dir" | |
85 | echo " This is a directory that contains XML files in the format" | |
86 | echo " above. Additionally, if the -d flag is specified, $PROGNAME" | |
87 | echo " will create temp files here if the server could not be reached." | |
88 | echo " On additional calls with the same -D path, if a connection to" | |
89 | echo " the server is successful, all temp files will be sent." | |
90 | exit 0 | |
91 | } | |
92 | ||
93 | send_data() { | |
94 | pdata="token=$token&cmd=submitcheck" | |
95 | if [ $file ]; then | |
96 | fdata="--data-urlencode XMLDATA@$file" | |
97 | rslt=`curl -f --silent --insecure -d "$pdata" $fdata "$url/"` | |
98 | else | |
99 | pdata="$pdata&XMLDATA=$1" | |
100 | rslt=`curl -f --silent --insecure -d "$pdata" "$url/"` | |
101 | fi | |
102 | ||
103 | ret=$? | |
104 | ||
105 | status=`echo $rslt | sed -n 's|.*<status>\(.*\)</status>.*|\1|p'` | |
106 | message=`echo $rslt | sed -n 's|.*<message>\(.*\)</message>.*|\1|p'` | |
107 | if [ $ret != 0 ];then | |
108 | echo "ERROR: could not connect to NRDP server at $url" | |
109 | # verify we are not processing the directory already and then write to the directory | |
110 | if [ ! "$2" ] && [ $directory ];then | |
111 | if [ ! -d "$directory" ];then | |
112 | mkdir -p "$directory" | |
113 | fi | |
114 | # This is where we write to the tmp directory | |
115 | echo $xml > `mktemp $directory/nrdp.XXXXXX` | |
116 | fi | |
117 | exit 1 | |
118 | fi | |
119 | ||
120 | if [ "$status" != "0" ];then | |
121 | # This means we couldn't connect to NRPD server | |
122 | echo "ERROR: The NRDP Server said $message" | |
123 | # verify we are not processing the directory already and then write to the directory | |
124 | if [ ! "$2" ] && [ $directory ];then | |
125 | if [ ! -d "$directory" ];then | |
126 | mkdir -p "$directory" | |
127 | fi | |
128 | # This is where we write to the tmp directory | |
129 | echo $xml > `mktemp $directory/nrdp.XXXXXX` | |
130 | fi | |
131 | ||
132 | exit 2 | |
133 | fi | |
134 | ||
135 | # If this was a directory call and was successful, remove the file | |
136 | if [ $2 ] && [ "$status" == "0" ];then | |
137 | rm -f "$2" | |
138 | fi | |
139 | ||
140 | # If we weren't successful error | |
141 | if [ $ret != 0 ];then | |
142 | echo "exited with error "$ret | |
143 | exit $ret | |
144 | fi | |
145 | } | |
146 | ||
147 | # Parse parameters | |
148 | url="<%= @naemon_url %>" | |
149 | token="<%= @naemon_token %>" | |
150 | ||
151 | while getopts "u:t:H:s:S:o:f:d:c:D:hv" option | |
152 | do | |
153 | case $option in | |
154 | u) url=$OPTARG ;; | |
155 | t) token=$OPTARG ;; | |
156 | H) host=$OPTARG ;; | |
157 | s) service=$OPTARG ;; | |
158 | S) State=$OPTARG ;; | |
159 | o) output=$OPTARG ;; | |
160 | f) file=$OPTARG ;; | |
161 | d) delim=$OPTARG ;; | |
162 | c) checktype=$OPTARG ;; | |
163 | D) directory=$OPTARG ;; | |
164 | h) print_help 0;; | |
165 | v) print_release | |
166 | exit 0 ;; | |
167 | esac | |
168 | done | |
169 | ||
170 | if [ ! $checktype ]; then | |
171 | checktype=1 | |
172 | fi | |
173 | if [ ! $delim ]; then | |
174 | delim=`echo -e "\t"` | |
175 | fi | |
176 | ||
177 | if [ "x$url" == "x" -o "x$token" == "x" ] | |
178 | then | |
179 | echo "Usage: send_nrdp -u url -t token" | |
180 | exit 1 | |
181 | fi | |
182 | # detecting curl | |
183 | if [[ `which curl` =~ "/curl" ]] | |
184 | then curl=1; | |
185 | fi | |
186 | ||
187 | if [[ ! $curl ]]; | |
188 | then | |
189 | echo "Either curl or wget are required to run $PROGNAME" | |
190 | exit 1 | |
191 | fi | |
192 | ||
193 | checkcount=0 | |
194 | ||
195 | if [ $host ]; then | |
196 | xml="" | |
197 | # we are not getting piped results | |
198 | if [ "$host" == "" ] || [ "$State" == "" ]; then | |
199 | echo "You must provide a host -H and State -S" | |
200 | exit 2 | |
201 | fi | |
202 | if [ "$service" != "" ]; then | |
203 | xml="$xml<checkresult type='service' checktype='$checktype'><servicename>$service</servicename>" | |
204 | else | |
205 | xml="$xml<checkresult type='host' checktype='$checktype'>" | |
206 | fi | |
207 | ||
208 | # urlencode XML special chars | |
209 | output=${output//&/%26} | |
210 | output=${output//</%3C} | |
211 | output=${output//>/%3E} | |
212 | ||
213 | xml="$xml<hostname>$host</hostname><state>$State</state><output><![CDATA["$output"]]></output></checkresult>" | |
214 | checkcount=1 | |
215 | fi | |
216 | ||
217 | # If only url and token have been provided then it is assumed that data is being piped | |
218 | ######################## | |
219 | if [[ ! $host && ! $State && ! $file && ! $directory ]]; then | |
220 | xml="" | |
221 | # we know we are being piped results | |
222 | IFS=$delim | |
223 | ||
224 | while read -r line ; do | |
225 | arr=($line) | |
226 | if [ ${#arr[@]} != 0 ];then | |
227 | if [[ ${#arr[@]} < 3 ]] || [[ ${#arr[@]} > 4 ]];then | |
228 | echo "ERROR: STDIN must be either 3 or 4 fields long, I found "${#arr[@]} | |
229 | else | |
230 | if [ ${#arr[@]} == 4 ]; then | |
231 | xml="$xml<checkresult type='service' checktype='$checktype'> | |
232 | <servicename>${arr[1]}</servicename> | |
233 | <hostname>${arr[0]}</hostname> | |
234 | <state>${arr[2]}</state> | |
235 | <output>${arr[3]}</output>" | |
236 | else | |
237 | xml="$xml<checkresult type='host' checktype='$checktype'> | |
238 | <hostname>${arr[0]}</hostname> | |
239 | <state>${arr[1]}</state> | |
240 | <output>${arr[2]}</output>" | |
241 | fi | |
242 | ||
243 | xml="$xml</checkresult>" | |
244 | checkcount=$[checkcount+1] | |
245 | fi | |
246 | fi | |
247 | done | |
248 | IFS=" " | |
249 | fi | |
250 | ||
251 | if [ $file ]; then | |
252 | xml=`cat $file` | |
253 | send_data "$xml" | |
254 | fi | |
255 | ||
256 | if [ $directory ]; then | |
257 | #echo "Processing directory..." | |
258 | for f in `ls $directory` | |
259 | do | |
260 | #echo "Processing $f file..." | |
261 | # take action on each file. $f store current file name | |
262 | xml=`cat $directory/$f` | |
263 | send_data "$xml" "$directory/$f" | |
264 | done | |
265 | fi | |
266 | ||
267 | if [ "x$file" == "x" ] && [ "x$directory" == "x" ]; then | |
268 | xml="<?xml version='1.0'?><checkresults>$xml</checkresults>" | |
269 | send_data "$xml" | |
270 | echo "Sent $checkcount checks to $url" | |
271 | fi |