diff options
-rw-r--r-- | plugins/check_http.c | 200 |
1 files changed, 111 insertions, 89 deletions
diff --git a/plugins/check_http.c b/plugins/check_http.c index db5d50dd..ecf61288 100644 --- a/plugins/check_http.c +++ b/plugins/check_http.c | |||
@@ -54,7 +54,8 @@ certificate expiration times.\n" | |||
54 | -I, --IP-address=ADDRESS\n\ | 54 | -I, --IP-address=ADDRESS\n\ |
55 | IP address or name (use numeric address if possible to bypass DNS lookup).\n\ | 55 | IP address or name (use numeric address if possible to bypass DNS lookup).\n\ |
56 | -e, --expect=STRING\n\ | 56 | -e, --expect=STRING\n\ |
57 | String to expect in first line of server response (default: %s)\n\ | 57 | String to expect in first (status) line of server response (default: %s)\n\ |
58 | If specified skips all other status line logic (ex: 3xx, 4xx, 5xx processing)\n\ | ||
58 | -s, --string=STRING\n\ | 59 | -s, --string=STRING\n\ |
59 | String to expect in the content\n\ | 60 | String to expect in the content\n\ |
60 | -u, --url=PATH\n\ | 61 | -u, --url=PATH\n\ |
@@ -186,6 +187,7 @@ char *server_address = NULL; | |||
186 | char *host_name = NULL; | 187 | char *host_name = NULL; |
187 | char *server_url = NULL; | 188 | char *server_url = NULL; |
188 | int server_url_length = 0; | 189 | int server_url_length = 0; |
190 | int server_expect_yn = 0; | ||
189 | char server_expect[MAX_INPUT_BUFFER] = HTTP_EXPECT; | 191 | char server_expect[MAX_INPUT_BUFFER] = HTTP_EXPECT; |
190 | char string_expect[MAX_INPUT_BUFFER] = ""; | 192 | char string_expect[MAX_INPUT_BUFFER] = ""; |
191 | int warning_time = 0; | 193 | int warning_time = 0; |
@@ -383,6 +385,8 @@ process_arguments (int argc, char **argv) | |||
383 | onredirect = STATE_WARNING; | 385 | onredirect = STATE_WARNING; |
384 | if (!strcmp (optarg, "critical")) | 386 | if (!strcmp (optarg, "critical")) |
385 | onredirect = STATE_CRITICAL; | 387 | onredirect = STATE_CRITICAL; |
388 | if (verbose) | ||
389 | printf("option f:%d \n", onredirect); | ||
386 | break; | 390 | break; |
387 | /* Note: H, I, and u must be malloc'd or will fail on redirects */ | 391 | /* Note: H, I, and u must be malloc'd or will fail on redirects */ |
388 | case 'H': /* Host Name (virtual host) */ | 392 | case 'H': /* Host Name (virtual host) */ |
@@ -416,6 +420,7 @@ process_arguments (int argc, char **argv) | |||
416 | case 'e': /* string or substring */ | 420 | case 'e': /* string or substring */ |
417 | strncpy (server_expect, optarg, MAX_INPUT_BUFFER - 1); | 421 | strncpy (server_expect, optarg, MAX_INPUT_BUFFER - 1); |
418 | server_expect[MAX_INPUT_BUFFER - 1] = 0; | 422 | server_expect[MAX_INPUT_BUFFER - 1] = 0; |
423 | server_expect_yn = 1; | ||
419 | break; | 424 | break; |
420 | case 'R': /* regex */ | 425 | case 'R': /* regex */ |
421 | #ifdef HAVE_REGEX_H | 426 | #ifdef HAVE_REGEX_H |
@@ -615,6 +620,8 @@ check_http (void) | |||
615 | } | 620 | } |
616 | sprintf (buffer, "%s %s HTTP/1.0\r\n", http_method, server_url); | 621 | sprintf (buffer, "%s %s HTTP/1.0\r\n", http_method, server_url); |
617 | send (sd, buffer, strlen (buffer), 0); | 622 | send (sd, buffer, strlen (buffer), 0); |
623 | |||
624 | |||
618 | 625 | ||
619 | /* optionally send the host header info */ | 626 | /* optionally send the host header info */ |
620 | if (strcmp (host_name, "")) { | 627 | if (strcmp (host_name, "")) { |
@@ -716,103 +723,119 @@ check_http (void) | |||
716 | terminate (STATE_CRITICAL, msg); | 723 | terminate (STATE_CRITICAL, msg); |
717 | } | 724 | } |
718 | 725 | ||
719 | /* check the return code */ | ||
720 | /* server errors result in a critical state */ | ||
721 | if (strstr (status_line, "500") || | ||
722 | strstr (status_line, "501") || | ||
723 | strstr (status_line, "502") || | ||
724 | strstr (status_line, "503")) { | ||
725 | msg = ssprintf (msg, "HTTP CRITICAL: %s\n", status_line); | ||
726 | terminate (STATE_CRITICAL, msg); | ||
727 | } | ||
728 | 726 | ||
729 | /* client errors result in a warning state */ | 727 | /* Exit here if server_expect was set by user and not default */ |
730 | if (strstr (status_line, "400") || | 728 | if ( server_expect_yn ) { |
731 | strstr (status_line, "401") || | 729 | msg = ssprintf (msg, "HTTP OK: Status line output matched \"%s\"\n",server_expect); |
732 | strstr (status_line, "402") || | 730 | if (verbose) |
733 | strstr (status_line, "403") || | 731 | printf ("%s\n",msg); |
734 | strstr (status_line, "404")) { | 732 | |
735 | msg = ssprintf (msg, "HTTP WARNING: %s\n", status_line); | ||
736 | terminate (STATE_WARNING, msg); | ||
737 | } | 733 | } |
734 | else { | ||
735 | |||
736 | |||
737 | /* check the return code */ | ||
738 | /* server errors result in a critical state */ | ||
739 | if (strstr (status_line, "500") || | ||
740 | strstr (status_line, "501") || | ||
741 | strstr (status_line, "502") || | ||
742 | strstr (status_line, "503")) { | ||
743 | msg = ssprintf (msg, "HTTP CRITICAL: %s\n", status_line); | ||
744 | terminate (STATE_CRITICAL, msg); | ||
745 | } | ||
746 | |||
747 | /* client errors result in a warning state */ | ||
748 | if (strstr (status_line, "400") || | ||
749 | strstr (status_line, "401") || | ||
750 | strstr (status_line, "402") || | ||
751 | strstr (status_line, "403") || | ||
752 | strstr (status_line, "404")) { | ||
753 | msg = ssprintf (msg, "HTTP WARNING: %s\n", status_line); | ||
754 | terminate (STATE_WARNING, msg); | ||
755 | } | ||
738 | 756 | ||
739 | /* check redirected page if specified */ | 757 | /* check redirected page if specified */ |
740 | if (strstr (status_line, "300") || | 758 | if (strstr (status_line, "300") || |
741 | strstr (status_line, "301") || | 759 | strstr (status_line, "301") || |
742 | strstr (status_line, "302") || | 760 | strstr (status_line, "302") || |
743 | strstr (status_line, "303") || | 761 | strstr (status_line, "303") || |
744 | strstr (status_line, "304")) { | 762 | strstr (status_line, "304")) { |
745 | if (onredirect == STATE_DEPENDENT) { | 763 | if (onredirect == STATE_DEPENDENT) { |
746 | 764 | ||
747 | pos = header; | 765 | pos = header; |
748 | while (pos) { | 766 | while (pos) { |
749 | server_address = realloc (server_address, MAX_IPV4_HOSTLENGTH); | 767 | server_address = realloc (server_address, MAX_IPV4_HOSTLENGTH); |
750 | if (server_address == NULL) | 768 | if (server_address == NULL) |
751 | terminate (STATE_UNKNOWN, | ||
752 | "HTTP UNKNOWN: could not allocate server_address"); | ||
753 | if (strspn (pos, "\r\n") > server_url_length) { | ||
754 | server_url = realloc (server_url, strspn (pos, "\r\n")); | ||
755 | if (server_url == NULL) | ||
756 | terminate (STATE_UNKNOWN, | 769 | terminate (STATE_UNKNOWN, |
770 | "HTTP UNKNOWN: could not allocate server_address"); | ||
771 | if (strspn (pos, "\r\n") > server_url_length) { | ||
772 | server_url = realloc (server_url, strspn (pos, "\r\n")); | ||
773 | if (server_url == NULL) | ||
774 | terminate (STATE_UNKNOWN, | ||
757 | "HTTP UNKNOWN: could not allocate server_url"); | 775 | "HTTP UNKNOWN: could not allocate server_url"); |
758 | server_url_length = strspn (pos, "\r\n"); | 776 | server_url_length = strspn (pos, "\r\n"); |
759 | } | 777 | } |
760 | if (sscanf (pos, HDR_LOCATION URI_HTTP URI_HOST URI_PORT URI_PATH, server_type, server_address, server_port_text, server_url) == 4) { | 778 | if (sscanf (pos, HDR_LOCATION URI_HTTP URI_HOST URI_PORT URI_PATH, server_type, server_address, server_port_text, server_url) == 4) { |
761 | host_name = strscpy (host_name, server_address); | 779 | host_name = strscpy (host_name, server_address); |
762 | use_ssl = server_type_check (server_type); | 780 | use_ssl = server_type_check (server_type); |
763 | server_port = atoi (server_port_text); | 781 | server_port = atoi (server_port_text); |
764 | check_http (); | 782 | check_http (); |
765 | } | 783 | } |
766 | else if (sscanf (pos, HDR_LOCATION URI_HTTP URI_HOST URI_PATH, server_type, server_address, server_url) == 3) { | 784 | else if (sscanf (pos, HDR_LOCATION URI_HTTP URI_HOST URI_PATH, server_type, server_address, server_url) == 3 ) { |
767 | host_name = strscpy (host_name, server_address); | 785 | host_name = strscpy (host_name, server_address); |
768 | use_ssl = server_type_check (server_type); | 786 | use_ssl = server_type_check (server_type); |
769 | server_port = server_port_check (use_ssl); | 787 | server_port = server_port_check (use_ssl); |
770 | check_http (); | 788 | check_http (); |
771 | } | 789 | } |
772 | else if (sscanf (pos, HDR_LOCATION URI_HTTP URI_HOST URI_PORT, server_type, server_address, server_port_text) == 3) { | 790 | else if (sscanf (pos, HDR_LOCATION URI_HTTP URI_HOST URI_PORT, server_type, server_address, server_port_text) == 3) { |
773 | host_name = strscpy (host_name, server_address); | 791 | host_name = strscpy (host_name, server_address); |
774 | strcpy (server_url, "/"); | 792 | strcpy (server_url, "/"); |
775 | use_ssl = server_type_check (server_type); | 793 | use_ssl = server_type_check (server_type); |
776 | server_port = atoi (server_port_text); | 794 | server_port = atoi (server_port_text); |
777 | check_http (); | 795 | check_http (); |
778 | } | 796 | } |
779 | else if (sscanf (pos, HDR_LOCATION URI_HTTP URI_HOST, server_type, server_address) == 2) { | 797 | else if (sscanf (pos, HDR_LOCATION URI_HTTP URI_HOST, server_type, server_address) == 2) { |
780 | host_name = strscpy (host_name, server_address); | 798 | host_name = strscpy (host_name, server_address); |
781 | strcpy (server_url, "/"); | 799 | strcpy (server_url, "/"); |
782 | use_ssl = server_type_check (server_type); | 800 | use_ssl = server_type_check (server_type); |
783 | server_port = server_port_check (use_ssl); | 801 | server_port = server_port_check (use_ssl); |
784 | check_http (); | 802 | check_http (); |
785 | } | 803 | } |
786 | else if (sscanf (pos, HDR_LOCATION URI_PATH, server_url) == 1) { | 804 | else if (sscanf (pos, HDR_LOCATION URI_PATH, server_url) == 1) { |
787 | check_http (); | 805 | check_http (); |
788 | } | 806 | } |
789 | pos += (size_t) strcspn (pos, "\r\n"); | 807 | pos += (size_t) strcspn (pos, "\r\n"); |
790 | pos += (size_t) strspn (pos, "\r\n"); | 808 | pos += (size_t) strspn (pos, "\r\n"); |
791 | } /* end while (pos) */ | 809 | } /* end while (pos) */ |
792 | printf ("HTTP UNKNOWN: Could not find redirect location - %s%s", | 810 | printf ("HTTP UNKNOWN: Could not find redirect location - %s%s", |
793 | status_line, (display_html ? "</A>" : "")); | 811 | status_line, (display_html ? "</A>" : "")); |
794 | exit (STATE_UNKNOWN); | 812 | exit (STATE_UNKNOWN); |
795 | } /* end if (onredirect == STATE_DEPENDENT) */ | 813 | } /* end if (onredirect == STATE_DEPENDENT) */ |
796 | else if (onredirect == STATE_UNKNOWN) | 814 | |
797 | printf ("HTTP UNKNOWN"); | 815 | else if (onredirect == STATE_UNKNOWN) |
798 | else if (onredirect == STATE_OK) | 816 | printf ("HTTP UNKNOWN"); |
799 | printf ("HTTP ok"); | 817 | else if (onredirect == STATE_OK) |
800 | else if (onredirect == STATE_WARNING) | 818 | printf ("HTTP ok"); |
801 | printf ("HTTP WARNING"); | 819 | else if (onredirect == STATE_WARNING) |
802 | else if (onredirect == STATE_CRITICAL) | 820 | printf ("HTTP WARNING"); |
803 | printf ("HTTP CRITICAL"); | 821 | else if (onredirect == STATE_CRITICAL) |
804 | time (&end_time); | 822 | printf ("HTTP CRITICAL"); |
805 | msg = ssprintf (msg, ": %s - %d second response time %s%s\n", | 823 | time (&end_time); |
824 | msg = ssprintf (msg, ": %s - %d second response time %s%s\n", | ||
806 | status_line, (int) (end_time - start_time), | 825 | status_line, (int) (end_time - start_time), |
807 | timestamp, (display_html ? "</A>" : "")); | 826 | timestamp, (display_html ? "</A>" : "")); |
808 | terminate (onredirect, msg); | 827 | terminate (onredirect, msg); |
809 | } /* end if (strstr (status_line, "30[0-4]") */ | 828 | } /* end if (strstr (status_line, "30[0-4]") */ |
829 | |||
830 | |||
831 | } /* end else (server_expect_yn) */ | ||
810 | 832 | ||
833 | |||
811 | /* check elapsed time */ | 834 | /* check elapsed time */ |
812 | time (&end_time); | 835 | time (&end_time); |
813 | msg = ssprintf (msg, "HTTP problem: %s - %d second response time %s%s\n", | 836 | msg = ssprintf (msg, "HTTP problem: %s - %d second response time %s%s\n", |
814 | status_line, (int) (end_time - start_time), | 837 | status_line, (int) (end_time - start_time), |
815 | timestamp, (display_html ? "</A>" : "")); | 838 | timestamp, (display_html ? "</A>" : "")); |
816 | if (check_critical_time == TRUE && (end_time - start_time) > critical_time) | 839 | if (check_critical_time == TRUE && (end_time - start_time) > critical_time) |
817 | terminate (STATE_CRITICAL, msg); | 840 | terminate (STATE_CRITICAL, msg); |
818 | if (check_warning_time == TRUE && (end_time - start_time) > warning_time) | 841 | if (check_warning_time == TRUE && (end_time - start_time) > warning_time) |
@@ -865,12 +888,11 @@ check_http (void) | |||
865 | terminate (STATE_OK, msg); | 888 | terminate (STATE_OK, msg); |
866 | return STATE_UNKNOWN; | 889 | return STATE_UNKNOWN; |
867 | } | 890 | } |
868 | 891 | ||
869 | 892 | ||
870 | 893 | ||
871 | #ifdef HAVE_SSL | 894 | #ifdef HAVE_SSL |
872 | int | 895 | int connect_SSL (void) |
873 | connect_SSL (void) | ||
874 | { | 896 | { |
875 | SSL_METHOD *meth; | 897 | SSL_METHOD *meth; |
876 | 898 | ||