diff options
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/check_http.c | 30 | ||||
-rwxr-xr-x | plugins/tests/check_http.t | 11 |
2 files changed, 34 insertions, 7 deletions
diff --git a/plugins/check_http.c b/plugins/check_http.c index f81026f8..f54f4ab8 100644 --- a/plugins/check_http.c +++ b/plugins/check_http.c | |||
@@ -573,7 +573,25 @@ parse_time_string (const char *string) | |||
573 | } | 573 | } |
574 | } | 574 | } |
575 | 575 | ||
576 | /* Checks if the server 'reply' is one of the expected 'statuscodes' */ | ||
577 | static int | ||
578 | expected_statuscode (const char *reply, const char *statuscodes) | ||
579 | { | ||
580 | char *expected, *code; | ||
581 | int result = 0; | ||
582 | |||
583 | if ((expected = strdup (statuscodes)) == NULL) | ||
584 | die (STATE_UNKNOWN, _("HTTP UNKNOWN - Memory allocation error\n")); | ||
585 | |||
586 | for (code = strtok (expected, ","); code != NULL; code = strtok (NULL, ",")) | ||
587 | if (strstr (reply, code) != NULL) { | ||
588 | result = 1; | ||
589 | break; | ||
590 | } | ||
576 | 591 | ||
592 | free (expected); | ||
593 | return result; | ||
594 | } | ||
577 | 595 | ||
578 | static void | 596 | static void |
579 | check_document_dates (const char *headers) | 597 | check_document_dates (const char *headers) |
@@ -878,14 +896,15 @@ check_http (void) | |||
878 | (no_body ? " [[ skipped ]]" : page)); | 896 | (no_body ? " [[ skipped ]]" : page)); |
879 | 897 | ||
880 | /* make sure the status line matches the response we are looking for */ | 898 | /* make sure the status line matches the response we are looking for */ |
881 | if (!strstr (status_line, server_expect)) { | 899 | if (!expected_statuscode (status_line, server_expect)) { |
882 | if (server_port == HTTP_PORT) | 900 | if (server_port == HTTP_PORT) |
883 | asprintf (&msg, | 901 | asprintf (&msg, |
884 | _("Invalid HTTP response received from host\n")); | 902 | _("Invalid HTTP response received from host: %s\n"), |
903 | status_line); | ||
885 | else | 904 | else |
886 | asprintf (&msg, | 905 | asprintf (&msg, |
887 | _("Invalid HTTP response received from host on port %d\n"), | 906 | _("Invalid HTTP response received from host on port %d: %s\n"), |
888 | server_port); | 907 | server_port, status_line); |
889 | die (STATE_CRITICAL, "HTTP CRITICAL - %s", msg); | 908 | die (STATE_CRITICAL, "HTTP CRITICAL - %s", msg); |
890 | } | 909 | } |
891 | 910 | ||
@@ -1262,7 +1281,8 @@ print_help (void) | |||
1262 | #endif | 1281 | #endif |
1263 | 1282 | ||
1264 | printf (" %s\n", "-e, --expect=STRING"); | 1283 | printf (" %s\n", "-e, --expect=STRING"); |
1265 | printf (" %s\n", _("String to expect in first (status) line of server response (default: ")); | 1284 | printf (" %s\n", _("Comma-delimited list of strings, at least one of them is expected in")); |
1285 | printf (" %s", _("the first (status) line of the server response (default: ")); | ||
1266 | printf ("%s)\n", HTTP_EXPECT); | 1286 | printf ("%s)\n", HTTP_EXPECT); |
1267 | printf (" %s\n", _("If specified skips all other status line logic (ex: 3xx, 4xx, 5xx processing)")); | 1287 | printf (" %s\n", _("If specified skips all other status line logic (ex: 3xx, 4xx, 5xx processing)")); |
1268 | printf (" %s\n", "-s, --string=STRING"); | 1288 | printf (" %s\n", "-s, --string=STRING"); |
diff --git a/plugins/tests/check_http.t b/plugins/tests/check_http.t index d5a7c822..e4d770b3 100755 --- a/plugins/tests/check_http.t +++ b/plugins/tests/check_http.t | |||
@@ -18,6 +18,8 @@ my $pid = fork(); | |||
18 | if ($pid) { | 18 | if ($pid) { |
19 | # Parent | 19 | # Parent |
20 | #print "parent\n"; | 20 | #print "parent\n"; |
21 | # give our webserver some time to startup | ||
22 | sleep(1); | ||
21 | } else { | 23 | } else { |
22 | # Child | 24 | # Child |
23 | #print "child\n"; | 25 | #print "child\n"; |
@@ -58,7 +60,7 @@ if ($ARGV[0] && $ARGV[0] eq "-d") { | |||
58 | } | 60 | } |
59 | 61 | ||
60 | if (-x "./check_http") { | 62 | if (-x "./check_http") { |
61 | plan tests => 13; | 63 | plan tests => 15; |
62 | } else { | 64 | } else { |
63 | plan skip_all => "No check_http compiled"; | 65 | plan skip_all => "No check_http compiled"; |
64 | } | 66 | } |
@@ -97,5 +99,10 @@ like( $result->output, '/^HTTP OK HTTP/1.1 201 Created - 94 bytes in ([\d\.]+) s | |||
97 | $cmd = "$command -u /statuscode/201 -e 200"; | 99 | $cmd = "$command -u /statuscode/201 -e 200"; |
98 | $result = NPTest->testCmd( $cmd ); | 100 | $result = NPTest->testCmd( $cmd ); |
99 | is( $result->return_code, 2, $cmd); | 101 | is( $result->return_code, 2, $cmd); |
100 | like( $result->output, '/^HTTP CRITICAL - Invalid HTTP response received from host on port /', "Output correct: ".$result->output ); | 102 | like( $result->output, '/^HTTP CRITICAL - Invalid HTTP response received from host on port \d+: HTTP/1.1 201 Created/', "Output correct: ".$result->output ); |
103 | |||
104 | $cmd = "$command -u /statuscode/200 -e 200,201,202"; | ||
105 | $result = NPTest->testCmd( $cmd ); | ||
106 | is( $result->return_code, 0, $cmd); | ||
107 | like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - 89 bytes in ([\d\.]+) seconds/', "Output correct: ".$result->output ); | ||
101 | 108 | ||