1 /*****************************************************************************
2 *
3 * Nagios check_real plugin
4 *
5 * License: GPL
6 * Copyright (c) 2000-2014 Nagios Plugins Development Team
7 *
8 * Description:
9 *
10 * This file contains the check_real plugin
11 *
12 * This plugin tests the REAL service on the specified host.
13 *
14 *
15 * This program is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation, either version 3 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 *
28 *
29 *****************************************************************************/
30
31 const char *progname = "check_real";
32 const char *copyright = "2000-2014";
33 const char *email = "devel@nagios-plugins.org";
34
35 #include "common.h"
36 #include "netutils.h"
37 #include "utils.h"
38
39 enum {
40 PORT = 554
41 };
42
43 #define EXPECT "RTSP/1."
44 #define URL ""
45
46 int process_arguments (int, char **);
47 int validate_arguments (void);
48 void print_help (void);
49 void print_usage (void);
50
51 int server_port = PORT;
52 char *server_address;
53 char *host_name;
54 char *server_url = NULL;
55 char *server_expect;
56 int warning_time = 0;
57 int check_warning_time = FALSE;
58 int critical_time = 0;
59 int check_critical_time = FALSE;
60 int verbose = FALSE;
61
62
63
64 int
main(int argc,char ** argv)65 main (int argc, char **argv)
66 {
67 int sd;
68 int result = STATE_UNKNOWN;
69 char buffer[MAX_INPUT_BUFFER];
70 char *status_line = NULL;
71
72 setlocale (LC_ALL, ""); setlocale(LC_NUMERIC, "C");
73 bindtextdomain (PACKAGE, LOCALEDIR);
74 textdomain (PACKAGE);
75
76 /* Parse extra opts if any */
77 argv=np_extra_opts (&argc, argv, progname);
78
79 if (process_arguments (argc, argv) == ERROR)
80 usage4 (_("Could not parse arguments"));
81
82 /* initialize alarm signal handling */
83 signal (SIGALRM, socket_timeout_alarm_handler);
84
85 /* set socket timeout */
86 alarm (timeout_interval);
87 time (&start_time);
88
89 /* try to connect to the host at the given port number */
90 if (my_tcp_connect (server_address, server_port, &sd) != STATE_OK)
91 die (STATE_CRITICAL, _("Unable to connect to %s on port %d\n"),
92 server_address, server_port);
93
94 /* Part I - Server Check */
95
96 /* send the OPTIONS request */
97 sprintf (buffer, "OPTIONS rtsp://%s:%d RTSP/1.0\r\n", host_name, server_port);
98 if (send (sd, buffer, strlen (buffer), 0) == -1)
99 die (STATE_CRITICAL, _("Can not send data to %s\n"), host_name);
100
101 /* send the header sync */
102 sprintf (buffer, "CSeq: 1\r\n");
103 if (send (sd, buffer, strlen (buffer), 0) == -1)
104 die (STATE_CRITICAL, _("Can not send data to %s\n"), host_name);
105
106 /* send a newline so the server knows we're done with the request */
107 sprintf (buffer, "\r\n");
108 if (send (sd, buffer, strlen (buffer), 0) == -1)
109 die (STATE_CRITICAL, _("Can not send data to %s\n"), host_name);
110
111 /* watch for the REAL connection string */
112 if (recv (sd, buffer, MAX_INPUT_BUFFER - 1, 0) == -1) {
113 /* return a CRITICAL status if we couldn't read any data */
114 die (STATE_CRITICAL, _("No data received from %s\n"), host_name);
115 }
116
117 /* make sure we find the response we are looking for */
118 if (!strstr (buffer, server_expect)) {
119 if (server_port == PORT)
120 printf ("%s\n", _("Invalid REAL response received from host"));
121 else
122 printf (_("Invalid REAL response received from host on port %d\n"),
123 server_port);
124 }
125 else {
126 /* else we got the REAL string, so check the return code */
127
128 time (&end_time);
129
130 status_line = (char *) strtok (buffer, "\n");
131
132 if (strstr (status_line, "200"))
133 result = STATE_OK;
134
135 /* client errors result in a warning state */
136 else if (strstr (status_line, "400"))
137 result = STATE_WARNING;
138 else if (strstr (status_line, "401"))
139 result = STATE_WARNING;
140 else if (strstr (status_line, "402"))
141 result = STATE_WARNING;
142 else if (strstr (status_line, "403"))
143 result = STATE_WARNING;
144 else if (strstr (status_line, "404"))
145 result = STATE_WARNING;
146
147 /* server errors result in a critical state */
148 else if (strstr (status_line, "500"))
149 result = STATE_CRITICAL;
150 else if (strstr (status_line, "501"))
151 result = STATE_CRITICAL;
152 else if (strstr (status_line, "502"))
153 result = STATE_CRITICAL;
154 else if (strstr (status_line, "503"))
155 result = STATE_CRITICAL;
156
157 else
158 result = STATE_UNKNOWN;
159 }
160
161 /* Part II - Check stream exists and is ok */
162 if ((result == STATE_OK ) && (server_url != NULL) ) {
163
164 /* Part I - Server Check */
165
166 /* send the DESCRIBE request */
167 sprintf (buffer, "DESCRIBE rtsp://%s:%d%s RTSP/1.0\r\n", host_name,
168 server_port, server_url);
169 if (send (sd, buffer, strlen (buffer), 0) == -1)
170 die (STATE_CRITICAL, _("Can not send data to %s\n"), host_name);
171
172 /* send the header sync */
173 sprintf (buffer, "CSeq: 2\r\n");
174 if (send (sd, buffer, strlen (buffer), 0) == -1)
175 die (STATE_CRITICAL, _("Can not send data to %s\n"), host_name);
176
177 /* send a newline so the server knows we're done with the request */
178 sprintf (buffer, "\r\n");
179 if (send (sd, buffer, strlen (buffer), 0) == -1)
180 die (STATE_CRITICAL, _("Can not send data to %s\n"), host_name);
181
182 /* watch for the REAL connection string */
183 result = recv (sd, buffer, MAX_INPUT_BUFFER - 1, 0);
184 buffer[result] = '\0'; /* null terminate received buffer */
185
186 /* return a CRITICAL status if we couldn't read any data */
187 if (result == -1) {
188 printf (_("No data received from host\n"));
189 result = STATE_CRITICAL;
190 }
191 else {
192 /* make sure we find the response we are looking for */
193 if (!strstr (buffer, server_expect)) {
194 if (server_port == PORT)
195 printf ("%s\n", _("Invalid REAL response received from host"));
196 else
197 printf (_("Invalid REAL response received from host on port %d\n"),
198 server_port);
199 }
200 else {
201
202 /* else we got the REAL string, so check the return code */
203
204 time (&end_time);
205
206 status_line = (char *) strtok (buffer, "\n");
207
208 if (strstr (status_line, "200"))
209 result = STATE_OK;
210
211 /* client errors result in a warning state */
212 else if (strstr (status_line, "400"))
213 result = STATE_WARNING;
214 else if (strstr (status_line, "401"))
215 result = STATE_WARNING;
216 else if (strstr (status_line, "402"))
217 result = STATE_WARNING;
218 else if (strstr (status_line, "403"))
219 result = STATE_WARNING;
220 else if (strstr (status_line, "404"))
221 result = STATE_WARNING;
222
223 /* server errors result in a critical state */
224 else if (strstr (status_line, "500"))
225 result = STATE_CRITICAL;
226 else if (strstr (status_line, "501"))
227 result = STATE_CRITICAL;
228 else if (strstr (status_line, "502"))
229 result = STATE_CRITICAL;
230 else if (strstr (status_line, "503"))
231 result = STATE_CRITICAL;
232
233 else
234 result = STATE_UNKNOWN;
235 }
236 }
237 }
238
239 /* Return results */
240 if (result == STATE_OK) {
241
242 if (check_critical_time == TRUE
243 && (end_time - start_time) > critical_time) result = STATE_CRITICAL;
244 else if (check_warning_time == TRUE
245 && (end_time - start_time) > warning_time) result =
246 STATE_WARNING;
247
248 /* Put some HTML in here to create a dynamic link */
249 printf (_("REAL %s - %d second response time\n"),
250 state_text (result),
251 (int) (end_time - start_time));
252 }
253 else
254 printf ("%s\n", status_line);
255
256 /* close the connection */
257 close (sd);
258
259 /* reset the alarm */
260 alarm (0);
261
262 return result;
263 }
264
265
266
267 /* process command-line arguments */
268 int
process_arguments(int argc,char ** argv)269 process_arguments (int argc, char **argv)
270 {
271 int c;
272
273 int option = 0;
274 static struct option longopts[] = {
275 {"hostname", required_argument, 0, 'H'},
276 {"IPaddress", required_argument, 0, 'I'},
277 {"expect", required_argument, 0, 'e'},
278 {"url", required_argument, 0, 'u'},
279 {"port", required_argument, 0, 'p'},
280 {"critical", required_argument, 0, 'c'},
281 {"warning", required_argument, 0, 'w'},
282 {"timeout", required_argument, 0, 't'},
283 {"verbose", no_argument, 0, 'v'},
284 {"version", no_argument, 0, 'V'},
285 {"help", no_argument, 0, 'h'},
286 {0, 0, 0, 0}
287 };
288
289 if (argc < 2)
290 return ERROR;
291
292 for (c = 1; c < argc; c++) {
293 if (strcmp ("-to", argv[c]) == 0)
294 strcpy (argv[c], "-t");
295 else if (strcmp ("-wt", argv[c]) == 0)
296 strcpy (argv[c], "-w");
297 else if (strcmp ("-ct", argv[c]) == 0)
298 strcpy (argv[c], "-c");
299 }
300
301 while (1) {
302 c = getopt_long (argc, argv, "+hvVI:H:e:u:p:w:c:t:", longopts,
303 &option);
304
305 if (c == -1 || c == EOF)
306 break;
307
308 switch (c) {
309 case 'I': /* hostname */
310 case 'H': /* hostname */
311 if (server_address)
312 break;
313 else if (is_host (optarg))
314 server_address = optarg;
315 else
316 usage2 (_("Invalid hostname/address"), optarg);
317 break;
318 case 'e': /* string to expect in response header */
319 server_expect = optarg;
320 break;
321 case 'u': /* server URL */
322 server_url = optarg;
323 break;
324 case 'p': /* port */
325 if (is_intpos (optarg)) {
326 server_port = atoi (optarg);
327 }
328 else {
329 usage4 (_("Port must be a positive integer"));
330 }
331 break;
332 case 'w': /* warning time threshold */
333 if (is_intnonneg (optarg)) {
334 warning_time = atoi (optarg);
335 check_warning_time = TRUE;
336 }
337 else {
338 usage4 (_("Warning time must be a positive integer"));
339 }
340 break;
341 case 'c': /* critical time threshold */
342 if (is_intnonneg (optarg)) {
343 critical_time = atoi (optarg);
344 check_critical_time = TRUE;
345 }
346 else {
347 usage4 (_("Critical time must be a positive integer"));
348 }
349 break;
350 case 'v': /* verbose */
351 verbose = TRUE;
352 break;
353 case 't': /* timeout */
354 timeout_interval = parse_timeout_string (optarg);
355 break;
356 case 'V': /* version */
357 print_revision (progname, NP_VERSION);
358 exit (STATE_OK);
359 case 'h': /* help */
360 print_help ();
361 exit (STATE_OK);
362 case '?': /* usage */
363 usage5 ();
364 }
365 }
366
367 c = optind;
368 if (server_address==NULL && argc>c) {
369 if (is_host (argv[c])) {
370 server_address = argv[c++];
371 }
372 else {
373 usage2 (_("Invalid hostname/address"), argv[c]);
374 }
375 }
376
377 if (server_address==NULL)
378 usage4 (_("You must provide a server to check"));
379
380 if (host_name==NULL)
381 host_name = strdup (server_address);
382
383 if (server_expect == NULL)
384 server_expect = strdup(EXPECT);
385
386 return validate_arguments ();
387 }
388
389
390
391 int
validate_arguments(void)392 validate_arguments (void)
393 {
394 return OK;
395 }
396
397
398
399 void
print_help(void)400 print_help (void)
401 {
402 char *myport;
403 xasprintf (&myport, "%d", PORT);
404
405 print_revision (progname, NP_VERSION);
406
407 printf ("Copyright (c) 1999 Pedro Leite <leite@cic.ua.pt>\n");
408 printf (COPYRIGHT, copyright, email);
409
410 printf ("%s\n", _("This plugin tests the REAL service on the specified host."));
411
412 printf ("\n\n");
413
414 print_usage ();
415
416 printf (UT_HELP_VRSN);
417 printf (UT_EXTRA_OPTS);
418
419 printf (UT_HOST_PORT, 'p', myport);
420
421 printf (" %s\n", "-u, --url=STRING");
422 printf (" %s\n", _("Connect to this url"));
423 printf (" %s\n", "-e, --expect=STRING");
424 printf (_("String to expect in first line of server response (default: %s)\n"),
425 EXPECT);
426
427 printf (UT_WARN_CRIT);
428
429 printf (UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
430
431 printf (UT_VERBOSE);
432
433 printf ("\n");
434 printf ("%s\n", _("This plugin will attempt to open an RTSP connection with the host."));
435 printf ("%s\n", _("Successul connects return STATE_OK, refusals and timeouts return"));
436 printf ("%s\n", _("STATE_CRITICAL, other errors return STATE_UNKNOWN. Successful connects,"));
437 printf ("%s\n", _("but incorrect response messages from the host result in STATE_WARNING return"));
438 printf ("%s\n", _("values."));
439
440 printf (UT_SUPPORT);
441 }
442
443
444
445 void
print_usage(void)446 print_usage (void)
447 {
448 printf ("%s\n", _("Usage:"));
449 printf ("%s -H host [-e expect] [-p port] [-w warn] [-c crit] [-t timeout] [-v]\n", progname);
450 }
451