#! /usr/local/bin/perl
# 
# /*   $Id $
#
# *    Copyright (c) 1998 John Phillips
# *
# *    You may distribute under the terms of the Artistic License
# *
# */
#

$SAMPLEHOSTS="corp2 corpinter1 it hqroad3 ts ordev2 preprod1 tamu.edu";

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

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

$AGENTDETAIL=1;
$AGENTS=1;

$BCNUSTATIC=1;

#
# main
#

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

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

&gethosts;

(@HOSTS) = split(" ",$SAMPLEHOSTS);

$numhosts=@HOSTS;

$errlevel=0;
$sound=0;

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

#
# produce default home page
#
open (WEB,">$WWW/sample/systems.htm") || die "can't write web page $WWW/index.htm";

print WEB &cleanup(&header("All Systems - Detail",$errlevel,2));
print WEB &cleanup(&agent_head());
foreach $entry (sort keys %line) {
       	print WEB &cleanup($line{$entry});
        }
print WEB &cleanup(&footer());
close (WEB);

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

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

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

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

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

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

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

# generate query page
opendir(HDIR,"$DATA") || die "cannot open directory $BCNUDATA";
@files=grep (/\.log$/,readdir(HDIR));
foreach $file (sort @files) {
        next if $file =~ /\.$/;
        $mon{$file}++;
}
@logs = (keys (%mon));
@displog=sort grep(s/\.log$//,@logs);
@agentlog=sort @agents;

(@hosts) = split(" ",$SAMPLEHOSTS);

open (WEB,">$WWW/sample/bcnuqry.htm") || die "can't write sample query page";
print WEB &cleanup(&header("Query",0,200));
print WEB &cleanup(&selectform);
print WEB &cleanup(&footer());

# agent stuff below here
$sound=0;

if ($AGENTS) {

#Generate agent pages
foreach $host (@HOSTS) {

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

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

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

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

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

#####################################
sub cleanup {

local ($cleanpage)=@_;

$cleanpage =~ s/href=$BCNUURL\/cgi-bin//g;
$cleanpage =~ s/$BCNUURL\/images/..\/sample/g;
$cleanpage =~ s/$BCNUURL\/docs\//..\/docs\/htframe.htm/g;
$cleanpage =~ s/$BCNUURL/..\/sample/g;
$cleanpage =~ s/\hosts\///g;
$cleanpage =~ s/POST//g;

return ($cleanpage);

}
