#!/usr/bin/perl
$home = "/usr/users/luyer/snmp/perl";
require "$home/lib/disk.pl";
chdir $config'outdirs{'DiskD'}."/".week();
$| = 1;
webgraph'CgiStart();
$Selected{$in{'Sample'}} = 1 if (defined($in{'Sample'}));
$in{'QueryType'} = ifunshow($in{'QueryType'}) if(defined($in{'QueryType'}));
$Selected{($in{'QueryType'})} = 1 if (defined($in{'QueryType'}));
@files = glob "*-*";
$file = $in{'QueryType'};
$in{'Sample'} = "1 Hour" if(!defined($in{'Sample'}));
$Sample = 60 if ($in{'Sample'} eq "1 Hour");
$Sample = 120 if ($in{'Sample'} eq "2 Hours");
$Sample = 180 if ($in{'Sample'} eq "3 Hours");
$Sample = 360 if ($in{'Sample'} eq "6 Hours");
$Sample = 720 if ($in{'Sample'} eq "12 Hours");
$Sample = 1440 if ($in{'Sample'} eq "1 Day");
$Sample = 2880 if ($in{'Sample'} eq "2 Days");
$Sample = 4320 if ($in{'Sample'} eq "3 Days");
$Sample = 5760 if ($in{'Sample'} eq "4 Days");
$Sample = 10080 if ($in{'Sample'} eq "1 Week");
# REFRESH causes an IMS get of 60 * black.gif, etc.
#$seconds = $Sample ? $Sample : 60;
if (!defined($in{'QueryType'})) {
	$file = $config'default{"DiskD"};
	$Selected{"1 Hour"} = 1;
	$Selected{$file} = 1;
	$in{'QueryType'} = $file;
	$in{'Submit'} = "One Disk";
}
if($in{'Submit'} eq "Per-Host Statistics") {
	$url = $ENV{"SCRIPT_NAME"};
	$url =~ s/Disk/Host/;
	$url .= "?".$ENV{"QUERY_STRING"};
	$url =~ s/QueryType=([A-Za-z0-9\.]+)\-[^&]*/QueryType=$1/;
	print "Location: $url\n";
	print "Content-Type: text/plain\n\n";
	print "If you see this, it is because your web browser is seriously\n";
	print "outdated and doesn't support location headers.  I suggest you\n";
	print "get a newer web browser.  The redirect was to the URL:\n\n";
	print "\t$url\n\n";
	print "David Luyer <luyer\@ucs.uwa.edu.au>\n";
	exit;
}
print "Content-Type: text/html\n\n";
print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2//EN\">\n";
print "<HTML><HEAD>\n";
print "<TITLE>UWA Disk Statistics</TITLE>\n";
print "<LINK REV=made HREF=\"mailto:luyer\@ucs.uwa.edu.au\">\n";
#print "<META HTTP-EQUIV=REFRESH CONTENT=$seconds>\n";
print "</HEAD>";
print "<BODY BGCOLOR=\"#FFFFFF\">\n";
print "<H2 ALIGN=CENTER><STRONG>UWA Disk Statistics Page\n";
print "</STRONG><BR>";
webgraph'PrintTime(time());
print "</H2>\n";
#print "<P ALIGN=CENTER>This page is automatically refreshed every $seconds seconds.\n";
ProcessFile($file);
TrafficForm();
print "<ADDRESS>--<BR><A HREF=\"http://www.ucc.gu.uwa.edu.au/~luyer\">David Luyer</A>\n";
print "<A HREF=\"mailto:luyer\@ucs.uwa.edu.au\">&lt;luyer\@ucs.uwa.edu.au&gt;</A>\n";
print "</ADDRESS>\n";
print "<A HREF=\"http://www.uwa.edu.au/cgi-bin/validate\"><IMG ALIGN=RIGHT SRC=\"http://www.uwa.edu.au/cwis/valid32.gif\" ALT=\"HTML 3.2 Checked!\" HEIGHT=32 WIDTH=48 BORDER=0></A>\n";
print "</BODY></HTML>\n";

sub TrafficForm {
my ($file, $rtr, $str);

	for $str ("1 Hour", "2 Hours", "3 Hours", "6 Hours", "12 Hours",
		  "1 Day", "2 Days", "3 Days", "4 Days", "1 Week") {
		if(!exists($Selected{$str})) {
			$Selected{$str} = "OPTION";
		} else {
			$Selected{$str} = "OPTION SELECTED";
		}
	}
	print <<EOF;
<P ALIGN=CENTER>(note: a blue line represents a new day; a red line represents a new hour)
<P><FORM METHOD="GET" ACTION="$ENV{'SCRIPT_NAME'}">
<TABLE><TR><TD><SELECT NAME="Sample">
<$Selected{"1 Hour"}>1 Hour
<$Selected{"2 Hours"}>2 Hours
<$Selected{"3 Hours"}>3 Hours
<$Selected{"6 Hours"}>6 Hours
<$Selected{"12 Hours"}>12 Hours
<$Selected{"1 Day"}>1 Day
<$Selected{"2 Days"}>2 Days
<$Selected{"3 Days"}>3 Days
<$Selected{"4 Days"}>4 Days
<$Selected{"1 Week"}>1 Week
</SELECT>
<TD><INPUT TYPE=SUBMIT NAME=Submit VALUE="One Disk"></TD>
<TR><TD><SELECT NAME=QueryType>
EOF
	$rtr = (split(/-/, $in{'QueryType'}))[0];
	foreach $file (@files) {
		if ((split(/-/, $file))[0] eq $rtr) {
			if(exists($Selected{$file})) {
				print("<OPTION SELECTED>",ifshow($file),"\n");
			} else {
				print("<OPTION>",ifshow($file),"\n");
			}
		}
	}
	foreach $file (@files) {
		if ((split(/-/, $file))[0] ne $rtr) {
			print("<OPTION>",ifshow($file),"\n");
		}
	}
	print <<EOF;
</SELECT>
<TD><INPUT TYPE=SUBMIT NAME=Submit VALUE="Per-Host Statistics">
</TABLE></FORM>
EOF
}
