1 /*****************************************************************************
3 * Nagios check_real plugin
6 * Copyright (c) 2000-2007 Nagios Plugins Development Team
8 * Last Modified: $Date$
12 * This file contains the check_real plugin
14 * This plugin tests the REAL service on the specified host.
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 *****************************************************************************/
34 const char *progname
= "check_real";
35 const char *revision
= "$Revision$";
36 const char *copyright
= "2000-2007";
37 const char *email
= "nagiosplug-devel@lists.sourceforge.net";
47 #define EXPECT "RTSP/1."
50 int process_arguments (int, char **);
51 int validate_arguments (void);
52 void print_help (void);
53 void print_usage (void);
55 int server_port
= PORT
;
58 char *server_url
= NULL
;
61 int check_warning_time
= FALSE
;
62 int critical_time
= 0;
63 int check_critical_time
= FALSE
;
69 main (int argc
, char **argv
)
72 int result
= STATE_UNKNOWN
;
73 char buffer
[MAX_INPUT_BUFFER
];
74 char *status_line
= NULL
;
76 setlocale (LC_ALL
, "");
77 bindtextdomain (PACKAGE
, LOCALEDIR
);
80 /* Parse extra opts if any */
81 argv
=np_extra_opts (&argc
, argv
, progname
);
83 if (process_arguments (argc
, argv
) == ERROR
)
84 usage4 (_("Could not parse arguments"));
86 /* initialize alarm signal handling */
87 signal (SIGALRM
, socket_timeout_alarm_handler
);
89 /* set socket timeout */
90 alarm (socket_timeout
);
93 /* try to connect to the host at the given port number */
94 if (my_tcp_connect (server_address
, server_port
, &sd
) != STATE_OK
)
95 die (STATE_CRITICAL
, _("Unable to connect to %s on port %d\n"),
96 server_address
, server_port
);
98 /* Part I - Server Check */
100 /* send the OPTIONS request */
101 sprintf (buffer
, "OPTIONS rtsp://%s:%d RTSP/1.0\r\n", host_name
, server_port
);
102 result
= send (sd
, buffer
, strlen (buffer
), 0);
104 /* send the header sync */
105 sprintf (buffer
, "CSeq: 1\r\n");
106 result
= send (sd
, buffer
, strlen (buffer
), 0);
108 /* send a newline so the server knows we're done with the request */
109 sprintf (buffer
, "\r\n");
110 result
= send (sd
, buffer
, strlen (buffer
), 0);
112 /* watch for the REAL connection string */
113 result
= recv (sd
, buffer
, MAX_INPUT_BUFFER
- 1, 0);
115 /* return a CRITICAL status if we couldn't read any data */
117 die (STATE_CRITICAL
, _("No data received from %s\n"), host_name
);
119 /* make sure we find the response we are looking for */
120 if (!strstr (buffer
, server_expect
)) {
121 if (server_port
== PORT
)
122 printf ("%s\n", _("Invalid REAL response received from host"));
124 printf (_("Invalid REAL response received from host on port %d\n"),
128 /* else we got the REAL string, so check the return code */
134 status_line
= (char *) strtok (buffer
, "\n");
136 if (strstr (status_line
, "200"))
139 /* client errors result in a warning state */
140 else if (strstr (status_line
, "400"))
141 result
= STATE_WARNING
;
142 else if (strstr (status_line
, "401"))
143 result
= STATE_WARNING
;
144 else if (strstr (status_line
, "402"))
145 result
= STATE_WARNING
;
146 else if (strstr (status_line
, "403"))
147 result
= STATE_WARNING
;
148 else if (strstr (status_line
, "404"))
149 result
= STATE_WARNING
;
151 /* server errors result in a critical state */
152 else if (strstr (status_line
, "500"))
153 result
= STATE_CRITICAL
;
154 else if (strstr (status_line
, "501"))
155 result
= STATE_CRITICAL
;
156 else if (strstr (status_line
, "502"))
157 result
= STATE_CRITICAL
;
158 else if (strstr (status_line
, "503"))
159 result
= STATE_CRITICAL
;
162 result
= STATE_UNKNOWN
;
165 /* Part II - Check stream exists and is ok */
166 if ((result
== STATE_OK
)&& (server_url
!= NULL
) ) {
168 /* Part I - Server Check */
170 /* send the OPTIONS request */
171 sprintf (buffer
, "DESCRIBE rtsp://%s:%d%s RTSP/1.0\n", host_name
,
172 server_port
, server_url
);
173 result
= send (sd
, buffer
, strlen (buffer
), 0);
175 /* send the header sync */
176 sprintf (buffer
, "CSeq: 2\n");
177 result
= send (sd
, buffer
, strlen (buffer
), 0);
179 /* send a newline so the server knows we're done with the request */
180 sprintf (buffer
, "\n");
181 result
= send (sd
, buffer
, strlen (buffer
), 0);
183 /* watch for the REAL connection string */
184 result
= recv (sd
, buffer
, MAX_INPUT_BUFFER
- 1, 0);
186 /* return a CRITICAL status if we couldn't read any data */
188 printf (_("No data received from host\n"));
189 result
= STATE_CRITICAL
;
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"));
197 printf (_("Invalid REAL response received from host on port %d\n"),
202 /* else we got the REAL string, so check the return code */
208 status_line
= (char *) strtok (buffer
, "\n");
210 if (strstr (status_line
, "200"))
213 /* client errors result in a warning state */
214 else if (strstr (status_line
, "400"))
215 result
= STATE_WARNING
;
216 else if (strstr (status_line
, "401"))
217 result
= STATE_WARNING
;
218 else if (strstr (status_line
, "402"))
219 result
= STATE_WARNING
;
220 else if (strstr (status_line
, "403"))
221 result
= STATE_WARNING
;
222 else if (strstr (status_line
, "404"))
223 result
= STATE_WARNING
;
225 /* server errors result in a critical state */
226 else if (strstr (status_line
, "500"))
227 result
= STATE_CRITICAL
;
228 else if (strstr (status_line
, "501"))
229 result
= STATE_CRITICAL
;
230 else if (strstr (status_line
, "502"))
231 result
= STATE_CRITICAL
;
232 else if (strstr (status_line
, "503"))
233 result
= STATE_CRITICAL
;
236 result
= STATE_UNKNOWN
;
242 if (result
== STATE_OK
) {
244 if (check_critical_time
== TRUE
245 && (end_time
- start_time
) > critical_time
) result
= STATE_CRITICAL
;
246 else if (check_warning_time
== TRUE
247 && (end_time
- start_time
) > warning_time
) result
=
250 /* Put some HTML in here to create a dynamic link */
251 printf (_("REAL %s - %d second response time\n"),
253 (int) (end_time
- start_time
));
256 printf ("%s\n", status_line
);
258 /* close the connection */
261 /* reset the alarm */
269 /* process command-line arguments */
271 process_arguments (int argc
, char **argv
)
276 static struct option longopts
[] = {
277 {"hostname", required_argument
, 0, 'H'},
278 {"IPaddress", required_argument
, 0, 'I'},
279 {"expect", required_argument
, 0, 'e'},
280 {"url", required_argument
, 0, 'u'},
281 {"port", required_argument
, 0, 'p'},
282 {"critical", required_argument
, 0, 'c'},
283 {"warning", required_argument
, 0, 'w'},
284 {"timeout", required_argument
, 0, 't'},
285 {"verbose", no_argument
, 0, 'v'},
286 {"version", no_argument
, 0, 'V'},
287 {"help", no_argument
, 0, 'h'},
294 for (c
= 1; c
< argc
; c
++) {
295 if (strcmp ("-to", argv
[c
]) == 0)
296 strcpy (argv
[c
], "-t");
297 else if (strcmp ("-wt", argv
[c
]) == 0)
298 strcpy (argv
[c
], "-w");
299 else if (strcmp ("-ct", argv
[c
]) == 0)
300 strcpy (argv
[c
], "-c");
304 c
= getopt_long (argc
, argv
, "+hvVI:H:e:u:p:w:c:t:", longopts
,
307 if (c
== -1 || c
== EOF
)
311 case 'I': /* hostname */
312 case 'H': /* hostname */
315 else if (is_host (optarg
))
316 server_address
= optarg
;
318 usage2 (_("Invalid hostname/address"), optarg
);
320 case 'e': /* string to expect in response header */
321 server_expect
= optarg
;
323 case 'u': /* server URL */
327 if (is_intpos (optarg
)) {
328 server_port
= atoi (optarg
);
331 usage4 (_("Port must be a positive integer"));
334 case 'w': /* warning time threshold */
335 if (is_intnonneg (optarg
)) {
336 warning_time
= atoi (optarg
);
337 check_warning_time
= TRUE
;
340 usage4 (_("Warning time must be a positive integer"));
343 case 'c': /* critical time threshold */
344 if (is_intnonneg (optarg
)) {
345 critical_time
= atoi (optarg
);
346 check_critical_time
= TRUE
;
349 usage4 (_("Critical time must be a positive integer"));
352 case 'v': /* verbose */
355 case 't': /* timeout */
356 if (is_intnonneg (optarg
)) {
357 socket_timeout
= atoi (optarg
);
360 usage4 (_("Timeout interval must be a positive integer"));
363 case 'V': /* version */
364 print_revision (progname
, revision
);
369 case '?': /* usage */
375 if (server_address
==NULL
&& argc
>c
) {
376 if (is_host (argv
[c
])) {
377 server_address
= argv
[c
++];
380 usage2 (_("Invalid hostname/address"), argv
[c
]);
384 if (server_address
==NULL
)
385 usage4 (_("You must provide a server to check"));
388 host_name
= strdup (server_address
);
390 if (server_expect
== NULL
)
391 server_expect
= strdup(EXPECT
);
393 return validate_arguments ();
399 validate_arguments (void)
410 asprintf (&myport
, "%d", PORT
);
412 print_revision (progname
, revision
);
414 printf ("Copyright (c) 1999 Pedro Leite <leite@cic.ua.pt>\n");
415 printf (COPYRIGHT
, copyright
, email
);
417 printf ("%s\n", _("This plugin tests the REAL service on the specified host."));
423 printf (_(UT_HELP_VRSN
));
424 printf (_(UT_EXTRA_OPTS
));
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"),
434 printf (_(UT_WARN_CRIT
));
436 printf (_(UT_TIMEOUT
), DEFAULT_SOCKET_TIMEOUT
);
438 printf (_(UT_VERBOSE
));
441 printf ("%s\n", _("This plugin will attempt to open an RTSP connection with the host."));
442 printf ("%s\n", _("Successul connects return STATE_OK, refusals and timeouts return"));
443 printf ("%s\n", _("STATE_CRITICAL, other errors return STATE_UNKNOWN. Successful connects,"));
444 printf ("%s\n", _("but incorrect reponse messages from the host result in STATE_WARNING return"));
445 printf ("%s\n", _("values."));
449 printf ("%s\n", _("Notes:"));
450 printf (_(UT_EXTRA_OPTS_NOTES
));
453 printf (_(UT_SUPPORT
));
461 printf (_("Usage:"));
462 printf ("%s -H host [-e expect] [-p port] [-w warn] [-c crit] [-t timeout] [-v]\n", progname
);