#! /bin/sh /usr/share/dpatch/dpatch-run
## 02-cugrapher-config.patch.dpatch by  <russell-debian@stuart.id.au>
##
## All lines beginning with `## DP:' are a description of the patch.
## DP: Make CUGrapher.pl read settings from /etc/flowscan/CUGrapher.cf

@DPATCH@
diff -Nur CUFlow-1.7/CUGrapher.cf flowscan-cuflow-1.7/CUGrapher.cf
--- CUFlow-1.7/CUGrapher.cf	1970-01-01 10:00:00.000000000 +1000
+++ flowscan-cuflow-1.7/CUGrapher.cf	2006-01-11 16:58:03.000000000 +1000
@@ -0,0 +1,67 @@
+# Configuration file for CUGrapger.pl.
+#
+
+# Where to find the rrd's.  You must set this.
+# Make sure it is the same as OutputDir in CUFlow.cf.  
+# 
+# Eg:      OutputDir /cflow/reports/rrds
+# Default: not set
+
+# Organisation name - appears in the graph title.
+#
+# Eg:      Organization Columbia University Campus
+# Default: (blank)
+
+# Default number of hours to go back.
+#
+# Eg:      Hours 24
+# Default: Hours 48
+
+# Default width of graph in pixels.
+#
+# Eg:      Width 800
+# Default: Width 640
+
+# Default height of graph in pixels.
+#
+# Eg:      Height 480
+# Default: Height 320
+
+# Default image type.  Can be png or gif.
+#
+# Eg:      ImageType gif
+# Default: ImageType png
+
+# Default graph title.
+#
+# Eg:      Title My Graph
+# Default: Title Well Known Protocols/Services
+
+#
+# Specify a graph to be displayed on startup, when no query
+# has been entered.  To display multiple graphs supply
+# multiple "DefaultGraph" lines.  To generate the string
+# following the "DefaultGraph":
+#  1.  Use the web page to generate the graph you want.
+#  2.  Copy the query part of the URL displayed by your browser
+#      (ie everything part the '?').
+#  3.  Remove the ';showmenu=1' from the query string copied.
+#
+# Eg:      DefaultGraph report=bits;hours=48;imageType=png;width=640;height=320;duration=;router=all;all_all_services=1;legend=1;title=My%20Graph
+# Default: not set
+
+# The path to the AggregateScore web page build by CUFlow.  If
+# non-blank a like to AggregateScore web page will be displayed.
+# If supplied this must be the same as the file name given to
+# the AggregateScore setting in CUFlow.cf.
+#
+# Eg:      AggregateScore /var/local/netflow/cuflow/agg10.html
+# Default: (not set)
+
+# The path to the Scoreboard web page built by CUFlow.  If
+# non-blank a link to the Scoreboard web page will be
+# displayed.  If supplied this must be the same as the file
+# name given to the Scoreboard setting in CUFlow.cf.
+#
+# Eg:      Scoreboard /var/local/netflow/cuflow/top10.html
+# Default: (not set)
diff -Nur CUFlow-1.7/CUGrapher.pl flowscan-cuflow-1.7/CUGrapher.pl
--- CUFlow-1.7/CUGrapher.pl	2006-01-11 06:47:10.000000000 +1000
+++ flowscan-cuflow-1.7/CUGrapher.pl	2006-01-11 17:02:07.000000000 +1000
@@ -15,23 +15,68 @@
 
 ### Local settings ###
 
+# top 10 url's
+my $aggregateScoreFile;
+my $scoreboardFile;
 # directory with rrd files
-my $rrddir = "/cflow/reports/rrds";
+my $rrddir;
 # default number of hours to go back
 my $hours = 48;
 # duration of graph, starting from $hours ago
 my $duration;
 # organization name
-my $organization = "Estimated Columbia University Campus";
+my $organization = "";
 # default graph width
 my $width = 640;
 # default graph height
 my $height = 320;
 # default image type (png/gif)
 my $imageType = 'png';
