Code

the last round of pedantic compiler warnings
[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 ******************************************************************************/
19 const char *progname = "check_real";
20 const char *revision = "$Revision$";
21 const char *copyright = "2000-2003";
22 const char *email = "nagiosplug-devel@lists.sourceforge.net";
24 #include "common.h"
25 #include "netutils.h"
26 #include "utils.h"
28 enum {
29         PORT    = 554
30 };
32 #define EXPECT  "RTSP/1."
33 #define URL     ""
35 int process_arguments (int, char **);
36 int validate_arguments (void);
37 void print_help (void);
38 void print_usage (void);
40 int server_port = PORT;
41 char *server_address;
42 char *host_name;
43 char *server_url = NULL;
44 char *server_expect;
45 int warning_time = 0;
46 int check_warning_time = FALSE;
47 int critical_time = 0;
48 int check_critical_time = FALSE;
49 int verbose = FALSE;
55 \f
56 int
57 main (int argc, char **argv)
58 {
59         int sd;
60         int result;
61         char buffer[MAX_INPUT_BUFFER];
62         char *status_line = NULL;
64         if (process_arguments (argc, argv) != OK)
65                 usage (_("Invalid command arguments supplied\n"));
67         /* initialize alarm signal handling */
68         signal (SIGALRM, socket_timeout_alarm_handler);
70         /* set socket timeout */
71         alarm (socket_timeout);
72         time (&start_time);
74         /* try to connect to the host at the given port number */
75         if (my_tcp_connect (server_address, server_port, &sd) != STATE_OK)
76                 die (STATE_CRITICAL, _("Unable to connect to %s on port %d\n"),
77                                                          server_address, server_port);
79         /* Part I - Server Check */
81         /* send the OPTIONS request */
82         sprintf (buffer, "OPTIONS rtsp://%s:%d RTSP/1.0\r\n", host_name, server_port);
83         result = send (sd, buffer, strlen (buffer), 0);
85         /* send the header sync */
86         sprintf (buffer, "CSeq: 1\r\n");
87         result = send (sd, buffer, strlen (buffer), 0);
89         /* send a newline so the server knows we're done with the request */
90         sprintf (buffer, "\r\n");
91         result = send (sd, buffer, strlen (buffer), 0);
93         /* watch for the REAL connection string */
94         result = recv (sd, buffer, MAX_INPUT_BUFFER - 1, 0);
96         /* return a CRITICAL status if we couldn't read any data */
97         if (result == -1)
98                 die (STATE_CRITICAL, _("No data received from %s\n"), host_name);
100         /* make sure we find the response we are looking for */
101         if (!strstr (buffer, server_expect)) {
102                 if (server_port == PORT)
103                         printf (_("Invalid REAL response received from host\n"));
104                 else
105                         printf (_("Invalid REAL response received from host on port %d\n"),
106                                                         server_port);
107         }
108         else {
109                 /* else we got the REAL string, so check the return code */
111                 time (&end_time);
113                 result = STATE_OK;
115                 status_line = (char *) strtok (buffer, "\n");
117                 if (strstr (status_line, "200"))
118                         result = STATE_OK;
120                 /* client errors result in a warning state */
121                 else if (strstr (status_line, "400"))
122                         result = STATE_WARNING;
123                 else if (strstr (status_line, "401"))
124                         result = STATE_WARNING;
125                 else if (strstr (status_line, "402"))
126                         result = STATE_WARNING;
127                 else if (strstr (status_line, "403"))
128                         result = STATE_WARNING;
129                 else if (strstr (status_line, "404"))
130                         result = STATE_WARNING;
132                 /* server errors result in a critical state */
133                 else if (strstr (status_line, "500"))
134                         result = STATE_CRITICAL;
135                 else if (strstr (status_line, "501"))
136                         result = STATE_CRITICAL;
137                 else if (strstr (status_line, "502"))
138                         result = STATE_CRITICAL;
139                 else if (strstr (status_line, "503"))
140                         result = STATE_CRITICAL;
142                 else
143                         result = STATE_UNKNOWN;
144         }
146         /* Part II - Check stream exists and is ok */
147         if ((result == STATE_OK )&& (server_url != NULL) ) {
149                 /* Part I - Server Check */
151                 /* send the OPTIONS request */
152                 sprintf (buffer, "DESCRIBE rtsp://%s:%d%s RTSP/1.0\n", host_name,
153                                                  server_port, server_url);
154                 result = send (sd, buffer, strlen (buffer), 0);
156                 /* send the header sync */
157                 sprintf (buffer, "CSeq: 2\n");
158                 result = send (sd, buffer, strlen (buffer), 0);
160                 /* send a newline so the server knows we're done with the request */
161                 sprintf (buffer, "\n");
162                 result = send (sd, buffer, strlen (buffer), 0);
164                 /* watch for the REAL connection string */
165                 result = recv (sd, buffer, MAX_INPUT_BUFFER - 1, 0);
167                 /* return a CRITICAL status if we couldn't read any data */
168                 if (result == -1) {
169                         printf (_("No data received from host\n"));
170                         result = STATE_CRITICAL;
171                 }
172                 else {
173                         /* make sure we find the response we are looking for */
174                         if (!strstr (buffer, server_expect)) {
175                                 if (server_port == PORT)
176                                         printf (_("Invalid REAL response received from host\n"));
177                                 else
178                                         printf (_("Invalid REAL response received from host on port %d\n"),
179                                                                         server_port);
180                         }
181                         else {
183                                 /* else we got the REAL string, so check the return code */
185                                 time (&end_time);
187                                 result = STATE_OK;
189                                 status_line = (char *) strtok (buffer, "\n");
191                                 if (strstr (status_line, "200"))
192                                         result = STATE_OK;
194                                 /* client errors result in a warning state */
195                                 else if (strstr (status_line, "400"))
196                                         result = STATE_WARNING;
197                                 else if (strstr (status_line, "401"))
198                                         result = STATE_WARNING;
199                                 else if (strstr (status_line, "402"))
200                                         result = STATE_WARNING;
201                                 else if (strstr (status_line, "403"))
202                                         result = STATE_WARNING;
203                                 else if (strstr (status_line, "404"))
204                                         result = STATE_WARNING;
206                                 /* server errors result in a critical state */
207                                 else if (strstr (status_line, "500"))
208                                         result = STATE_CRITICAL;
209                                 else if (strstr (status_line, "501"))
210                                         result = STATE_CRITICAL;
211                                 else if (strstr (status_line, "502"))
212                                         result = STATE_CRITICAL;
213                                 else if (strstr (status_line, "503"))
214                                         result = STATE_CRITICAL;
216                                 else
217                                         result = STATE_UNKNOWN;
218                         }
219                 }
220         }
222         /* Return results */
223         if (result == STATE_OK) {
225                 if (check_critical_time == TRUE
226                                 && (end_time - start_time) > critical_time) result = STATE_CRITICAL;
227                 else if (check_warning_time == TRUE
228                                                  && (end_time - start_time) > warning_time) result =
229                                 STATE_WARNING;
231                 /* Put some HTML in here to create a dynamic link */
232                 printf (_("REAL %s - %d second response time\n"),
233                                                 state_text (result),
234                                                 (int) (end_time - start_time));
235         }
236         else
237                 printf ("%s\n", status_line);
239         /* close the connection */
240         close (sd);
242         /* reset the alarm */
243         alarm (0);
245         return result;
252 \f
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                                 usage (_("Invalid host name\n"));
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 (_("Server 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 nonnegative 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                         usage (_("Invalid argument\n"));
355                 }
356         }
358         c = optind;
359         if (server_address==NULL && argc>c) {
360                 if (is_host (argv[c])) {
361                         server_address = argv[c++];
362                 }
363                 else {
364                         usage (_("Invalid host name"));
365                 }
366         }
368         if (server_address==NULL)
369                 usage (_("You must provide a server to check\n"));
371         if (host_name==NULL)
372                 host_name = strdup (server_address);
374         if (server_expect == NULL)
375                 server_expect = strdup(EXPECT);
377         return validate_arguments ();
384 int
385 validate_arguments (void)
387         return OK;
393 \f
394 void
395 print_help (void)
397         char *myport;
398         asprintf (&myport, "%d", PORT);
400         print_revision (progname, "$Revision$");
402         printf (_("Copyright (c) 1999 Pedro Leite <leite@cic.ua.pt>\n"));
403         printf (_(COPYRIGHT), copyright, email);
405         printf (_("This plugin tests the REAL service on the specified host.\n\n"));
407         print_usage ();
409         printf (_(UT_HELP_VRSN));
411         printf (_(UT_HOST_PORT), 'p', myport);
413         printf (_("\
414  -u, --url=STRING\n\
415    Connect to this url\n\
416  -e, --expect=STRING\n\
417    String to expect in first line of server response (default: %s)\n"),
418                EXPECT);
420         printf (_(UT_WARN_CRIT));
422         printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
424         printf (_(UT_VERBOSE));
426         printf(_("\
427 This plugin will attempt to open an RTSP connection with the host.\n\
428 Successul connects return STATE_OK, refusals and timeouts return\n\
429 STATE_CRITICAL, other errors return STATE_UNKNOWN.  Successful connects,\n\
430 but incorrect reponse messages from the host result in STATE_WARNING return\n\
431 values."));
433         printf (_(UT_SUPPORT));
440 void
441 print_usage (void)
443         printf ("\
444 Usage: %s -H host [-e expect] [-p port] [-w warn] [-c crit]\n\
445   [-t timeout] [-v]\n", progname);
446         printf (_(UT_HLP_VRS), progname, progname);