1 /******************************************************************************
2 *
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.
7 *
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.
12 *
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.
16 *
17 *****************************************************************************/
19 /* progname changes depending on symlink called */
20 char *progname = "check_tcp";
21 const char *revision = "$Revision$";
22 const char *copyright = "2002-2003";
23 const char *authors = "Nagios Plugin Development Team";
24 const char *email = "nagiosplug-devel@lists.sourceforge.net";
26 const char *summary = "\
27 This plugin tests %s connections with the specified host.\n";
29 const char *option_summary = "\
30 -H host -p port [-w warn_time] [-c crit_time] [-s send]\n\
31 [-e expect] [-W wait] [-t to_sec] [-r refuse_state] [-v]\n";
33 const char *options = "\
34 -H, --hostname=ADDRESS\n\
35 Host name argument for servers using host headers (use numeric\n\
36 address if possible to bypass DNS lookup).\n\
37 -p, --port=INTEGER\n\
38 Port number\n\
39 -s, --send=STRING\n\
40 String to send to the server\n\
41 -e, --expect=STRING\n\
42 String to expect in server response\n\
43 -W, --wait=INTEGER\n\
44 Seconds to wait between sending string and polling for response\n\
45 -w, --warning=DOUBLE\n\
46 Response time to result in warning status (seconds)\n\
47 -c, --critical=DOUBLE\n\
48 Response time to result in critical status (seconds)\n\
49 -t, --timeout=INTEGER\n\
50 Seconds before connection times out (default: %d)\n\
51 -r, --refuse=ok|warn|crit\n\
52 Accept tcp refusals with states ok, warn, crit (default: crit)\n\
53 -v\n\
54 Show details for command-line debugging (do not use with nagios server)\n\
55 -h, --help\n\
56 Print detailed help screen\n\
57 -V, --version\n\
58 Print version information";
60 #include "config.h"
61 #include "common.h"
62 #include "netutils.h"
63 #include "utils.h"
65 #ifdef HAVE_SSL_H
66 #include <rsa.h>
67 #include <crypto.h>
68 #include <x509.h>
69 #include <pem.h>
70 #include <ssl.h>
71 #include <err.h>
72 #endif
74 #ifdef HAVE_OPENSSL_SSL_H
75 #include <openssl/rsa.h>
76 #include <openssl/crypto.h>
77 #include <openssl/x509.h>
78 #include <openssl/pem.h>
79 #include <openssl/ssl.h>
80 #include <openssl/err.h>
81 #endif
83 #ifdef HAVE_SSL
84 SSL_CTX *ctx;
85 SSL *ssl;
86 int connect_SSL (void);
87 #endif
89 enum {
90 TCP_PROTOCOL = 1,
91 UDP_PROTOCOL = 2,
92 MAXBUF = 1024
93 };
95 int process_arguments (int, char **);
96 void print_usage (void);
97 void print_help (void);
98 int my_recv (void);
100 char *SERVICE = NULL;
101 char *SEND = NULL;
102 char *EXPECT = NULL;
103 char *QUIT = NULL;
104 int PROTOCOL = 0;
105 int PORT = 0;
107 int server_port = 0;
108 char *server_address = NULL;
109 char *server_send = NULL;
110 char *server_quit = NULL;
111 char **server_expect = NULL;
112 int server_expect_count = 0;
113 char **warn_codes = NULL;
114 int warn_codes_count = 0;
115 char **crit_codes = NULL;
116 int crit_codes_count = 0;
117 int delay = 0;
118 double warning_time = 0;
119 int check_warning_time = FALSE;
120 double critical_time = 0;
121 int check_critical_time = FALSE;
122 double elapsed_time = 0;
123 int verbose = FALSE;
124 int use_ssl = FALSE;
125 int sd = 0;
126 char *buffer = "";
128 int
129 main (int argc, char **argv)
130 {
131 int result;
132 int i;
133 char *status = "";
134 struct timeval tv;
136 if (strstr (argv[0], "check_udp")) {
137 asprintf (&progname, "check_udp");
138 asprintf (&SERVICE, "UDP");
139 SEND = NULL;
140 EXPECT = NULL;
141 QUIT = NULL;
142 PROTOCOL = UDP_PROTOCOL;
143 PORT = 0;
144 }
145 else if (strstr (argv[0], "check_tcp")) {
146 asprintf (&progname, "check_tcp");
147 asprintf (&SERVICE, "TCP");
148 SEND = NULL;
149 EXPECT = NULL;
150 QUIT = NULL;
151 PROTOCOL = TCP_PROTOCOL;
152 PORT = 0;
153 }
154 else if (strstr (argv[0], "check_ftp")) {
155 asprintf (&progname, "check_ftp");
156 asprintf (&SERVICE, "FTP");
157 SEND = NULL;
158 asprintf (&EXPECT, "220");
159 asprintf (&QUIT, "QUIT\r\n");
160 PROTOCOL = TCP_PROTOCOL;
161 PORT = 21;
162 }
163 else if (strstr (argv[0], "check_smtp")) {
164 asprintf (&progname, "check_smtp");
165 asprintf (&SERVICE, "SMTP");
166 SEND = NULL;
167 asprintf (&EXPECT, "220");
168 asprintf (&QUIT, "QUIT\r\n");
169 PROTOCOL = TCP_PROTOCOL;
170 PORT = 25;
171 }
172 else if (strstr (argv[0], "check_pop")) {
173 asprintf (&progname, "check_pop");
174 asprintf (&SERVICE, "POP");
175 SEND = NULL;
176 asprintf (&EXPECT, "+OK");
177 asprintf (&QUIT, "QUIT\r\n");
178 PROTOCOL = TCP_PROTOCOL;
179 PORT = 110;
180 }
181 else if (strstr (argv[0], "check_imap")) {
182 asprintf (&progname, "check_imap");
183 asprintf (&SERVICE, "IMAP");
184 SEND = NULL;
185 asprintf (&EXPECT, "* OK");
186 asprintf (&QUIT, "a1 LOGOUT\r\n");
187 PROTOCOL = TCP_PROTOCOL;
188 PORT = 143;
189 }
190 #ifdef HAVE_SSL
191 else if (strstr(argv[0],"check_simap")) {
192 asprintf (&progname, "check_simap");
193 asprintf (&SERVICE, "SIMAP");
194 SEND=NULL;
195 asprintf (&EXPECT, "* OK");
196 asprintf (&QUIT, "a1 LOGOUT\r\n");
197 PROTOCOL=TCP_PROTOCOL;
198 use_ssl=TRUE;
199 PORT=993;
200 }
201 else if (strstr(argv[0],"check_spop")) {
202 asprintf (&progname, "check_spop");
203 asprintf (&SERVICE, "SPOP");
204 SEND=NULL;
205 asprintf (&EXPECT, "+OK");
206 asprintf (&QUIT, "QUIT\r\n");
207 PROTOCOL=TCP_PROTOCOL;
208 use_ssl=TRUE;
209 PORT=995;
210 }
211 #endif
212 else if (strstr (argv[0], "check_nntp")) {
213 asprintf (&progname, "check_nntp");
214 asprintf (&SERVICE, "NNTP");
215 SEND = NULL;
216 EXPECT = NULL;
217 server_expect = realloc (server_expect, ++server_expect_count);
218 asprintf (&server_expect[server_expect_count - 1], "200");
219 server_expect = realloc (server_expect, ++server_expect_count);
220 asprintf (&server_expect[server_expect_count - 1], "201");
221 asprintf (&QUIT, "QUIT\r\n");
222 PROTOCOL = TCP_PROTOCOL;
223 PORT = 119;
224 }
225 else {
226 usage ("ERROR: Generic check_tcp called with unknown service\n");
227 }
229 asprintf (&server_address, "127.0.0.1");
230 server_port = PORT;
231 server_send = SEND;
232 server_quit = QUIT;
234 if (process_arguments (argc, argv) == ERROR)
235 usage ("Could not parse arguments\n");
237 /* use default expect if none listed in process_arguments() */
238 if (EXPECT && server_expect_count == 0) {
239 server_expect = malloc (++server_expect_count);
240 server_expect[server_expect_count - 1] = EXPECT;
241 }
243 /* initialize alarm signal handling */
244 signal (SIGALRM, socket_timeout_alarm_handler);
246 /* set socket timeout */
247 alarm (socket_timeout);
249 /* try to connect to the host at the given port number */
250 gettimeofday (&tv, NULL);
251 #ifdef HAVE_SSL
252 if (use_ssl)
253 result = connect_SSL ();
254 else
255 #endif
256 {
257 if (PROTOCOL == UDP_PROTOCOL)
258 result = my_udp_connect (server_address, server_port, &sd);
259 else
260 /* default is TCP */
261 result = my_tcp_connect (server_address, server_port, &sd);
262 }
264 if (result == STATE_CRITICAL)
265 return STATE_CRITICAL;
267 if (server_send != NULL) { /* Something to send? */
268 asprintf (&server_send, "%s\r\n", server_send);
269 #ifdef HAVE_SSL
270 if (use_ssl)
271 SSL_write(ssl, server_send, strlen (server_send));
272 else
273 #endif
274 send (sd, server_send, strlen (server_send), 0);
275 }
277 if (delay > 0) {
278 tv.tv_sec += delay;
279 sleep (delay);
280 }
282 if (server_send || server_expect_count > 0) {
284 buffer = malloc (MAXBUF);
285 memset (buffer, '\0', MAXBUF);
286 /* watch for the expect string */
287 while ((i = my_recv ()) > 0) {
288 buffer[i] = '\0';
289 asprintf (&status, "%s%s", status, buffer);
290 if (buffer[i-2] == '\r' && buffer[i-1] == '\n')
291 break;
292 }
294 /* return a CRITICAL status if we couldn't read any data */
295 if (status == NULL)
296 terminate (STATE_CRITICAL, "No data received from host\n");
298 strip (status);
300 if (status && verbose)
301 printf ("%s\n", status);
303 if (server_expect_count > 0) {
304 for (i = 0;; i++) {
305 if (verbose)
306 printf ("%d %d\n", i, server_expect_count);
307 if (i >= server_expect_count)
308 terminate (STATE_WARNING, "Invalid response from host\n");
309 if (strstr (status, server_expect[i]))
310 break;
311 }
312 }
313 }
315 if (server_quit != NULL)
316 #ifdef HAVE_SSL
317 if (use_ssl) {
318 SSL_write (ssl, QUIT, strlen (QUIT));
319 SSL_shutdown (ssl);
320 SSL_free (ssl);
321 SSL_CTX_free (ctx);
322 }
323 else
324 #endif
325 send (sd, server_quit, strlen (server_quit), 0);
327 /* close the connection */
328 if (sd)
329 close (sd);
331 elapsed_time = delta_time (tv);
333 if (check_critical_time == TRUE && elapsed_time > critical_time)
334 result = STATE_CRITICAL;
335 else if (check_warning_time == TRUE && elapsed_time > warning_time)
336 result = STATE_WARNING;
338 /* reset the alarm */
339 alarm (0);
341 printf
342 ("%s %s%s - %.3f second response time on port %d",
343 SERVICE,
344 state_text (result),
345 (was_refused) ? " (refused)" : "",
346 elapsed_time, server_port);
348 if (status && strlen(status) > 0)
349 printf (" [%s]", status);
351 printf ("|time=%.3f\n", elapsed_time);
353 return result;
354 }
362 /* process command-line arguments */
363 int
364 process_arguments (int argc, char **argv)
365 {
366 int c;
368 int option_index = 0;
369 static struct option long_options[] = {
370 {"hostname", required_argument, 0, 'H'},
371 {"critical-time", required_argument, 0, 'c'},
372 {"warning-time", required_argument, 0, 'w'},
373 {"critical-codes", required_argument, 0, 'C'},
374 {"warning-codes", required_argument, 0, 'W'},
375 {"timeout", required_argument, 0, 't'},
376 {"protocol", required_argument, 0, 'P'},
377 {"port", required_argument, 0, 'p'},
378 {"send", required_argument, 0, 's'},
379 {"expect", required_argument, 0, 'e'},
380 {"quit", required_argument, 0, 'q'},
381 {"delay", required_argument, 0, 'd'},
382 {"refuse", required_argument, 0, 'r'},
383 {"verbose", no_argument, 0, 'v'},
384 {"version", no_argument, 0, 'V'},
385 {"help", no_argument, 0, 'h'},
386 {0, 0, 0, 0}
387 };
389 if (argc < 2)
390 usage ("No arguments found\n");
392 /* backwards compatibility */
393 for (c = 1; c < argc; c++) {
394 if (strcmp ("-to", argv[c]) == 0)
395 strcpy (argv[c], "-t");
396 else if (strcmp ("-wt", argv[c]) == 0)
397 strcpy (argv[c], "-w");
398 else if (strcmp ("-ct", argv[c]) == 0)
399 strcpy (argv[c], "-c");
400 }
402 if (!is_option (argv[1])) {
403 server_address = argv[1];
404 argv[1] = argv[0];
405 argv = &argv[1];
406 argc--;
407 }
409 while (1) {
410 c = getopt_long (argc, argv, "+hVvH:s:e:q:c:w:t:p:C:W:d:Sr:", long_options,
411 &option_index);
413 if (c == -1 || c == EOF || c == 1)
414 break;
416 switch (c) {
417 case '?': /* print short usage statement if args not parsable */
418 printf ("%s: Unknown argument: %s\n\n", progname, optarg);
419 print_usage ();
420 exit (STATE_UNKNOWN);
421 case 'h': /* help */
422 print_help ();
423 exit (STATE_OK);
424 case 'V': /* version */
425 print_revision (progname, "$Revision$");
426 exit (STATE_OK);
427 case 'v': /* verbose mode */
428 verbose = TRUE;
429 break;
430 case 'H': /* hostname */
431 if (is_host (optarg) == FALSE)
432 usage2 ("invalid host name or address", optarg);
433 server_address = optarg;
434 break;
435 case 'c': /* critical */
436 if (!is_intnonneg (optarg))
437 usage ("Critical threshold must be a nonnegative integer\n");
438 critical_time = strtod (optarg, NULL);
439 check_critical_time = TRUE;
440 break;
441 case 'w': /* warning */
442 if (!is_intnonneg (optarg))
443 usage ("Warning threshold must be a nonnegative integer\n");
444 warning_time = strtod (optarg, NULL);
445 check_warning_time = TRUE;
446 break;
447 case 'C':
448 crit_codes = realloc (crit_codes, ++crit_codes_count);
449 crit_codes[crit_codes_count - 1] = optarg;
450 break;
451 case 'W':
452 warn_codes = realloc (warn_codes, ++warn_codes_count);
453 warn_codes[warn_codes_count - 1] = optarg;
454 break;
455 case 't': /* timeout */
456 if (!is_intpos (optarg))
457 usage ("Timeout interval must be a positive integer\n");
458 socket_timeout = atoi (optarg);
459 break;
460 case 'p': /* port */
461 if (!is_intpos (optarg))
462 usage ("Server port must be a positive integer\n");
463 server_port = atoi (optarg);
464 break;
465 case 's':
466 server_send = optarg;
467 break;
468 case 'e': /* expect string (may be repeated) */
469 EXPECT = NULL;
470 if (server_expect_count == 0)
471 server_expect = malloc (++server_expect_count);
472 else
473 server_expect = realloc (server_expect, ++server_expect_count);
474 server_expect[server_expect_count - 1] = optarg;
475 break;
476 case 'q':
477 server_quit = optarg;
478 break;
479 case 'r':
480 if (!strncmp(optarg,"ok",2))
481 econn_refuse_state = STATE_OK;
482 else if (!strncmp(optarg,"warn",4))
483 econn_refuse_state = STATE_WARNING;
484 else if (!strncmp(optarg,"crit",4))
485 econn_refuse_state = STATE_CRITICAL;
486 else
487 usage ("Refuse mut be one of ok, warn, crit\n");
488 break;
489 case 'd':
490 if (is_intpos (optarg))
491 delay = atoi (optarg);
492 else
493 usage ("Delay must be a positive integer\n");
494 break;
495 case 'S':
496 #ifndef HAVE_SSL
497 terminate (STATE_UNKNOWN,
498 "SSL support not available. Install OpenSSL and recompile.");
499 #endif
500 use_ssl = TRUE;
501 break;
502 }
503 }
505 if (server_address == NULL)
506 usage ("You must provide a server address\n");
508 return OK;
509 }
511 void
512 print_help (void)
513 {
514 print_revision (progname, revision);
515 printf ("Copyright (c) %s %s\n\t<%s>\n\n",
516 copyright, authors, email);
517 printf (summary, SERVICE);
518 print_usage ();
519 printf ("\nOptions:\n");
520 printf (options, DEFAULT_SOCKET_TIMEOUT);
521 support ();
522 }
524 void
525 print_usage (void)
526 {
527 printf
528 ("Usage: %s %s\n"
529 " %s (-h|--help)\n"
530 " %s (-V|--version)\n", progname, option_summary, progname, progname);
531 }
533 #ifdef HAVE_SSL
534 int
535 connect_SSL (void)
536 {
537 SSL_METHOD *meth;
539 /* Initialize SSL context */
540 SSLeay_add_ssl_algorithms ();
541 meth = SSLv2_client_method ();
542 SSL_load_error_strings ();
543 if ((ctx = SSL_CTX_new (meth)) == NULL)
544 {
545 printf ("ERROR: Cannot create SSL context.\n");
546 return STATE_CRITICAL;
547 }
549 /* Initialize alarm signal handling */
550 signal (SIGALRM, socket_timeout_alarm_handler);
552 /* Set socket timeout */
553 alarm (socket_timeout);
555 /* Save start time */
556 time (&start_time);
558 /* Make TCP connection */
559 if (my_tcp_connect (server_address, server_port, &sd) == STATE_OK && was_refused == FALSE)
560 {
561 /* Do the SSL handshake */
562 if ((ssl = SSL_new (ctx)) != NULL)
563 {
564 SSL_set_fd (ssl, sd);
565 if (SSL_connect (ssl) != -1)
566 return OK;
567 ERR_print_errors_fp (stderr);
568 }
569 else
570 {
571 printf ("ERROR: Cannot initiate SSL handshake.\n");
572 }
573 SSL_free (ssl);
574 }
576 SSL_CTX_free (ctx);
577 close (sd);
579 return STATE_CRITICAL;
580 }
581 #endif
585 int
586 my_recv (void)
587 {
588 int i;
590 #ifdef HAVE_SSL
591 if (use_ssl) {
592 i = SSL_read (ssl, buffer, MAXBUF - 1);
593 }
594 else {
595 #endif
596 i = read (sd, buffer, MAXBUF - 1);
597 #ifdef HAVE_SSL
598 }
599 #endif
601 return i;
602 }