diff options
author | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-08-01 14:29:48 +0200 |
---|---|---|
committer | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-08-01 14:29:48 +0200 |
commit | b54869391faab7ef91586c81de21f18a61bac5aa (patch) | |
tree | b3fbeefcd98c1de0350f5f5693cb928c816300be | |
parent | 4966b920a2392484a6ae83a4a499c24a2942c832 (diff) | |
download | monitoring-plugins-b54869391faab7ef91586c81de21f18a61bac5aa.tar.gz |
Quick save
-rw-r--r-- | plugins/check_curl.c | 51 | ||||
-rw-r--r-- | plugins/check_curl.d/config.h | 3 |
2 files changed, 29 insertions, 25 deletions
diff --git a/plugins/check_curl.c b/plugins/check_curl.c index 88c5f3e7..94b726d0 100644 --- a/plugins/check_curl.c +++ b/plugins/check_curl.c | |||
@@ -127,8 +127,6 @@ static curlhelp_statusline status_line; | |||
127 | static bool put_buf_initialized = false; | 127 | static bool put_buf_initialized = false; |
128 | static curlhelp_read_curlbuf put_buf; | 128 | static curlhelp_read_curlbuf put_buf; |
129 | 129 | ||
130 | static struct curl_slist *server_ips = NULL; // TODO maybe unused | ||
131 | static int redir_depth = 0; // Maybe global | ||
132 | static CURL *curl; | 130 | static CURL *curl; |
133 | static struct curl_slist *header_list = NULL; | 131 | static struct curl_slist *header_list = NULL; |
134 | static long code; | 132 | static long code; |
@@ -138,7 +136,6 @@ typedef union { | |||
138 | struct curl_slist *to_info; | 136 | struct curl_slist *to_info; |
139 | struct curl_certinfo *to_certinfo; | 137 | struct curl_certinfo *to_certinfo; |
140 | } cert_ptr_union; | 138 | } cert_ptr_union; |
141 | static cert_ptr_union cert_ptr; | ||
142 | static bool is_openssl_callback = false; | 139 | static bool is_openssl_callback = false; |
143 | static bool add_sslctx_verify_fun = false; | 140 | static bool add_sslctx_verify_fun = false; |
144 | 141 | ||
@@ -146,7 +143,6 @@ static bool add_sslctx_verify_fun = false; | |||
146 | static X509 *cert = NULL; | 143 | static X509 *cert = NULL; |
147 | #endif /* defined(HAVE_SSL) && defined(USE_OPENSSL) */ | 144 | #endif /* defined(HAVE_SSL) && defined(USE_OPENSSL) */ |
148 | 145 | ||
149 | static int address_family = AF_UNSPEC; | ||
150 | static curlhelp_ssl_library ssl_library = CURLHELP_SSL_LIBRARY_UNKNOWN; | 146 | static curlhelp_ssl_library ssl_library = CURLHELP_SSL_LIBRARY_UNKNOWN; |
151 | 147 | ||
152 | typedef struct { | 148 | typedef struct { |
@@ -156,8 +152,11 @@ typedef struct { | |||
156 | static check_curl_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); | 152 | static check_curl_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); |
157 | 153 | ||
158 | static void handle_curl_option_return_code(CURLcode res, const char *option); | 154 | static void handle_curl_option_return_code(CURLcode res, const char *option); |
159 | static mp_state_enum check_http(check_curl_config /*config*/); | 155 | static mp_state_enum check_http(check_curl_config /*config*/, int redir_depth); |
160 | static void redir(curlhelp_write_curlbuf * /*header_buf*/, check_curl_config /*config*/); | 156 | |
157 | static void redir(curlhelp_write_curlbuf * /*header_buf*/, check_curl_config /*config*/, | ||
158 | int redir_depth); | ||
159 | |||
161 | static char *perfd_time(double elapsed_time, thresholds * /*thlds*/, long /*socket_timeout*/); | 160 | static char *perfd_time(double elapsed_time, thresholds * /*thlds*/, long /*socket_timeout*/); |
162 | static char *perfd_time_connect(double elapsed_time_connect, long /*socket_timeout*/); | 161 | static char *perfd_time_connect(double elapsed_time_connect, long /*socket_timeout*/); |
163 | static char *perfd_time_ssl(double elapsed_time_ssl, long /*socket_timeout*/); | 162 | static char *perfd_time_ssl(double elapsed_time_ssl, long /*socket_timeout*/); |
@@ -224,7 +223,9 @@ int main(int argc, char **argv) { | |||
224 | config.virtual_port ? config.virtual_port : config.server_port, config.server_url); | 223 | config.virtual_port ? config.virtual_port : config.server_port, config.server_url); |
225 | } | 224 | } |
226 | 225 | ||
227 | exit((int)check_http(config)); | 226 | int redir_depth = 0; |
227 | |||
228 | exit((int)check_http(config, redir_depth)); | ||
228 | } | 229 | } |
229 | 230 | ||
230 | #ifdef HAVE_SSL | 231 | #ifdef HAVE_SSL |
@@ -326,10 +327,10 @@ void handle_curl_option_return_code(CURLcode res, const char *option) { | |||
326 | } | 327 | } |
327 | } | 328 | } |
328 | 329 | ||
329 | int lookup_host(const char *host, char *buf, size_t buflen) { | 330 | int lookup_host(const char *host, char *buf, size_t buflen, sa_family_t addr_family) { |
330 | struct addrinfo hints; | 331 | struct addrinfo hints; |
331 | memset(&hints, 0, sizeof(hints)); | 332 | memset(&hints, 0, sizeof(hints)); |
332 | hints.ai_family = address_family; | 333 | hints.ai_family = addr_family; |
333 | hints.ai_socktype = SOCK_STREAM; | 334 | hints.ai_socktype = SOCK_STREAM; |
334 | hints.ai_flags |= AI_CANONNAME; | 335 | hints.ai_flags |= AI_CANONNAME; |
335 | 336 | ||
@@ -408,7 +409,7 @@ static void cleanup(void) { | |||
408 | put_buf_initialized = false; | 409 | put_buf_initialized = false; |
409 | } | 410 | } |
410 | 411 | ||
411 | mp_state_enum check_http(check_curl_config config) { | 412 | mp_state_enum check_http(check_curl_config config, int redir_depth) { |
412 | /* initialize curl */ | 413 | /* initialize curl */ |
413 | if (curl_global_init(CURL_GLOBAL_DEFAULT) != CURLE_OK) { | 414 | if (curl_global_init(CURL_GLOBAL_DEFAULT) != CURLE_OK) { |
414 | die(STATE_UNKNOWN, "HTTP UNKNOWN - curl_global_init failed\n"); | 415 | die(STATE_UNKNOWN, "HTTP UNKNOWN - curl_global_init failed\n"); |
@@ -448,8 +449,7 @@ mp_state_enum check_http(check_curl_config config) { | |||
448 | } | 449 | } |
449 | body_buf_initialized = true; | 450 | body_buf_initialized = true; |
450 | handle_curl_option_return_code( | 451 | handle_curl_option_return_code( |
451 | curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, | 452 | curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlhelp_buffer_write_callback), |
452 | curlhelp_buffer_write_callback), | ||
453 | "CURLOPT_WRITEFUNCTION"); | 453 | "CURLOPT_WRITEFUNCTION"); |
454 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&body_buf), | 454 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&body_buf), |
455 | "CURLOPT_WRITEDATA"); | 455 | "CURLOPT_WRITEDATA"); |
@@ -460,8 +460,7 @@ mp_state_enum check_http(check_curl_config config) { | |||
460 | } | 460 | } |
461 | header_buf_initialized = true; | 461 | header_buf_initialized = true; |
462 | handle_curl_option_return_code( | 462 | handle_curl_option_return_code( |
463 | curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, | 463 | curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, curlhelp_buffer_write_callback), |
464 | curlhelp_buffer_write_callback), | ||
465 | "CURLOPT_HEADERFUNCTION"); | 464 | "CURLOPT_HEADERFUNCTION"); |
466 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_WRITEHEADER, (void *)&header_buf), | 465 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_WRITEHEADER, (void *)&header_buf), |
467 | "CURLOPT_WRITEHEADER"); | 466 | "CURLOPT_WRITEHEADER"); |
@@ -490,7 +489,8 @@ mp_state_enum check_http(check_curl_config config) { | |||
490 | char addrstr[DEFAULT_BUFFER_SIZE / 2]; | 489 | char addrstr[DEFAULT_BUFFER_SIZE / 2]; |
491 | if (config.use_ssl && config.host_name != NULL) { | 490 | if (config.use_ssl && config.host_name != NULL) { |
492 | int res; | 491 | int res; |
493 | if ((res = lookup_host(config.server_address, addrstr, DEFAULT_BUFFER_SIZE / 2)) != 0) { | 492 | if ((res = lookup_host(config.server_address, addrstr, DEFAULT_BUFFER_SIZE / 2, |
493 | config.sin_family)) != 0) { | ||
494 | snprintf(msg, DEFAULT_BUFFER_SIZE, | 494 | snprintf(msg, DEFAULT_BUFFER_SIZE, |
495 | _("Unable to lookup IP address for '%s': getaddrinfo returned %d - %s"), | 495 | _("Unable to lookup IP address for '%s': getaddrinfo returned %d - %s"), |
496 | config.server_address, res, gai_strerror(res)); | 496 | config.server_address, res, gai_strerror(res)); |
@@ -810,16 +810,16 @@ mp_state_enum check_http(check_curl_config config) { | |||
810 | } | 810 | } |
811 | 811 | ||
812 | /* IPv4 or IPv6 forced DNS resolution */ | 812 | /* IPv4 or IPv6 forced DNS resolution */ |
813 | if (address_family == AF_UNSPEC) { | 813 | if (config.sin_family == AF_UNSPEC) { |
814 | handle_curl_option_return_code( | 814 | handle_curl_option_return_code( |
815 | curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_WHATEVER), | 815 | curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_WHATEVER), |
816 | "CURLOPT_IPRESOLVE(CURL_IPRESOLVE_WHATEVER)"); | 816 | "CURLOPT_IPRESOLVE(CURL_IPRESOLVE_WHATEVER)"); |
817 | } else if (address_family == AF_INET) { | 817 | } else if (config.sin_family == AF_INET) { |
818 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4), | 818 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4), |
819 | "CURLOPT_IPRESOLVE(CURL_IPRESOLVE_V4)"); | 819 | "CURLOPT_IPRESOLVE(CURL_IPRESOLVE_V4)"); |
820 | } | 820 | } |
821 | #if defined(USE_IPV6) && defined(LIBCURL_FEATURE_IPV6) | 821 | #if defined(USE_IPV6) && defined(LIBCURL_FEATURE_IPV6) |
822 | else if (address_family == AF_INET6) { | 822 | else if (config.sin_family == AF_INET6) { |
823 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V6), | 823 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V6), |
824 | "CURLOPT_IPRESOLVE(CURL_IPRESOLVE_V6)"); | 824 | "CURLOPT_IPRESOLVE(CURL_IPRESOLVE_V6)"); |
825 | } | 825 | } |
@@ -889,8 +889,7 @@ mp_state_enum check_http(check_curl_config config) { | |||
889 | /* free header and server IP resolve lists, we don't need it anymore */ | 889 | /* free header and server IP resolve lists, we don't need it anymore */ |
890 | curl_slist_free_all(header_list); | 890 | curl_slist_free_all(header_list); |
891 | header_list = NULL; | 891 | header_list = NULL; |
892 | curl_slist_free_all(server_ips); | 892 | |
893 | server_ips = NULL; | ||
894 | if (host) { | 893 | if (host) { |
895 | curl_slist_free_all(host); | 894 | curl_slist_free_all(host); |
896 | host = NULL; | 895 | host = NULL; |
@@ -926,6 +925,7 @@ mp_state_enum check_http(check_curl_config config) { | |||
926 | } else { | 925 | } else { |
927 | struct curl_slist *slist; | 926 | struct curl_slist *slist; |
928 | 927 | ||
928 | cert_ptr_union cert_ptr = {0}; | ||
929 | cert_ptr.to_info = NULL; | 929 | cert_ptr.to_info = NULL; |
930 | res = curl_easy_getinfo(curl, CURLINFO_CERTINFO, &cert_ptr.to_info); | 930 | res = curl_easy_getinfo(curl, CURLINFO_CERTINFO, &cert_ptr.to_info); |
931 | if (!res && cert_ptr.to_info) { | 931 | if (!res && cert_ptr.to_info) { |
@@ -1103,7 +1103,7 @@ mp_state_enum check_http(check_curl_config config) { | |||
1103 | * back here, we are in the same status as with | 1103 | * back here, we are in the same status as with |
1104 | * the libcurl method | 1104 | * the libcurl method |
1105 | */ | 1105 | */ |
1106 | redir(&header_buf, config); | 1106 | redir(&header_buf, config, redir_depth); |
1107 | } | 1107 | } |
1108 | } else { | 1108 | } else { |
1109 | /* this is a specific code in the command line to | 1109 | /* this is a specific code in the command line to |
@@ -1287,7 +1287,8 @@ char *uri_string(const UriTextRangeA range, char *buf, size_t buflen) { | |||
1287 | return buf; | 1287 | return buf; |
1288 | } | 1288 | } |
1289 | 1289 | ||
1290 | void redir(curlhelp_write_curlbuf *header_buf, check_curl_config config) { | 1290 | void redir(curlhelp_write_curlbuf *header_buf, check_curl_config config, int redir_depth) { |
1291 | |||
1291 | curlhelp_statusline status_line; | 1292 | curlhelp_statusline status_line; |
1292 | struct phr_header headers[255]; | 1293 | struct phr_header headers[255]; |
1293 | size_t msglen; | 1294 | size_t msglen; |
@@ -1443,7 +1444,7 @@ void redir(curlhelp_write_curlbuf *header_buf, check_curl_config config) { | |||
1443 | */ | 1444 | */ |
1444 | 1445 | ||
1445 | cleanup(); | 1446 | cleanup(); |
1446 | check_http(config); | 1447 | check_http(config, redir_depth); |
1447 | } | 1448 | } |
1448 | 1449 | ||
1449 | /* check whether a file exists */ | 1450 | /* check whether a file exists */ |
@@ -1899,11 +1900,11 @@ check_curl_config_wrapper process_arguments(int argc, char **argv) { | |||
1899 | } | 1900 | } |
1900 | break; | 1901 | break; |
1901 | case '4': | 1902 | case '4': |
1902 | address_family = AF_INET; | 1903 | result.config.sin_family = AF_INET; |
1903 | break; | 1904 | break; |
1904 | case '6': | 1905 | case '6': |
1905 | #if defined(USE_IPV6) && defined(LIBCURL_FEATURE_IPV6) | 1906 | #if defined(USE_IPV6) && defined(LIBCURL_FEATURE_IPV6) |
1906 | address_family = AF_INET6; | 1907 | result.config.sin_family = AF_INET6; |
1907 | #else | 1908 | #else |
1908 | usage4(_("IPv6 support not available")); | 1909 | usage4(_("IPv6 support not available")); |
1909 | #endif | 1910 | #endif |
diff --git a/plugins/check_curl.d/config.h b/plugins/check_curl.d/config.h index 90ea3810..43be0306 100644 --- a/plugins/check_curl.d/config.h +++ b/plugins/check_curl.d/config.h | |||
@@ -6,6 +6,7 @@ | |||
6 | #include "thresholds.h" | 6 | #include "thresholds.h" |
7 | #include <stddef.h> | 7 | #include <stddef.h> |
8 | #include <string.h> | 8 | #include <string.h> |
9 | #include <sys/socket.h> | ||
9 | #include "curl/curl.h" | 10 | #include "curl/curl.h" |
10 | 11 | ||
11 | enum { | 12 | enum { |
@@ -37,6 +38,7 @@ typedef struct { | |||
37 | unsigned short virtual_port; | 38 | unsigned short virtual_port; |
38 | char *host_name; | 39 | char *host_name; |
39 | char *server_url; | 40 | char *server_url; |
41 | sa_family_t sin_family; | ||
40 | 42 | ||
41 | bool automatic_decompression; | 43 | bool automatic_decompression; |
42 | bool haproxy_protocol; | 44 | bool haproxy_protocol; |
@@ -91,6 +93,7 @@ check_curl_config check_curl_config_init() { | |||
91 | .virtual_port = 0, | 93 | .virtual_port = 0, |
92 | .host_name = NULL, | 94 | .host_name = NULL, |
93 | .server_url = strdup(DEFAULT_SERVER_URL), | 95 | .server_url = strdup(DEFAULT_SERVER_URL), |
96 | .sin_family = AF_UNSPEC, | ||
94 | 97 | ||
95 | .automatic_decompression = false, | 98 | .automatic_decompression = false, |
96 | .haproxy_protocol = false, | 99 | .haproxy_protocol = false, |