Not picking up solaris systems correctly for check_dhcp. Only
[monitoring-plugins.git] / plugins / check_real.c
blobd333c2a3c8509172b25dde74f37c07f5f0eff207
1 /*****************************************************************************
2 *
3 * Nagios check_real plugin
4 *
5 * License: GPL
6 * Copyright (c) 2000-2007 Nagios Plugins Development Team
7 *
8 * Last Modified: $Date$
9 *
10 * Description:
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/>.
30 * $Id$
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";
39 #include "common.h"
40 #include "netutils.h"
41 #include "utils.h"
43 enum {
44 PORT = 554
47 #define EXPECT "RTSP/1."
48 #define URL ""
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;
56 char *server_address;
57 char *host_name;
58 char *server_url = NULL;
59 char *server_expect;
60 int warning_time = 0;
61 int check_warning_time = FALSE;
62 int critical_time = 0;
63 int check_critical_time = FALSE;
64 int verbose = FALSE;
68 int
69 main (int argc, char **argv)
71 int sd;
72 int result = STATE_UNKNOWN;
73 char buffer[MAX_INPUT_BUFFER];
74 char *status_line = NULL;
76 setlocale (LC_ALL, "");
77 bindtextdomain (PACKAGE, LOCALEDIR);
78 textdomain (PACKAGE);
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);
91 time (&start_time);
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 */
116 if (result == -1)
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"));
123 else
124 printf (_("Invalid REAL response received from host on port %d\n"),
125 server_port);
127 else {
128 /* else we got the REAL string, so check the return code */
130 time (&end_time);
132 result = STATE_OK;
134 status_line = (char *) strtok (buffer, "\n");
136 if (strstr (status_line, "200"))
137 result = STATE_OK;
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;
161 else
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 */
187 if (result == -1) {
188 printf (_("No data received from host\n"));
189 result = STATE_CRITICAL;
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);
200 else {
202 /* else we got the REAL string, so check the return code */
204 time (&end_time);
206 result = STATE_OK;
208 status_line = (char *) strtok (buffer, "\n");
210 if (strstr (status_line, "200"))
211 result = STATE_OK;
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;
235 else
236 result = STATE_UNKNOWN;
241 /* Return results */
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 =
248 STATE_WARNING;
250 /* Put some HTML in here to create a dynamic link */
251 printf (_("REAL %s - %d second response time\n"),
252 state_text (result),
253 (int) (end_time - start_time));
255 else
256 printf ("%s\n", status_line);
258 /* close the connection */
259 close (sd);
261 /* reset the alarm */
262 alarm (0);
264 return result;
269 /* process command-line arguments */
271 process_arguments (int argc, char **argv)
273 int c;
275 int option = 0;
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'},
288 {0, 0, 0, 0}
291 if (argc < 2)
292 return ERROR;
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");
303 while (1) {
304 c = getopt_long (argc, argv, "+hvVI:H:e:u:p:w:c:t:", longopts,
305 &option);
307 if (c == -1 || c == EOF)
308 break;
310 switch (c) {
311 case 'I': /* hostname */
312 case 'H': /* hostname */
313 if (server_address)
314 break;
315 else if (is_host (optarg))
316 server_address = optarg;
317 else
318 usage2 (_("Invalid hostname/address"), optarg);
319 break;
320 case 'e': /* string to expect in response header */
321 server_expect = optarg;
322 break;
323 case 'u': /* server URL */
324 server_url = optarg;
325 break;
326 case 'p': /* port */
327 if (is_intpos (optarg)) {
328 server_port = atoi (optarg);
330 else {
331 usage4 (_("Port must be a positive integer"));
333 break;
334 case 'w': /* warning time threshold */
335 if (is_intnonneg (optarg)) {
336 warning_time = atoi (optarg);
337 check_warning_time = TRUE;
339 else {
340 usage4 (_("Warning time must be a positive integer"));
342 break;
343 case 'c': /* critical time threshold */
344 if (is_intnonneg (optarg)) {
345 critical_time = atoi (optarg);
346 check_critical_time = TRUE;
348 else {
349 usage4 (_("Critical time must be a positive integer"));
351 break;
352 case 'v': /* verbose */
353 verbose = TRUE;
354 break;
355 case 't': /* timeout */
356 if (is_intnonneg (optarg)) {
357 socket_timeout = atoi (optarg);
359 else {
360 usage4 (_("Timeout interval must be a positive integer"));
362 break;
363 case 'V': /* version */
364 print_revision (progname, revision);
365 exit (STATE_OK);
366 case 'h': /* help */
367 print_help ();
368 exit (STATE_OK);
369 case '?': /* usage */
370 usage5 ();
374 c = optind;
375 if (server_address==NULL && argc>c) {
376 if (is_host (argv[c])) {
377 server_address = argv[c++];
379 else {
380 usage2 (_("Invalid hostname/address"), argv[c]);
384 if (server_address==NULL)
385 usage4 (_("You must provide a server to check"));
387 if (host_name==NULL)
388 host_name = strdup (server_address);
390 if (server_expect == NULL)
391 server_expect = strdup(EXPECT);
393 return validate_arguments ();
399 validate_arguments (void)
401 return OK;
406 void
407 print_help (void)
409 char *myport;
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."));
419 printf ("\n\n");
421 print_usage ();
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"),
432 EXPECT);
434 printf (_(UT_WARN_CRIT));
436 printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
438 printf (_(UT_VERBOSE));
440 printf ("\n");
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."));
447 #ifdef NP_EXTRA_OPTS
448 printf ("\n");
449 printf ("%s\n", _("Notes:"));
450 printf (_(UT_EXTRA_OPTS_NOTES));
451 #endif
453 printf (_(UT_SUPPORT));
458 void
459 print_usage (void)
461 printf (_("Usage:"));
462 printf ("%s -H host [-e expect] [-p port] [-w warn] [-c crit] [-t timeout] [-v]\n", progname);