#! /usr/local/apps/perl/bin/perl

#
#       Copyright (c) 1997  San Diego Supercomputer Center (SDSC)
#               San Diego, California, USA
#
#       Users and possessors of this source code are hereby granted a
#       nonexclusive, royalty-free copyright and design patent license to
#       use this code in individual software.  License is not granted for
#       commercial resale, in whole or in part, without prior written
#       permission from SDSC.  This source is provided "AS IS" without
#       express or implied warranty of any kind.
#

# Author: Michael Smith 
#         San Diego Supercomputer Center
#         3/5/97
#         filter.cgi

# This cgi script is designed to gather information about when the backups 
# have been performed and present them in such a way as to be easily 
# understandable.

# Variables that need to be initialized

$LOGDIR="/misc/www/staff/Workstation_services/Backups/Reports";
@machlist=(`ls -1 $LOGDIR | sed -e 's/\.log//'`);

# Prevents cacheing of data and makes update new every time

print"Pragma: no-cache\n";
print "Content-type: text/html\n";
print "\n";

#&htmldie ("This script must be referenced using the POST method")
#    if ($ENV{REQUEST_METHOD} ne "POST") ;
 
#&htmldie ("This script can only be used to decode forms")
#    if ($ENV{CONTENT_TYPE} !~ /application\/x-www-form-urlencoded/) ;

read (STDIN, $GN_QUERY, $ENV{CONTENT_LENGTH}); # Read data
 
@QUERY_LIST = split( /&/, $GN_QUERY); # Split data into different fields

foreach $item (@QUERY_LIST) {
    $item =~ s/\+/ /g;                     # Convert plus's into spaces
    $item =~ s/%(..)/pack("c",hex($1))/ge; # Convert %XX from hex to char
    $loc=index($item,"=");
    $param=substr($item, 0, $loc);
    $value=substr($item, $loc+1);
    $QUERY_ARRAY{$param} .= $value;
}

$choice=$QUERY_ARRAY{choice};
 if($choice == 0)
 {
 &Process_Summary;
 }
if($choice == 1)
{
 $machine=$QUERY_ARRAY{machine1};
if($machine eq "ALL")
{
 foreach $mach (@machlist)
  {
  $machine=$mach;
  &Process_Level;
  }
}
 else
 {
 &Process_Level;
 }
}
 if($choice == 2 )
 {
 $machine=$QUERY_ARRAY{machine2};
 if($machine eq "ALL")
 {
 foreach $mach (@machlist)
  {
  $machine=$mach;
  &Process_Machine;
  }
 }
 else
 {
 &Process_Machine;
 } 
 }
&End_Form;
exit;

# Prints out the end of the form information

sub End_Form
{
print "\n</PRE>";
print "\n<P><P><P>\n<A HREF=\"filter.html\">Return to Backup Status page</A>";
print "\n</BODY>\n</HTML>";
}

# Gets information for a specific machine
# Accesses global $machine that contains the machine name

sub Process_Machine
{
print "<HTML>";
print "\n<HEAD><TITLE> Backup Summary Report </TITLE> </HEAD>";
print "<H1> Backup Summary Report </H1> <P>";
print "<H3> $machine </H3>";
print "\n<BODY>";
print "\n<FONT SIZE=+1>";
$value=`ls $LOGDIR/*.log`; 
print "\n<PRE>";
$value=`cat $LOGDIR/$machine.log`;
print $value;
}

sub Process_Summary
{
print "<HTML>";
print "\n<HEAD><TITLE> Backup Summary Report </TITLE> </HEAD>";
print "<H1> Backup Summary Report </H1> <P>";
print "\n<BODY>";
print "\n<FONT SIZE=+1>";
$value=`ls $LOGDIR/*.log`;
print "\n<PRE>";
@logfiles=split("\n",$value);
print "\nName        Level   Start Time     Finish Time      Total MB Failed \n\n";
foreach $logfile (@logfiles)
{
$value=`tail -1 $logfile`;
@field=split(" ",$value);
if(@field[9] == 0)
{
print "<FONT COLOR=\"BLACK\">";
}
else
{
print "<FONT COLOR=\"NAVY\">";
}
print "$value";
}
print "\n</PRE>";
}


# Process level takes the values of the last level 0,3,6 was taken from
# a variable $machine and prints them as the body of a web page

sub Process_Level
{ 
print "<HTML>";
print "\n<HEAD><TITLE> Backup Summary Report </TITLE> </HEAD>";
print "<H1> Backup Summary Report </H1> <P>";
print "<H3> $machine </H3>";
print "\n<BODY>";
print "\n<FONT SIZE=+1>";
$value=`ls $LOGDIR/*.log`;
print "\n<PRE>";
@value=`cat $LOGDIR/$machine.log`;
foreach $line (@value)
{
@field=split(" ",$line);
if(@field[9] == 0)
{
$value{@field[1]}=$line;
}
}
print "\nName        Level   Start Time     Finish Time      Total MB Failed \n\n";
print "$value{0}";
print "$value{3}";
print "$value{6}";
}

# End of File


