commit af3a7f973a6b9c0db8191ccb61c855faf9f494cd
Author: Hamish Coleman <hamish@zot.org>
Date:   Tue Dec 18 10:57:10 2007 +1100

    Make debug text a param, rename "test" again, start extracting hosttype functions

diff --git a/Makefile b/Makefile
index 0903362..1e22c24 100644
--- a/Makefile
+++ b/Makefile
@@ -8,7 +8,7 @@ ifeq ($(test -d $(WWW_PERIL) && echo yes),yes)
 else
 	WWW := $(WWW_DEBIAN)
 endif
-#WWW := $(WWW_PERIL)
+WWW := $(WWW_PERIL)
 
 all:
 	@echo error
diff --git a/mrtg.cgi b/mrtg.cgi
index 4918f32..41e8da4 100755
--- a/mrtg.cgi
+++ b/mrtg.cgi
@@ -162,24 +162,75 @@ sub config_read_filesystem() {
 	}
 }
 
-sub maketree_grouphosttest($$) {
-	my ($name,$sequence) = @_;
+sub extract_grouphosttest($) {
+	my ($name) = @_;
 
-	# Do not classify any test with no separators in it
+	# Do not classify a test with no separators in it
 	if ($name !~ /,/) {
-		$db->{tree}->{other}->{$name} = $sequence;
-		return;
+		return undef;
 	}
 
-
 	my ($group,$host,$test) = split ',', $name;
 	# TODO cope with missing group/host/test parts.
-	# for now, just assume it is not a group/host/test
 	if (!defined $group || !defined $host || !defined $test) {
+		return undef;
+	}
+	return ($group,$host,$test);
+}
+
+# Read through the list of targets and find common hostname prefixes,
+# so that we can group these hosts together
+# (a hosttype entry)
+#
+sub maketree_find_hosttypes() {
+
+	for my $name (keys %{$db->{target}}) {
+		my ($group,$host,$test) = extract_grouphosttest($name);
+		if (!defined($group)) {
+			next;
+		}
+		
+		my $hosttype;
+		# TODO - this needs to have a better way...
+		# hosttype is the non digit hostname prefix - thus
+		# "proxy-1" and "proxy-2" would both end up in the
+		# "proxy-" hosttype
+		if ($host =~ m/^(.*[^0-9])(\d+)$/) {
+			$hosttype = $1.'*';
+		} else {
+			next;
+		}
+
+		$db->{target}->{$name}->{_hosttype} = $hosttype;
+		$db->{hosttype}->{$hosttype}->{$name}++;
+	}
+
+	#for my $hosttype (keys %{$db->{hosttype}}) {
+	#	if ((keys %{$db->{hosttype}->{$hosttype}})<2) {
+	#		delete $db->{hosttype}->{$hosttype};
+	#	}
+	#}
+
+	# a valid hosttype is:
+	#	defined($db->{target}->{$name}->{hosttype}) &&
+	#	defined($db->{hosttype}->{$hosttype})
+}
+
+sub maketree_grouphosttest($$) {
+	my ($name,$sequence) = @_;
+
+	my ($group,$host,$test) = extract_grouphosttest($name);
+	if (!defined($group)) {
+		# TODO cope with missing group/host/test parts.
+		# for now, just assume it is not a group/host/test
 		$db->{tree}->{other}->{$name} = $sequence;
 		return;
 	}
 
+	#my $hosttype = $db->{target}->{$name}->{_hosttype};
+	#if (!defined($db->{hosttype}->{$hosttype})) {
+	#	$hosttype=undef;
+	#}
 	my $hosttype;
 	# TODO - this needs to have a better way...
 	# hosttype is the non digit hostname prefix - thus
@@ -190,7 +241,7 @@ sub maketree_grouphosttest($$) {
 	}
 
 	# TODO - determine a better description for this tree leaf
-	my $testtreename = 'Gorganzola';
+	my $testtreename = 'Graph Type';
 
 	if (defined $hosttype) {
 		$db->{tree}->{group}->{$group}->{$hosttype}->{$host}->{$test}->{$name} = $sequence;
@@ -204,7 +255,6 @@ sub maketree_grouphosttest($$) {
 		$db->{tree}->{group}->{ALL}->{$testtreename}->{$test}->{$host}->{$name} = $sequence;
 		$db->{tree}->{group}->{$group}->{$testtreename}->{$test}->{$host}->{$name} = $sequence;
 	}
-
 }
 
 sub maketree_cfgfile($$) {
@@ -235,6 +285,8 @@ sub maketree_cfgfile($$) {
 # Look through the target database and create tree entries for each cfgfile
 #
 sub maketree() {
+	maketree_find_hosttypes();
+
 	for my $name (keys %{$db->{target}}) {
 		my $sequence = $db->{target}->{$name}->{_sequence};
 		maketree_cfgfile($name,$sequence);
@@ -250,6 +302,13 @@ sub emit_tree_one($$$);
 sub emit_tree_one($$$) {
 	my ($parentnode,$path,$dir) = @_;
 
+	my $debug;	
+	if (defined(param('debug'))) {
+		$debug = 'debug=1&';
+	} else {
+		$debug = '';
+	}
+
 	if (!ref($dir)) {
 		#print "FIXME !REF $dir\n";
 		print "insDoc($parentnode, gLnk(\"S\", \"FIXME !REF $dir\", \"?\"))\n";
@@ -263,7 +322,7 @@ sub emit_tree_one($$$) {
 		}
 
 		my $node = 'n'.$node_next++;
-		print "$node = insFld($parentnode, gFld(\"$i\", \"?path=$path/$i\"))\n";
+		print "$node = insFld($parentnode, gFld(\"$i\", \"?".$debug."path=$path/$i\"))\n";
 		emit_tree_one("$node","$path/$i",$val);
 	}
 }
@@ -1070,11 +1129,11 @@ EOF
 
 }
 
-if ($warnings && param('debug')) {
-	print "<pre>\n", "$warnings", "</pre>\n";
+if (param('debug')) {
+	#print "<pre>\n", "$warnings", "</pre>\n";
+	print "<pre>\n";
+	print Dumper(\$db),"\n";
+	print "</pre>";
 }
 
-#print "<pre>\n";
-#print Dumper(\$db),"\n";
-#print "</pre>";
 