+# default image display on startup
+my @defaultGraph;
+# Page title
+my $title = "Well Known Protocols/Services";
 
 ### End local settings ###
 
+## Read the configuration file
+my $conffile = "/etc/flowscan/CUGrapher.cf";
+if (open(FH,$conffile)) {
+    while(<FH>) {
+	s/\#.*$//;		# Strip out everything after a #
+	next if /^\s*$/;	# Skip blank lines
+
+	if (/^\s*OutputDir\s+(\S+)\s*$/) {
+	    $rrddir = $1;
+	} elsif (/^\s*AggregateScore\s+(\S.*)\s*$/) {
+	    $aggregateScoreFile = $1;
+	} elsif (/^\s*Scoreboard\s+(\S.*)\s*$/) {
+	    $scoreboardFile = $1;
+	} elsif (/^\s*Organization\s+(\S.*)\s*$/) {
+	    $organization = $1;
+	} elsif (/^\s*Width\s+(\S+)\s*$/) {
+	    $width = $1;
+	} elsif (/^\s*Height\s+(\S.*)\s*$/) {
+	    $height = $1;
+	} elsif (/^\s*ImageType\s+(\S.*)\s*$/) {
+	    $imageType = $1;
+	} elsif (/^\s*Hours\s+(\S.*)\s*$/) {
+	    $hours = $1;
+	} elsif (/^\s*Title\s+(\S.*)\s*$/) {
+	    $title = $1;
+	} elsif (/^\s*DefaultGraph\s+(\S*)\s*$/) {
+	    $defaultGraph[@defaultGraph] = $1;
+	} else {
+	    &browserDie( "Invalid line $. in $conffile\n\t$_\n" );
+	}
+    }
+    close(FH);
+}
+
+if (! $rrddir) {
+    &browserDie( "OutputDir not set in $conffile\n" );
+}
+
 # auto-flush STDOUT
 $| = 1;
 
@@ -41,12 +86,21 @@
 		   'flows' => 'flows' );
 
 unless( param() ) {
-    &showMenu();
+    &showMenu(@defaultGraph);
 }
 
 if (param('showmenu')) {
     Delete('showmenu');
-    &showMenu(self_url());
+    @defaultGraph = ( query_string() );
+    &showMenu(@defaultGraph);
+}
+
+if (param("show-aggregateScore")) {
+    &showFile('AggregateScore', $aggregateScoreFile);
+}
+
+if (param("show-scoreboard")) {
+    &showFile('Scoreboard', $scoreboardFile);
 }
 
 # protocol/service -> filename
@@ -72,6 +126,7 @@
 &getDuration();
 &getWidth();
 &getHeight();
+&getTitle();
 &getTotal();
 &getDebug();
 
@@ -104,15 +159,57 @@
     }
 }
 
