summaryrefslogtreecommitdiffstats
path: root/plugins/check_http.c
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/check_http.c')
-rw-r--r--plugins/check_http.c78
1 files changed, 39 insertions, 39 deletions
diff --git a/plugins/check_http.c b/plugins/check_http.c
index 703e3174..77a235e4 100644
--- a/plugins/check_http.c
+++ b/plugins/check_http.c
@@ -147,7 +147,7 @@ main (int argc, char **argv)
147 /* Set default URL. Must be malloced for subsequent realloc if --onredirect=follow */ 147 /* Set default URL. Must be malloced for subsequent realloc if --onredirect=follow */
148 server_url = strdup(HTTP_URL); 148 server_url = strdup(HTTP_URL);
149 server_url_length = strlen(server_url); 149 server_url_length = strlen(server_url);
150 asprintf (&user_agent, "User-Agent: check_http/v%s (nagios-plugins %s)", 150 xasprintf (&user_agent, "User-Agent: check_http/v%s (nagios-plugins %s)",
151 NP_VERSION, VERSION); 151 NP_VERSION, VERSION);
152 152
153 /* Parse extra opts if any */ 153 /* Parse extra opts if any */
@@ -265,7 +265,7 @@ process_arguments (int argc, char **argv)
265 warning_thresholds = optarg; 265 warning_thresholds = optarg;
266 break; 266 break;
267 case 'A': /* User Agent String */ 267 case 'A': /* User Agent String */
268 asprintf (&user_agent, "User-Agent: %s", optarg); 268 xasprintf (&user_agent, "User-Agent: %s", optarg);
269 break; 269 break;
270 case 'k': /* Additional headers */ 270 case 'k': /* Additional headers */
271 if (http_opt_headers_count == 0) 271 if (http_opt_headers_count == 0)
@@ -273,7 +273,7 @@ process_arguments (int argc, char **argv)
273 else 273 else
274 http_opt_headers = realloc (http_opt_headers, sizeof (char *) * (++http_opt_headers_count)); 274 http_opt_headers = realloc (http_opt_headers, sizeof (char *) * (++http_opt_headers_count));
275 http_opt_headers[http_opt_headers_count - 1] = optarg; 275 http_opt_headers[http_opt_headers_count - 1] = optarg;
276 /* asprintf (&http_opt_headers, "%s", optarg); */ 276 /* xasprintf (&http_opt_headers, "%s", optarg); */
277 break; 277 break;
278 case 'L': /* show html link */ 278 case 'L': /* show html link */
279 display_html = TRUE; 279 display_html = TRUE;
@@ -394,7 +394,7 @@ process_arguments (int argc, char **argv)
394 server_expect_yn = 1; 394 server_expect_yn = 1;
395 break; 395 break;
396 case 'T': /* Content-type */ 396 case 'T': /* Content-type */
397 asprintf (&http_content_type, "%s", optarg); 397 xasprintf (&http_content_type, "%s", optarg);
398 break; 398 break;
399 case 'l': /* linespan */ 399 case 'l': /* linespan */
400 cflags &= ~REG_NEWLINE; 400 cflags &= ~REG_NEWLINE;
@@ -690,31 +690,31 @@ check_document_dates (const char *headers, char **msg)
690 690
691 /* Done parsing the body. Now check the dates we (hopefully) parsed. */ 691 /* Done parsing the body. Now check the dates we (hopefully) parsed. */
692 if (!server_date || !*server_date) { 692 if (!server_date || !*server_date) {
693 asprintf (msg, _("%sServer date unknown, "), *msg); 693 xasprintf (msg, _("%sServer date unknown, "), *msg);
694 date_result = max_state_alt(STATE_UNKNOWN, date_result); 694 date_result = max_state_alt(STATE_UNKNOWN, date_result);
695 } else if (!document_date || !*document_date) { 695 } else if (!document_date || !*document_date) {
696 asprintf (msg, _("%sDocument modification date unknown, "), *msg); 696 xasprintf (msg, _("%sDocument modification date unknown, "), *msg);
697 date_result = max_state_alt(STATE_CRITICAL, date_result); 697 date_result = max_state_alt(STATE_CRITICAL, date_result);
698 } else { 698 } else {
699 time_t srv_data = parse_time_string (server_date); 699 time_t srv_data = parse_time_string (server_date);
700 time_t doc_data = parse_time_string (document_date); 700 time_t doc_data = parse_time_string (document_date);
701 701
702 if (srv_data <= 0) { 702 if (srv_data <= 0) {
703 asprintf (msg, _("%sServer date \"%100s\" unparsable, "), *msg, server_date); 703 xasprintf (msg, _("%sServer date \"%100s\" unparsable, "), *msg, server_date);
704 date_result = max_state_alt(STATE_CRITICAL, date_result); 704 date_result = max_state_alt(STATE_CRITICAL, date_result);
705 } else if (doc_data <= 0) { 705 } else if (doc_data <= 0) {
706 asprintf (msg, _("%sDocument date \"%100s\" unparsable, "), *msg, document_date); 706 xasprintf (msg, _("%sDocument date \"%100s\" unparsable, "), *msg, document_date);
707 date_result = max_state_alt(STATE_CRITICAL, date_result); 707 date_result = max_state_alt(STATE_CRITICAL, date_result);
708 } else if (doc_data > srv_data + 30) { 708 } else if (doc_data > srv_data + 30) {
709 asprintf (msg, _("%sDocument is %d seconds in the future, "), *msg, (int)doc_data - (int)srv_data); 709 xasprintf (msg, _("%sDocument is %d seconds in the future, "), *msg, (int)doc_data - (int)srv_data);
710 date_result = max_state_alt(STATE_CRITICAL, date_result); 710 date_result = max_state_alt(STATE_CRITICAL, date_result);
711 } else if (doc_data < srv_data - maximum_age) { 711 } else if (doc_data < srv_data - maximum_age) {
712 int n = (srv_data - doc_data); 712 int n = (srv_data - doc_data);
713 if (n > (60 * 60 * 24 * 2)) { 713 if (n > (60 * 60 * 24 * 2)) {
714 asprintf (msg, _("%sLast modified %.1f days ago, "), *msg, ((float) n) / (60 * 60 * 24)); 714 xasprintf (msg, _("%sLast modified %.1f days ago, "), *msg, ((float) n) / (60 * 60 * 24));
715 date_result = max_state_alt(STATE_CRITICAL, date_result); 715 date_result = max_state_alt(STATE_CRITICAL, date_result);
716 } else { 716 } else {
717 asprintf (msg, _("%sLast modified %d:%02d:%02d ago, "), *msg, n / (60 * 60), (n / 60) % 60, n % 60); 717 xasprintf (msg, _("%sLast modified %d:%02d:%02d ago, "), *msg, n / (60 * 60), (n / 60) % 60, n % 60);
718 date_result = max_state_alt(STATE_CRITICAL, date_result); 718 date_result = max_state_alt(STATE_CRITICAL, date_result);
719 } 719 }
720 } 720 }
@@ -831,10 +831,10 @@ check_http (void)
831 } 831 }
832#endif /* HAVE_SSL */ 832#endif /* HAVE_SSL */
833 833
834 asprintf (&buf, "%s %s %s\r\n%s\r\n", http_method, server_url, host_name ? "HTTP/1.1" : "HTTP/1.0", user_agent); 834 xasprintf (&buf, "%s %s %s\r\n%s\r\n", http_method, server_url, host_name ? "HTTP/1.1" : "HTTP/1.0", user_agent);
835 835
836 /* tell HTTP/1.1 servers not to keep the connection alive */ 836 /* tell HTTP/1.1 servers not to keep the connection alive */
837 asprintf (&buf, "%sConnection: close\r\n", buf); 837 xasprintf (&buf, "%sConnection: close\r\n", buf);
838 838
839 /* optionally send the host header info */ 839 /* optionally send the host header info */
840 if (host_name) { 840 if (host_name) {
@@ -845,16 +845,16 @@ check_http (void)
845 */ 845 */
846 if ((use_ssl == FALSE && server_port == HTTP_PORT) || 846 if ((use_ssl == FALSE && server_port == HTTP_PORT) ||
847 (use_ssl == TRUE && server_port == HTTPS_PORT)) 847 (use_ssl == TRUE && server_port == HTTPS_PORT))
848 asprintf (&buf, "%sHost: %s\r\n", buf, host_name); 848 xasprintf (&buf, "%sHost: %s\r\n", buf, host_name);
849 else 849 else
850 asprintf (&buf, "%sHost: %s:%d\r\n", buf, host_name, server_port); 850 xasprintf (&buf, "%sHost: %s:%d\r\n", buf, host_name, server_port);
851 } 851 }
852 852
853 /* optionally send any other header tag */ 853 /* optionally send any other header tag */
854 if (http_opt_headers_count) { 854 if (http_opt_headers_count) {
855 for (i = 0; i < http_opt_headers_count ; i++) { 855 for (i = 0; i < http_opt_headers_count ; i++) {
856 for ((pos = strtok(http_opt_headers[i], INPUT_DELIMITER)); pos; (pos = strtok(NULL, INPUT_DELIMITER))) 856 for ((pos = strtok(http_opt_headers[i], INPUT_DELIMITER)); pos; (pos = strtok(NULL, INPUT_DELIMITER)))
857 asprintf (&buf, "%s%s\r\n", buf, pos); 857 xasprintf (&buf, "%s%s\r\n", buf, pos);
858 } 858 }
859 /* This cannot be free'd here because a redirection will then try to access this and segfault */ 859 /* This cannot be free'd here because a redirection will then try to access this and segfault */
860 /* Covered in a testcase in tests/check_http.t */ 860 /* Covered in a testcase in tests/check_http.t */
@@ -864,29 +864,29 @@ check_http (void)
864 /* optionally send the authentication info */ 864 /* optionally send the authentication info */
865 if (strlen(user_auth)) { 865 if (strlen(user_auth)) {
866 base64_encode_alloc (user_auth, strlen (user_auth), &auth); 866 base64_encode_alloc (user_auth, strlen (user_auth), &auth);
867 asprintf (&buf, "%sAuthorization: Basic %s\r\n", buf, auth); 867 xasprintf (&buf, "%sAuthorization: Basic %s\r\n", buf, auth);
868 } 868 }
869 869
870 /* optionally send the proxy authentication info */ 870 /* optionally send the proxy authentication info */
871 if (strlen(proxy_auth)) { 871 if (strlen(proxy_auth)) {
872 base64_encode_alloc (proxy_auth, strlen (proxy_auth), &auth); 872 base64_encode_alloc (proxy_auth, strlen (proxy_auth), &auth);
873 asprintf (&buf, "%sProxy-Authorization: Basic %s\r\n", buf, auth); 873 xasprintf (&buf, "%sProxy-Authorization: Basic %s\r\n", buf, auth);
874 } 874 }
875 875
876 /* either send http POST data (any data, not only POST)*/ 876 /* either send http POST data (any data, not only POST)*/
877 if (http_post_data) { 877 if (http_post_data) {
878 if (http_content_type) { 878 if (http_content_type) {
879 asprintf (&buf, "%sContent-Type: %s\r\n", buf, http_content_type); 879 xasprintf (&buf, "%sContent-Type: %s\r\n", buf, http_content_type);
880 } else { 880 } else {
881 asprintf (&buf, "%sContent-Type: application/x-www-form-urlencoded\r\n", buf); 881 xasprintf (&buf, "%sContent-Type: application/x-www-form-urlencoded\r\n", buf);
882 } 882 }
883 883
884 asprintf (&buf, "%sContent-Length: %i\r\n\r\n", buf, (int)strlen (http_post_data)); 884 xasprintf (&buf, "%sContent-Length: %i\r\n\r\n", buf, (int)strlen (http_post_data));
885 asprintf (&buf, "%s%s%s", buf, http_post_data, CRLF); 885 xasprintf (&buf, "%s%s%s", buf, http_post_data, CRLF);
886 } 886 }
887 else { 887 else {
888 /* or just a newline so the server knows we're done with the request */ 888 /* or just a newline so the server knows we're done with the request */
889 asprintf (&buf, "%s%s", buf, CRLF); 889 xasprintf (&buf, "%s%s", buf, CRLF);
890 } 890 }
891 891
892 if (verbose) printf ("%s\n", buf); 892 if (verbose) printf ("%s\n", buf);
@@ -896,7 +896,7 @@ check_http (void)
896 full_page = strdup(""); 896 full_page = strdup("");
897 while ((i = my_recv (buffer, MAX_INPUT_BUFFER-1)) > 0) { 897 while ((i = my_recv (buffer, MAX_INPUT_BUFFER-1)) > 0) {
898 buffer[i] = '\0'; 898 buffer[i] = '\0';
899 asprintf (&full_page_new, "%s%s", full_page, buffer); 899 xasprintf (&full_page_new, "%s%s", full_page, buffer);
900 free (full_page); 900 free (full_page);
901 full_page = full_page_new; 901 full_page = full_page_new;
902 pagesize += i; 902 pagesize += i;
@@ -981,11 +981,11 @@ check_http (void)
981 /* make sure the status line matches the response we are looking for */ 981 /* make sure the status line matches the response we are looking for */
982 if (!expected_statuscode (status_line, server_expect)) { 982 if (!expected_statuscode (status_line, server_expect)) {
983 if (server_port == HTTP_PORT) 983 if (server_port == HTTP_PORT)
984 asprintf (&msg, 984 xasprintf (&msg,
985 _("Invalid HTTP response received from host: %s\n"), 985 _("Invalid HTTP response received from host: %s\n"),
986 status_line); 986 status_line);
987 else 987 else
988 asprintf (&msg, 988 xasprintf (&msg,
989 _("Invalid HTTP response received from host on port %d: %s\n"), 989 _("Invalid HTTP response received from host on port %d: %s\n"),
990 server_port, status_line); 990 server_port, status_line);
991 die (STATE_CRITICAL, "HTTP CRITICAL - %s", msg); 991 die (STATE_CRITICAL, "HTTP CRITICAL - %s", msg);
@@ -994,7 +994,7 @@ check_http (void)
994 /* Bypass normal status line check if server_expect was set by user and not default */ 994 /* Bypass normal status line check if server_expect was set by user and not default */
995 /* NOTE: After this if/else block msg *MUST* be an asprintf-allocated string */ 995 /* NOTE: After this if/else block msg *MUST* be an asprintf-allocated string */
996 if ( server_expect_yn ) { 996 if ( server_expect_yn ) {
997 asprintf (&msg, 997 xasprintf (&msg,
998 _("Status line output matched \"%s\" - "), server_expect); 998 _("Status line output matched \"%s\" - "), server_expect);
999 if (verbose) 999 if (verbose)
1000 printf ("%s\n",msg); 1000 printf ("%s\n",msg);
@@ -1017,12 +1017,12 @@ check_http (void)
1017 } 1017 }
1018 /* server errors result in a critical state */ 1018 /* server errors result in a critical state */
1019 else if (http_status >= 500) { 1019 else if (http_status >= 500) {
1020 asprintf (&msg, _("%s - "), status_line); 1020 xasprintf (&msg, _("%s - "), status_line);
1021 result = STATE_CRITICAL; 1021 result = STATE_CRITICAL;
1022 } 1022 }
1023 /* client errors result in a warning state */ 1023 /* client errors result in a warning state */
1024 else if (http_status >= 400) { 1024 else if (http_status >= 400) {
1025 asprintf (&msg, _("%s - "), status_line); 1025 xasprintf (&msg, _("%s - "), status_line);
1026 result = max_state_alt(STATE_WARNING, result); 1026 result = max_state_alt(STATE_WARNING, result);
1027 } 1027 }
1028 /* check redirected page if specified */ 1028 /* check redirected page if specified */
@@ -1032,11 +1032,11 @@ check_http (void)
1032 redir (header, status_line); 1032 redir (header, status_line);
1033 else 1033 else
1034 result = max_state_alt(onredirect, result); 1034 result = max_state_alt(onredirect, result);
1035 asprintf (&msg, _("%s - "), status_line); 1035 xasprintf (&msg, _("%s - "), status_line);
1036 } /* end if (http_status >= 300) */ 1036 } /* end if (http_status >= 300) */
1037 else { 1037 else {
1038 /* Print OK status anyway */ 1038 /* Print OK status anyway */
1039 asprintf (&msg, _("%s - "), status_line); 1039 xasprintf (&msg, _("%s - "), status_line);
1040 } 1040 }
1041 1041
1042 } /* end else (server_expect_yn) */ 1042 } /* end else (server_expect_yn) */
@@ -1056,7 +1056,7 @@ check_http (void)
1056 if(output_string_search[sizeof(output_string_search)-1]!='\0') { 1056 if(output_string_search[sizeof(output_string_search)-1]!='\0') {
1057 bcopy("...",&output_string_search[sizeof(output_string_search)-4],4); 1057 bcopy("...",&output_string_search[sizeof(output_string_search)-4],4);
1058 } 1058 }
1059 asprintf (&msg, _("%sstring '%s' not found on '%s://%s:%d%s', "), msg, output_string_search, use_ssl ? "https" : "http", host_name ? host_name : server_address, server_port, server_url); 1059 xasprintf (&msg, _("%sstring '%s' not found on '%s://%s:%d%s', "), msg, output_string_search, use_ssl ? "https" : "http", host_name ? host_name : server_address, server_port, server_url);
1060 result = STATE_CRITICAL; 1060 result = STATE_CRITICAL;
1061 } 1061 }
1062 } 1062 }
@@ -1069,15 +1069,15 @@ check_http (void)
1069 } 1069 }
1070 else if ((errcode == REG_NOMATCH && invert_regex == 0) || (errcode == 0 && invert_regex == 1)) { 1070 else if ((errcode == REG_NOMATCH && invert_regex == 0) || (errcode == 0 && invert_regex == 1)) {
1071 if (invert_regex == 0) 1071 if (invert_regex == 0)
1072 asprintf (&msg, _("%spattern not found, "), msg); 1072 xasprintf (&msg, _("%spattern not found, "), msg);
1073 else 1073 else
1074 asprintf (&msg, _("%spattern found, "), msg); 1074 xasprintf (&msg, _("%spattern found, "), msg);
1075 result = STATE_CRITICAL; 1075 result = STATE_CRITICAL;
1076 } 1076 }
1077 else { 1077 else {
1078 /* FIXME: Shouldn't that be UNKNOWN? */ 1078 /* FIXME: Shouldn't that be UNKNOWN? */
1079 regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER); 1079 regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER);
1080 asprintf (&msg, _("%sExecute Error: %s, "), msg, errbuf); 1080 xasprintf (&msg, _("%sExecute Error: %s, "), msg, errbuf);
1081 result = STATE_CRITICAL; 1081 result = STATE_CRITICAL;
1082 } 1082 }
1083 } 1083 }
@@ -1093,10 +1093,10 @@ check_http (void)
1093 */ 1093 */
1094 page_len = pagesize; 1094 page_len = pagesize;
1095 if ((max_page_len > 0) && (page_len > max_page_len)) { 1095 if ((max_page_len > 0) && (page_len > max_page_len)) {
1096 asprintf (&msg, _("%spage size %d too large, "), msg, page_len); 1096 xasprintf (&msg, _("%spage size %d too large, "), msg, page_len);
1097 result = max_state_alt(STATE_WARNING, result); 1097 result = max_state_alt(STATE_WARNING, result);
1098 } else if ((min_page_len > 0) && (page_len < min_page_len)) { 1098 } else if ((min_page_len > 0) && (page_len < min_page_len)) {
1099 asprintf (&msg, _("%spage size %d too small, "), msg, page_len); 1099 xasprintf (&msg, _("%spage size %d too small, "), msg, page_len);
1100 result = max_state_alt(STATE_WARNING, result); 1100 result = max_state_alt(STATE_WARNING, result);
1101 } 1101 }
1102 1102
@@ -1107,7 +1107,7 @@ check_http (void)
1107 msg[strlen(msg)-3] = '\0'; 1107 msg[strlen(msg)-3] = '\0';
1108 1108
1109 /* check elapsed time */ 1109 /* check elapsed time */
1110 asprintf (&msg, 1110 xasprintf (&msg,
1111 _("%s - %d bytes in %.3f second response time %s|%s %s"), 1111 _("%s - %d bytes in %.3f second response time %s|%s %s"),
1112 msg, page_len, elapsed_time, 1112 msg, page_len, elapsed_time,
1113 (display_html ? "</A>" : ""), 1113 (display_html ? "</A>" : ""),
@@ -1214,7 +1214,7 @@ redir (char *pos, char *status_line)
1214 if ((url[0] != '/')) { 1214 if ((url[0] != '/')) {
1215 if ((x = strrchr(server_url, '/'))) 1215 if ((x = strrchr(server_url, '/')))
1216 *x = '\0'; 1216 *x = '\0';
1217 asprintf (&url, "%s/%s", server_url, url); 1217 xasprintf (&url, "%s/%s", server_url, url);
1218 } 1218 }
1219 i = server_port; 1219 i = server_port;
1220 strcpy (type, server_type); 1220 strcpy (type, server_type);