Code

--help output cleanup (plus removal of spaces on blank lines)
[nagiosplug.git] / plugins / check_real.c
1 /*****************************************************************************
2
3 * Nagios check_real plugin
4
5 * License: GPL
6 * Copyright (c) 2000-2007 Nagios Plugins Development Team
7
8 * Last Modified: $Date$
9
10 * Description:
11
12 * This file contains the check_real plugin
13
14 * This plugin tests the REAL service on the specified host.
15
16
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
21
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25 * GNU General Public License for more details.
26
27 * You should have received a copy of the GNU General Public License
28 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29
30 * $Id$
31
32 *****************************************************************************/
34 const char *progname = "check_real";
35 const char *revision = "$Revision$";
36 const char *copyright = "2000-2007";
37 const char *email = "nagiosplug-devel@lists.sourceforge.net";
39 #include "common.h"
40 #include "netutils.h"
41 #include "utils.h"
43 enum {
44         PORT    = 554
45 };
47 #define EXPECT  "RTSP/1."
48 #define URL     ""
50 int process_arguments (int, char **);
51 int validate_arguments (void);
52 void print_help (void);
53 void print_usage (void);
55 int server_port = PORT;
56 char *server_address;
57 char *host_name;
58 char *server_url = NULL;
59 char *server_expect;
60 int warning_time = 0;
61 int check_warning_time = FALSE;
62 int critical_time = 0;
63 int check_critical_time = FALSE;
64 int verbose = FALSE;
68 int
69 main (int argc, char **argv)
70 {
71         int sd;
72         int result = STATE_UNKNOWN;
73         char buffer[MAX_INPUT_BUFFER];
74         char *status_line = NULL;
76         setlocale (LC_ALL, "");
77         bindtextdomain (PACKAGE, LOCALEDIR);
78         textdomain (PACKAGE);
80         if (process_arguments (argc, argv) == ERROR)
81                 usage4 (_("Could not parse arguments"));
83         /* initialize alarm signal handling */
84         signal (SIGALRM, socket_timeout_alarm_handler);
86         /* set socket timeout */
87         alarm (socket_timeout);
88         time (&start_time);
90         /* try to connect to the host at the given port number */
91         if (my_tcp_connect (server_address, server_port, &sd) != STATE_OK)
92                 die (STATE_CRITICAL, _("Unable to connect to %s on port %d\n"),
93                                                          server_address, server_port);
95         /* Part I - Server Check */
97         /* send the OPTIONS request */
98         sprintf (buffer, "OPTIONS rtsp://%s:%d RTSP/1.0\r\n", host_name, server_port);
99         result = send (sd, buffer, strlen (buffer), 0);
101         /* send the header sync */
102         sprintf (buffer, "CSeq: 1\r\n");
103         result = send (sd, buffer, strlen (buffer), 0);
105         /* send a newline so the server knows we're done with the request */
106         sprintf (buffer, "\r\n");
107         result = send (sd, buffer, strlen (buffer), 0);
109         /* watch for the REAL connection string */
110         result = recv (sd, buffer, MAX_INPUT_BUFFER - 1, 0);
112         /* return a CRITICAL status if we couldn't read any data */
113         if (result == -1)
114                 die (STATE_CRITICAL, _("No data received from %s\n"), host_name);
116         /* make sure we find the response we are looking for */
117         if (!strstr (buffer, server_expect)) {
118                 if (server_port == PORT)
119                         printf ("%s\n", _("Invalid REAL response received from host"));
120                 else
121                         printf (_("Invalid REAL response received from host on port %d\n"),
122                                                         server_port);
123         }
124         else {
125                 /* else we got the REAL string, so check the return code */
127                 time (&end_time);
129                 result = STATE_OK;
131                 status_line = (char *) strtok (buffer, "\n");
133                 if (strstr (status_line, "200"))
134                         result = STATE_OK;
136                 /* client errors result in a warning state */
137                 else if (strstr (status_line, "400"))
138                         result = STATE_WARNING;
139                 else if (strstr (status_line, "401"))
140                         result = STATE_WARNING;
141                 else if (strstr (status_line, "402"))
142                         result = STATE_WARNING;
143                 else if (strstr (status_line, "403"))
144                         result = STATE_WARNING;
145                 else if (strstr (status_line, "404"))
146                         result = STATE_WARNING;
148                 /* server errors result in a critical state */
149                 else if (strstr (status_line, "500"))
150                         result = STATE_CRITICAL;
151                 else if (strstr (status_line, "501"))
152                         result = STATE_CRITICAL;
153                 else if (strstr (status_line, "502"))
154                         result = STATE_CRITICAL;
155                 else if (strstr (status_line, "503"))
156                         result = STATE_CRITICAL;
158                 else
159                         result = STATE_UNKNOWN;
160         }
162         /* Part II - Check stream exists and is ok */
163         if ((result == STATE_OK )&& (server_url != NULL) ) {
165                 /* Part I - Server Check */
167                 /* send the OPTIONS request */
168                 sprintf (buffer, "DESCRIBE rtsp://%s:%d%s RTSP/1.0\n", host_name,
169                                                  server_port, server_url);
170                 result = send (sd, buffer, strlen (buffer), 0);
172                 /* send the header sync */
173                 sprintf (buffer, "CSeq: 2\n");
174                 result = send (sd, buffer, strlen (buffer), 0);
176                 /* send a newline so the server knows we're done with the request */
177                 sprintf (buffer, "\n");
178                 result = send (sd, buffer, strlen (buffer), 0);
180                 /* watch for the REAL connection string */
181                 result = recv (sd, buffer, MAX_INPUT_BUFFER - 1, 0);
183                 /* return a CRITICAL status if we couldn't read any data */
184                 if (result == -1) {
185                         printf (_("No data received from host\n"));
186                         result = STATE_CRITICAL;
187                 }
188                 else {
189                         /* make sure we find the response we are looking for */
190                         if (!strstr (buffer, server_expect)) {
191                                 if (server_port == PORT)
192                                         printf ("%s\n", _("Invalid REAL response received from host"));
193                                 else
194                                         printf (_("Invalid REAL response received from host on port %d\n"),
195                                                                         server_port);
196                         }
197                         else {
199                                 /* else we got the REAL string, so check the return code */
201                                 time (&end_time);
203                                 result = STATE_OK;
205                                 status_line = (char *) strtok (buffer, "\n");
207                                 if (strstr (status_line, "200"))
208                                         result = STATE_OK;
210                                 /* client errors result in a warning state */
211                                 else if (strstr (status_line, "400"))
212                                         result = STATE_WARNING;
213                                 else if (strstr (status_line, "401"))
214                                         result = STATE_WARNING;
215                                 else if (strstr (status_line, "402"))
216                                         result = STATE_WARNING;
217                                 else if (strstr (status_line, "403"))
218                                         result = STATE_WARNING;
219                                 else if (strstr (status_line, "404"))
220                                         result = STATE_WARNING;
222                                 /* server errors result in a critical state */
223                                 else if (strstr (status_line, "500"))
224                                         result = STATE_CRITICAL;
225                                 else if (strstr (status_line, "501"))
226                                         result = STATE_CRITICAL;
227                                 else if (strstr (status_line, "502"))
228                                         result = STATE_CRITICAL;
229                                 else if (strstr (status_line, "503"))
230                                         result = STATE_CRITICAL;
232                                 else
233                                         result = STATE_UNKNOWN;
234                         }
235                 }
236         }
238         /* Return results */
239         if (result == STATE_OK) {
241                 if (check_critical_time == TRUE
242                                 && (end_time - start_time) > critical_time) result = STATE_CRITICAL;
243                 else if (check_warning_time == TRUE
244                                                  && (end_time - start_time) > warning_time) result =
245                                 STATE_WARNING;
247                 /* Put some HTML in here to create a dynamic link */
248                 printf (_("REAL %s - %d second response time\n"),
249                                                 state_text (result),
250                                                 (int) (end_time - start_time));
251         }
252         else
253                 printf ("%s\n", status_line);
255         /* close the connection */
256         close (sd);
258         /* reset the alarm */
259         alarm (0);
261         return result;
266 /* process command-line arguments */
267 int
268 process_arguments (int argc, char **argv)
270         int c;
272         int option = 0;
273         static struct option longopts[] = {
274                 {"hostname", required_argument, 0, 'H'},
275                 {"IPaddress", required_argument, 0, 'I'},
276                 {"expect", required_argument, 0, 'e'},
277                 {"url", required_argument, 0, 'u'},
278                 {"port", required_argument, 0, 'p'},
279                 {"critical", required_argument, 0, 'c'},
280                 {"warning", required_argument, 0, 'w'},
281                 {"timeout", required_argument, 0, 't'},
282                 {"verbose", no_argument, 0, 'v'},
283                 {"version", no_argument, 0, 'V'},
284                 {"help", no_argument, 0, 'h'},
285                 {0, 0, 0, 0}
286         };
288         if (argc < 2)
289                 return ERROR;
291         for (c = 1; c < argc; c++) {
292                 if (strcmp ("-to", argv[c]) == 0)
293                         strcpy (argv[c], "-t");
294                 else if (strcmp ("-wt", argv[c]) == 0)
295                         strcpy (argv[c], "-w");
296                 else if (strcmp ("-ct", argv[c]) == 0)
297                         strcpy (argv[c], "-c");
298         }
300         while (1) {
301                 c = getopt_long (argc, argv, "+hvVI:H:e:u:p:w:c:t:", longopts,
302                                                                          &option);
304                 if (c == -1 || c == EOF)
305                         break;
307                 switch (c) {
308                 case 'I':                                                                       /* hostname */
309                 case 'H':                                                                       /* hostname */
310                         if (server_address)
311                                 break;
312                         else if (is_host (optarg))
313                                 server_address = optarg;
314                         else
315                                 usage2 (_("Invalid hostname/address"), optarg);
316                         break;
317                 case 'e':                                                                       /* string to expect in response header */
318                         server_expect = optarg;
319                         break;
320                 case 'u':                                                                       /* server URL */
321                         server_url = optarg;
322                         break;
323                 case 'p':                                                                       /* port */
324                         if (is_intpos (optarg)) {
325                                 server_port = atoi (optarg);
326                         }
327                         else {
328                                 usage4 (_("Port must be a positive integer"));
329                         }
330                         break;
331                 case 'w':                                                                       /* warning time threshold */
332                         if (is_intnonneg (optarg)) {
333                                 warning_time = atoi (optarg);
334                                 check_warning_time = TRUE;
335                         }
336                         else {
337                                 usage4 (_("Warning time must be a positive integer"));
338                         }
339                         break;
340                 case 'c':                                                                       /* critical time threshold */
341                         if (is_intnonneg (optarg)) {
342                                 critical_time = atoi (optarg);
343                                 check_critical_time = TRUE;
344                         }
345                         else {
346                                 usage4 (_("Critical time must be a positive integer"));
347                         }
348                         break;
349                 case 'v':                                                                       /* verbose */
350                         verbose = TRUE;
351                         break;
352                 case 't':                                                                       /* timeout */
353                         if (is_intnonneg (optarg)) {
354                                 socket_timeout = atoi (optarg);
355                         }
356                         else {
357                                 usage4 (_("Timeout interval must be a positive integer"));
358                         }
359                         break;
360                 case 'V':                                                                       /* version */
361                         print_revision (progname, revision);
362                         exit (STATE_OK);
363                 case 'h':                                                                       /* help */
364                         print_help ();
365                         exit (STATE_OK);
366                 case '?':                                                                       /* usage */
367                         usage5 ();
368                 }
369         }
371         c = optind;
372         if (server_address==NULL && argc>c) {
373                 if (is_host (argv[c])) {
374                         server_address = argv[c++];
375                 }
376                 else {
377                         usage2 (_("Invalid hostname/address"), argv[c]);
378                 }
379         }
381         if (server_address==NULL)
382                 usage4 (_("You must provide a server to check"));
384         if (host_name==NULL)
385                 host_name = strdup (server_address);
387         if (server_expect == NULL)
388                 server_expect = strdup(EXPECT);
390         return validate_arguments ();
395 int
396 validate_arguments (void)
398         return OK;
403 void
404 print_help (void)
406         char *myport;
407         asprintf (&myport, "%d", PORT);
409         print_revision (progname, revision);
411         printf ("Copyright (c) 1999 Pedro Leite <leite@cic.ua.pt>\n");
412         printf (COPYRIGHT, copyright, email);
414         printf ("%s\n", _("This plugin tests the REAL service on the specified host."));
416   printf ("\n\n");
418         print_usage ();
420         printf (_(UT_HELP_VRSN));
422         printf (_(UT_HOST_PORT), 'p', myport);
424         printf (" %s\n", "-u, --url=STRING");
425   printf ("    %s\n", _("Connect to this url"));
426   printf (" %s\n", "-e, --expect=STRING");
427   printf (_("String to expect in first line of server response (default: %s)\n"),
428                EXPECT);
430         printf (_(UT_WARN_CRIT));
432         printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
434         printf (_(UT_VERBOSE));
436   printf ("\n");
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);