Code

59ad7d0396bd994337bda387eaf348b641e7d53e
[nagiosplug.git] / plugins / check_real.c
1 /******************************************************************************
3  This program is free software; you can redistribute it and/or modify
4  it under the terms of the GNU General Public License as published by
5  the Free Software Foundation; either version 2 of the License, or
6  (at your option) any later version.
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  GNU General Public License for more details.
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  $Id$
18  
19 ******************************************************************************/
21 const char *progname = "check_real";
22 const char *revision = "$Revision$";
23 const char *copyright = "2000-2003";
24 const char *email = "nagiosplug-devel@lists.sourceforge.net";
26 #include "common.h"
27 #include "netutils.h"
28 #include "utils.h"
30 enum {
31         PORT    = 554
32 };
34 #define EXPECT  "RTSP/1."
35 #define URL     ""
37 int process_arguments (int, char **);
38 int validate_arguments (void);
39 void print_help (void);
40 void print_usage (void);
42 int server_port = PORT;
43 char *server_address;
44 char *host_name;
45 char *server_url = NULL;
46 char *server_expect;
47 int warning_time = 0;
48 int check_warning_time = FALSE;
49 int critical_time = 0;
50 int check_critical_time = FALSE;
51 int verbose = FALSE;
55 int
56 main (int argc, char **argv)
57 {
58         int sd;
59         int result;
60         char buffer[MAX_INPUT_BUFFER];
61         char *status_line = NULL;
63         setlocale (LC_ALL, "");
64         bindtextdomain (PACKAGE, LOCALEDIR);
65         textdomain (PACKAGE);
67         if (process_arguments (argc, argv) != OK)
68                 usage (_("check_real: could not parse arguments\n"));
70         /* initialize alarm signal handling */
71         signal (SIGALRM, socket_timeout_alarm_handler);
73         /* set socket timeout */
74         alarm (socket_timeout);
75         time (&start_time);
77         /* try to connect to the host at the given port number */
78         if (my_tcp_connect (server_address, server_port, &sd) != STATE_OK)
79                 die (STATE_CRITICAL, _("Unable to connect to %s on port %d\n"),
80                                                          server_address, server_port);
82         /* Part I - Server Check */
84         /* send the OPTIONS request */
85         sprintf (buffer, "OPTIONS rtsp://%s:%d RTSP/1.0\r\n", host_name, server_port);
86         result = send (sd, buffer, strlen (buffer), 0);
88         /* send the header sync */
89         sprintf (buffer, "CSeq: 1\r\n");
90         result = send (sd, buffer, strlen (buffer), 0);
92         /* send a newline so the server knows we're done with the request */
93         sprintf (buffer, "\r\n");
94         result = send (sd, buffer, strlen (buffer), 0);
96         /* watch for the REAL connection string */
97         result = recv (sd, buffer, MAX_INPUT_BUFFER - 1, 0);
99         /* return a CRITICAL status if we couldn't read any data */
100         if (result == -1)
101                 die (STATE_CRITICAL, _("No data received from %s\n"), host_name);
103         /* make sure we find the response we are looking for */
104         if (!strstr (buffer, server_expect)) {
105                 if (server_port == PORT)
106                         printf (_("Invalid REAL response received from host\n"));
107                 else
108                         printf (_("Invalid REAL response received from host on port %d\n"),
109                                                         server_port);
110         }
111         else {
112                 /* else we got the REAL string, so check the return code */
114                 time (&end_time);
116                 result = STATE_OK;
118                 status_line = (char *) strtok (buffer, "\n");
120                 if (strstr (status_line, "200"))
121                         result = STATE_OK;
123                 /* client errors result in a warning state */
124                 else if (strstr (status_line, "400"))
125                         result = STATE_WARNING;
126                 else if (strstr (status_line, "401"))
127                         result = STATE_WARNING;
128                 else if (strstr (status_line, "402"))
129                         result = STATE_WARNING;
130                 else if (strstr (status_line, "403"))
131                         result = STATE_WARNING;
132                 else if (strstr (status_line, "404"))
133                         result = STATE_WARNING;
135                 /* server errors result in a critical state */
136                 else if (strstr (status_line, "500"))
137                         result = STATE_CRITICAL;
138                 else if (strstr (status_line, "501"))
139                         result = STATE_CRITICAL;
140                 else if (strstr (status_line, "502"))
141                         result = STATE_CRITICAL;
142                 else if (strstr (status_line, "503"))
143                         result = STATE_CRITICAL;
145                 else
146                         result = STATE_UNKNOWN;
147         }
149         /* Part II - Check stream exists and is ok */
150         if ((result == STATE_OK )&& (server_url != NULL) ) {
152                 /* Part I - Server Check */
154                 /* send the OPTIONS request */
155                 sprintf (buffer, "DESCRIBE rtsp://%s:%d%s RTSP/1.0\n", host_name,
156                                                  server_port, server_url);
157                 result = send (sd, buffer, strlen (buffer), 0);
159                 /* send the header sync */
160                 sprintf (buffer, "CSeq: 2\n");
161                 result = send (sd, buffer, strlen (buffer), 0);
163                 /* send a newline so the server knows we're done with the request */
164                 sprintf (buffer, "\n");
165                 result = send (sd, buffer, strlen (buffer), 0);
167                 /* watch for the REAL connection string */
168                 result = recv (sd, buffer, MAX_INPUT_BUFFER - 1, 0);
170                 /* return a CRITICAL status if we couldn't read any data */
171                 if (result == -1) {
172                         printf (_("No data received from host\n"));
173                         result = STATE_CRITICAL;
174                 }
175                 else {
176                         /* make sure we find the response we are looking for */
177                         if (!strstr (buffer, server_expect)) {
178                                 if (server_port == PORT)
179                                         printf (_("Invalid REAL response received from host\n"));
180                                 else
181                                         printf (_("Invalid REAL response received from host on port %d\n"),
182                                                                         server_port);
183                         }
184                         else {
186                                 /* else we got the REAL string, so check the return code */
188                                 time (&end_time);
190                                 result = STATE_OK;
192                                 status_line = (char *) strtok (buffer, "\n");
194                                 if (strstr (status_line, "200"))
195                                         result = STATE_OK;
197                                 /* client errors result in a warning state */
198                                 else if (strstr (status_line, "400"))
199                                         result = STATE_WARNING;
200                                 else if (strstr (status_line, "401"))
201                                         result = STATE_WARNING;
202                                 else if (strstr (status_line, "402"))
203                                         result = STATE_WARNING;
204                                 else if (strstr (status_line, "403"))
205                                         result = STATE_WARNING;
206                                 else if (strstr (status_line, "404"))
207                                         result = STATE_WARNING;
209                                 /* server errors result in a critical state */
210                                 else if (strstr (status_line, "500"))
211                                         result = STATE_CRITICAL;
212                                 else if (strstr (status_line, "501"))
213                                         result = STATE_CRITICAL;
214                                 else if (strstr (status_line, "502"))
215                                         result = STATE_CRITICAL;
216                                 else if (strstr (status_line, "503"))
217                                         result = STATE_CRITICAL;
219                                 else
220                                         result = STATE_UNKNOWN;
221                         }
222                 }
223         }
225         /* Return results */
226         if (result == STATE_OK) {
228                 if (check_critical_time == TRUE
229                                 && (end_time - start_time) > critical_time) result = STATE_CRITICAL;
230                 else if (check_warning_time == TRUE
231                                                  && (end_time - start_time) > warning_time) result =
232                                 STATE_WARNING;
234                 /* Put some HTML in here to create a dynamic link */
235                 printf (_("REAL %s - %d second response time\n"),
236                                                 state_text (result),
237                                                 (int) (end_time - start_time));
238         }
239         else
240                 printf ("%s\n", status_line);
242         /* close the connection */
243         close (sd);
245         /* reset the alarm */
246         alarm (0);
248         return result;
253 /* process command-line arguments */
254 int
255 process_arguments (int argc, char **argv)
257         int c;
259         int option = 0;
260         static struct option longopts[] = {
261                 {"hostname", required_argument, 0, 'H'},
262                 {"IPaddress", required_argument, 0, 'I'},
263                 {"expect", required_argument, 0, 'e'},
264                 {"url", required_argument, 0, 'u'},
265                 {"port", required_argument, 0, 'p'},
266                 {"critical", required_argument, 0, 'c'},
267                 {"warning", required_argument, 0, 'w'},
268                 {"timeout", required_argument, 0, 't'},
269                 {"verbose", no_argument, 0, 'v'},
270                 {"version", no_argument, 0, 'V'},
271                 {"help", no_argument, 0, 'h'},
272                 {0, 0, 0, 0}
273         };
275         if (argc < 2)
276                 return ERROR;
278         for (c = 1; c < argc; c++) {
279                 if (strcmp ("-to", argv[c]) == 0)
280                         strcpy (argv[c], "-t");
281                 else if (strcmp ("-wt", argv[c]) == 0)
282                         strcpy (argv[c], "-w");
283                 else if (strcmp ("-ct", argv[c]) == 0)
284                         strcpy (argv[c], "-c");
285         }
287         while (1) {
288                 c = getopt_long (argc, argv, "+hVI:H:e:u:p:w:c:t:", longopts,
289                                                                          &option);
291                 if (c == -1 || c == EOF)
292                         break;
294                 switch (c) {
295                 case 'I':                                                                       /* hostname */
296                 case 'H':                                                                       /* hostname */
297                         if (server_address)
298                                 break;
299                         else if (is_host (optarg))
300                                 server_address = optarg;
301                         else
302                                 usage2 (_("Invalid hostname/address"), optarg);
303                         break;
304                 case 'e':                                                                       /* string to expect in response header */
305                         server_expect = optarg;
306                         break;
307                 case 'u':                                                                       /* server URL */
308                         server_url = optarg;
309                         break;
310                 case 'p':                                                                       /* port */
311                         if (is_intpos (optarg)) {
312                                 server_port = atoi (optarg);
313                         }
314                         else {
315                                 usage (_("Port must be a positive integer\n"));
316                         }
317                         break;
318                 case 'w':                                                                       /* warning time threshold */
319                         if (is_intnonneg (optarg)) {
320                                 warning_time = atoi (optarg);
321                                 check_warning_time = TRUE;
322                         }
323                         else {
324                                 usage (_("Warning time must be a positive integer\n"));
325                         }
326                         break;
327                 case 'c':                                                                       /* critical time threshold */
328                         if (is_intnonneg (optarg)) {
329                                 critical_time = atoi (optarg);
330                                 check_critical_time = TRUE;
331                         }
332                         else {
333                                 usage (_("Critical time must be a nonnegative integer\n"));
334                         }
335                         break;
336                 case 'v':                                                                       /* verbose */
337                         verbose = TRUE;
338                         break;
339                 case 't':                                                                       /* timeout */
340                         if (is_intnonneg (optarg)) {
341                                 socket_timeout = atoi (optarg);
342                         }
343                         else {
344                                 usage (_("Time interval must be a nonnegative integer\n"));
345                         }
346                         break;
347                 case 'V':                                                                       /* version */
348                         print_revision (progname, revision);
349                         exit (STATE_OK);
350                 case 'h':                                                                       /* help */
351                         print_help ();
352                         exit (STATE_OK);
353                 case '?':                                                                       /* usage */
354                         printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
355                         print_usage ();
356                         exit (STATE_UNKNOWN);
357                 }
358         }
360         c = optind;
361         if (server_address==NULL && argc>c) {
362                 if (is_host (argv[c])) {
363                         server_address = argv[c++];
364                 }
365                 else {
366                         usage2 (_("Invalid hostname/address"), argv[c]);
367                 }
368         }
370         if (server_address==NULL)
371                 usage (_("You must provide a server to check\n"));
373         if (host_name==NULL)
374                 host_name = strdup (server_address);
376         if (server_expect == NULL)
377                 server_expect = strdup(EXPECT);
379         return validate_arguments ();
384 int
385 validate_arguments (void)
387         return OK;
392 void
393 print_help (void)
395         char *myport;
396         asprintf (&myport, "%d", PORT);
398         print_revision (progname, revision);
400         printf ("Copyright (c) 1999 Pedro Leite <leite@cic.ua.pt>\n");
401         printf (COPYRIGHT, copyright, email);
403         printf (_("This plugin tests the REAL service on the specified host.\n\n"));
405         print_usage ();
407         printf (_(UT_HELP_VRSN));
409         printf (_(UT_HOST_PORT), 'p', myport);
411         printf (_("\
412  -u, --url=STRING\n\
413    Connect to this url\n\
414  -e, --expect=STRING\n\
415    String to expect in first line of server response (default: %s)\n"),
416                EXPECT);
418         printf (_(UT_WARN_CRIT));
420         printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
422         printf (_(UT_VERBOSE));
424         printf(_("\
425 This plugin will attempt to open an RTSP connection with the host.\n\
426 Successul connects return STATE_OK, refusals and timeouts return\n\
427 STATE_CRITICAL, other errors return STATE_UNKNOWN.  Successful connects,\n\
428 but incorrect reponse messages from the host result in STATE_WARNING return\n\
429 values."));
431         printf (_(UT_SUPPORT));
436 void
437 print_usage (void)
439         printf ("\
440 Usage: %s -H host [-e expect] [-p port] [-w warn] [-c crit]\n\
441   [-t timeout] [-v]\n", progname);
442         printf (_(UT_HLP_VRS), progname, progname);