1 /*****************************************************************************
2 *
3 * CHECK_REAL.C
4 *
5 * Program: RealMedia plugin for Nagios
6 * License: GPL
7 * Copyright (c) 1999 Pedro Leite (leite@cic.ua.pt)
8 *
9 * Based on CHECK_HTTP.C
10 * Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)
11 *
12 * Last Modified: $Date$
13 *
14 * Command line: CHECK_REAL <host_address> [-e expect] [-u url] [-p port]
15 * [-hn host_name] [-wt warn_time] [-ct crit_time]
16 * [-to to_sec]
17 *
18 * Description:
19 *
20 * This plugin will attempt to open an RTSP connection with the host.
21 * Successul connects return STATE_OK, refusals and timeouts return
22 * STATE_CRITICAL, other errors return STATE_UNKNOWN. Successful connects,
23 * but incorrect reponse messages from the host result in STATE_WARNING return
24 * values. If you are checking a virtual server that uses "host headers"you
25 * must supply the FQDN (fully qualified domain name) as the [host_name]
26 * argument.
27 *
28 * License Information:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License
41 * along with this program; if not, write to the Free Software
42 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
43 *
44 ****************************************************************************/
46 #include "config.h"
47 #include "common.h"
48 #include "netutils.h"
49 #include "utils.h"
51 const char *progname = "check_real";
53 #define PORT 554
54 #define EXPECT "RTSP/1."
55 #define URL ""
57 int process_arguments (int, char **);
58 int validate_arguments (void);
59 int check_disk (int usp, int free_disk);
60 void print_help (void);
61 void print_usage (void);
63 int server_port = PORT;
64 char *server_address = "";
65 char *host_name = NULL;
66 char *server_url = NULL;
67 char *server_expect = EXPECT;
68 int warning_time = 0;
69 int check_warning_time = FALSE;
70 int critical_time = 0;
71 int check_critical_time = FALSE;
72 int verbose = FALSE;
74 int
75 main (int argc, char **argv)
76 {
77 int sd;
78 int result;
79 char buffer[MAX_INPUT_BUFFER];
80 char *status_line = NULL;
82 if (process_arguments (argc, argv) != OK)
83 usage ("Invalid command arguments supplied\n");
85 /* initialize alarm signal handling */
86 signal (SIGALRM, socket_timeout_alarm_handler);
88 /* set socket timeout */
89 alarm (socket_timeout);
90 time (&start_time);
92 /* try to connect to the host at the given port number */
93 if (my_tcp_connect (server_address, server_port, &sd) != STATE_OK)
94 terminate (STATE_CRITICAL, "Unable to connect to %s on port %d\n",
95 server_address, server_port);
97 /* Part I - Server Check */
99 /* send the OPTIONS request */
100 sprintf (buffer, "OPTIONS rtsp://%s:%d RTSP/1.0\n", host_name, server_port);
101 result = send (sd, buffer, strlen (buffer), 0);
103 /* send the header sync */
104 sprintf (buffer, "CSeq: 1\n");
105 result = send (sd, buffer, strlen (buffer), 0);
107 /* send a newline so the server knows we're done with the request */
108 sprintf (buffer, "\n");
109 result = send (sd, buffer, strlen (buffer), 0);
111 /* watch for the REAL connection string */
112 result = recv (sd, buffer, MAX_INPUT_BUFFER - 1, 0);
114 /* return a CRITICAL status if we couldn't read any data */
115 if (result == -1)
116 terminate (STATE_CRITICAL, "No data received from %s\n", host_name);
118 /* make sure we find the response we are looking for */
119 if (!strstr (buffer, server_expect)) {
120 if (server_port == PORT)
121 printf ("Invalid REAL response received from host\n");
122 else
123 printf ("Invalid REAL response received from host on port %d\n",
124 server_port);
125 }
126 else {
127 /* else we got the REAL string, so check the return code */
129 time (&end_time);
131 result = STATE_OK;
133 status_line = (char *) strtok (buffer, "\n");
135 if (strstr (status_line, "200"))
136 result = STATE_OK;
138 /* client errors result in a warning state */
139 else if (strstr (status_line, "400"))
140 result = STATE_WARNING;
141 else if (strstr (status_line, "401"))
142 result = STATE_WARNING;
143 else if (strstr (status_line, "402"))
144 result = STATE_WARNING;
145 else if (strstr (status_line, "403"))
146 result = STATE_WARNING;
147 else if (strstr (status_line, "404"))
148 result = STATE_WARNING;
150 /* server errors result in a critical state */
151 else if (strstr (status_line, "500"))
152 result = STATE_CRITICAL;
153 else if (strstr (status_line, "501"))
154 result = STATE_CRITICAL;
155 else if (strstr (status_line, "502"))
156 result = STATE_CRITICAL;
157 else if (strstr (status_line, "503"))
158 result = STATE_CRITICAL;
160 else
161 result = STATE_UNKNOWN;
162 }
164 /* Part II - Check stream exists and is ok */
165 if ((result == STATE_OK) && (server_url != NULL)) {
167 /* Part I - Server Check */
169 /* send the OPTIONS request */
170 sprintf (buffer, "DESCRIBE rtsp://%s:%d%s RTSP/1.0\n", host_name,
171 server_port, server_url);
172 result = send (sd, buffer, strlen (buffer), 0);
174 /* send the header sync */
175 sprintf (buffer, "CSeq: 2\n");
176 result = send (sd, buffer, strlen (buffer), 0);
178 /* send a newline so the server knows we're done with the request */
179 sprintf (buffer, "\n");
180 result = send (sd, buffer, strlen (buffer), 0);
182 /* watch for the REAL connection string */
183 result = recv (sd, buffer, MAX_INPUT_BUFFER - 1, 0);
185 /* return a CRITICAL status if we couldn't read any data */
186 if (result == -1) {
187 printf ("No data received from host\n");
188 result = STATE_CRITICAL;
189 }
190 else {
191 /* make sure we find the response we are looking for */
192 if (!strstr (buffer, server_expect)) {
193 if (server_port == PORT)
194 printf ("Invalid REAL response received from host\n");
195 else
196 printf ("Invalid REAL response received from host on port %d\n",
197 server_port);
198 }
199 else {
201 /* else we got the REAL string, so check the return code */
203 time (&end_time);
205 result = STATE_OK;
207 status_line = (char *) strtok (buffer, "\n");
209 if (strstr (status_line, "200"))
210 result = STATE_OK;
212 /* client errors result in a warning state */
213 else if (strstr (status_line, "400"))
214 result = STATE_WARNING;
215 else if (strstr (status_line, "401"))
216 result = STATE_WARNING;
217 else if (strstr (status_line, "402"))
218 result = STATE_WARNING;
219 else if (strstr (status_line, "403"))
220 result = STATE_WARNING;
221 else if (strstr (status_line, "404"))
222 result = STATE_WARNING;
224 /* server errors result in a critical state */
225 else if (strstr (status_line, "500"))
226 result = STATE_CRITICAL;
227 else if (strstr (status_line, "501"))
228 result = STATE_CRITICAL;
229 else if (strstr (status_line, "502"))
230 result = STATE_CRITICAL;
231 else if (strstr (status_line, "503"))
232 result = STATE_CRITICAL;
234 else
235 result = STATE_UNKNOWN;
236 }
237 }
238 }
240 /* Return results */
241 if (result == STATE_OK) {
243 if (check_critical_time == TRUE
244 && (end_time - start_time) > critical_time) result = STATE_CRITICAL;
245 else if (check_warning_time == TRUE
246 && (end_time - start_time) > warning_time) result =
247 STATE_WARNING;
249 /* Put some HTML in here to create a dynamic link */
250 printf ("REAL %s - %d second response time\n",
251 (result == STATE_OK) ? "ok" : "problem",
252 (int) (end_time - start_time));
253 }
254 else
255 printf ("%s\n", status_line);
257 /* close the connection */
258 close (sd);
260 /* reset the alarm */
261 alarm (0);
263 return result;
264 }
271 /* process command-line arguments */
272 int
273 process_arguments (int argc, char **argv)
274 {
275 int c;
277 #ifdef HAVE_GETOPT_H
278 int option_index = 0;
279 static struct option long_options[] = {
280 {"hostname", required_argument, 0, 'H'},
281 {"IPaddress", required_argument, 0, 'I'},
282 {"expect", required_argument, 0, 'e'},
283 {"url", required_argument, 0, 'u'},
284 {"port", required_argument, 0, 'p'},
285 {"critical", required_argument, 0, 'c'},
286 {"warning", required_argument, 0, 'w'},
287 {"timeout", required_argument, 0, 't'},
288 {"verbose", no_argument, 0, 'v'},
289 {"version", no_argument, 0, 'V'},
290 {"help", no_argument, 0, 'h'},
291 {0, 0, 0, 0}
292 };
293 #endif
295 if (argc < 2)
296 return ERROR;
298 for (c = 1; c < argc; c++) {
299 if (strcmp ("-to", argv[c]) == 0)
300 strcpy (argv[c], "-t");
301 else if (strcmp ("-wt", argv[c]) == 0)
302 strcpy (argv[c], "-w");
303 else if (strcmp ("-ct", argv[c]) == 0)
304 strcpy (argv[c], "-c");
305 }
307 while (1) {
308 #ifdef HAVE_GETOPT_H
309 c =
310 getopt_long (argc, argv, "+hVI:H:e:u:p:w:c:t:", long_options,
311 &option_index);
312 #else
313 c = getopt (argc, argv, "+?hVI:H:e:u:p:w:c:t");
314 #endif
316 if (c == -1 || c == EOF)
317 break;
319 switch (c) {
320 case 'I': /* hostname */
321 case 'H': /* hostname */
322 if (is_host (optarg)) {
323 server_address = optarg;
324 }
325 else {
326 usage ("Invalid host name\n");
327 }
328 break;
329 case 'e': /* string to expect in response header */
330 server_expect = optarg;
331 break;
332 case 'u': /* server URL */
333 server_url = optarg;
334 break;
335 case 'p': /* port */
336 if (is_intpos (optarg)) {
337 server_port = atoi (optarg);
338 }
339 else {
340 usage ("Server port must be a positive integer\n");
341 }
342 break;
343 case 'w': /* warning time threshold */
344 if (is_intnonneg (optarg)) {
345 warning_time = atoi (optarg);
346 check_warning_time = TRUE;
347 }
348 else {
349 usage ("Warning time must be a nonnegative integer\n");
350 }
351 break;
352 case 'c': /* critical time threshold */
353 if (is_intnonneg (optarg)) {
354 critical_time = atoi (optarg);
355 check_critical_time = TRUE;
356 }
357 else {
358 usage ("Critical time must be a nonnegative integer\n");
359 }
360 break;
361 case 'v': /* verbose */
362 verbose = TRUE;
363 break;
364 case 't': /* timeout */
365 if (is_intnonneg (optarg)) {
366 socket_timeout = atoi (optarg);
367 }
368 else {
369 usage ("Time interval must be a nonnegative integer\n");
370 }
371 break;
372 case 'V': /* version */
373 print_revision (progname, "$Revision$");
374 exit (STATE_OK);
375 case 'h': /* help */
376 print_help ();
377 exit (STATE_OK);
378 case '?': /* help */
379 usage ("Invalid argument\n");
380 }
381 }
383 c = optind;
384 if (strlen(server_address) == 0 && argc > c) {
385 if (is_host (argv[c])) {
386 server_address = argv[c++];
387 }
388 else {
389 usage ("Invalid host name");
390 }
391 }
393 return validate_arguments ();
394 }
400 int
401 validate_arguments (void)
402 {
403 return OK;
404 }
410 void
411 print_help (void)
412 {
413 print_revision (progname, "$Revision$");
414 printf
415 ("Copyright (c) 2000 Pedro Leite (leite@cic.ua.pt)/Karl DeBisschop\n\n"
416 "This plugin tests the REAL service on the specified host.\n\n");
417 print_usage ();
418 printf
419 ("\nOptions:\n"
420 " -H, --hostname=STRING or IPADDRESS\n"
421 " Check this server on the indicated host\n"
422 " -I, --IPaddress=STRING or IPADDRESS\n"
423 " Check server at this host address\n"
424 " -p, --port=INTEGER\n"
425 " Make connection on the indicated port (default: %d)\n"
426 " -u, --url=STRING\n"
427 " Connect to this url\n"
428 " -e, --expect=STRING\n"
429 " String to expect in first line of server response (default: %s)\n"
430 " -w, --warning=INTEGER\n"
431 " Seconds necessary to result in a warning status\n"
432 " -c, --critical=INTEGER\n"
433 " Seconds necessary to result in a critical status\n"
434 " -t, --timeout=INTEGER\n"
435 " Seconds before connection attempt times out (default: %d)\n"
436 " -v, --verbose\n"
437 " Print extra information (command-line use only)\n"
438 " -h, --help\n"
439 " Print detailed help screen\n"
440 " -V, --version\n"
441 " Print version information\n\n",
442 PORT, EXPECT, DEFAULT_SOCKET_TIMEOUT);
443 support ();
444 }
450 void
451 print_usage (void)
452 {
453 printf
454 ("Usage: %s -H host [-e expect] [-p port] [-w warn] [-c crit]\n"
455 " [-t timeout] [-v]\n"
456 " %s --help\n"
457 " %s --version\n", progname, progname, progname);
458 }
463 /*
464 // process command-line arguments
465 int
466 process_arguments (int argc, char **argv)
467 {
468 int x;
470 // no options were supplied
471 if (argc < 2)
472 return ERROR;
474 // first option is always the server name/address
475 strncpy (server_address, argv[1], sizeof (server_address) - 1);
476 server_address[sizeof (server_address) - 1] = 0;
478 // set the host name to the server address (until its overridden)
479 strcpy (host_name, server_address);
481 // process all remaining arguments
482 for (x = 3; x <= argc; x++)
483 {
485 // we got the string to expect from the server
486 if (!strcmp (argv[x - 1], "-e"))
487 {
488 if (x < argc)
489 {
490 strncpy (server_expect, argv[x], sizeof (server_expect) - 1);
491 server_expect[sizeof (server_expect) - 1] = 0;
492 x++;
493 }
494 else
495 return ERROR;
496 }
498 // we got the URL to check
499 else if (!strcmp (argv[x - 1], "-u"))
500 {
501 if (x < argc)
502 {
503 strncpy (server_url, argv[x], sizeof (server_url) - 1);
504 server_url[sizeof (server_url) - 1] = 0;
505 x++;
506 }
507 else
508 return ERROR;
509 }
511 // we go the host name to use in the host header
512 else if (!strcmp (argv[x - 1], "-hn"))
513 {
514 if (x < argc)
515 {
516 strncpy (host_name, argv[x], sizeof (host_name) - 1);
517 host_name[sizeof (host_name) - 1] = 0;
518 x++;
519 }
520 else
521 return ERROR;
522 }
524 // we got the port number to use
525 else if (!strcmp (argv[x - 1], "-p"))
526 {
527 if (x < argc)
528 {
529 server_port = atoi (argv[x]);
530 x++;
531 }
532 else
533 return ERROR;
534 }
536 // we got the socket timeout
537 else if (!strcmp (argv[x - 1], "-to"))
538 {
539 if (x < argc)
540 {
541 socket_timeout = atoi (argv[x]);
542 if (socket_timeout <= 0)
543 return ERROR;
544 x++;
545 }
546 else
547 return ERROR;
548 }
550 // we got the warning threshold time
551 else if (!strcmp (argv[x - 1], "-wt"))
552 {
553 if (x < argc)
554 {
555 warning_time = atoi (argv[x]);
556 check_warning_time = TRUE;
557 x++;
558 }
559 else
560 return ERROR;
561 }
563 // we got the critical threshold time
564 else if (!strcmp (argv[x - 1], "-ct"))
565 {
566 if (x < argc)
567 {
568 critical_time = atoi (argv[x]);
569 check_critical_time = TRUE;
570 x++;
571 }
572 else
573 return ERROR;
574 }
576 // else we got something else...
577 else
578 return ERROR;
579 }
581 return OK;
582 }
584 result = process_arguments (argc, argv);
586 if (result != OK)
587 {
589 printf ("Incorrect number of arguments supplied\n");
590 printf ("\n");
591 print_revision(argv[0],"$Revision$");
592 printf ("Copyright (c) 1999 Pedro Leite (leite@cic.ua.pt)\n");
593 printf ("Last Modified: 30-10-1999\n");
594 printf ("License: GPL\n");
595 printf ("\n");
596 printf ("Usage: %s <host_address> [-e expect] [-u url] [-p port] [-hn host_name] [-wt warn_time]\n",argv[0]);
597 printf(" [-ct crit_time] [-to to_sec] [-a auth]\n");
598 printf ("\n");
599 printf ("Options:\n");
600 printf (" [expect] = String to expect in first line of server response - default is \"%s\"\n", EXPECT);
601 printf (" [url] = Optional URL to GET - default is root document\n");
602 printf (" [port] = Optional port number to use - default is %d\n", PORT);
603 printf (" [host_name] = Optional host name argument to GET command - used for servers using host headers\n");
604 printf (" [warn_time] = Response time in seconds necessary to result in a warning status\n");
605 printf (" [crit_time] = Response time in seconds necessary to result in a critical status\n");
606 printf (" [to_sec] = Number of seconds before connection attempt times out - default is %d seconds\n", DEFAULT_SOCKET_TIMEOUT);
607 printf (" [auth] = Optional username:password for sites requiring basic authentication\n");
608 printf ("\n");
609 printf ("This plugin attempts to contact the REAL service on the specified host.\n");
610 printf ("If possible, supply an IP address for the host address, as this will bypass the DNS lookup.\n");
611 printf ("\n");
613 return STATE_UNKNOWN;
614 }
616 */