summaryrefslogtreecommitdiffstats
path: root/plugins/check_curl.c
diff options
context:
space:
mode:
authorAndreas Baumann <mail@andreasbaumann.cc>2023-02-11 19:11:07 +0100
committerAndreas Baumann <mail@andreasbaumann.cc>2023-02-11 19:11:07 +0100
commitf6978deaa1bf7c6a7196363104ebfcef143080ab (patch)
treee0f84ae5c919c929a6877e63f6387733da725622 /plugins/check_curl.c
parent27b0c6964559ba60ff6c7a626d51e62e5256ed62 (diff)
downloadmonitoring-plugins-f6978deaa1bf7c6a7196363104ebfcef143080ab.tar.gz
added --cookie-jar and doing proper cleanup of libcurl
Diffstat (limited to 'plugins/check_curl.c')
-rw-r--r--plugins/check_curl.c47
1 files changed, 34 insertions, 13 deletions
diff --git a/plugins/check_curl.c b/plugins/check_curl.c
index 406f6f88..35d1237b 100644
--- a/plugins/check_curl.c
+++ b/plugins/check_curl.c
@@ -214,6 +214,7 @@ int address_family = AF_UNSPEC;
214curlhelp_ssl_library ssl_library = CURLHELP_SSL_LIBRARY_UNKNOWN; 214curlhelp_ssl_library ssl_library = CURLHELP_SSL_LIBRARY_UNKNOWN;
215int curl_http_version = CURL_HTTP_VERSION_NONE; 215int curl_http_version = CURL_HTTP_VERSION_NONE;
216int automatic_decompression = FALSE; 216int automatic_decompression = FALSE;
217char *cookie_jar_file = NULL;
217 218
218int process_arguments (int, char**); 219int process_arguments (int, char**);
219void handle_curl_option_return_code (CURLcode res, const char* option); 220void handle_curl_option_return_code (CURLcode res, const char* option);
@@ -412,6 +413,19 @@ lookup_host (const char *host, char *buf, size_t buflen)
412 return 0; 413 return 0;
413} 414}
414 415
416static void
417cleanup (void)
418{
419 curlhelp_free_statusline(&status_line);
420 curl_easy_cleanup (curl);
421 curl_global_cleanup ();
422 curlhelp_freewritebuffer (&body_buf);
423 curlhelp_freewritebuffer (&header_buf);
424 if (!strcmp (http_method, "PUT")) {
425 curlhelp_freereadbuffer (&put_buf);
426 }
427}
428
415int 429int
416check_http (void) 430check_http (void)
417{ 431{
@@ -743,7 +757,16 @@ check_http (void)
743 handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_INFILESIZE, (curl_off_t)strlen (http_post_data)), "CURLOPT_INFILESIZE"); 757 handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_INFILESIZE, (curl_off_t)strlen (http_post_data)), "CURLOPT_INFILESIZE");
744 } 758 }
745 } 759 }
760
761 /* cookie handling */
762 if (cookie_jar_file != NULL) {
763 handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_COOKIEJAR, cookie_jar_file), "CURLOPT_COOKIEJAR");
764 handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_COOKIEFILE, cookie_jar_file), "CURLOPT_COOKIEFILE");
765 }
746 766
767 /* register cleanup function to shut down libcurl properly */
768 atexit (cleanup);
769
747 /* do the request */ 770 /* do the request */
748 res = curl_easy_perform(curl); 771 res = curl_easy_perform(curl);
749 772
@@ -1021,7 +1044,7 @@ GOT_FIRST_CERT:
1021 else 1044 else
1022 msg[strlen(msg)-3] = '\0'; 1045 msg[strlen(msg)-3] = '\0';
1023 } 1046 }
1024 1047
1025 /* TODO: separate _() msg and status code: die (result, "HTTP %s: %s\n", state_text(result), msg); */ 1048 /* TODO: separate _() msg and status code: die (result, "HTTP %s: %s\n", state_text(result), msg); */
1026 die (result, "HTTP %s: %s %d %s%s%s - %d bytes in %.3f second response time %s|%s\n%s%s", 1049 die (result, "HTTP %s: %s %d %s%s%s - %d bytes in %.3f second response time %s|%s\n%s%s",
1027 state_text(result), string_statuscode (status_line.http_major, status_line.http_minor), 1050 state_text(result), string_statuscode (status_line.http_major, status_line.http_minor),
@@ -1033,16 +1056,6 @@ GOT_FIRST_CERT:
1033 (show_body ? body_buf.buf : ""), 1056 (show_body ? body_buf.buf : ""),
1034 (show_body ? "\n" : "") ); 1057 (show_body ? "\n" : "") );
1035 1058
1036 /* proper cleanup after die? */
1037 curlhelp_free_statusline(&status_line);
1038 curl_easy_cleanup (curl);
1039 curl_global_cleanup ();
1040 curlhelp_freewritebuffer (&body_buf);
1041 curlhelp_freewritebuffer (&header_buf);
1042 if (!strcmp (http_method, "PUT")) {
1043 curlhelp_freereadbuffer (&put_buf);
1044 }
1045
1046 return result; 1059 return result;
1047} 1060}
1048 1061
@@ -1239,7 +1252,8 @@ process_arguments (int argc, char **argv)
1239 CONTINUE_AFTER_CHECK_CERT, 1252 CONTINUE_AFTER_CHECK_CERT,
1240 CA_CERT_OPTION, 1253 CA_CERT_OPTION,
1241 HTTP_VERSION_OPTION, 1254 HTTP_VERSION_OPTION,
1242 AUTOMATIC_DECOMPRESSION 1255 AUTOMATIC_DECOMPRESSION,
1256 COOKIE_JAR
1243 }; 1257 };
1244 1258
1245 int option = 0; 1259 int option = 0;
@@ -1285,6 +1299,7 @@ process_arguments (int argc, char **argv)
1285 {"max-redirs", required_argument, 0, MAX_REDIRS_OPTION}, 1299 {"max-redirs", required_argument, 0, MAX_REDIRS_OPTION},
1286 {"http-version", required_argument, 0, HTTP_VERSION_OPTION}, 1300 {"http-version", required_argument, 0, HTTP_VERSION_OPTION},
1287 {"enable-automatic-decompression", no_argument, 0, AUTOMATIC_DECOMPRESSION}, 1301 {"enable-automatic-decompression", no_argument, 0, AUTOMATIC_DECOMPRESSION},
1302 {"cookie-jar", required_argument, 0, COOKIE_JAR},
1288 {0, 0, 0, 0} 1303 {0, 0, 0, 0}
1289 }; 1304 };
1290 1305
@@ -1691,6 +1706,9 @@ process_arguments (int argc, char **argv)
1691 case AUTOMATIC_DECOMPRESSION: 1706 case AUTOMATIC_DECOMPRESSION:
1692 automatic_decompression = TRUE; 1707 automatic_decompression = TRUE;
1693 break; 1708 break;
1709 case COOKIE_JAR:
1710 cookie_jar_file = optarg;
1711 break;
1694 case '?': 1712 case '?':
1695 /* print short usage statement if args not parsable */ 1713 /* print short usage statement if args not parsable */
1696 usage5 (); 1714 usage5 ();
@@ -1910,6 +1928,8 @@ print_help (void)
1910 printf (" %s\n", _("1.0 = HTTP/1.0, 1.1 = HTTP/1.1, 2.0 = HTTP/2 (HTTP/2 will fail without -S)")); 1928 printf (" %s\n", _("1.0 = HTTP/1.0, 1.1 = HTTP/1.1, 2.0 = HTTP/2 (HTTP/2 will fail without -S)"));
1911 printf (" %s\n", "--enable-automatic-decompression"); 1929 printf (" %s\n", "--enable-automatic-decompression");
1912 printf (" %s\n", _("Enable automatic decompression of body (CURLOPT_ACCEPT_ENCODING).")); 1930 printf (" %s\n", _("Enable automatic decompression of body (CURLOPT_ACCEPT_ENCODING)."));
1931 printf (" %s\n", "---cookie-jar=FILE");
1932 printf (" %s\n", _("Store cookies in the cookie jar and send them out when requested."));
1913 printf ("\n"); 1933 printf ("\n");
1914 1934
1915 printf (UT_WARN_CRIT); 1935 printf (UT_WARN_CRIT);
@@ -1994,7 +2014,8 @@ print_usage (void)
1994 printf (" [-P string] [-m <min_pg_size>:<max_pg_size>] [-4|-6] [-N] [-M <age>]\n"); 2014 printf (" [-P string] [-m <min_pg_size>:<max_pg_size>] [-4|-6] [-N] [-M <age>]\n");
1995 printf (" [-A string] [-k string] [-S <version>] [--sni]\n"); 2015 printf (" [-A string] [-k string] [-S <version>] [--sni]\n");
1996 printf (" [-T <content-type>] [-j method]\n"); 2016 printf (" [-T <content-type>] [-j method]\n");
1997 printf (" [--http-version=<version>]\n"); 2017 printf (" [--http-version=<version>] [--enable-automatic-decompression]\n");
2018 printf (" [--cookie-jar=<cookie jar file>\n");
1998 printf (" %s -H <vhost> | -I <IP-address> -C <warn_age>[,<crit_age>]\n",progname); 2019 printf (" %s -H <vhost> | -I <IP-address> -C <warn_age>[,<crit_age>]\n",progname);
1999 printf (" [-p <port>] [-t <timeout>] [-4|-6] [--sni]\n"); 2020 printf (" [-p <port>] [-t <timeout>] [-4|-6] [--sni]\n");
2000 printf ("\n"); 2021 printf ("\n");