diff options
author | Sven Nierlein <sven@nierlein.de> | 2017-03-14 20:29:17 +0100 |
---|---|---|
committer | Sven Nierlein <sven@nierlein.de> | 2017-03-14 20:29:17 +0100 |
commit | afb41b29f7a444826a1f23535def3a57fb241375 (patch) | |
tree | d29707fa299e8b0e2d0eb3262245d72832f90ba5 | |
parent | f640677c9d47f313dd62befae7c0ccf59e44546c (diff) | |
download | monitoring-plugins-afb41b2.tar.gz |
check_curl: implement optional http headers
Signed-off-by: Sven Nierlein <sven@nierlein.de>
-rw-r--r-- | plugins/check_curl.c | 29 |
1 files changed, 19 insertions, 10 deletions
diff --git a/plugins/check_curl.c b/plugins/check_curl.c index 209b4495..c6a7ab85 100644 --- a/plugins/check_curl.c +++ b/plugins/check_curl.c | |||
@@ -103,6 +103,7 @@ curlhelp_curlbuf body_buf; | |||
103 | curlhelp_curlbuf header_buf; | 103 | curlhelp_curlbuf header_buf; |
104 | curlhelp_statusline status_line; | 104 | curlhelp_statusline status_line; |
105 | char http_header[DEFAULT_BUFFER_SIZE]; | 105 | char http_header[DEFAULT_BUFFER_SIZE]; |
106 | struct curl_slist *http_opt_headers = NULL; | ||
106 | long code; | 107 | long code; |
107 | long socket_timeout = DEFAULT_SOCKET_TIMEOUT; | 108 | long socket_timeout = DEFAULT_SOCKET_TIMEOUT; |
108 | double total_time; | 109 | double total_time; |
@@ -219,10 +220,10 @@ check_http (void) | |||
219 | /* always close connection, be nice to servers */ | 220 | /* always close connection, be nice to servers */ |
220 | snprintf (http_header, DEFAULT_BUFFER_SIZE, "Connection: close"); | 221 | snprintf (http_header, DEFAULT_BUFFER_SIZE, "Connection: close"); |
221 | header_list = curl_slist_append (header_list, http_header); | 222 | header_list = curl_slist_append (header_list, http_header); |
222 | 223 | ||
223 | /* set HTTP headers */ | 224 | /* set HTTP headers */ |
224 | curl_easy_setopt( curl, CURLOPT_HTTPHEADER, header_list ); | 225 | curl_easy_setopt( curl, CURLOPT_HTTPHEADER, header_list ); |
225 | 226 | ||
226 | /* set SSL version, warn about unsecure or unsupported versions */ | 227 | /* set SSL version, warn about unsecure or unsupported versions */ |
227 | if (use_ssl) { | 228 | if (use_ssl) { |
228 | curl_easy_setopt (curl, CURLOPT_SSLVERSION, ssl_version); | 229 | curl_easy_setopt (curl, CURLOPT_SSLVERSION, ssl_version); |
@@ -289,11 +290,15 @@ check_http (void) | |||
289 | */ | 290 | */ |
290 | } | 291 | } |
291 | 292 | ||
293 | /* set optional http header */ | ||
294 | curl_easy_setopt(curl, CURLOPT_HTTPHEADER, http_opt_headers); | ||
295 | |||
292 | /* do the request */ | 296 | /* do the request */ |
293 | res = curl_easy_perform(curl); | 297 | res = curl_easy_perform(curl); |
294 | 298 | ||
295 | /* free header list, we don't need it anymore */ | 299 | /* free header list, we don't need it anymore */ |
296 | curl_slist_free_all (header_list); | 300 | curl_slist_free_all(header_list); |
301 | curl_slist_free_all(http_opt_headers); | ||
297 | 302 | ||
298 | /* Curl errors, result in critical Nagios state */ | 303 | /* Curl errors, result in critical Nagios state */ |
299 | if (res != CURLE_OK) { | 304 | if (res != CURLE_OK) { |
@@ -460,6 +465,7 @@ process_arguments (int argc, char **argv) | |||
460 | {"ca-cert", required_argument, 0, CA_CERT_OPTION}, | 465 | {"ca-cert", required_argument, 0, CA_CERT_OPTION}, |
461 | {"useragent", required_argument, 0, 'A'}, | 466 | {"useragent", required_argument, 0, 'A'}, |
462 | {"invert-regex", no_argument, NULL, INVERT_REGEX}, | 467 | {"invert-regex", no_argument, NULL, INVERT_REGEX}, |
468 | {"header", required_argument, 0, 'k'}, | ||
463 | {0, 0, 0, 0} | 469 | {0, 0, 0, 0} |
464 | }; | 470 | }; |
465 | 471 | ||
@@ -467,7 +473,7 @@ process_arguments (int argc, char **argv) | |||
467 | return ERROR; | 473 | return ERROR; |
468 | 474 | ||
469 | while (1) { | 475 | while (1) { |
470 | c = getopt_long (argc, argv, "Vvht:c:w:A:H:j:I:a:p:s:r:u:f:C:J:K:S::", longopts, &option); | 476 | c = getopt_long (argc, argv, "Vvht:c:w:A:k:H:j:I:a:p:s:r:u:f:C:J:K:S::", longopts, &option); |
471 | if (c == -1 || c == EOF || c == 1) | 477 | if (c == -1 || c == EOF || c == 1) |
472 | break; | 478 | break; |
473 | 479 | ||
@@ -526,6 +532,9 @@ process_arguments (int argc, char **argv) | |||
526 | case 'A': /* useragent */ | 532 | case 'A': /* useragent */ |
527 | snprintf (user_agent, DEFAULT_BUFFER_SIZE, optarg); | 533 | snprintf (user_agent, DEFAULT_BUFFER_SIZE, optarg); |
528 | break; | 534 | break; |
535 | case 'k': /* Additional headers */ | ||
536 | http_opt_headers = curl_slist_append(http_opt_headers, optarg); | ||
537 | break; | ||
529 | case 'C': /* Check SSL cert validity */ | 538 | case 'C': /* Check SSL cert validity */ |
530 | #ifdef LIBCURL_FEATURE_SSL | 539 | #ifdef LIBCURL_FEATURE_SSL |
531 | /* TODO: C:, check age of certificate for backward compatible | 540 | /* TODO: C:, check age of certificate for backward compatible |
@@ -854,20 +863,20 @@ curlhelp_freebuffer (curlhelp_curlbuf *buf) | |||
854 | /* TODO: should be moved into 'gl' and should be probed, glibc has | 863 | /* TODO: should be moved into 'gl' and should be probed, glibc has |
855 | * a strrstr | 864 | * a strrstr |
856 | */ | 865 | */ |
857 | const char* | 866 | const char* |
858 | strrstr2(const char *haystack, const char *needle) | 867 | strrstr2(const char *haystack, const char *needle) |
859 | { | 868 | { |
860 | int counter; | 869 | int counter; |
861 | size_t len; | 870 | size_t len; |
862 | const char *prev_pos; | 871 | const char *prev_pos; |
863 | const char *pos; | 872 | const char *pos; |
864 | 873 | ||
865 | if (haystack == NULL || needle == NULL) | 874 | if (haystack == NULL || needle == NULL) |
866 | return NULL; | 875 | return NULL; |
867 | 876 | ||
868 | if (haystack[0] == '\0' || needle[0] == '\0') | 877 | if (haystack[0] == '\0' || needle[0] == '\0') |
869 | return NULL; | 878 | return NULL; |
870 | 879 | ||
871 | counter = 0; | 880 | counter = 0; |
872 | prev_pos = NULL; | 881 | prev_pos = NULL; |
873 | pos = haystack; | 882 | pos = haystack; |
@@ -902,7 +911,7 @@ curlhelp_parse_statusline (const char *buf, curlhelp_statusline *status_line) | |||
902 | start += 2; | 911 | start += 2; |
903 | buf = start; | 912 | buf = start; |
904 | } | 913 | } |
905 | 914 | ||
906 | first_line_end = strstr(buf, "\r\n"); | 915 | first_line_end = strstr(buf, "\r\n"); |
907 | if (first_line_end == NULL) return -1; | 916 | if (first_line_end == NULL) return -1; |
908 | 917 | ||