summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndreas Baumann <mail@andreasbaumann.cc>2017-03-19 18:19:51 +0100
committerAndreas Baumann <mail@andreasbaumann.cc>2017-03-19 18:19:51 +0100
commit244712dd91615bd1a728b3d57ac55b6c71e4ddb0 (patch)
tree45216e93147ed7aada027462dfbc5ada1e395523
parent501eedf8f114998e690e87bd45c886dba75276da (diff)
downloadmonitoring-plugins-244712dd91615bd1a728b3d57ac55b6c71e4ddb0.tar.gz
added POST support (-P/-T options)
-rw-r--r--plugins/check_curl.c38
1 files changed, 33 insertions, 5 deletions
diff --git a/plugins/check_curl.c b/plugins/check_curl.c
index f6eaba61..4129acca 100644
--- a/plugins/check_curl.c
+++ b/plugins/check_curl.c
@@ -107,6 +107,8 @@ int show_extended_perfdata = FALSE;
107int min_page_len = 0; 107int min_page_len = 0;
108int max_page_len = 0; 108int max_page_len = 0;
109char *http_method = NULL; 109char *http_method = NULL;
110char *http_post_data = NULL;
111char *http_content_type = NULL;
110CURL *curl; 112CURL *curl;
111struct curl_slist *header_list = NULL; 113struct curl_slist *header_list = NULL;
112curlhelp_curlbuf body_buf; 114curlhelp_curlbuf body_buf;
@@ -368,9 +370,21 @@ check_http (void)
368 else if (address_family == AF_INET6) 370 else if (address_family == AF_INET6)
369 curl_easy_setopt (curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V6); 371 curl_easy_setopt (curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V6);
370 372
373 /* either send http POST data (any data, not only POST)*/
374 if (http_post_data) {
375 if (http_content_type) {
376 snprintf (http_header, DEFAULT_BUFFER_SIZE, "Content-Type: %s", http_content_type);
377 header_list = curl_slist_append (header_list, http_header);
378 }
379 curl_easy_setopt (curl, CURLOPT_POSTFIELDS, http_post_data);
380 }
381
371 /* do the request */ 382 /* do the request */
372 res = curl_easy_perform(curl); 383 res = curl_easy_perform(curl);
373 384
385 if (verbose>=2 && http_post_data)
386 printf ("**** REQUEST CONTENT ****\n%s\n", http_post_data);
387
374 /* free header list, we don't need it anymore */ 388 /* free header list, we don't need it anymore */
375 curl_slist_free_all(header_list); 389 curl_slist_free_all(header_list);
376 390
@@ -603,6 +617,7 @@ process_arguments (int argc, char **argv)
603 STD_LONG_OPTS, 617 STD_LONG_OPTS,
604 {"ssl", optional_argument, 0, 'S'}, 618 {"ssl", optional_argument, 0, 'S'},
605 {"sni", no_argument, 0, SNI_OPTION}, 619 {"sni", no_argument, 0, SNI_OPTION},
620 {"post", required_argument, 0, 'P'},
606 {"method", required_argument, 0, 'j'}, 621 {"method", required_argument, 0, 'j'},
607 {"IP-address", required_argument, 0, 'I'}, 622 {"IP-address", required_argument, 0, 'I'},
608 {"url", required_argument, 0, 'u'}, 623 {"url", required_argument, 0, 'u'},
@@ -623,6 +638,7 @@ process_arguments (int argc, char **argv)
623 {"useragent", required_argument, 0, 'A'}, 638 {"useragent", required_argument, 0, 'A'},
624 {"header", required_argument, 0, 'k'}, 639 {"header", required_argument, 0, 'k'},
625 {"no-body", no_argument, 0, 'N'}, 640 {"no-body", no_argument, 0, 'N'},
641 {"content-type", required_argument, 0, 'T'},
626 {"pagesize", required_argument, 0, 'm'}, 642 {"pagesize", required_argument, 0, 'm'},
627 {"invert-regex", no_argument, NULL, INVERT_REGEX}, 643 {"invert-regex", no_argument, NULL, INVERT_REGEX},
628 {"use-ipv4", no_argument, 0, '4'}, 644 {"use-ipv4", no_argument, 0, '4'},
@@ -649,7 +665,7 @@ process_arguments (int argc, char **argv)
649 } 665 }
650 666
651 while (1) { 667 while (1) {
652 c = getopt_long (argc, argv, "Vvh46t:c:w:A:k:H:j:I:a:p:d:e:s:R:r:u:f:C:J:K:S::m:NE", longopts, &option); 668 c = getopt_long (argc, argv, "Vvh46t:c:w:A:k:H:P:j:T:I:a:p:d:e:s:R:r:u:f:C:J:K:S::m:NE", longopts, &option);
653 if (c == -1 || c == EOF || c == 1) 669 if (c == -1 || c == EOF || c == 1)
654 break; 670 break;
655 671
@@ -716,6 +732,12 @@ process_arguments (int argc, char **argv)
716 strncpy (user_auth, optarg, MAX_INPUT_BUFFER - 1); 732 strncpy (user_auth, optarg, MAX_INPUT_BUFFER - 1);
717 user_auth[MAX_INPUT_BUFFER - 1] = 0; 733 user_auth[MAX_INPUT_BUFFER - 1] = 0;
718 break; 734 break;
735 case 'P': /* HTTP POST data in URL encoded format; ignored if settings already */
736 if (! http_post_data)
737 http_post_data = strdup (optarg);
738 if (! http_method)
739 http_method = strdup("POST");
740 break;
719 case 'j': /* Set HTTP method */ 741 case 'j': /* Set HTTP method */
720 if (http_method) 742 if (http_method)
721 free(http_method); 743 free(http_method);
@@ -884,6 +906,9 @@ process_arguments (int argc, char **argv)
884 server_expect[MAX_INPUT_BUFFER - 1] = 0; 906 server_expect[MAX_INPUT_BUFFER - 1] = 0;
885 server_expect_yn = 1; 907 server_expect_yn = 1;
886 break; 908 break;
909 case 'T': /* Content-type */
910 http_content_type = strdup (optarg);
911 break;
887 case 'l': /* linespan */ 912 case 'l': /* linespan */
888 cflags &= ~REG_NEWLINE; 913 cflags &= ~REG_NEWLINE;
889 break; 914 break;
@@ -987,7 +1012,7 @@ print_help (void)
987 print_revision (progname, NP_VERSION); 1012 print_revision (progname, NP_VERSION);
988 1013
989 printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n"); 1014 printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
990 printf ("Copyright (c) 2017 Andreas Baumann <abaumann@yahoo.com>\n"); 1015 printf ("Copyright (c) 2017 Andreas Baumann <mail@andreasbaumann.cc>\n");
991 printf (COPYRIGHT, copyright, email); 1016 printf (COPYRIGHT, copyright, email);
992 1017
993 printf ("%s\n", _("This plugin tests the HTTP service on the specified host. It can test")); 1018 printf ("%s\n", _("This plugin tests the HTTP service on the specified host. It can test"));
@@ -1058,11 +1083,15 @@ print_help (void)
1058 printf (" %s\n", _("String to expect in the content")); 1083 printf (" %s\n", _("String to expect in the content"));
1059 printf (" %s\n", "-u, --url=PATH"); 1084 printf (" %s\n", "-u, --url=PATH");
1060 printf (" %s\n", _("URL to GET or POST (default: /)")); 1085 printf (" %s\n", _("URL to GET or POST (default: /)"));
1086 printf (" %s\n", "-P, --post=STRING");
1087 printf (" %s\n", _("URL encoded http POST data"));
1061 printf (" %s\n", "-j, --method=STRING (for example: HEAD, OPTIONS, TRACE, PUT, DELETE, CONNECT)"); 1088 printf (" %s\n", "-j, --method=STRING (for example: HEAD, OPTIONS, TRACE, PUT, DELETE, CONNECT)");
1062 printf (" %s\n", _("Set HTTP method.")); 1089 printf (" %s\n", _("Set HTTP method."));
1063 printf (" %s\n", "-N, --no-body"); 1090 printf (" %s\n", "-N, --no-body");
1064 printf (" %s\n", _("Don't wait for document body: stop reading after headers.")); 1091 printf (" %s\n", _("Don't wait for document body: stop reading after headers."));
1065 printf (" %s\n", _("(Note that this still does an HTTP GET or POST, not a HEAD.)")); 1092 printf (" %s\n", _("(Note that this still does an HTTP GET or POST, not a HEAD.)"));
1093 printf (" %s\n", "-T, --content-type=STRING");
1094 printf (" %s\n", _("specify Content-Type header media type when POSTing\n"));
1066 printf (" %s\n", "-l, --linespan"); 1095 printf (" %s\n", "-l, --linespan");
1067 printf (" %s\n", _("Allow regex to span newlines (must precede -r or -R)")); 1096 printf (" %s\n", _("Allow regex to span newlines (must precede -r or -R)"));
1068 printf (" %s\n", "-r, --regex, --ereg=STRING"); 1097 printf (" %s\n", "-r, --regex, --ereg=STRING");
@@ -1152,10 +1181,9 @@ print_usage (void)
1152 printf (" [-w <warn time>] [-c <critical time>] [-t <timeout>] [-E] [-a auth]\n"); 1181 printf (" [-w <warn time>] [-c <critical time>] [-t <timeout>] [-E] [-a auth]\n");
1153 printf (" [-f <ok|warning|critcal|follow>]\n"); 1182 printf (" [-f <ok|warning|critcal|follow>]\n");
1154 printf (" [-e <expect>] [-d string] [-s string] [-l] [-r <regex> | -R <case-insensitive regex>]\n"); 1183 printf (" [-e <expect>] [-d string] [-s string] [-l] [-r <regex> | -R <case-insensitive regex>]\n");
1155 printf (" [-m <min_pg_size>:<max_pg_size>] [-N]\n"); 1184 printf (" [-P string] [-m <min_pg_size>:<max_pg_size>] [-4|-6] [-N]\n");
1156 printf (" [-4|-6] [-N]\n");
1157 printf (" [-A string] [-k string] [-S <version>] [--sni] [-C <warn_age>[,<crit_age>]]\n"); 1185 printf (" [-A string] [-k string] [-S <version>] [--sni] [-C <warn_age>[,<crit_age>]]\n");
1158 printf (" [-v verbose]\n", progname); 1186 printf (" [-T <content-type>] [-j method]\n", progname);
1159 printf ("\n"); 1187 printf ("\n");
1160 printf ("%s\n", _("WARNING: check_curl is experimental. Please use")); 1188 printf ("%s\n", _("WARNING: check_curl is experimental. Please use"));
1161 printf ("%s\n\n", _("check_http if you need a stable version.")); 1189 printf ("%s\n\n", _("check_http if you need a stable version."));