#! /usr/local/bin/perl
# 
# /*   $Id: bcnu.cgi,v 1.6 2000/05/05 13:05:11 jap Exp $
#
# *    Copyright (c) 1998 John Phillips
# *
# *    You may distribute under the terms of the Artistic License
# *
# */
#
require "bcnu.inc";
require "bcnulib.pl";

#
# main
#

&methodparse;
$ighost=$in{"ignore"};
$noticehost=$in{"notice"};
$view=$in{"view"};

$insubmit=$in{"submit"};
@inacks=split('!',$in{"ackchk"});
@inreasons=split('!',$in{"reason"});


$navigate=&navigate();
$showkey=&showkey();


$errlevel=0;
$sound=1;

$readrecs=0;

&cklogin();

if ($insubmit) {

	open (ACK,"$BCNUHOME/flags/bcnud.ack") || &bcnuerror("Acknowledge", "cannot open file $BCNUHOME/flags/bcnud.ack");
	@ackfile=(<ACK>);
	close (ACK);
	grep(s/\+/ /g,@inacks);
	$ct=0;
	foreach $del ( @inacks ){
		grep (s/$del.*\n//,@ackfile);
		&weback("Acknowledge: ($del) :Reason: " . ($inreasons[$ct] ? $inreasons[$ct] : 'None'));
		$ct++;
	}
	open (ACK,">$BCNUHOME/flags/bcnud.ack") || &bcnuerror("Acknowledge", "cannot open file $BCNUHOME/flags/bcnud.ack");
	$acks=0;
	foreach $del ( @ackfile) {
		if ($del ne "") {
			print ACK "$del";
			$acks++;
		}
	}
	close (ACK);
	unlink("$BCNUHOME/flags/bcnud.ack") unless $acks;
#	print "Location: $ENV{HTTP_REFERER}\r\n\r\n";
	print "Location: $BCNUURL/cgi-bin/bcnu.cgi\r\n\r\n";
	exit;
}


if ($loginid && &readack) {

	$acks=0;
	$ackdisp="";
	foreach $hist (sort keys %record) {
		$inlogfile=$loghash{$hist};
		$ackdisp .= &agentdisp($inhost,$inagent,$record{$hist});
		&genackform;
		$ackdisp .="<TD $CELL>$ackform</TR>\n";
		$acks++;
	}
	$agdisp = '</TABLE><P><TABLE border=1>';
	$agdisp .= "<TR><TH bgcolor=#bbaacc colspan=6 align=center>Recent Error History - $acks alerts unacknowledged</TH></TR>";
	$agdisp .= "<FORM ACTION=\"$ENV{SCRIPT_NAME}\" METHOD=\"POST\" NAME=\"mainForm\">";
	$agdisp .= "<TR><TH $DEFCELL>System<TH $DEFCELL>Status<TH $DEFCELL>Agent<TH $DEFCELL>Date/Time<TH $DEFCELL>Message<TH $DEFCELL>Comment/Clear history</TR>\n";

	$agdisp .= $ackdisp;

	$agdisp .= "</TABLE>\n";
	$agdisp .= &jscript();
	$agdisp .= "<INPUT TYPE=submit NAME=submit VALUE=Acknowledge></FORM>";
}

$errlevel=0;

# walk thru the hosts directory and get a list of agents for each host
#

if ($view) {
	&viewhosts($view);
        }
else
        {
        $numhosts=&gethosts();
        }

foreach $host (sort @hosts) {
	&sumhost($host);
}

#
# produce default home page
#

print &printhdr;

if (scalar(keys %cell) < $PERLINE) { $PERLINE=scalar(keys %cell);}

if ($in{"status"}) {
	print &header("Status only",$errlevel,$REFRESH);
	if ($errlevel) {
		print "<TR><TH COLSPAN=$PERLINE ALIGN=CENTER $BGCELL>$numhosts Systems Monitored</TR>\n";
		foreach $entry (sort keys %cell) {
			if ($perline==0) {
				print "<TR $BASEROW>\n";
				$perline=$PERLINE;
			}
			if ($hoststat{$entry} > 0) {
			$perline--;
        		print $cell{$entry} if $hoststat{$entry} > 0;
				}
	        	}
		}
	}
elsif ($in{"platform"}) {
# get an array with each host in it's correct platform
foreach $h (sort keys %platform)	{
	$plat{$platform{$h}}.="$h:";
	}

print &header("All Systems - Summary by type",$errlevel,$REFRESH);
print "<TR><TH colspan=$PERLINE ALIGN=CENTER $BGCELL>$numhosts Systems Monitored</TH></TR>\n";
foreach $p (sort keys %plat)    {
	(@plattype) = split(":",$plat{$p});
	$numthishost=@plattype;
	print "<TR><TH colspan=$PERLINE ALIGN=CENTER $ECELL>$numthishost $bcnu_host_type{$p} Systems</TH></TR>\n";
	$perline=0;
	foreach $entry (sort @plattype) {
		if ($perline==0) {
			print "<TR $BASEROW>\n";
			$perline=$PERLINE;
		}
		$perline--;
        	print $cell{$entry};
	        }
	}

}
else	{
	print &header("All Systems - Summary",$errlevel,$REFRESH);
	print "<TR><TH colspan=$PERLINE ALIGN=CENTER $BGCELL>$numhosts Systems Monitored</TH></TR>\n";
	foreach $entry (sort keys %cell) {
		if ($perline==0) {
			print "<TR $BASEROW>\n";
			$perline=$PERLINE;
		}
		$perline--;
        	print $cell{$entry};
	        }
}
print $agdisp;
print &footer();
#print &PrintVariables(%ENV);

sub jscript {

$js = <<ENDJAVA;

<script lang="JavaScript">
agent = navigator.userAgent;
browserOk = 0;
if (navigator.appName == "Netscape" && navigator.appVersion.substring(0,1) >= 2) {
	browserOk = 1
} else {
	if (navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion.substring(0,1) >= 4) {
		browserOk = 1 
	}
}

function checkUncheck(foo) {
	if (browserOk) {
		for (i = 0; i < document.forms["mainForm"].length; i++) {
			document.forms["mainForm"].elements[i].checked = foo
		}
	}
}

</SCRIPT>
<A HREF="javascript:checkUncheck(true)" onMouseOver="window.status='Select all messages';return true" onMouseOut="window.status='';return true">
<FONT SIZE="-2" COLOR="#000000" FACE="Geneva, Arial">Select All</FONT></A>
<A HREF="javascript:checkUncheck(false)" onMouseOver="window.status='Deselect all messages';return true" onMouseOut="window.status='';return true">
<FONT SIZE="-2" COLOR="#000000" FACE="Geneva, Arial"> | Deselect all</FONT></A>
<P>

ENDJAVA

return $js;
}
