Code

Fix translations when extra-opts aren't enabled
[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 * Description:
9
10 * This file contains the check_real plugin
11
12 * This plugin tests the REAL service on the specified host.
13
14
15 * This program is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation, either version 3 of the License, or
18 * (at your option) any later version.
19
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 * GNU General Public License for more details.
24
25 * You should have received a copy of the GNU General Public License
26 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27
28
29 *****************************************************************************/
31 const char *progname = "check_real";
32 const char *copyright = "2000-2007";
33 const char *email = "nagiosplug-devel@lists.sourceforge.net";
35 #include "common.h"
36 #include "netutils.h"
37 #include "utils.h"
39 enum {
40         PORT    = 554
41 };
43 #define EXPECT  "RTSP/1."
44 #define URL     ""
46 int process_arguments (int, char **);
47 int validate_arguments (void);
48 void print_help (void);
49 void print_usage (void);
51 int server_port = PORT;
52 char *server_address;
53 char *host_name;
54 char *server_url = NULL;
55 char *server_expect;
56 int warning_time = 0;
57 int check_warning_time = FALSE;
58 int critical_time = 0;
59 int check_critical_time = FALSE;
60 int verbose = FALSE;
64 int
65 main (int argc, char **argv)
66 {
67         int sd;
68         int result = STATE_UNKNOWN;
69         char buffer[MAX_INPUT_BUFFER];
70         char *status_line = NULL;
72         setlocale (LC_ALL, "");
73         bindtextdomain (PACKAGE, LOCALEDIR);
74         textdomain (PACKAGE);
76         /* Parse extra opts if any */
77         argv=np_extra_opts (&argc, argv, progname);
79         if (process_arguments (argc, argv) == ERROR)
80                 usage4 (_("Could not parse arguments"));
82         /* initialize alarm signal handling */
83         signal (SIGALRM, socket_timeout_alarm_handler);
85         /* set socket timeout */
86         alarm (socket_timeout);
87         time (&start_time);
89         /* try to connect to the host at the given port number */
90         if (my_tcp_connect (server_address, server_port, &sd) != STATE_OK)
91                 die (STATE_CRITICAL, _("Unable to connect to %s on port %d\n"),
92                                                          server_address, server_port);
94         /* Part I - Server Check */
96         /* send the OPTIONS request */
97         sprintf (buffer, "OPTIONS rtsp://%s:%d RTSP/1.0\r\n", host_name, server_port);
98         result = send (sd, buffer, strlen (buffer), 0);
100         /* send the header sync */
101         sprintf (buffer, "CSeq: 1\r\n");
102         result = send (sd, buffer, strlen (buffer), 0);
104         /* send a newline so the server knows we're done with the request */
105         sprintf (buffer, "\r\n");
106         result = send (sd, buffer, strlen (buffer), 0);
108         /* watch for the REAL connection string */
109         result = recv (sd, buffer, MAX_INPUT_BUFFER - 1, 0);
111         /* return a CRITICAL status if we couldn't read any data */
112         if (result == -1)
113                 die (STATE_CRITICAL, _("No data received from %s\n"), host_name);
115         /* make sure we find the response we are looking for */
116         if (!strstr (buffer, server_expect)) {
117                 if (server_port == PORT)
118                         printf ("%s\n", _("Invalid REAL response received from host"));
119                 else
120                         printf (_("Invalid REAL response received from host on port %d\n"),
121                                                         server_port);
122         }
123         else {
124                 /* else we got the REAL string, so check the return code */
126                 time (&end_time);
128                 result = STATE_OK;
130                 status_line = (char *) strtok (buffer, "\n");
132                 if (strstr (status_line, "200"))
133                         result = STATE_OK;
135                 /* client errors result in a warning state */
136                 else if (strstr (status_line, "400"))
137                         result = STATE_WARNING;
138                 else if (strstr (status_line, "401"))
139                         result = STATE_WARNING;
140                 else if (strstr (status_line, "402"))
141                         result = STATE_WARNING;
142                 else if (strstr (status_line, "403"))
143                         result = STATE_WARNING;
144                 else if (strstr (status_line, "404"))
145                         result = STATE_WARNING;
147                 /* server errors result in a critical state */
148                 else if (strstr (status_line, "500"))
149                         result = STATE_CRITICAL;
150                 else if (strstr (status_line, "501"))
151                         result = STATE_CRITICAL;
152                 else if (strstr (status_line, "502"))
153                         result = STATE_CRITICAL;
154                 else if (strstr (status_line, "503"))
155                         result = STATE_CRITICAL;
157                 else
158                         result = STATE_UNKNOWN;
159         }
161         /* Part II - Check stream exists and is ok */
162         if ((result == STATE_OK )&& (server_url != NULL) ) {
164                 /* Part I - Server Check */
166                 /* send the OPTIONS request */
167                 sprintf (buffer, "DESCRIBE rtsp://%s:%d%s RTSP/1.0\n", host_name,
168                                                  server_port, server_url);
169                 result = send (sd, buffer, strlen (buffer), 0);
171                 /* send the header sync */
172                 sprintf (buffer, "CSeq: 2\n");
173                 result = send (sd, buffer, strlen (buffer), 0);
175                 /* send a newline so the server knows we're done with the request */
176                 sprintf (buffer, "\n");
177                 result = send (sd, buffer, strlen (buffer), 0);
179                 /* watch for the REAL connection string */
180                 result = recv (sd, buffer, MAX_INPUT_BUFFER - 1, 0);
182                 /* return a CRITICAL status if we couldn't read any data */
183                 if (result == -1) {
184                         printf (_("No data received from host\n"));
185                         result = STATE_CRITICAL;
186                 }
187                 else {
188                         /* make sure we find the response we are looking for */
189                         if (!strstr (buffer, server_expect)) {
190                                 if (server_port == PORT)
191                                         printf ("%s\n", _("Invalid REAL response received from host"));
192                                 else
193                                         printf (_("Invalid REAL response received from host on port %d\n"),
194                                                                         server_port);
195                         }
196                         else {
198                                 /* else we got the REAL string, so check the return code */
200                                 time (&end_time);
202                                 result = STATE_OK;
204                                 status_line = (char *) strtok (buffer, "\n");
206                                 if (strstr (status_line, "200"))
207                                         result = STATE_OK;
209                                 /* client errors result in a warning state */
210                                 else if (strstr (status_line, "400"))
211                                         result = STATE_WARNING;
212                                 else if (strstr (status_line, "401"))
213                                         result = STATE_WARNING;
214                                 else if (strstr (status_line, "402"))
215                                         result = STATE_WARNING;
216                                 else if (strstr (status_line, "403"))
217                                         result = STATE_WARNING;
218                                 else if (strstr (status_line, "404"))
219                                         result = STATE_WARNING;
221                                 /* server errors result in a critical state */
222                                 else if (strstr (status_line, "500"))
223                                         result = STATE_CRITICAL;
224                                 else if (strstr (status_line, "501"))
225                                         result = STATE_CRITICAL;
226                                 else if (strstr (status_line, "502"))
227                                         result = STATE_CRITICAL;
228                                 else if (strstr (status_line, "503"))
229                                         result = STATE_CRITICAL;
231                                 else
232                                         result = STATE_UNKNOWN;
233                         }
234                 }
235         }
237         /* Return results */
238         if (result == STATE_OK) {
240                 if (check_critical_time == TRUE
241                                 && (end_time - start_time) > critical_time) result = STATE_CRITICAL;
242                 else if (check_warning_time == TRUE
243                                                  && (end_time - start_time) > warning_time) result =
244                                 STATE_WARNING;
246                 /* Put some HTML in here to create a dynamic link */
247                 printf (_("REAL %s - %d second response time\n"),
248                                                 state_text (result),
249                                                 (int) (end_time - start_time));
250         }
251         else
252                 printf ("%s\n", status_line);
254         /* close the connection */
255         close (sd);
257         /* reset the alarm */
258         alarm (0);
260         return result;
265 /* process command-line arguments */
266 int
267 process_arguments (int argc, char **argv)
269         int c;
271         int option = 0;
272         static struct option longopts[] = {
273                 {"hostname", required_argument, 0, 'H'},
274                 {"IPaddress", required_argument, 0, 'I'},
275                 {"expect", required_argument, 0, 'e'},
276                 {"url", required_argument, 0, 'u'},
277                 {"port", required_argument, 0, 'p'},
278                 {"critical", required_argument, 0, 'c'},
279                 {"warning", required_argument, 0, 'w'},
280                 {"timeout", required_argument, 0, 't'},
281                 {"verbose", no_argument, 0, 'v'},
282                 {"version", no_argument, 0, 'V'},
283                 {"help", no_argument, 0, 'h'},
284                 {0, 0, 0, 0}
285         };
287         if (argc < 2)
288                 return ERROR;
290         for (c = 1; c < argc; c++) {
291                 if (strcmp ("-to", argv[c]) == 0)
292                         strcpy (argv[c], "-t");
293                 else if (strcmp ("-wt", argv[c]) == 0)
294                         strcpy (argv[c], "-w");
295                 else if (strcmp ("-ct", argv[c]) == 0)
296                         strcpy (argv[c], "-c");
297         }
299         while (1) {
300                 c = getopt_long (argc, argv, "+hvVI:H:e:u:p:w:c:t:", longopts,
301                                                                          &option);
303                 if (c == -1 || c == EOF)
304                         break;
306                 switch (c) {
307                 case 'I':                                                                       /* hostname */
308                 case 'H':                                                                       /* hostname */
309                         if (server_address)
310                                 break;
311                         else if (is_host (optarg))
312                                 server_address = optarg;
313                         else
314                                 usage2 (_("Invalid hostname/address"), optarg);
315                         break;
316                 case 'e':                                                                       /* string to expect in response header */
317                         server_expect = optarg;
318                         break;
319                 case 'u':                                                                       /* server URL */
320                         server_url = optarg;
321                         break;
322                 case 'p':                                                                       /* port */
323                         if (is_intpos (optarg)) {
324                                 server_port = atoi (optarg);
325                         }
326                         else {
327                                 usage4 (_("Port must be a positive integer"));
328                         }
329                         break;
330                 case 'w':                                                                       /* warning time threshold */
331                         if (is_intnonneg (optarg)) {
332                                 warning_time = atoi (optarg);
333                                 check_warning_time = TRUE;
334                         }
335                         else {
336                                 usage4 (_("Warning time must be a positive integer"));
337                         }
338                         break;
339                 case 'c':                                                                       /* critical time threshold */
340                         if (is_intnonneg (optarg)) {
341                                 critical_time = atoi (optarg);
342                                 check_critical_time = TRUE;
343                         }
344                         else {
345                                 usage4 (_("Critical time must be a positive integer"));
346                         }
347                         break;
348                 case 'v':                                                                       /* verbose */
349                         verbose = TRUE;
350                         break;
351                 case 't':                                                                       /* timeout */
352                         if (is_intnonneg (optarg)) {
353                                 socket_timeout = atoi (optarg);
354                         }
355                         else {
356                                 usage4 (_("Timeout interval must be a positive integer"));
357                         }
358                         break;
359                 case 'V':                                                                       /* version */
360                         print_revision (progname, NP_VERSION);
361                         exit (STATE_OK);
362                 case 'h':                                                                       /* help */
363                         print_help ();
364                         exit (STATE_OK);
365                 case '?':                                                                       /* usage */
366                         usage5 ();
367                 }
368         }
370         c = optind;
371         if (server_address==NULL && argc>c) {
372                 if (is_host (argv[c])) {
373                         server_address = argv[c++];
374                 }
375                 else {
376                         usage2 (_("Invalid hostname/address"), argv[c]);
377                 }
378         }
380         if (server_address==NULL)
381                 usage4 (_("You must provide a server to check"));
383         if (host_name==NULL)
384                 host_name = strdup (server_address);
386         if (server_expect == NULL)
387                 server_expect = strdup(EXPECT);
389         return validate_arguments ();
394 int
395 validate_arguments (void)
397         return OK;
402 void
403 print_help (void)
405         char *myport;
406         asprintf (&myport, "%d", PORT);
408         print_revision (progname, NP_VERSION);
410         printf ("Copyright (c) 1999 Pedro Leite <leite@cic.ua.pt>\n");
411         printf (COPYRIGHT, copyright, email);
413         printf ("%s\n", _("This plugin tests the REAL service on the specified host."));
415   printf ("\n\n");
417         print_usage ();
419         printf (UT_HELP_VRSN);
420         printf (UT_EXTRA_OPTS);
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 #ifdef NP_EXTRA_OPTS
444   printf ("\n");
445   printf ("%s\n", _("Notes:"));
446   printf (UT_EXTRA_OPTS_NOTES);
447 #endif
449         printf (UT_SUPPORT);
454 void
455 print_usage (void)
457   printf (_("Usage:"));
458         printf ("%s -H host [-e expect] [-p port] [-w warn] [-c crit] [-t timeout] [-v]\n", progname);