diff options
| -rw-r--r-- | plugins/check_curl.c | 42 |
1 files changed, 31 insertions, 11 deletions
diff --git a/plugins/check_curl.c b/plugins/check_curl.c index 064fb16b..a7e580d1 100644 --- a/plugins/check_curl.c +++ b/plugins/check_curl.c | |||
| @@ -90,6 +90,8 @@ char *server_address; | |||
| 90 | char *host_name; | 90 | char *host_name; |
| 91 | char *server_url = DEFAULT_SERVER_URL; | 91 | char *server_url = DEFAULT_SERVER_URL; |
| 92 | unsigned short server_port = HTTP_PORT; | 92 | unsigned short server_port = HTTP_PORT; |
| 93 | int virtual_port = 0; | ||
| 94 | int host_name_length; | ||
| 93 | char output_string_search[30] = ""; | 95 | char output_string_search[30] = ""; |
| 94 | char *warning_thresholds = NULL; | 96 | char *warning_thresholds = NULL; |
| 95 | char *critical_thresholds = NULL; | 97 | char *critical_thresholds = NULL; |
| @@ -105,7 +107,6 @@ curlhelp_curlbuf body_buf; | |||
| 105 | curlhelp_curlbuf header_buf; | 107 | curlhelp_curlbuf header_buf; |
| 106 | curlhelp_statusline status_line; | 108 | curlhelp_statusline status_line; |
| 107 | char http_header[DEFAULT_BUFFER_SIZE]; | 109 | char http_header[DEFAULT_BUFFER_SIZE]; |
| 108 | struct curl_slist *http_opt_headers = NULL; | ||
| 109 | long code; | 110 | long code; |
| 110 | long socket_timeout = DEFAULT_SOCKET_TIMEOUT; | 111 | long socket_timeout = DEFAULT_SOCKET_TIMEOUT; |
| 111 | double total_time; | 112 | double total_time; |
| @@ -195,7 +196,7 @@ check_http (void) | |||
| 195 | if ((curl = curl_easy_init()) == NULL) | 196 | if ((curl = curl_easy_init()) == NULL) |
| 196 | die (STATE_UNKNOWN, "HTTP UNKNOWN - curl_easy_init failed\n"); | 197 | die (STATE_UNKNOWN, "HTTP UNKNOWN - curl_easy_init failed\n"); |
| 197 | 198 | ||
| 198 | if (verbose >= 3) | 199 | if (verbose >= 1) |
| 199 | curl_easy_setopt (curl, CURLOPT_VERBOSE, TRUE); | 200 | curl_easy_setopt (curl, CURLOPT_VERBOSE, TRUE); |
| 200 | 201 | ||
| 201 | /* print everything on stdout like check_http would do */ | 202 | /* print everything on stdout like check_http would do */ |
| @@ -238,8 +239,14 @@ check_http (void) | |||
| 238 | } | 239 | } |
| 239 | 240 | ||
| 240 | /* set hostname (virtual hosts) */ | 241 | /* set hostname (virtual hosts) */ |
| 241 | snprintf (http_header, DEFAULT_BUFFER_SIZE, "Host: %s", host_name); | 242 | if(host_name != NULL) { |
| 242 | header_list = curl_slist_append (header_list, http_header); | 243 | if((virtual_port != HTTP_PORT && !use_ssl) || (virtual_port != HTTPS_PORT && use_ssl)) { |
| 244 | snprintf(http_header, DEFAULT_BUFFER_SIZE, "Host: %s:%d", host_name, virtual_port); | ||
| 245 | } else { | ||
| 246 | snprintf(http_header, DEFAULT_BUFFER_SIZE, "Host: %s", host_name); | ||
| 247 | } | ||
| 248 | header_list = curl_slist_append (header_list, http_header); | ||
| 249 | } | ||
| 243 | 250 | ||
| 244 | /* always close connection, be nice to servers */ | 251 | /* always close connection, be nice to servers */ |
| 245 | snprintf (http_header, DEFAULT_BUFFER_SIZE, "Connection: close"); | 252 | snprintf (http_header, DEFAULT_BUFFER_SIZE, "Connection: close"); |
| @@ -316,15 +323,11 @@ check_http (void) | |||
| 316 | */ | 323 | */ |
| 317 | } | 324 | } |
| 318 | 325 | ||
| 319 | /* set optional http header */ | ||
| 320 | curl_easy_setopt(curl, CURLOPT_HTTPHEADER, http_opt_headers); | ||
| 321 | |||
| 322 | /* do the request */ | 326 | /* do the request */ |
| 323 | res = curl_easy_perform(curl); | 327 | res = curl_easy_perform(curl); |
| 324 | 328 | ||
| 325 | /* free header list, we don't need it anymore */ | 329 | /* free header list, we don't need it anymore */ |
| 326 | curl_slist_free_all(header_list); | 330 | curl_slist_free_all(header_list); |
| 327 | curl_slist_free_all(http_opt_headers); | ||
| 328 | 331 | ||
| 329 | /* Curl errors, result in critical Nagios state */ | 332 | /* Curl errors, result in critical Nagios state */ |
| 330 | if (res != CURLE_OK) { | 333 | if (res != CURLE_OK) { |
| @@ -491,6 +494,7 @@ test_file (char *path) | |||
| 491 | int | 494 | int |
| 492 | process_arguments (int argc, char **argv) | 495 | process_arguments (int argc, char **argv) |
| 493 | { | 496 | { |
| 497 | char *p; | ||
| 494 | int c = 1; | 498 | int c = 1; |
| 495 | char *temp; | 499 | char *temp; |
| 496 | 500 | ||
| @@ -559,6 +563,22 @@ process_arguments (int argc, char **argv) | |||
| 559 | break; | 563 | break; |
| 560 | case 'H': /* virtual host */ | 564 | case 'H': /* virtual host */ |
| 561 | host_name = strdup (optarg); | 565 | host_name = strdup (optarg); |
| 566 | if (host_name[0] == '[') { | ||
| 567 | if ((p = strstr (host_name, "]:")) != NULL) { /* [IPv6]:port */ | ||
| 568 | virtual_port = atoi (p + 2); | ||
| 569 | /* cut off the port */ | ||
| 570 | host_name_length = strlen (host_name) - strlen (p) - 1; | ||
| 571 | free (host_name); | ||
| 572 | host_name = strndup (optarg, host_name_length); | ||
| 573 | } | ||
| 574 | } else if ((p = strchr (host_name, ':')) != NULL | ||
| 575 | && strchr (++p, ':') == NULL) { /* IPv4:port or host:port */ | ||
| 576 | virtual_port = atoi (p); | ||
| 577 | /* cut off the port */ | ||
| 578 | host_name_length = strlen (host_name) - strlen (p) - 1; | ||
| 579 | free (host_name); | ||
| 580 | host_name = strndup (optarg, host_name_length); | ||
| 581 | } | ||
| 562 | break; | 582 | break; |
| 563 | case 'I': /* internet address */ | 583 | case 'I': /* internet address */ |
| 564 | server_address = strdup (optarg); | 584 | server_address = strdup (optarg); |
| @@ -588,7 +608,7 @@ process_arguments (int argc, char **argv) | |||
| 588 | snprintf (user_agent, DEFAULT_BUFFER_SIZE, optarg); | 608 | snprintf (user_agent, DEFAULT_BUFFER_SIZE, optarg); |
| 589 | break; | 609 | break; |
| 590 | case 'k': /* Additional headers */ | 610 | case 'k': /* Additional headers */ |
| 591 | http_opt_headers = curl_slist_append(http_opt_headers, optarg); | 611 | header_list = curl_slist_append(header_list, optarg); |
| 592 | break; | 612 | break; |
| 593 | case 'C': /* Check SSL cert validity */ | 613 | case 'C': /* Check SSL cert validity */ |
| 594 | #ifdef LIBCURL_FEATURE_SSL | 614 | #ifdef LIBCURL_FEATURE_SSL |
| @@ -733,8 +753,8 @@ process_arguments (int argc, char **argv) | |||
| 733 | if (client_cert && !client_privkey) | 753 | if (client_cert && !client_privkey) |
| 734 | usage4 (_("If you use a client certificate you must also specify a private key file")); | 754 | usage4 (_("If you use a client certificate you must also specify a private key file")); |
| 735 | 755 | ||
| 736 | //~ if (virtual_port == 0) | 756 | if (virtual_port == 0) |
| 737 | //~ virtual_port = server_port; | 757 | virtual_port = server_port; |
| 738 | 758 | ||
| 739 | return TRUE; | 759 | return TRUE; |
| 740 | } | 760 | } |
