1 /******************************************************************************
3 * Nagios check_real plugin
6 * Copyright (c) 1999-2006 nagios-plugins 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 * 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.
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";
51 #define EXPECT "RTSP/1."
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
;
62 char *server_url
= NULL
;
65 int check_warning_time
= FALSE
;
66 int critical_time
= 0;
67 int check_critical_time
= FALSE
;
73 main (int argc
, char **argv
)
76 int result
= STATE_UNKNOWN
;
77 char buffer
[MAX_INPUT_BUFFER
];
78 char *status_line
= NULL
;
80 setlocale (LC_ALL
, "");
81 bindtextdomain (PACKAGE
, LOCALEDIR
);
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
);
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 */
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"));
125 printf (_("Invalid REAL response received from host on port %d\n"),
129 /* else we got the REAL string, so check the return code */
135 status_line
= (char *) strtok (buffer
, "\n");
137 if (strstr (status_line
, "200"))
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
;
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 */
189 printf (_("No data received from host\n"));
190 result
= STATE_CRITICAL
;
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"));
198 printf (_("Invalid REAL response received from host on port %d\n"),
203 /* else we got the REAL string, so check the return code */
209 status_line
= (char *) strtok (buffer
, "\n");
211 if (strstr (status_line
, "200"))
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
;
237 result
= STATE_UNKNOWN
;
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
=
251 /* Put some HTML in here to create a dynamic link */
252 printf (_("REAL %s - %d second response time\n"),
254 (int) (end_time
- start_time
));
257 printf ("%s\n", status_line
);
259 /* close the connection */
262 /* reset the alarm */
270 /* process command-line arguments */
272 process_arguments (int argc
, char **argv
)
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'},
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");
305 c
= getopt_long (argc
, argv
, "+hvVI:H:e:u:p:w:c:t:", longopts
,
308 if (c
== -1 || c
== EOF
)
312 case 'I': /* hostname */
313 case 'H': /* hostname */
316 else if (is_host (optarg
))
317 server_address
= optarg
;
319 usage2 (_("Invalid hostname/address"), optarg
);
321 case 'e': /* string to expect in response header */
322 server_expect
= optarg
;
324 case 'u': /* server URL */
328 if (is_intpos (optarg
)) {
329 server_port
= atoi (optarg
);
332 usage4 (_("Port must be a positive integer"));
335 case 'w': /* warning time threshold */
336 if (is_intnonneg (optarg
)) {
337 warning_time
= atoi (optarg
);
338 check_warning_time
= TRUE
;
341 usage4 (_("Warning time must be a positive integer"));
344 case 'c': /* critical time threshold */
345 if (is_intnonneg (optarg
)) {
346 critical_time
= atoi (optarg
);
347 check_critical_time
= TRUE
;
350 usage4 (_("Critical time must be a positive integer"));
353 case 'v': /* verbose */
356 case 't': /* timeout */
357 if (is_intnonneg (optarg
)) {
358 socket_timeout
= atoi (optarg
);
361 usage4 (_("Timeout interval must be a positive integer"));
364 case 'V': /* version */
365 print_revision (progname
, revision
);
370 case '?': /* usage */
376 if (server_address
==NULL
&& argc
>c
) {
377 if (is_host (argv
[c
])) {
378 server_address
= argv
[c
++];
381 usage2 (_("Invalid hostname/address"), argv
[c
]);
385 if (server_address
==NULL
)
386 usage4 (_("You must provide a server to check"));
389 host_name
= strdup (server_address
);
391 if (server_expect
== NULL
)
392 server_expect
= strdup(EXPECT
);
394 return validate_arguments ();
400 validate_arguments (void)
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."));
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"),
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
));
454 printf (_("Usage:"));
455 printf ("%s -H host [-e expect] [-p port] [-w warn] [-c crit] [-t timeout] [-v]\n", progname
);