#!/usr/local/bin/perl
# 
# /*   $Id: bcnuweb,v 1.5 2000/05/05 13:07:42 jap Exp $
#
# *    Copyright (c) 1998 John Phillips
# *
# *    You may distribute under the terms of the Artistic License
# *
# */
#
require "getopts.pl";

push (@INC,"$ENV{'BCNUHOME'}/cgi-bin");
push (@INC,"/usr/local/bcnu/cgi-bin");

require "bcnu.inc";
require "bcnulib.pl";

&Getopts("adm");
if ($opt_a) {
	$AGENTS=1;
   }
if ($opt_d) {
	$AGENTDETAIL=1;
	$AGENTS=1;
   }
if ($opt_m) {
	$AGENTDETAIL=1;
	$AGENTS=1;
	$MONITORONLY=1;
   }

$BCNUSTATIC=1;

#
# main
#

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

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

$errlevel=0;
$sound=1;

$numhosts=&gethosts;

foreach $host (sort @hosts) {
        next if $host =~ /\.$/;
	&sumhost($host);
}

# Produce status page
open (WEB,">$WWW/status.htm") || die "can't write web page $WWW/status.htm";

$perline=0;
print WEB &header("Status Only",$errlevel,5);
if ($errlevel) {
	print WEB "<TR><TH COLSPAN=$PERLINE ALIGN=CENTER $BGCELL>$numhosts Systems Monitored</TR>\n";
	foreach $entry (sort keys %cell) {
		if ($perline==0) {
			print WEB "<TR $BASEROW>\n";
			$perline=$PERLINE;
		}
		if ($hoststat{$entry} > 0) {
		$perline--;
       		print WEB $cell{$entry} if $hoststat{$entry} > 0;
			}
        	}
	}

print WEB &footer();
close (WEB);

# Produce summary page
open (WEB,">$WWW/summ.htm") || die "can't write web page $WWW/summ.htm";

$perline=0;
print WEB &header("All Systems - Summary",$errlevel,5);
print WEB "<TR><TH COLSPAN=$PERLINE ALIGN=CENTER $BGCELL>$numhosts Systems Monitored</TR>\n";
foreach $entry (sort keys %cell) {
	if ($perline==0) {
		print WEB "<TR $BASEROW>\n";
		$perline=$PERLINE;
	}
	$perline--;
	print WEB $cell{$entry};
	}

print WEB &footer();
close (WEB);

# produce platform page

# get an array with each host in it's correct platform
foreach $h (sort keys %platform)	{
	$plat{$platform{$h}}.="$h:";
	}

#
open (WEB,">$WWW/platforms.htm") || die "can't write web page $WWW/platforms.htm";

$perline=0;


print WEB &header("All Systems - Summary by type",$errlevel,5);
print WEB "<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 WEB "<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 WEB "<TR $BASEROW>\n";
			$perline=$PERLINE;
		}
		$perline--;
        	print WEB $cell{$entry};
	        }
}
print WEB &footer();

close WEB;


$sound=0;

if ($AGENTS) {

#Generate agent pages
foreach $host (sort @hosts) {

	$errlevel=0;
	undef %mon;
	&getagents ($host);
	undef %mon;
        &agenthost ($host);

	open (WEB,">$WWW/hosts/$host.htm") || die "can't write web page $WWW/hosts/$host.htm";
	chmod 0644,"$WWW/hosts/$host.htm";

        print WEB &header("$host - Agent Summary",$errlevel,2);
        print WEB $agdisp;
        print WEB &footer();
	close (WEB);

	if ($AGENTDETAIL) {	
	foreach $agent (@agents) {
	        &agentdetail ($host,$agent);
		open (WEB,">$WWW/hosts/$host.$ag.htm") || die "can't write web page $WWW/hosts/$host_$ag.htm";

	        print WEB &header("$host - Agent ($ag)",$errlevel,2);
	        print WEB $agdisp;
	        print WEB &footer();
		close (WEB);
		}
	}
	}
}

