Code

- bindtextdomain for gettext, a few other smale cleanups here and there
[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         setlocale (LC_ALL, "");
65         bindtextdomain (PACKAGE, LOCALEDIR);
66         textdomain (PACKAGE);
68         if (process_arguments (argc, argv) != OK)
69                 usage (_("Invalid command arguments supplied\n"));
71         /* initialize alarm signal handling */
72         signal (SIGALRM, socket_timeout_alarm_handler);
74         /* set socket timeout */
75         alarm (socket_timeout);
76         time (&start_time);
78         /* try to connect to the host at the given port number */
79         if (my_tcp_connect (server_address, server_port, &sd) != STATE_OK)
80                 die (STATE_CRITICAL, _("Unable to connect to %s on port %d\n"),
81                                                          server_address, server_port);
83         /* Part I - Server Check */
85         /* send the OPTIONS request */
86         sprintf (buffer, "OPTIONS rtsp://%s:%d RTSP/1.0\r\n", host_name, server_port);
87         result = send (sd, buffer, strlen (buffer), 0);
89         /* send the header sync */
90         sprintf (buffer, "CSeq: 1\r\n");
91         result = send (sd, buffer, strlen (buffer), 0);
93         /* send a newline so the server knows we're done with the request */
94         sprintf (buffer, "\r\n");
95         result = send (sd, buffer, strlen (buffer), 0);
97         /* watch for the REAL connection string */
98         result = recv (sd, buffer, MAX_INPUT_BUFFER - 1, 0);
100         /* return a CRITICAL status if we couldn't read any data */
101         if (result == -1)
102                 die (STATE_CRITICAL, _("No data received from %s\n"), host_name);
104         /* make sure we find the response we are looking for */
105         if (!strstr (buffer, server_expect)) {
106                 if (server_port == PORT)
107                         printf (_("Invalid REAL response received from host\n"));
108                 else
109                         printf (_("Invalid REAL response received from host on port %d\n"),
110                                                         server_port);
111         }
112         else {
113                 /* else we got the REAL string, so check the return code */
115                 time (&end_time);
117                 result = STATE_OK;
119                 status_line = (char *) strtok (buffer, "\n");
121                 if (strstr (status_line, "200"))
122                         result = STATE_OK;
124                 /* client errors result in a warning state */
125                 else if (strstr (status_line, "400"))
126                         result = STATE_WARNING;
127                 else if (strstr (status_line, "401"))
128                         result = STATE_WARNING;
129                 else if (strstr (status_line, "402"))
130                         result = STATE_WARNING;
131                 else if (strstr (status_line, "403"))
132                         result = STATE_WARNING;
133                 else if (strstr (status_line, "404"))
134                         result = STATE_WARNING;
136                 /* server errors result in a critical state */
137                 else if (strstr (status_line, "500"))
138                         result = STATE_CRITICAL;
139                 else if (strstr (status_line, "501"))
140                         result = STATE_CRITICAL;
141                 else if (strstr (status_line, "502"))
142                         result = STATE_CRITICAL;
143                 else if (strstr (status_line, "503"))
144                         result = STATE_CRITICAL;
146                 else
147                         result = STATE_UNKNOWN;
148         }
150         /* Part II - Check stream exists and is ok */
151         if ((result == STATE_OK )&& (server_url != NULL) ) {
153                 /* Part I - Server Check */
155                 /* send the OPTIONS request */
156                 sprintf (buffer, "DESCRIBE rtsp://%s:%d%s RTSP/1.0\n", host_name,
157                                                  server_port, server_url);
158                 result = send (sd, buffer, strlen (buffer), 0);
160                 /* send the header sync */
161                 sprintf (buffer, "CSeq: 2\n");
162                 result = send (sd, buffer, strlen (buffer), 0);
164                 /* send a newline so the server knows we're done with the request */
165                 sprintf (buffer, "\n");
166                 result = send (sd, buffer, strlen (buffer), 0);
168                 /* watch for the REAL connection string */
169                 result = recv (sd, buffer, MAX_INPUT_BUFFER - 1, 0);
171                 /* return a CRITICAL status if we couldn't read any data */
172                 if (result == -1) {
173                         printf (_("No data received from host\n"));
174                         result = STATE_CRITICAL;
175                 }
176                 else {
177                         /* make sure we find the response we are looking for */
178                         if (!strstr (buffer, server_expect)) {
179                                 if (server_port == PORT)
180                                         printf (_("Invalid REAL response received from host\n"));
181                                 else
182                                         printf (_("Invalid REAL response received from host on port %d\n"),
183                                                                         server_port);
184                         }
185                         else {
187                                 /* else we got the REAL string, so check the return code */
189                                 time (&end_time);
191                                 result = STATE_OK;
193                                 status_line = (char *) strtok (buffer, "\n");
195                                 if (strstr (status_line, "200"))
196                                         result = STATE_OK;
198                                 /* client errors result in a warning state */
199                                 else if (strstr (status_line, "400"))
200                                         result = STATE_WARNING;
201                                 else if (strstr (status_line, "401"))
202                                         result = STATE_WARNING;
203                                 else if (strstr (status_line, "402"))
204                                         result = STATE_WARNING;
205                                 else if (strstr (status_line, "403"))
206                                         result = STATE_WARNING;
207                                 else if (strstr (status_line, "404"))
208                                         result = STATE_WARNING;
210                                 /* server errors result in a critical state */
211                                 else if (strstr (status_line, "500"))
212                                         result = STATE_CRITICAL;
213                                 else if (strstr (status_line, "501"))
214                                         result = STATE_CRITICAL;
215                                 else if (strstr (status_line, "502"))
216                                         result = STATE_CRITICAL;
217                                 else if (strstr (status_line, "503"))
218                                         result = STATE_CRITICAL;
220                                 else
221                                         result = STATE_UNKNOWN;
222                         }
223                 }
224         }
226         /* Return results */
227         if (result == STATE_OK) {
229                 if (check_critical_time == TRUE
230                                 && (end_time - start_time) > critical_time) result = STATE_CRITICAL;
231                 else if (check_warning_time == TRUE
232                                                  && (end_time - start_time) > warning_time) result =
233                                 STATE_WARNING;
235                 /* Put some HTML in here to create a dynamic link */
236                 printf (_("REAL %s - %d second response time\n"),
237                                                 state_text (result),
238                                                 (int) (end_time - start_time));
239         }
240         else
241                 printf ("%s\n", status_line);
243         /* close the connection */
244         close (sd);
246         /* reset the alarm */
247         alarm (0);
249         return result;
256 \f
257 /* process command-line arguments */
258 int
259 process_arguments (int argc, char **argv)
261         int c;
263         int option = 0;
264         static struct option longopts[] = {
265                 {"hostname", required_argument, 0, 'H'},
266                 {"IPaddress", required_argument, 0, 'I'},
267                 {"expect", required_argument, 0, 'e'},
268                 {"url", required_argument, 0, 'u'},
269                 {"port", required_argument, 0, 'p'},
270                 {"critical", required_argument, 0, 'c'},
271                 {"warning", required_argument, 0, 'w'},
272                 {"timeout", required_argument, 0, 't'},
273                 {"verbose", no_argument, 0, 'v'},
274                 {"version", no_argument, 0, 'V'},
275                 {"help", no_argument, 0, 'h'},
276                 {0, 0, 0, 0}
277         };
279         if (argc < 2)
280                 return ERROR;
282         for (c = 1; c < argc; c++) {
283                 if (strcmp ("-to", argv[c]) == 0)
284                         strcpy (argv[c], "-t");
285                 else if (strcmp ("-wt", argv[c]) == 0)
286                         strcpy (argv[c], "-w");
287                 else if (strcmp ("-ct", argv[c]) == 0)
288                         strcpy (argv[c], "-c");
289         }
291         while (1) {
292                 c = getopt_long (argc, argv, "+hVI:H:e:u:p:w:c:t:", longopts,
293                                                                          &option);
295                 if (c == -1 || c == EOF)
296                         break;
298                 switch (c) {
299                 case 'I':                                                                       /* hostname */
300                 case 'H':                                                                       /* hostname */
301                         if (server_address)
302                                 break;
303                         else if (is_host (optarg))
304                                 server_address = optarg;
305                         else
306                                 usage (_("Invalid host name\n"));
307                         break;
308                 case 'e':                                                                       /* string to expect in response header */
309                         server_expect = optarg;
310                         break;
311                 case 'u':                                                                       /* server URL */
312                         server_url = optarg;
313                         break;
314                 case 'p':                                                                       /* port */
315                         if (is_intpos (optarg)) {
316                                 server_port = atoi (optarg);
317                         }
318                         else {
319                                 usage (_("Server port must be a positive integer\n"));
320                         }
321                         break;
322                 case 'w':                                                                       /* warning time threshold */
323                         if (is_intnonneg (optarg)) {
324                                 warning_time = atoi (optarg);
325                                 check_warning_time = TRUE;
326                         }
327                         else {
328                                 usage (_("Warning time must be a nonnegative integer\n"));
329                         }
330                         break;
331                 case 'c':                                                                       /* critical time threshold */
332                         if (is_intnonneg (optarg)) {
333                                 critical_time = atoi (optarg);
334                                 check_critical_time = TRUE;
335                         }
336                         else {
337                                 usage (_("Critical time must be a nonnegative integer\n"));
338                         }
339                         break;
340                 case 'v':                                                                       /* verbose */
341                         verbose = TRUE;
342                         break;
343                 case 't':                                                                       /* timeout */
344                         if (is_intnonneg (optarg)) {
345                                 socket_timeout = atoi (optarg);
346                         }
347                         else {
348                                 usage (_("Time interval must be a nonnegative integer\n"));
349                         }
350                         break;
351                 case 'V':                                                                       /* version */
352                         print_revision (progname, "$Revision$");
353                         exit (STATE_OK);
354                 case 'h':                                                                       /* help */
355                         print_help ();
356                         exit (STATE_OK);
357                 case '?':                                                                       /* usage */
358                         usage (_("Invalid argument\n"));
359                 }
360         }
362         c = optind;
363         if (server_address==NULL && argc>c) {
364                 if (is_host (argv[c])) {
365                         server_address = argv[c++];
366                 }
367                 else {
368                         usage (_("Invalid host name"));
369                 }
370         }
372         if (server_address==NULL)
373                 usage (_("You must provide a server to check\n"));
375         if (host_name==NULL)
376                 host_name = strdup (server_address);
378         if (server_expect == NULL)
379                 server_expect = strdup(EXPECT);
381         return validate_arguments ();
388 int
389 validate_arguments (void)
391         return OK;
397 \f
398 void
399 print_help (void)
401         char *myport;
402         asprintf (&myport, "%d", PORT);
404         print_revision (progname, "$Revision$");
406         printf (_("Copyright (c) 1999 Pedro Leite <leite@cic.ua.pt>\n"));
407         printf (_(COPYRIGHT), copyright, email);
409         printf (_("This plugin tests the REAL service on the specified host.\n\n"));
411         print_usage ();
413         printf (_(UT_HELP_VRSN));
415         printf (_(UT_HOST_PORT), 'p', myport);
417         printf (_("\
418  -u, --url=STRING\n\
419    Connect to this url\n\
420  -e, --expect=STRING\n\
421    String to expect in first line of server response (default: %s)\n"),
422                EXPECT);
424         printf (_(UT_WARN_CRIT));
426         printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
428         printf (_(UT_VERBOSE));
430         printf(_("\
431 This plugin will attempt to open an RTSP connection with the host.\n\
432 Successul connects return STATE_OK, refusals and timeouts return\n\
433 STATE_CRITICAL, other errors return STATE_UNKNOWN.  Successful connects,\n\
434 but incorrect reponse messages from the host result in STATE_WARNING return\n\
435 values."));
437         printf (_(UT_SUPPORT));
444 void
445 print_usage (void)
447         printf ("\
448 Usage: %s -H host [-e expect] [-p port] [-w warn] [-c crit]\n\
449   [-t timeout] [-v]\n", progname);
450         printf (_(UT_HLP_VRS), progname, progname);