diff -rNu nagios_orig/plugins/check_tcp.c nagios-plugins-1.4/plugins/check_tcp.c --- nagios_orig/plugins/check_tcp.c 2004-12-30 01:41:40.000000000 +0100 +++ nagios-plugins-1.4/plugins/check_tcp.c 2005-03-09 15:46:03.000000000 +0100 @@ -80,6 +80,8 @@ char *server_address = NULL; char *server_send = NULL; char *server_quit = NULL; +char *input_file_name = NULL; +char *output_file_name = NULL; char **server_expect = NULL; size_t server_expect_count = 0; int maxbytes = 0; @@ -132,6 +134,15 @@ PROTOCOL = TCP_PROTOCOL; PORT = 0; } + else if (strstr (argv[0], "check_tcp_bin")) { + progname = strdup ("check_tcp_bin"); + SERVICE = strdup ("TCP"); + SEND = NULL; + EXPECT = NULL; + QUIT = NULL; + PROTOCOL = TCP_PROTOCOL; + PORT = 0; + } else if (strstr (argv[0], "check_ftp")) { progname = strdup ("check_ftp"); SERVICE = strdup ("FTP"); @@ -299,6 +310,42 @@ send (sd, server_send, strlen(server_send), 0); } + /* send content of input file ? */ + if (input_file_name != NULL) { + struct stat istat; + if (stat(input_file_name, &istat) != 0) { + printf("UNKNOWN - File not found %s\n", input_file_name); + return STATE_UNKNOWN; + } + size_t in_len = istat.st_size; + + FILE * in = fopen(input_file_name, "rb"); + if (in == NULL) { + printf("UNKNOWN - Can not open file: %s\n", input_file_name); + return STATE_UNKNOWN; + } + char * indata = malloc(in_len); + if (indata == NULL) { + printf(_("UNKNOWN - Not enough memory\n")); + return STATE_UNKNOWN; + } + size_t bytes = fread(indata, 1, in_len, in); + if (bytes != in_len) { + printf("UNKNOWN - Error reading file: \n", input_file_name); + free(indata); + fclose(in); + return STATE_UNKNOWN; + } + fclose(in); +#ifdef HAVE_SSL + if (use_ssl) + SSL_write(ssl, indata, in_len); + else +#endif + send (sd, indata, in_len, 0); + free(indata); + } + if (delay > 0) { tv.tv_sec += delay; sleep (delay); @@ -345,6 +392,57 @@ } } + /* Verify server response */ + if (output_file_name != NULL) { + struct stat ostat; + if (stat(output_file_name, &ostat) != 0) { + printf("UNKNOWN - File not found %s\n", output_file_name); + return STATE_UNKNOWN; + } + size_t out_len = ostat.st_size; + + FILE * out = fopen(output_file_name, "rb"); + if (out == NULL) { + printf("UNKNOWN - Can not open file: %s\n", output_file_name); + return STATE_UNKNOWN; + } + char * outdata = malloc(out_len); + if (outdata == NULL) { + printf(_("UNKNOWN - Not enough memory\n")); + return STATE_UNKNOWN; + } + size_t bytes = fread(outdata, 1, out_len, out); + if (bytes != out_len) { + printf("UNKNOWN - Error reading file: \n", output_file_name); + free(outdata); + fclose(out); + return STATE_UNKNOWN; + } + fclose(out); + + char * rcvbuf = malloc(out_len); + if (rcvbuf != NULL) { + char *p = rcvbuf; + size_t count = 0; + + do { + count += my_bin_recv(p, out_len-count); + p += count; + } while (count < out_len); + + if (memcmp(outdata, rcvbuf, out_len) == 0) { + result = STATE_OK; + } else { + printf(_("WARNING response does not match\n")); + result = expect_mismatch_state; + } + free(rcvbuf); + } else { + result = STATE_UNKNOWN; + } + + } + if (server_quit != NULL) { #ifdef HAVE_SSL if (use_ssl) { @@ -417,6 +515,8 @@ {"expect", required_argument, 0, 'e'}, {"maxbytes", required_argument, 0, 'm'}, {"quit", required_argument, 0, 'q'}, + {"inputfile", required_argument, 0, 'i'}, + {"outputfile", required_argument, 0, 'o'}, {"jail", required_argument, 0, 'j'}, {"delay", required_argument, 0, 'd'}, {"refuse", required_argument, 0, 'r'}, @@ -454,8 +554,8 @@ } while (1) { - c = getopt_long (argc, argv, "+hVv46H:s:e:q:m:c:w:t:p:C:W:d:Sr:jD:M:", - longopts, &option); + c = getopt_long (argc, argv, + "+hVv46H:s:e:q:m:c:i:o:w:t:p:C:W:d:Sr:jD:M:", longopts, &option); if (c == -1 || c == EOF || c == 1) break; @@ -527,6 +627,12 @@ case 's': server_send = optarg; break; + case 'i': + input_file_name = optarg; + break; + case 'o': + output_file_name = optarg; + break; case 'e': /* expect string (may be repeated) */ EXPECT = NULL; exact_matching = FALSE; @@ -748,7 +854,18 @@ return i; } - +int my_bin_recv(char * buf, size_t len) { + int i; +#ifdef HAVE_SSL + if (use_ssl) { + i = SSL_read (ssl, buf, len); + } else { +#endif + i = read (sd, buf, len); +#ifdef HAVE_SSL + } +#endif +} void print_help (void) @@ -786,6 +903,10 @@ Hide output from TCP socket\n\ -m, --maxbytes=INTEGER\n\ Close connection once more than this number of bytes are received\n\ + -i, --inputfile=file_name\n\ + Input file to send to server\n\ + -o, --outputfile=file_name\n\ + Output file to binary compare with server response\n\ -d, --delay=INTEGER\n\ Seconds to wait between sending string and polling for response\n")); @@ -816,5 +937,6 @@ [-s ] [-e ] [-q ]\n\ [-m ] [-d ] [-t ]\n\ [-r ] [-M ] [-v] [-4|-6] [-j]\n\ + [-i ] [-o ]\n\ [-D ] [-S ]\n", progname); } diff -rNu nagios_orig/plugins/Makefile.am nagios-plugins-1.4/plugins/Makefile.am --- nagios_orig/plugins/Makefile.am 2005-02-01 13:30:38.000000000 +0100 +++ nagios-plugins-1.4/plugins/Makefile.am 2005-03-09 17:25:07.000000000 +0100 @@ -16,8 +16,8 @@ check_udp check_ups check_users negate urlize check_icmp\ @EXTRAS@ -check_tcp_programs = check_ftp check_imap check_nntp check_pop check_udp2 \ - @check_tcp_ssl@ +check_tcp_programs = check_ftp check_imap check_nntp check_pop check_tcp_bin \ + check_udp2 @check_tcp_ssl@ EXTRA_PROGRAMS = check_mysql check_radius check_pgsql check_snmp check_hpjd \ check_swap check_fping check_ldap check_game check_dig \