#!/usr/bin/perl

use CGI;

my $form = new CGI;

if ($form->param('exclude'))
{
    @excludes = split /\+/,$form->param('exclude');
}
if ($form->param('only'))
{
    @only = split /\+/,$form->param('only');
}

require "pong3.conf";

opendir (STATUSDIR, $pongstatusdir);
@allfiles = readdir(STATUSDIR);
closedir (STATUSDIR);

$occurrences{'red'} = 0;
$occurrences{'yellow'} = 0;
$occurrences{'green'} = 0;

foreach $file (@allfiles)
{

    ($host,$service,$color) = $file =~ /^(.*)-([^-]+)-([^-]+)$/;

#    $DEBUG = ($service =~ /clientv/)? 1 : 0;

    print STDERR "Checking File $file\n" if $DEBUG;

    # EXCLUDE THIS FILE IF WE NO LONGER KNOW THE HOST
    
    next if !$groups{$host};

    # EXCLUDE THIS FILE IF IT IS SERVICE FREE

    next if $service eq 'free';         # Skipping Free Files Until
                                        # Fully Implemented

    # EXCLUDE ANY FILES SPECIFICALLY LISTED IN @EXCLUDES
    # _OR_ IF WE'RE USING @ONLY, EXCLUDE ANY EXCEPT THOSE
    # LISTED IN @ONLY

    @this_host_groups = split /\s/,$groups{$host};
    $excluded = 0;

    if (@excludes)
    {
	foreach $group (@this_host_groups)
	{
	    print STDERR "Checking for group $group in @excludes\n" if $DEBUG;
	    if (grep /^$group$/,@excludes)
	    {
		print STDERR "$host is excluded by group $group\n" if $DEBUG;
		$excluded = 1;
	    }
	}
    }
    
    if (@only)
    {
	$excluded = 1;
	foreach $group (@only)
	{
	    $excluded = 0 if grep /^$group$/,@this_host_groups;
	}
    }
    next if $excluded;

    # EXCLUDE IF THIS SERVICE IS NOT REALLY REQUESTED (OLD DATA)

    $foo = $services{$host};
    $foo =~ s/\bclient\b/free df load/gi;

    @this_host_services = split /\s/,$foo;
    next if !grep /^$service$/,@this_host_services;
      
    # IF WE'VE GOTTEN THIS FILE, THE FILE IS GOOD.

    if (!$worstcolor)
    {
	$worstcolor = $color;
    }
    elsif ($worstcolor eq 'green')
    {
	$worstcolor = $color;
    }
    elsif ( ($worstcolor eq 'yellow') and
	    ($color eq 'red') )
    {
	$worstcolor = $color;
    }

    print STDERR "This is a useful file\n" if $DEBUG;
    
    $occurrences{$color} ++;

    push @useful_files, $file;
    push @allservices, $service if !grep /^$service$/,@allservices;
}
    
print "Refresh: 60\n";
print "Content-type: text/html\n\n";

print <<EOF;
<HTML>
<HEAD>
<TITLE>Status of Monitored Machines</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" BACKGROUND="images/bkg-$worstcolor.gif" 
TEXT="#0000FF">
<CENTER>
<TABLE BORDER=2><TR><TD>
<IMG SRC="$DISPLAY_HEADER_IMAGE">
</TD></TR></TABLE>
<P>
EOF
    ;

open LSLRT, "ls -lrt $pongstatusdir |";
@lslrt = (<LSLRT>);
close LSLRT;

$firsttime = $lslrt[1];
($firstfilename) = $lslrt[1] =~ /\s(\S+)$/;
$last_idx = $#lslrt;
$lasttime = $lslrt[$last_idx];
($lastfilename) = $lslrt[$last_idx] =~ /\s(\S+)$/;

($date1) = $firsttime =~ /(\w{3}\s+\d+\s+\d+\:\d{2})/;
($date2) = $lasttime =~ /(\w{3}\s+\d+\s+\d+\:\d{2})/;

($blah,$blah,$blah,$blah,$blah,$blah,$blah,$blah,
 $fatime,$fmtime,$fctime) = stat("$pongstatusdir/$lastfilename");  # make this watch the most recent, not the oldest