+sub showFile {
+    my ($title, $fileName) = @_;
+    my $q = new CGI::Pretty(""); # Avoid inheriting the parameter list
+
+    print $q->header;
+
+    open(HTMLFILE, $fileName) or
+      &browserDie("Can't open ${title} ${fileName}");
+    while (<HTMLFILE>) {
+      print $_;
+    }
+    exit;
+}
+
+sub defParam {
+  my ($name, $default) = @_;
+  my $result = param($name);
+  
+  return $result if ($result);
+  return $default;
+}
+
 sub showMenu {
-    my ($imgurl) = @_;
+    my (@graphs) = @_;
     my $q = new CGI::Pretty(""); # Avoid inheriting the parameter list
+    my $url = $q->url(-relative => 1);
+    my $default;
     
 
     print $q->header, $q->start_html( -title => 'Generate FlowScan graphs on the fly',
 				      -bgcolor => 'ffffff' );
+    
+    print $q->h1({-align => "center"}, "NetFlow report for ${organization}");
+
+    print $q->start_p({-align => "center"});
+    if ($aggregateScoreFile) {
+        print $q->a(
+	  { -href => "${url}?show-aggregateScore=1" },
+	  $q->font({-size => "+1"}, "See the Aggregate Scores"));
+	print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
+    }
+    if ($scoreboardFile) {
+        print $q->a(
+	  { -href => "${url}?show-scoreboard=1" },
+	  $q->font({-size => "+1"}, "See the Scoreboard"));
+    }
+    print $q->end_p();
+
+    foreach my $imguri (@graphs) {
+	my $imgurl = $url . "?" . $imguri;
 
-    if ($imgurl) {
 	if ($imgurl =~ /debug=1/) {
 	    (my $cleanimgurl = $imgurl) =~ s/debug=1//;
 	    print $q->center( $q->a( { href => $imgurl},
@@ -124,8 +221,8 @@
 					-alt => 'The Graph you requested'}));
 	}
     }
-    
-    print $q->start_form( -action => $q->url(), -method => 'get' ); # Just the url, without query string
+      
+    print $q->start_form( -action => $url, -method => 'get' ); # Just the url, without query string
 
     print $q->start_table( { -align => 'center',
 			     -cellspacing => '10' } );
@@ -137,37 +234,41 @@
 		  "Report: ",
 		  $q->popup_menu( -name => 'report',
 				  -values => [sort keys %reportName],
-				  -default => '' ) );
+				  -default => defParam('report', 'bin') ) );
     
-    my %hours = ( 24 => '24 hours',
+    my %hours = ( 12 => '12 hours',
+		  24 => '24 hours',
 		  36 => '36 hours',
 		  48 => '48 hours',
 		  168 => '1 week',
 		  720 => '1 month' );
+    $hours{defParam('hours', $hours)} = defParam('hours', $hours) . " hours" 
+      if (!$hours{defParam('hours', $hours)}); 
 
     print $q->td( { -align => 'right' },
 		  "Time period: ",
 		  $q->popup_menu( -name => 'hours',
 				  -values => [sort {$a <=> $b} keys %hours],
-				  -default => $hours,
+				  -default => defParam('hours', $hours) + "",
 				  -labels => \%hours ) );
     
     print $q->td( { -rowspan => '2' },
 		  "Image type: ",
 		  $q->popup_menu( -name => 'imageType',
 				  -values => ['png', 'gif'],
-				  -default => 'png' ) );
+				  -default => defParam('imageType', 'png') ) );
     
+    $default = param
     print $q->td( { -rowspan => '2' },
 		  "Width:",
 		  $q->textfield( -name => "width",
-				 -default => $width,
+				 -default => defParam('width', $width),
 				 -size => 7 ) );
     
     print $q->td( { -rowspan => '2' },
 		  "Height:",
 		  $q->textfield( -name => "height",
-				 -default => $height,
+				 -default => defParam('height', $height),
 				 -size => 7 ) );
 
     print $q->end_Tr();
@@ -267,6 +368,7 @@
     print $q->br;
 
     print $q->hidden('showmenu','1');
+    print $q->hidden('title',$title);
 
     print $q->center( $q->submit( -name => '',
 				  -value => 'Generate graph' ),
@@ -355,6 +457,12 @@
     }
 }
 
+sub getTitle {
+  if ( param('title') ) {
+    $title = param('title');
+  }
+}
+
 sub getDebug {
     if( param('debug') && param('debug') eq '1' ) {
 	$debug = 1;
@@ -652,8 +760,7 @@
     push @args, ('--interlaced',
 		 '--imgformat='.uc($imageType),
 		 '--vertical-label='.$reportName{$reportType}.' per second',
-		 "--title=${organization} Well Known Protocols/Services, ".
-		 "\u${reportName{$reportType}}, +out/-in",
+		 "--title=${title}, \u${reportName{$reportType}}, +out/-in",
 		 "--start=".(time - $hours*60*60),
 		 "--end=".(time - $hours*60*60 + $duration*60*60),
 		 "--width=${width}",
