[hobbit] [patch] bb-findhost.cgi redirects to the page when only one host matches

Charles Goyard cgoyard at cvf.fr
Wed Jun 28 16:03:11 CEST 2006


Henrik Stoerner a écrit :
> On Tue, Jun 27, 2006 at 12:05:14PM +0200, Charles Goyard wrote:
> > 
> > I made a small modification to bb-findhost.c so that when a search
> > yields only one results, you get transported to that page.
> > 
> I like the overall idea, but the implementation is not so nice.

Didn't I warned you :) ? Thank you for including this 'idea' into
hobbit.

I made another modification, so that when all matches are on the same
page, you get to that page at once. Why ? My Perl search engine on BB
behaves this way, and most users use it not to jump to a specific host,
but to a specific page: We use domain names to group hosts by
project/farm/customer, like everything in the DMZ is called
somehost.dmz01.tld. So to have a look at the dmz, people type "dmz01"
(short, easy, ideal for lazy people).  It proves so useful I can barely
recall how the pages are organized :).

There's two flavours of the patch made against today's snapshot (Chances
are you will find a better way to implement it :

- you get to the page without the host anchor ;
- you get to the first match.


Regards,

-- 
Charles
-------------- next part --------------
--- ../web-DIST/bb-findhost.c	2006-06-27 23:53:46.000000000 +0200
+++ ./bb-findhost.c	2006-06-28 15:47:40.000000000 +0200
@@ -121,7 +121,9 @@
 	strbuffer_t *outbuf;
 	char msgline[4096];
 	char oneurl[10240];
+	char onepage[10240];
 	int gotany = 0;
+	int gotonepage;
 
 	/*[wm] regex support */
 	#define BUFSIZE		256
@@ -189,8 +191,15 @@
 			addtobuffer(outbuf, "<tr>\n");
 			sprintf(msgline, "<td align=left> %s </td>\n", displayname ? displayname : hostname);
 			addtobuffer(outbuf, msgline);
-			sprintf(oneurl, "%s/%s/#%s",
+			if(!gotany) {
+			  sprintf(oneurl, "%s/%s/#%s",
 				xgetenv("BBWEB"), bbh_item(hostwalk, BBH_PAGEPATH), hostname);
+			  strcpy(onepage, bbh_item(hostwalk, BBH_PAGEPATH));
+			  gotonepage = 1;
+			}
+			else if((strcmp(onepage, bbh_item(hostwalk, BBH_PAGEPATH)) != 0)) {
+			  gotonepage = 0;
+			}
 			sprintf(msgline, "<td align=left> <a href=\"%s\">%s</a>\n",
 				oneurl, bbh_item(hostwalk, BBH_PAGEPATHTITLE));
 			addtobuffer(outbuf, msgline);
@@ -218,10 +227,14 @@
 	}
 	regfree (&re); 	/*[wm] - free regex compiled patern */
 	
-	if (gotany == 1) {
+	if (gotany == 1 || (gotany > 1 && gotonepage == 1)) {
 		printf("Location: %s%s\n\n", xgetenv("BBWEBHOST"), oneurl);
 		return 0;
 	}
+	else if (gotany > 1 && gotonepage == 1) {
+        	printf("Location: %s%s/%s\n\n", xgetenv("BBWEBHOST"), xgetenv("BBWEB"), onepage);
+	        return 0;
+	}
 
 	print_header();
 	if (!gotany) {
-------------- next part --------------
--- ../web-DIST/bb-findhost.c	2006-06-27 23:53:46.000000000 +0200
+++ ./bb-findhost.c	2006-06-28 15:42:51.000000000 +0200
@@ -121,7 +121,9 @@
 	strbuffer_t *outbuf;
 	char msgline[4096];
 	char oneurl[10240];
+	char onepage[10240];
 	int gotany = 0;
+	int gotonepage = -1;
 
 	/*[wm] regex support */
 	#define BUFSIZE		256
@@ -191,6 +193,13 @@
 			addtobuffer(outbuf, msgline);
 			sprintf(oneurl, "%s/%s/#%s",
 				xgetenv("BBWEB"), bbh_item(hostwalk, BBH_PAGEPATH), hostname);
+			if(gotonepage == -1) { /* first pass */
+			  strcpy(onepage, bbh_item(hostwalk, BBH_PAGEPATH));
+			  gotonepage = 1;
+			}
+			else if(gotonepage == 1 && (strcmp(onepage, bbh_item(hostwalk, BBH_PAGEPATH)) != 0)) {
+				  gotonepage = 0;
+			}
 			sprintf(msgline, "<td align=left> <a href=\"%s\">%s</a>\n",
 				oneurl, bbh_item(hostwalk, BBH_PAGEPATHTITLE));
 			addtobuffer(outbuf, msgline);
@@ -222,6 +231,10 @@
 		printf("Location: %s%s\n\n", xgetenv("BBWEBHOST"), oneurl);
 		return 0;
 	}
+	else if (gotany > 1 && gotonepage == 1) {
+        	printf("Location: %s%s/%s\n\n", xgetenv("BBWEBHOST"), xgetenv("BBWEB"), onepage);
+	        return 0;
+	}
 
 	print_header();
 	if (!gotany) {


More information about the Xymon mailing list