Code

\r\n patch from Mathias
[nagiosplug.git] / plugins / check_real.c
1 /*****************************************************************************
2 *
3 * CHECK_REAL.C
4 *
5 * Program: RealMedia plugin for Nagios
6 * License: GPL
7 * Copyright (c) 1999 Pedro Leite (leite@cic.ua.pt)
8 *
9 * Based on CHECK_HTTP.C
10 * Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)
11 *
12 * Last Modified: $Date$
13 *
14 * Command line: CHECK_REAL <host_address> [-e expect] [-u url] [-p port]
15 *                          [-hn host_name] [-wt warn_time] [-ct crit_time]
16 *                          [-to to_sec]
17 *
18 * Description:
19 *
20 * This plugin will attempt to open an RTSP connection with the host.
21 * Successul connects return STATE_OK, refusals and timeouts return
22 * STATE_CRITICAL, other errors return STATE_UNKNOWN.  Successful connects,
23 * but incorrect reponse messages from the host result in STATE_WARNING return
24 * values.  If you are checking a virtual server that uses "host headers"you
25 * must supply the FQDN (fully qualified domain name) as the [host_name]
26 * argument.
27 *
28 * License Information:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License
41 * along with this program; if not, write to the Free Software
42 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
43 *
44 ****************************************************************************/
46 #include "config.h"
47 #include "common.h"
48 #include "netutils.h"
49 #include "utils.h"
51 const char *progname = "check_real";
53 #define PORT    554
54 #define EXPECT  "RTSP/1."
55 #define URL     ""
57 int process_arguments (int, char **);
58 int validate_arguments (void);
59 int check_disk (int usp, int free_disk);
60 void print_help (void);
61 void print_usage (void);
63 int server_port = PORT;
64 char *server_address = "";
65 char *host_name = "";
66 char *server_url = NULL;
67 char *server_expect = EXPECT;
68 int warning_time = 0;
69 int check_warning_time = FALSE;
70 int critical_time = 0;
71 int check_critical_time = FALSE;
72 int verbose = FALSE;
74 int
75 main (int argc, char **argv)
76 {
77         int sd;
78         int result;
79         char buffer[MAX_INPUT_BUFFER];
80         char *status_line = NULL;
82         if (process_arguments (argc, argv) != OK)
83                 usage ("Invalid command arguments supplied\n");
85         /* initialize alarm signal handling */
86         signal (SIGALRM, socket_timeout_alarm_handler);
88         /* set socket timeout */
89         alarm (socket_timeout);
90         time (&start_time);
92         /* try to connect to the host at the given port number */
93         if (my_tcp_connect (server_address, server_port, &sd) != STATE_OK)
94                 terminate (STATE_CRITICAL, "Unable to connect to %s on port %d\n",
95                                                          server_address, server_port);
97         /* Part I - Server Check */
99         /* send the OPTIONS request */
100         sprintf (buffer, "OPTIONS rtsp://%s:%d RTSP/1.0\r\n", host_name, server_port);
101         result = send (sd, buffer, strlen (buffer), 0);
103         /* send the header sync */
104         sprintf (buffer, "CSeq: 1\r\n");
105         result = send (sd, buffer, strlen (buffer), 0);
107         /* send a newline so the server knows we're done with the request */
108         sprintf (buffer, "\r\n");
109         result = send (sd, buffer, strlen (buffer), 0);
111         /* watch for the REAL connection string */
112         result = recv (sd, buffer, MAX_INPUT_BUFFER - 1, 0);
114         /* return a CRITICAL status if we couldn't read any data */
115         if (result == -1)
116                 terminate (STATE_CRITICAL, "No data received from %s\n", host_name);
118         /* make sure we find the response we are looking for */
119         if (!strstr (buffer, server_expect)) {
120                 if (server_port == PORT)
121                         printf ("Invalid REAL response received from host\n");
122                 else
123                         printf ("Invalid REAL response received from host on port %d\n",
124                                                         server_port);
125         }
126         else {
127                 /* else we got the REAL string, so check the return code */
129                 time (&end_time);
131                 result = STATE_OK;
133                 status_line = (char *) strtok (buffer, "\n");
135                 if (strstr (status_line, "200"))
136                         result = STATE_OK;
138                 /* client errors result in a warning state */
139                 else if (strstr (status_line, "400"))
140                         result = STATE_WARNING;
141                 else if (strstr (status_line, "401"))
142                         result = STATE_WARNING;
143                 else if (strstr (status_line, "402"))
144                         result = STATE_WARNING;
145                 else if (strstr (status_line, "403"))
146                         result = STATE_WARNING;
147                 else if (strstr (status_line, "404"))
148                         result = STATE_WARNING;
150                 /* server errors result in a critical state */
151                 else if (strstr (status_line, "500"))
152                         result = STATE_CRITICAL;
153                 else if (strstr (status_line, "501"))
154                         result = STATE_CRITICAL;
155                 else if (strstr (status_line, "502"))
156                         result = STATE_CRITICAL;
157                 else if (strstr (status_line, "503"))
158                         result = STATE_CRITICAL;
160                 else
161                         result = STATE_UNKNOWN;
162         }
164         /* Part II - Check stream exists and is ok */
165         if ((result == STATE_OK )&& (server_url != NULL) ) {
167                 /* Part I - Server Check */
169                 /* send the OPTIONS request */
170                 sprintf (buffer, "DESCRIBE rtsp://%s:%d%s RTSP/1.0\n", host_name,
171                                                  server_port, server_url);
172                 result = send (sd, buffer, strlen (buffer), 0);
174                 /* send the header sync */
175                 sprintf (buffer, "CSeq: 2\n");
176                 result = send (sd, buffer, strlen (buffer), 0);
178                 /* send a newline so the server knows we're done with the request */
179                 sprintf (buffer, "\n");
180                 result = send (sd, buffer, strlen (buffer), 0);
182                 /* watch for the REAL connection string */
183                 result = recv (sd, buffer, MAX_INPUT_BUFFER - 1, 0);
185                 /* return a CRITICAL status if we couldn't read any data */
186                 if (result == -1) {
187                         printf ("No data received from host\n");
188                         result = STATE_CRITICAL;
189                 }
190                 else {
191                         /* make sure we find the response we are looking for */
192                         if (!strstr (buffer, server_expect)) {
193                                 if (server_port == PORT)
194                                         printf ("Invalid REAL response received from host\n");
195                                 else
196                                         printf ("Invalid REAL response received from host on port %d\n",
197                                                                         server_port);
198                         }
199                         else {
201                                 /* else we got the REAL string, so check the return code */
203                                 time (&end_time);
205                                 result = STATE_OK;
207                                 status_line = (char *) strtok (buffer, "\n");
209                                 if (strstr (status_line, "200"))
210                                         result = STATE_OK;
212                                 /* client errors result in a warning state */
213                                 else if (strstr (status_line, "400"))
214                                         result = STATE_WARNING;
215                                 else if (strstr (status_line, "401"))
216                                         result = STATE_WARNING;
217                                 else if (strstr (status_line, "402"))
218                                         result = STATE_WARNING;
219                                 else if (strstr (status_line, "403"))
220                                         result = STATE_WARNING;
221                                 else if (strstr (status_line, "404"))
222                                         result = STATE_WARNING;
224                                 /* server errors result in a critical state */
225                                 else if (strstr (status_line, "500"))
226                                         result = STATE_CRITICAL;
227                                 else if (strstr (status_line, "501"))
228                                         result = STATE_CRITICAL;
229                                 else if (strstr (status_line, "502"))
230                                         result = STATE_CRITICAL;
231                                 else if (strstr (status_line, "503"))
232                                         result = STATE_CRITICAL;
234                                 else
235                                         result = STATE_UNKNOWN;
236                         }
237                 }
238         }
240         /* Return results */
241         if (result == STATE_OK) {
243                 if (check_critical_time == TRUE
244                                 && (end_time - start_time) > critical_time) result = STATE_CRITICAL;
245                 else if (check_warning_time == TRUE
246                                                  && (end_time - start_time) > warning_time) result =
247                                 STATE_WARNING;
249                 /* Put some HTML in here to create a dynamic link */
250                 printf ("REAL %s - %d second response time\n",
251                                                 (result == STATE_OK) ? "ok" : "problem",
252                                                 (int) (end_time - start_time));
253         }
254         else
255                 printf ("%s\n", status_line);
257         /* close the connection */
258         close (sd);
260         /* reset the alarm */
261         alarm (0);
263         return result;
271 /* process command-line arguments */
272 int
273 process_arguments (int argc, char **argv)
275         int c;
277         int option_index = 0;
278         static struct option long_options[] = {
279                 {"hostname", required_argument, 0, 'H'},
280                 {"IPaddress", required_argument, 0, 'I'},
281                 {"expect", required_argument, 0, 'e'},
282                 {"url", required_argument, 0, 'u'},
283                 {"port", required_argument, 0, 'p'},
284                 {"critical", required_argument, 0, 'c'},
285                 {"warning", required_argument, 0, 'w'},
286                 {"timeout", required_argument, 0, 't'},
287                 {"verbose", no_argument, 0, 'v'},
288                 {"version", no_argument, 0, 'V'},
289                 {"help", no_argument, 0, 'h'},
290                 {0, 0, 0, 0}
291         };
293         if (argc < 2)
294                 return ERROR;
296         for (c = 1; c < argc; c++) {
297                 if (strcmp ("-to", argv[c]) == 0)
298                         strcpy (argv[c], "-t");
299                 else if (strcmp ("-wt", argv[c]) == 0)
300                         strcpy (argv[c], "-w");
301                 else if (strcmp ("-ct", argv[c]) == 0)
302                         strcpy (argv[c], "-c");
303         }
305         while (1) {
306                 c = getopt_long (argc, argv, "+hVI:H:e:u:p:w:c:t:", long_options,
307                                                                          &option_index);
309                 if (c == -1 || c == EOF)
310                         break;
312                 switch (c) {
313                 case 'I':                                                                       /* hostname */
314                 case 'H':                                                                       /* hostname */
315                         if (is_host (optarg)) {
316                                 server_address = optarg;
317                         }
318                         else {
319                                 usage ("Invalid host name\n");
320                         }
321                         break;
322                 case 'e':                                                                       /* string to expect in response header */
323                         server_expect = optarg;
324                         break;
325                 case 'u':                                                                       /* server URL */
326                         server_url = optarg;
327                         break;
328                 case 'p':                                                                       /* port */
329                         if (is_intpos (optarg)) {
330                                 server_port = atoi (optarg);
331                         }
332                         else {
333                                 usage ("Server port must be a positive integer\n");
334                         }
335                         break;
336                 case 'w':                                                                       /* warning time threshold */
337                         if (is_intnonneg (optarg)) {
338                                 warning_time = atoi (optarg);
339                                 check_warning_time = TRUE;
340                         }
341                         else {
342                                 usage ("Warning time must be a nonnegative integer\n");
343                         }
344                         break;
345                 case 'c':                                                                       /* critical time threshold */
346                         if (is_intnonneg (optarg)) {
347                                 critical_time = atoi (optarg);
348                                 check_critical_time = TRUE;
349                         }
350                         else {
351                                 usage ("Critical time must be a nonnegative integer\n");
352                         }
353                         break;
354                 case 'v':                                                                       /* verbose */
355                         verbose = TRUE;
356                         break;
357                 case 't':                                                                       /* timeout */
358                         if (is_intnonneg (optarg)) {
359                                 socket_timeout = atoi (optarg);
360                         }
361                         else {
362                                 usage ("Time interval must be a nonnegative integer\n");
363                         }
364                         break;
365                 case 'V':                                                                       /* version */
366                         print_revision (progname, "$Revision$");
367                         exit (STATE_OK);
368                 case 'h':                                                                       /* help */
369                         print_help ();
370                         exit (STATE_OK);
371                 case '?':                                                                       /* help */
372                         usage ("Invalid argument\n");
373                 }
374         }
376         c = optind;
377         if (strlen(server_address)==0 && argc>c) {
378                 if (is_host (argv[c])) {
379                         server_address = argv[c++];
380                 }
381                 else {
382                         usage ("Invalid host name");
383                 }
384         }
386         if (strlen(server_address) == 0)
387                 usage ("You must provide a server to check\n");
389         if (strlen(host_name) == 0)
390                 asprintf (&host_name, "%s", server_address);
392         return validate_arguments ();
399 int
400 validate_arguments (void)
402         return OK;
409 void
410 print_help (void)
412         print_revision (progname, "$Revision$");
413         printf
414                 ("Copyright (c) 2000 Pedro Leite (leite@cic.ua.pt)/Karl DeBisschop\n\n"
415                  "This plugin tests the REAL service on the specified host.\n\n");
416         print_usage ();
417         printf
418                 ("\nOptions:\n"
419                  " -H, --hostname=STRING or IPADDRESS\n"
420                  "   Check this server on the indicated host\n"
421                  " -I, --IPaddress=STRING or IPADDRESS\n"
422                  "   Check server at this host address\n"
423                  " -p, --port=INTEGER\n"
424                  "   Make connection on the indicated port (default: %d)\n"
425                  " -u, --url=STRING\n"
426                  "   Connect to this url\n"
427                  " -e, --expect=STRING\n"
428                  "   String to expect in first line of server response (default: %s)\n"
429                  " -w, --warning=INTEGER\n"
430                  "   Seconds necessary to result in a warning status\n"
431                  " -c, --critical=INTEGER\n"
432                  "   Seconds necessary to result in a critical status\n"
433                  " -t, --timeout=INTEGER\n"
434                  "   Seconds before connection attempt times out (default: %d)\n"
435                  " -v, --verbose\n"
436                  "   Print extra information (command-line use only)\n"
437                  " -h, --help\n"
438                  "   Print detailed help screen\n"
439                  " -V, --version\n"
440                  "   Print version information\n\n",
441                  PORT, EXPECT, DEFAULT_SOCKET_TIMEOUT);
442         support ();
449 void
450 print_usage (void)
452         printf
453                 ("Usage: %s -H host [-e expect] [-p port] [-w warn] [-c crit]\n"
454                  "            [-t timeout] [-v]\n"
455                  "       %s --help\n"
456                  "       %s --version\n", progname, progname, progname);