From e7cdcfee2a6025b41f67ead5020df3965ef05a98 Mon Sep 17 00:00:00 2001
From: Thomas Guyot-Sionnest <dermoth@aei.ca>
Date: Sat, 21 Mar 2009 02:32:50 -0400
Subject: check_http: add --onredirect=stickyport - also follow the same port


diff --git a/NEWS b/NEWS
index a03877c..164ef9f 100644
--- a/NEWS
+++ b/NEWS
@@ -21,6 +21,7 @@ This file documents the major additions and syntax changes between releases.
 	Fixed check_mrtg returning UNKNOWN instead of OK (bug #2378068)
 	Fixed check_http behaviour: all check are now performed as long as a valid response is returned (sf.net #1460312)
 	check_http --onredirect=sticky follows using the same IP address (sf.net #2550208)
+	check_http --onredirect=stickyport also follows the same port
 	Fixed coredump from check_nt when invalid drive is specified (#2179754 - Olli Hauer)
 	Fixed passing of quotes in OID for check_snmp (#1985230 - Jan Wagner, patch by John Barbuto)
 	Fixed check_http sending HTTP/1.0 with v1.1 headers (#2638765)
diff --git a/plugins/check_http.c b/plugins/check_http.c
index 72d0a2b..5a859f9 100644
--- a/plugins/check_http.c
+++ b/plugins/check_http.c
@@ -44,6 +44,9 @@ const char *email = "nagiosplug-devel@lists.sourceforge.net";
 #include <ctype.h>
 
 #define INPUT_DELIMITER ";"
+#define STICKY_NONE 0
+#define STICKY_HOST 1
+#define STICKY_PORT 2
 
 #define HTTP_EXPECT "HTTP/1."
 enum {
@@ -106,7 +109,7 @@ int display_html = FALSE;
 char **http_opt_headers;
 int http_opt_headers_count = 0;
 int onredirect = STATE_OK;
-int followsticky = 0;
+int followsticky = STICKY_NONE;
 int use_ssl = FALSE;
 int verbose = FALSE;
 int sd;
@@ -300,10 +303,12 @@ process_arguments (int argc, char **argv)
         server_port = HTTPS_PORT;
       break;
     case 'f': /* onredirect */
+      if (!strcmp (optarg, "stickyport"))
+        onredirect = STATE_DEPENDENT, followsticky = STICKY_HOST|STICKY_PORT;
       if (!strcmp (optarg, "sticky"))
-        onredirect = STATE_DEPENDENT, followsticky = 1;
+        onredirect = STATE_DEPENDENT, followsticky = STICKY_HOST;
       if (!strcmp (optarg, "follow"))
-        onredirect = STATE_DEPENDENT, followsticky = 0;
+        onredirect = STATE_DEPENDENT, followsticky = STICKY_NONE;
       if (!strcmp (optarg, "unknown"))
         onredirect = STATE_UNKNOWN;
       if (!strcmp (optarg, "ok"))
@@ -1203,15 +1208,18 @@ redir (char *pos, char *status_line)
   free (host_name);
   host_name = strdup (addr);
 
-  if (followsticky == 0) {
+  if (!(followsticky & STICKY_HOST)) {
     free (server_address);
     server_address = strdup (addr);
   }
+  if (!(followsticky & STICKY_PORT)) {
+    server_port = i;
+  }
 
   free (server_url);
   server_url = url;
 
-  if ((server_port = i) > MAX_PORT)
+  if (server_port > MAX_PORT)
     die (STATE_UNKNOWN,
          _("HTTP UNKNOWN - Redirection to port above %d - %s://%s:%d%s%s\n"),
          MAX_PORT, server_type, server_address, server_port, server_url,
@@ -1343,9 +1351,9 @@ print_help (void)
   printf ("    %s\n", _(" Any other tags to be sent in http header. Use multiple times for additional headers"));
   printf (" %s\n", "-L, --link");
   printf ("    %s\n", _("Wrap output in HTML link (obsoleted by urlize)"));
-  printf (" %s\n", "-f, --onredirect=<ok|warning|critical|follow|sticky>");
+  printf (" %s\n", "-f, --onredirect=<ok|warning|critical|follow|sticky|stickyport>");
   printf ("    %s\n", _("How to handle redirected pages. sticky is like follow but stick to the"));
-  printf ("    %s\n", _("specified IP address"));
+  printf ("    %s\n", _("specified IP address. stickyport also ensure post stays the same."));
   printf (" %s\n", "-m, --pagesize=INTEGER<:INTEGER>");
   printf ("    %s\n", _("Minimum page size required (bytes) : Maximum page size required (bytes)"));
 
diff --git a/plugins/tests/check_http.t b/plugins/tests/check_http.t
index b0df960..88b77d3 100755
--- a/plugins/tests/check_http.t
+++ b/plugins/tests/check_http.t
@@ -125,11 +125,11 @@ sub run_server {
 			} elsif ($r->url->path eq "/redirect") {
 				$c->send_redirect( "/redirect2" );
 			} elsif ($r->url->path eq "/redir_external") {
-				$c->send_redirect( "http://169.254.169.254/redirect2" );
+				$c->send_redirect(($d->isa('HTTP::Daemon::SSL') ? "https" : "http") . "://169.254.169.254/redirect2" );
 			} elsif ($r->url->path eq "/redirect2") {
 				$c->send_basic_header;
 				$c->send_crlf;
-				$c->send_response("redirected");
+				$c->send_response(HTTP::Response->new( 200, 'OK', undef, 'redirected' ));
 			} elsif ($r->url->path eq "/redir_timeout") {
 				$c->send_redirect( "/timeout" );
 			} elsif ($r->url->path eq "/timeout") {
@@ -157,7 +157,7 @@ if ($ARGV[0] && $ARGV[0] eq "-d") {
 	}
 }
 
-my $common_tests = 55;
+my $common_tests = 62;
 my $ssl_only_tests = 6;
 if (-x "./check_http") {
 	plan tests => $common_tests * 2 + $ssl_only_tests;
@@ -181,7 +181,6 @@ SKIP: {
 	is( $result->return_code, 1, "$command -p $port_https -S -C 14000" );
 	like( $result->output, '/WARNING - Certificate expires in \d+ day\(s\) \(03/03/2019 21:41\)./', "output ok" );
 
-
 	# Expired cert tests
 	$result = NPTest->testCmd( "$command -p $port_https_expired -S -C 7" );
 	is( $result->return_code, 2, "$command -p $port_https_expired -S -C 7" );
@@ -316,7 +315,6 @@ sub run_common_tests {
 	is( $result->return_code, 0, $cmd);
 	like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
 
-
 	$cmd = "$command -u /redirect -k 'follow: me'";
 	$result = NPTest->testCmd( $cmd );
 	is( $result->return_code, 0, $cmd);
@@ -327,25 +325,50 @@ sub run_common_tests {
 	is( $result->return_code, 0, $cmd);
 	like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
 
+	$cmd = "$command -f sticky -u /redirect -k 'follow: me'";
+	$result = NPTest->testCmd( $cmd );
+	is( $result->return_code, 0, $cmd);
+	like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
+
+	$cmd = "$command -f stickyport -u /redirect -k 'follow: me'";
+	$result = NPTest->testCmd( $cmd );
+	is( $result->return_code, 0, $cmd);
+	like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
+
   # These tests may block
 	print "ALRM\n";
 
-	$cmd = "$command -f sticky -u /redir_external -t 5";
+	# stickyport - on full urlS port is set back to 80 otherwise
+	$cmd = "$command -f stickyport -u /redir_external -t 5 -s redirected";
 	eval {
 		local $SIG{ALRM} = sub { die "alarm\n" };
 		alarm(2);
 		$result = NPTest->testCmd( $cmd );
 		alarm(0);	};
-	isnt( $@, "alarm\n", $cmd);
+	isnt( $@, "alarm\n", $cmd );
+	is( $result->return_code, 0, $cmd );
 
-	# Will this one work everywhere???
-	$cmd = "$command -f follow -u /redir_external -t 5";
+	# Let's hope there won't be any web server on :80 returning "redirected"!
+	$cmd = "$command -f sticky -u /redir_external -t 5 -s redirected";
 	eval {
 		local $SIG{ALRM} = sub { die "alarm\n" };
 		alarm(2);
 		$result = NPTest->testCmd( $cmd );
 		alarm(0); };
-	is( $@, "alarm\n", $cmd);
+	isnt( $@, "alarm\n", $cmd );
+	isnt( $result->return_code, 0, $cmd );
+
+	# Test an external address - timeout
+	SKIP: {
+		skip "This doesn't seems to work all the time", 1 unless ($ENV{HTTP_EXTERNAL});
+		$cmd = "$command -f follow -u /redir_external -t 5";
+		eval {
+			local $SIG{ALRM} = sub { die "alarm\n" };
+			alarm(2);
+			$result = NPTest->testCmd( $cmd );
+			alarm(0); };
+		is( $@, "alarm\n", $cmd );
+	}
 
 	$cmd = "$command -u /timeout -t 5";
 	eval {
@@ -353,7 +376,7 @@ sub run_common_tests {
 		alarm(2);
 		$result = NPTest->testCmd( $cmd );
 		alarm(0); };
-	is( $@, "alarm\n", $cmd);
+	is( $@, "alarm\n", $cmd );
 
 	$cmd = "$command -f follow -u /redir_timeout -t 2";
 	eval {
@@ -361,6 +384,7 @@ sub run_common_tests {
 		alarm(5);
 		$result = NPTest->testCmd( $cmd );
 		alarm(0); };
-	isnt( $@, "alarm\n", $cmd);
+	isnt( $@, "alarm\n", $cmd );
 
 }
+
-- 
cgit v0.10-9-g596f