if ($fmtime < ( time-(15 * 60)) )
{
    $bgcolor='#FF0000';
    $fgcolor='#000000';
}
else
{
    $bgcolor='#FFFFFF';
    $fgcolor='#000000';
}

print "<TABLE BORDER=2><TR><TD BGCOLOR=\"$bgcolor\">\n";
print "<FONT SIZE=\"+1\" COLOR=\"$fgcolor\">Data collected $date1 through $date2</FONT>\n";
print "</TD></TR></TABLE>\n<P>\n";

#print "OLDEST: $firstfilename NEWEST: $lastfilename<BR>\n";

print <<EOF;
<TABLE BORDER=1 WIDTH="40%">
<TR>
  <TH COLSPAN=3 BGCOLOR="#000000">
     <FONT COLOR="#FFFFFF">Color Summary</FONT>
  </TH>
</TR>

<TR>
    <TH WIDTH="33%" BGCOLOR="#000000">
      <FONT COLOR="#30FF30">Green: $occurrences{'green'} </FONT>
    </TH>
    <TH WIDTH="34%" BGCOLOR="#000000">
      <FONT COLOR="#FFFF30">Yellow: $occurrences{'yellow'} </FONT>
    </TH>
    <TH WIDTH="33%" BGCOLOR="#000000">
      <FONT COLOR="#FF6060">Red: $occurrences{'red'} </FONT>
    </TH>
</TR>
</TABLE>

<P>
EOF
    ;
foreach $filename (@useful_files)
{
    ($service) = $filename =~ /\-(\w+)\-(red|green|yellow)/;

    if (! grep /$service/, @allservices)
    {
	push @allservices,$service;
    }
}

print "<TABLE BORDER=1>\n";


$colspan = $#allservices + 2;

foreach $group (@groups)
{

    next if grep /^$group$/,@excludes;
    next if (@only) and (! grep /^$group$/,@only);

    print "<TR>\n";
    print "<TH COLSPAN=\"$colspan\" BGCOLOR=\"#D0D0FF\">$group</TH></TR>\n";

    print_headers();

    foreach $host (sort(keys(%services)))
    {
	next if $groups{$host} !~ /\b$group\b/;
	$long_host = $long_name{$host} || $host;

	print "<TR><TH ALIGN=\"LEFT\" BGCOLOR=\"#000000\"><FONT COLOR=\"#C0C0FF\">$long_host</FONT></TH>\n";
	foreach $service (sort(@allservices))
	{
	    if ($service eq 'clientv')
	    {
		$filename = "$host-$service-green";
		($statusfound) = grep /^$filename/,@useful_files;
		print "<TD ALIGN=\"CENTER\" BGCOLOR=\"#000000\"><FONT COLOR=\"#FFFFFF\">&nbsp;";
		if ($statusfound)
		{
		    open FOO, "$pongserverdir/status/$host-$service-green";
		    $clientv = <FOO>;
		    chomp $clientv;
		    close FOO;
		    print "$clientv ";
		}
		print "</FONT></TD>\n";
		
	    }
	    else
	    {
		print "<TD ALIGN=\"CENTER\" BGCOLOR=\"#000000\">";
		$found = 0;
		foreach $color ('red','green','yellow')
		{
		    $filename = "$host\-$service\-$color";
#	    print STDERR "Looking for $filename...";
		    ($statusfound) = grep /^$filename/,@useful_files;
		    if ($statusfound)
		    {
			print "<A HREF=\"pong3detail.pl\?color=$color\&host=$host\&service=$service\">";
			print "<IMG BORDER=0 SRC=\"images/$color.gif\">";
			print "</A>";
			($shorthost) = $host =~ /^(\w+)\./;
#		print "$shorthost $service $status";
			$found = 1;
		    }
#	    print STDERR "\n";
		}
		print " &nbsp;" if !$found;
		print "</TD>\n";
	    }
	}
	print "</TR>\n";
    }

}
print "</TABLE>\n";



sub print_headers
{
    print "<TR>\n";
    print "<TH BGCOLOR=\"#000000\"><FONT COLOR=\"#C0C0FF\">HOSTNAME</FONT></TH>\n";
    
    foreach $service (sort(@allservices))
    {
	print "<TH BGCOLOR=\"#000000\"><FONT COLOR=\"#C0C0FF\">$servicename{$service}</FONT></TH>\n";
    }
    print "</TR>\n";
}
