aboutsummaryrefslogtreecommitdiff
path: root/plugins
diff options
context:
space:
mode:
authorGravatar Holger Weiss <hweiss@users.sourceforge.net> 2008-08-25 11:42:57 +0000
committerGravatar Holger Weiss <hweiss@users.sourceforge.net> 2008-08-25 11:42:57 +0000
commitc349438e32a99d1d3171bb4cef6c70815c138c45 (patch)
treec966452dcd503dad1909ca6ff86e504db04f57e3 /plugins
parente61022df19da2c1b1ab02cd8cc2a6932b4ef9413 (diff)
downloadmonitoring-plugins-c349438e32a99d1d3171bb4cef6c70815c138c45.tar.gz
The "-e" option now accepts a comma-delimited list of expected status
lines (Sven Nierlein - 1894496). git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@2046 f882894a-f735-0410-b71e-b25c423dba1c
Diffstat (limited to 'plugins')
-rw-r--r--plugins/check_http.c30
-rwxr-xr-xplugins/tests/check_http.t11
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)
}
}
+/* Checks if the server 'reply' is one of the expected 'statuscodes' */
+static int
+expected_statuscode (const char *reply, const char *statuscodes)
+{
+ char *expected, *code;
+ int result = 0;
+
+ if ((expected = strdup (statuscodes)) == NULL)
+ die (STATE_UNKNOWN, _("HTTP UNKNOWN - Memory allocation error\n"));
+
+ for (code = strtok (expected, ","); code != NULL; code = strtok (NULL, ","))
+ if (strstr (reply, code) != NULL) {
+ result = 1;
+ break;
+ }
+ free (expected);
+ return result;
+}
static void
check_document_dates (const char *headers)
@@ -878,14 +896,15 @@ check_http (void)
(no_body ? " [[ skipped ]]" : page));
/* make sure the status line matches the response we are looking for */
- if (!strstr (status_line, server_expect)) {
+ if (!expected_statuscode (status_line, server_expect)) {
if (server_port == HTTP_PORT)
asprintf (&msg,
- _("Invalid HTTP response received from host\n"));
+ _("Invalid HTTP response received from host: %s\n"),
+ status_line);
else
asprintf (&msg,
- _("Invalid HTTP response received from host on port %d\n"),
- server_port);
+ _("Invalid HTTP response received from host on port %d: %s\n"),
+ server_port, status_line);
die (STATE_CRITICAL, "HTTP CRITICAL - %s", msg);
}
@@ -1262,7 +1281,8 @@ print_help (void)
#endif
printf (" %s\n", "-e, --expect=STRING");
- printf (" %s\n", _("String to expect in first (status) line of server response (default: "));
+ printf (" %s\n", _("Comma-delimited list of strings, at least one of them is expected in"));
+ printf (" %s", _("the first (status) line of the server response (default: "));
printf ("%s)\n", HTTP_EXPECT);
printf (" %s\n", _("If specified skips all other status line logic (ex: 3xx, 4xx, 5xx processing)"));
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();
if ($pid) {
# Parent
#print "parent\n";
+ # give our webserver some time to startup
+ sleep(1);
} else {
# Child
#print "child\n";
@@ -58,7 +60,7 @@ if ($ARGV[0] && $ARGV[0] eq "-d") {
}
if (-x "./check_http") {
- plan tests => 13;
+ plan tests => 15;
} else {
plan skip_all => "No check_http compiled";
}
@@ -97,5 +99,10 @@ like( $result->output, '/^HTTP OK HTTP/1.1 201 Created - 94 bytes in ([\d\.]+) s
$cmd = "$command -u /statuscode/201 -e 200";
$result = NPTest->testCmd( $cmd );
is( $result->return_code, 2, $cmd);
-like( $result->output, '/^HTTP CRITICAL - Invalid HTTP response received from host on port /', "Output correct: ".$result->output );
+like( $result->output, '/^HTTP CRITICAL - Invalid HTTP response received from host on port \d+: HTTP/1.1 201 Created/', "Output correct: ".$result->output );
+
+$cmd = "$command -u /statuscode/200 -e 200,201,202";
+$result = NPTest->testCmd( $cmd );
+is( $result->return_code, 0, $cmd);
+like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - 89 bytes in ([\d\.]+) seconds/', "Output correct: ".$result->output );