Code

cleaning help and usage + license
[nagiosplug.git] / plugins / check_real.c
1 /******************************************************************************
2 *
3 * Nagios check_real plugin
4 *
5 * License: GPL
6 * Copyright (c) 1999-2006 nagios-plugins team
7 *
8 * Last Modified: $Date$
9 *
10 * Description:
11 *
12 * This file contains the check_real plugin
13 *
14 * License Information:
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 *
30 *
31 * $Id$
32
33 ******************************************************************************/
35 const char *progname = "check_real";
36 const char *revision = "$Revision$";
37 const char *copyright = "2000-2006";
38 const char *email = "nagiosplug-devel@lists.sourceforge.net";
40 #include "common.h"
41 #include "netutils.h"
42 #include "utils.h"
44 enum {
45         PORT    = 554
46 };
48 #define EXPECT  "RTSP/1."
49 #define URL     ""
51 int process_arguments (int, char **);
52 int validate_arguments (void);
53 void print_help (void);
54 void print_usage (void);
56 int server_port = PORT;
57 char *server_address;
58 char *host_name;
59 char *server_url = NULL;
60 char *server_expect;
61 int warning_time = 0;
62 int check_warning_time = FALSE;
63 int critical_time = 0;
64 int check_critical_time = FALSE;
65 int verbose = FALSE;
69 int
70 main (int argc, char **argv)
71 {
72         int sd;
73         int result = STATE_UNKNOWN;
74         char buffer[MAX_INPUT_BUFFER];
75         char *status_line = NULL;
77         setlocale (LC_ALL, "");
78         bindtextdomain (PACKAGE, LOCALEDIR);
79         textdomain (PACKAGE);
81         if (process_arguments (argc, argv) == ERROR)
82                 usage4 (_("Could not parse arguments"));
84         /* initialize alarm signal handling */
85         signal (SIGALRM, socket_timeout_alarm_handler);
87         /* set socket timeout */
88         alarm (socket_timeout);
89         time (&start_time);
91         /* try to connect to the host at the given port number */
92         if (my_tcp_connect (server_address, server_port, &sd) != STATE_OK)
93                 die (STATE_CRITICAL, _("Unable to connect to %s on port %d\n"),
94                                                          server_address, server_port);
96         /* Part I - Server Check */
98         /* send the OPTIONS request */
99         sprintf (buffer, "OPTIONS rtsp://%s:%d RTSP/1.0\r\n", host_name, server_port);
100         result = send (sd, buffer, strlen (buffer), 0);
102         /* send the header sync */
103         sprintf (buffer, "CSeq: 1\r\n");
104         result = send (sd, buffer, strlen (buffer), 0);
106         /* send a newline so the server knows we're done with the request */
107         sprintf (buffer, "\r\n");
108         result = send (sd, buffer, strlen (buffer), 0);
110         /* watch for the REAL connection string */
111         result = recv (sd, buffer, MAX_INPUT_BUFFER - 1, 0);
113         /* return a CRITICAL status if we couldn't read any data */
114         if (result == -1)
115                 die (STATE_CRITICAL, _("No data received from %s\n"), host_name);
117         /* make sure we find the response we are looking for */
118         if (!strstr (buffer, server_expect)) {
119                 if (server_port == PORT)
120                         printf ("%s\n", _("Invalid REAL response received from host"));
121                 else
122                         printf (_("Invalid REAL response received from host on port %d\n"),
123                                                         server_port);
124         }
125         else {
126                 /* else we got the REAL string, so check the return code */
128                 time (&end_time);
130                 result = STATE_OK;
132                 status_line = (char *) strtok (buffer, "\n");
134                 if (strstr (status_line, "200"))
135                         result = STATE_OK;
137                 /* client errors result in a warning state */
138                 else if (strstr (status_line, "400"))
139                         result = STATE_WARNING;
140                 else if (strstr (status_line, "401"))
141                         result = STATE_WARNING;
142                 else if (strstr (status_line, "402"))
143                         result = STATE_WARNING;
144                 else if (strstr (status_line, "403"))
145                         result = STATE_WARNING;
146                 else if (strstr (status_line, "404"))
147                         result = STATE_WARNING;
149                 /* server errors result in a critical state */
150                 else if (strstr (status_line, "500"))
151                         result = STATE_CRITICAL;
152                 else if (strstr (status_line, "501"))
153                         result = STATE_CRITICAL;
154                 else if (strstr (status_line, "502"))
155                         result = STATE_CRITICAL;
156                 else if (strstr (status_line, "503"))
157                         result = STATE_CRITICAL;
159                 else
160                         result = STATE_UNKNOWN;
161         }
163         /* Part II - Check stream exists and is ok */
164         if ((result == STATE_OK )&& (server_url != NULL) ) {
166                 /* Part I - Server Check */
168                 /* send the OPTIONS request */
169                 sprintf (buffer, "DESCRIBE rtsp://%s:%d%s RTSP/1.0\n", host_name,
170                                                  server_port, server_url);
171                 result = send (sd, buffer, strlen (buffer), 0);
173                 /* send the header sync */
174                 sprintf (buffer, "CSeq: 2\n");
175                 result = send (sd, buffer, strlen (buffer), 0);
177                 /* send a newline so the server knows we're done with the request */
178                 sprintf (buffer, "\n");
179                 result = send (sd, buffer, strlen (buffer), 0);
181                 /* watch for the REAL connection string */
182                 result = recv (sd, buffer, MAX_INPUT_BUFFER - 1, 0);
184                 /* return a CRITICAL status if we couldn't read any data */
185                 if (result == -1) {
186                         printf (_("No data received from host\n"));
187                         result = STATE_CRITICAL;
188                 }
189                 else {
190                         /* make sure we find the response we are looking for */
191                         if (!strstr (buffer, server_expect)) {
192                                 if (server_port == PORT)
193                                         printf ("%s\n", _("Invalid REAL response received from host"));
194                                 else
195                                         printf (_("Invalid REAL response received from host on port %d\n"),
196                                                                         server_port);
197                         }
198                         else {
200                                 /* else we got the REAL string, so check the return code */
202                                 time (&end_time);
204                                 result = STATE_OK;
206                                 status_line = (char *) strtok (buffer, "\n");
208                                 if (strstr (status_line, "200"))
209                                         result = STATE_OK;
211                                 /* client errors result in a warning state */
212                                 else if (strstr (status_line, "400"))
213                                         result = STATE_WARNING;
214                                 else if (strstr (status_line, "401"))
215                                         result = STATE_WARNING;
216                                 else if (strstr (status_line, "402"))
217                                         result = STATE_WARNING;
218                                 else if (strstr (status_line, "403"))
219                                         result = STATE_WARNING;
220                                 else if (strstr (status_line, "404"))
221                                         result = STATE_WARNING;
223                                 /* server errors result in a critical state */
224                                 else if (strstr (status_line, "500"))
225                                         result = STATE_CRITICAL;
226                                 else if (strstr (status_line, "501"))
227                                         result = STATE_CRITICAL;
228                                 else if (strstr (status_line, "502"))
229                                         result = STATE_CRITICAL;
230                                 else if (strstr (status_line, "503"))
231                                         result = STATE_CRITICAL;
233                                 else
234                                         result = STATE_UNKNOWN;
235                         }
236                 }
237         }
239         /* Return results */
240         if (result == STATE_OK) {
242                 if (check_critical_time == TRUE
243                                 && (end_time - start_time) > critical_time) result = STATE_CRITICAL;
244                 else if (check_warning_time == TRUE
245                                                  && (end_time - start_time) > warning_time) result =
246                                 STATE_WARNING;
248                 /* Put some HTML in here to create a dynamic link */
249                 printf (_("REAL %s - %d second response time\n"),
250                                                 state_text (result),
251                                                 (int) (end_time - start_time));
252         }
253         else
254                 printf ("%s\n", status_line);
256         /* close the connection */
257         close (sd);
259         /* reset the alarm */
260         alarm (0);
262         return result;
267 /* process command-line arguments */
268 int
269 process_arguments (int argc, char **argv)
271         int c;
273         int option = 0;
274         static struct option longopts[] = {
275                 {"hostname", required_argument, 0, 'H'},
276                 {"IPaddress", required_argument, 0, 'I'},
277                 {"expect", required_argument, 0, 'e'},
278                 {"url", required_argument, 0, 'u'},
279                 {"port", required_argument, 0, 'p'},
280                 {"critical", required_argument, 0, 'c'},
281                 {"warning", required_argument, 0, 'w'},
282                 {"timeout", required_argument, 0, 't'},
283                 {"verbose", no_argument, 0, 'v'},
284                 {"version", no_argument, 0, 'V'},
285                 {"help", no_argument, 0, 'h'},
286                 {0, 0, 0, 0}
287         };
289         if (argc < 2)
290                 return ERROR;
292         for (c = 1; c < argc; c++) {
293                 if (strcmp ("-to", argv[c]) == 0)
294                         strcpy (argv[c], "-t");
295                 else if (strcmp ("-wt", argv[c]) == 0)
296                         strcpy (argv[c], "-w");
297                 else if (strcmp ("-ct", argv[c]) == 0)
298                         strcpy (argv[c], "-c");
299         }
301         while (1) {
302                 c = getopt_long (argc, argv, "+hVI:H:e:u:p:w:c:t:", longopts,
303                                                                          &option);
305                 if (c == -1 || c == EOF)
306                         break;
308                 switch (c) {
309                 case 'I':                                                                       /* hostname */
310                 case 'H':                                                                       /* hostname */
311                         if (server_address)
312                                 break;
313                         else if (is_host (optarg))
314                                 server_address = optarg;
315                         else
316                                 usage2 (_("Invalid hostname/address"), optarg);
317                         break;
318                 case 'e':                                                                       /* string to expect in response header */
319                         server_expect = optarg;
320                         break;
321                 case 'u':                                                                       /* server URL */
322                         server_url = optarg;
323                         break;
324                 case 'p':                                                                       /* port */
325                         if (is_intpos (optarg)) {
326                                 server_port = atoi (optarg);
327                         }
328                         else {
329                                 usage4 (_("Port must be a positive integer"));
330                         }
331                         break;
332                 case 'w':                                                                       /* warning time threshold */
333                         if (is_intnonneg (optarg)) {
334                                 warning_time = atoi (optarg);
335                                 check_warning_time = TRUE;
336                         }
337                         else {
338                                 usage4 (_("Warning time must be a positive integer"));
339                         }
340                         break;
341                 case 'c':                                                                       /* critical time threshold */
342                         if (is_intnonneg (optarg)) {
343                                 critical_time = atoi (optarg);
344                                 check_critical_time = TRUE;
345                         }
346                         else {
347                                 usage4 (_("Critical time must be a positive integer"));
348                         }
349                         break;
350                 case 'v':                                                                       /* verbose */
351                         verbose = TRUE;
352                         break;
353                 case 't':                                                                       /* timeout */
354                         if (is_intnonneg (optarg)) {
355                                 socket_timeout = atoi (optarg);
356                         }
357                         else {
358                                 usage4 (_("Timeout interval must be a positive integer"));
359                         }
360                         break;
361                 case 'V':                                                                       /* version */
362                         print_revision (progname, revision);
363                         exit (STATE_OK);
364                 case 'h':                                                                       /* help */
365                         print_help ();
366                         exit (STATE_OK);
367                 case '?':                                                                       /* usage */
368                         usage2 (_("Unknown argument"), optarg);
369                 }
370         }
372         c = optind;
373         if (server_address==NULL && argc>c) {
374                 if (is_host (argv[c])) {
375                         server_address = argv[c++];
376                 }
377                 else {
378                         usage2 (_("Invalid hostname/address"), argv[c]);
379                 }
380         }
382         if (server_address==NULL)
383                 usage4 (_("You must provide a server to check"));
385         if (host_name==NULL)
386                 host_name = strdup (server_address);
388         if (server_expect == NULL)
389                 server_expect = strdup(EXPECT);
391         return validate_arguments ();
396 int
397 validate_arguments (void)
399         return OK;
404 void
405 print_help (void)
407         char *myport;
408         asprintf (&myport, "%d", PORT);
410         print_revision (progname, revision);
412         printf ("Copyright (c) 1999 Pedro Leite <leite@cic.ua.pt>\n");
413         printf (COPYRIGHT, copyright, email);
415         printf ("%s\n", _("This plugin tests the REAL service on the specified host."));
417   printf ("\n\n");
419         print_usage ();
421         printf (_(UT_HELP_VRSN));
423         printf (_(UT_HOST_PORT), 'p', myport);
425         printf (" %s\n", "-u, --url=STRING");
426   printf ("    %s\n", _("Connect to this url"));
427   printf (" %s\n", "-e, --expect=STRING");
428   printf (_("String to expect in first line of server response (default: %s)\n"),
429                EXPECT);
431         printf (_(UT_WARN_CRIT));
433         printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
435         printf (_(UT_VERBOSE));
437         printf ("%s\n", _("This plugin will attempt to open an RTSP connection with the host."));
438   printf ("%s\n", _("Successul connects return STATE_OK, refusals and timeouts return"));
439   printf ("%s\n", _("STATE_CRITICAL, other errors return STATE_UNKNOWN.  Successful connects,"));
440   printf ("%s\n", _("but incorrect reponse messages from the host result in STATE_WARNING return"));
441   printf ("%s\n", _("values."));
443         printf (_(UT_SUPPORT));
448 void
449 print_usage (void)
451   printf (_("Usage:"));
452         printf ("%s -H host [-e expect] [-p port] [-w warn] [-c crit] [-t timeout] [-v]\n", progname);