Fix INADDR_NONE value (for systems which don't define it).
[monitoring-plugins.git] / plugins / check_real.c
blob0ccc1c098c6d8e85723a1006fe7d7942f040bd25
1 /******************************************************************************
3 * Nagios check_real plugin
5 * License: GPL
6 * Copyright (c) 1999-2006 nagios-plugins team
8 * Last Modified: $Date$
10 * Description:
12 * This file contains the check_real plugin
14 * This plugin tests the REAL service on the specified host.
17 * License Information:
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
34 * $Id$
36 ******************************************************************************/
38 const char *progname = "check_real";
39 const char *revision = "$Revision$";
40 const char *copyright = "2000-2006";
41 const char *email = "nagiosplug-devel@lists.sourceforge.net";
43 #include "common.h"
44 #include "netutils.h"
45 #include "utils.h"
47 enum {
48 PORT = 554
51 #define EXPECT "RTSP/1."
52 #define URL ""
54 int process_arguments (int, char **);
55 int validate_arguments (void);
56 void print_help (void);
57 void print_usage (void);
59 int server_port = PORT;
60 char *server_address;
61 char *host_name;
62 char *server_url = NULL;
63 char *server_expect;
64 int warning_time = 0;
65 int check_warning_time = FALSE;
66 int critical_time = 0;
67 int check_critical_time = FALSE;
68 int verbose = FALSE;
72 int
73 main (int argc, char **argv)
75 int sd;
76 int result = STATE_UNKNOWN;
77 char buffer[MAX_INPUT_BUFFER];
78 char *status_line = NULL;
80 setlocale (LC_ALL, "");
81 bindtextdomain (PACKAGE, LOCALEDIR);
82 textdomain (PACKAGE);
84 if (process_arguments (argc, argv) == ERROR)
85 usage4 (_("Could not parse arguments"));
87 /* initialize alarm signal handling */
88 signal (SIGALRM, socket_timeout_alarm_handler);
90 /* set socket timeout */
91 alarm (socket_timeout);
92 time (&start_time);
94 /* try to connect to the host at the given port number */
95 if (my_tcp_connect (server_address, server_port, &sd) != STATE_OK)
96 die (STATE_CRITICAL, _("Unable to connect to %s on port %d\n"),
97 server_address, server_port);
99 /* Part I - Server Check */
101 /* send the OPTIONS request */
102 sprintf (buffer, "OPTIONS rtsp://%s:%d RTSP/1.0\r\n", host_name, server_port);
103 result = send (sd, buffer, strlen (buffer), 0);
105 /* send the header sync */
106 sprintf (buffer, "CSeq: 1\r\n");
107 result = send (sd, buffer, strlen (buffer), 0);
109 /* send a newline so the server knows we're done with the request */
110 sprintf (buffer, "\r\n");
111 result = send (sd, buffer, strlen (buffer), 0);
113 /* watch for the REAL connection string */
114 result = recv (sd, buffer, MAX_INPUT_BUFFER - 1, 0);
116 /* return a CRITICAL status if we couldn't read any data */
117 if (result == -1)
118 die (STATE_CRITICAL, _("No data received from %s\n"), host_name);
120 /* make sure we find the response we are looking for */
121 if (!strstr (buffer, server_expect)) {
122 if (server_port == PORT)
123 printf ("%s\n", _("Invalid REAL response received from host"));
124 else
125 printf (_("Invalid REAL response received from host on port %d\n"),
126 server_port);
128 else {
129 /* else we got the REAL string, so check the return code */
131 time (&end_time);
133 result = STATE_OK;
135 status_line = (char *) strtok (buffer, "\n");
137 if (strstr (status_line, "200"))
138 result = STATE_OK;
140 /* client errors result in a warning state */
141 else if (strstr (status_line, "400"))
142 result = STATE_WARNING;
143 else if (strstr (status_line, "401"))
144 result = STATE_WARNING;
145 else if (strstr (status_line, "402"))
146 result = STATE_WARNING;
147 else if (strstr (status_line, "403"))
148 result = STATE_WARNING;
149 else if (strstr (status_line, "404"))
150 result = STATE_WARNING;
152 /* server errors result in a critical state */
153 else if (strstr (status_line, "500"))
154 result = STATE_CRITICAL;
155 else if (strstr (status_line, "501"))
156 result = STATE_CRITICAL;
157 else if (strstr (status_line, "502"))
158 result = STATE_CRITICAL;
159 else if (strstr (status_line, "503"))
160 result = STATE_CRITICAL;
162 else
163 result = STATE_UNKNOWN;
166 /* Part II - Check stream exists and is ok */
167 if ((result == STATE_OK )&& (server_url != NULL) ) {
169 /* Part I - Server Check */
171 /* send the OPTIONS request */
172 sprintf (buffer, "DESCRIBE rtsp://%s:%d%s RTSP/1.0\n", host_name,
173 server_port, server_url);
174 result = send (sd, buffer, strlen (buffer), 0);
176 /* send the header sync */
177 sprintf (buffer, "CSeq: 2\n");
178 result = send (sd, buffer, strlen (buffer), 0);
180 /* send a newline so the server knows we're done with the request */
181 sprintf (buffer, "\n");
182 result = send (sd, buffer, strlen (buffer), 0);
184 /* watch for the REAL connection string */
185 result = recv (sd, buffer, MAX_INPUT_BUFFER - 1, 0);
187 /* return a CRITICAL status if we couldn't read any data */
188 if (result == -1) {
189 printf (_("No data received from host\n"));
190 result = STATE_CRITICAL;
192 else {
193 /* make sure we find the response we are looking for */
194 if (!strstr (buffer, server_expect)) {
195 if (server_port == PORT)
196 printf ("%s\n", _("Invalid REAL response received from host"));
197 else
198 printf (_("Invalid REAL response received from host on port %d\n"),
199 server_port);
201 else {
203 /* else we got the REAL string, so check the return code */
205 time (&end_time);
207 result = STATE_OK;
209 status_line = (char *) strtok (buffer, "\n");
211 if (strstr (status_line, "200"))
212 result = STATE_OK;
214 /* client errors result in a warning state */
215 else if (strstr (status_line, "400"))
216 result = STATE_WARNING;
217 else if (strstr (status_line, "401"))
218 result = STATE_WARNING;
219 else if (strstr (status_line, "402"))
220 result = STATE_WARNING;
221 else if (strstr (status_line, "403"))
222 result = STATE_WARNING;
223 else if (strstr (status_line, "404"))
224 result = STATE_WARNING;
226 /* server errors result in a critical state */
227 else if (strstr (status_line, "500"))
228 result = STATE_CRITICAL;
229 else if (strstr (status_line, "501"))
230 result = STATE_CRITICAL;
231 else if (strstr (status_line, "502"))
232 result = STATE_CRITICAL;
233 else if (strstr (status_line, "503"))
234 result = STATE_CRITICAL;
236 else
237 result = STATE_UNKNOWN;
242 /* Return results */
243 if (result == STATE_OK) {
245 if (check_critical_time == TRUE
246 && (end_time - start_time) > critical_time) result = STATE_CRITICAL;
247 else if (check_warning_time == TRUE
248 && (end_time - start_time) > warning_time) result =
249 STATE_WARNING;
251 /* Put some HTML in here to create a dynamic link */
252 printf (_("REAL %s - %d second response time\n"),
253 state_text (result),
254 (int) (end_time - start_time));
256 else
257 printf ("%s\n", status_line);
259 /* close the connection */
260 close (sd);
262 /* reset the alarm */
263 alarm (0);
265 return result;
270 /* process command-line arguments */
272 process_arguments (int argc, char **argv)
274 int c;
276 int option = 0;
277 static struct option longopts[] = {
278 {"hostname", required_argument, 0, 'H'},
279 {"IPaddress", required_argument, 0, 'I'},
280 {"expect", required_argument, 0, 'e'},
281 {"url", required_argument, 0, 'u'},
282 {"port", required_argument, 0, 'p'},
283 {"critical", required_argument, 0, 'c'},
284 {"warning", required_argument, 0, 'w'},
285 {"timeout", required_argument, 0, 't'},
286 {"verbose", no_argument, 0, 'v'},
287 {"version", no_argument, 0, 'V'},
288 {"help", no_argument, 0, 'h'},
289 {0, 0, 0, 0}
292 if (argc < 2)
293 return ERROR;
295 for (c = 1; c < argc; c++) {
296 if (strcmp ("-to", argv[c]) == 0)
297 strcpy (argv[c], "-t");
298 else if (strcmp ("-wt", argv[c]) == 0)
299 strcpy (argv[c], "-w");
300 else if (strcmp ("-ct", argv[c]) == 0)
301 strcpy (argv[c], "-c");
304 while (1) {
305 c = getopt_long (argc, argv, "+hvVI:H:e:u:p:w:c:t:", longopts,
306 &option);
308 if (c == -1 || c == EOF)
309 break;
311 switch (c) {
312 case 'I': /* hostname */
313 case 'H': /* hostname */
314 if (server_address)
315 break;
316 else if (is_host (optarg))
317 server_address = optarg;
318 else
319 usage2 (_("Invalid hostname/address"), optarg);
320 break;
321 case 'e': /* string to expect in response header */
322 server_expect = optarg;
323 break;
324 case 'u': /* server URL */
325 server_url = optarg;
326 break;
327 case 'p': /* port */
328 if (is_intpos (optarg)) {
329 server_port = atoi (optarg);
331 else {
332 usage4 (_("Port must be a positive integer"));
334 break;
335 case 'w': /* warning time threshold */
336 if (is_intnonneg (optarg)) {
337 warning_time = atoi (optarg);
338 check_warning_time = TRUE;
340 else {
341 usage4 (_("Warning time must be a positive integer"));
343 break;
344 case 'c': /* critical time threshold */
345 if (is_intnonneg (optarg)) {
346 critical_time = atoi (optarg);
347 check_critical_time = TRUE;
349 else {
350 usage4 (_("Critical time must be a positive integer"));
352 break;
353 case 'v': /* verbose */
354 verbose = TRUE;
355 break;
356 case 't': /* timeout */
357 if (is_intnonneg (optarg)) {
358 socket_timeout = atoi (optarg);
360 else {
361 usage4 (_("Timeout interval must be a positive integer"));
363 break;
364 case 'V': /* version */
365 print_revision (progname, revision);
366 exit (STATE_OK);
367 case 'h': /* help */
368 print_help ();
369 exit (STATE_OK);
370 case '?': /* usage */
371 usage5 ();
375 c = optind;
376 if (server_address==NULL && argc>c) {
377 if (is_host (argv[c])) {
378 server_address = argv[c++];
380 else {
381 usage2 (_("Invalid hostname/address"), argv[c]);
385 if (server_address==NULL)
386 usage4 (_("You must provide a server to check"));
388 if (host_name==NULL)
389 host_name = strdup (server_address);
391 if (server_expect == NULL)
392 server_expect = strdup(EXPECT);
394 return validate_arguments ();
400 validate_arguments (void)
402 return OK;
407 void
408 print_help (void)
410 char *myport;
411 asprintf (&myport, "%d", PORT);
413 print_revision (progname, revision);
415 printf ("Copyright (c) 1999 Pedro Leite <leite@cic.ua.pt>\n");
416 printf (COPYRIGHT, copyright, email);
418 printf ("%s\n", _("This plugin tests the REAL service on the specified host."));
420 printf ("\n\n");
422 print_usage ();
424 printf (_(UT_HELP_VRSN));
426 printf (_(UT_HOST_PORT), 'p', myport);
428 printf (" %s\n", "-u, --url=STRING");
429 printf (" %s\n", _("Connect to this url"));
430 printf (" %s\n", "-e, --expect=STRING");
431 printf (_("String to expect in first line of server response (default: %s)\n"),
432 EXPECT);
434 printf (_(UT_WARN_CRIT));
436 printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
438 printf (_(UT_VERBOSE));
440 printf ("%s\n", _("This plugin will attempt to open an RTSP connection with the host."));
441 printf ("%s\n", _("Successul connects return STATE_OK, refusals and timeouts return"));
442 printf ("%s\n", _("STATE_CRITICAL, other errors return STATE_UNKNOWN. Successful connects,"));
443 printf ("%s\n", _("but incorrect reponse messages from the host result in STATE_WARNING return"));
444 printf ("%s\n", _("values."));
446 printf (_(UT_SUPPORT));
451 void
452 print_usage (void)
454 printf (_("Usage:"));
455 printf ("%s -H host [-e expect] [-p port] [-w warn] [-c crit] [-t timeout] [-v]\n", progname);