Xymon Mailing List Archive search

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

7 messages in this thread

list Charles Goyard · Tue, 27 Jun 2006 12:05:14 +0200 ·
hi,

I made a small modification to bb-findhost.c so that when a search
yields only one results, you get transported to that page.

Since we only know there's one result after a full scan of the bb-hosts
file, and don't want to scan the file twice, I use an ugly way to do it:
a javascript redirect held in a <img> tag.

This works for 4.1.2p1, but the patch applies in 4.2-beta-20060605 too (not
tested live, thought).


Regards,

-- 
Charles Goyard
rediscovering C
-------------- next part --------------
Index: bbdisplay/bb-findhost.c
===================================================================
--- bbdisplay/bb-findhost.c	(revision 1)
+++ bbdisplay/bb-findhost.c	(working copy)
@@ -128,6 +128,7 @@
 int main(int argc, char *argv[])
 {
 	namelist_t *hosthead, *hostwalk, *clonewalk;
+	namelist_t *uniquematch;
 	int argi;
 	char *envarea = NULL;
 
@@ -232,7 +233,9 @@
 					bbh_item(hostwalk, BBH_PAGEPATH),
 					hostname,
 					bbh_item(hostwalk, BBH_PAGEPATHTITLE));
• +				if(!gotany) {
+				        uniquematch=hostwalk;
+				}
 				clonewalk = hostwalk->next;
 				while (clonewalk && (strcmp(hostwalk->bbhostname, clonewalk->bbhostname) == 0)) {
 					printf("<br><a href=\"%s/%s#%s\">%s</a>\n",
@@ -251,7 +254,16 @@
 
 		regfree (&re); 	/*[wm] - free regex compiled patern */
 	
-		if (!gotany) printf("<tr><td align=left>%s</td><td align=left>Not found</td></tr>\n", pSearchPat);
+		if (!gotany) {
+		        printf("<tr><td align=left>%s</td><td align=left>Not found</td></tr>\n", pSearchPat);
+		}
+		else if (gotany == 1) {
+		        printf("<tr><td><img src=\"%s/clear.gif\" onload=\"window.location='%s/%s#%s'\" alt=\"redirect\"/></td></tr>",
+			       xgetenv("BBSKIN"),
+			       xgetenv("BBWEB"),
+			       bbh_item(uniquematch, BBH_PAGEPATH),
+			       uniquematch->bbhostname);
+		}
 	}
list Henrik Størner · Tue, 27 Jun 2006 23:53:01 +0200 ·
quoted from Charles Goyard
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.

Since we only know there's one result after a full scan of the bb-hosts
file, and don't want to scan the file twice, I use an ugly way to do it:
a javascript redirect held in a <img> tag.
I like the overall idea, but the implementation is not so nice.

So I made a different implementation of the same idea, but without the
JavaScript/img tag stuff. You'll find it in the snapshot that is
generated in a few minutes.


Regards,
Henrik
list Frédéric Mangeant · Wed, 28 Jun 2006 12:52:20 +0200 ·
quoted from Henrik Størner
Henrik Stoerner a écrit :
I like the overall idea, but the implementation is not so nice.

So I made a different implementation of the same idea, but without the
JavaScript/img tag stuff. You'll find it in the snapshot that is
generated in a few minutes.
  
Hi Henrik

it's working, but I get redirected to 
http://hostname/hobbit/page/subpage/#host  instead of  
http://xx.xx.xx.xx/hobbit/page/subpage/#host

-- 

Frédéric Mangeant

Steria EDC Sophia-Antipolis
list Frédéric Mangeant · Wed, 28 Jun 2006 13:08:36 +0200 ·
Frédéric Mangeant a écrit :
it's working, but I get redirected to http://hostname/hobbit/page/subpage/#host  instead of  http://xx.xx.xx.xx/hobbit/page/subpage/#host

Forget about my mail, the web browser I was using had a misconfigured proxy...

It's working perfectly.

-- 

Frédéric Mangeant

Steria EDC Sophia-Antipolis
list Henrik Størner · Wed, 28 Jun 2006 13:10:51 +0200 ·
quoted from Frédéric Mangeant
On Wed, Jun 28, 2006 at 12:52:20PM +0200, Fr?d?ric Mangeant wrote:
Henrik Stoerner a écrit :
I like the overall idea, but the implementation is not so nice.

So I made a different implementation of the same idea, but without the
JavaScript/img tag stuff. You'll find it in the snapshot that is
generated in a few minutes.
 
it's working, but I get redirected to 
http://hostname/hobbit/page/subpage/#host  instead of  
http://xx.xx.xx.xx/hobbit/page/subpage/#host
Then fix your BBWEBHOST (or BBSERVERWWWNAME) setting in hobbitserver.cfg.


Regards,
Henrik
list Charles Goyard · Wed, 28 Jun 2006 16:03:11 +0200 ·
quoted from Henrik Størner
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) {
list Henrik Størner · Sun, 9 Jul 2006 18:05:09 +0200 ·
quoted from Charles Goyard
On Wed, Jun 28, 2006 at 04:03:11PM +0200, Charles Goyard wrote:
I made another modification, so that when all matches are on the same
page, you get to that page at once.
[snip]
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.
I chose the second option, implemented it slightly differently, and 
made an option on the findhost form so you can choose whether or not
to jump directly to the host or go via the choose-page.


Regards,
Henrik