#! /usr/local/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: Chris Daharsh
#         San Diego Supercomputer Center
#         1997/03

# This script filters the messages that come from the backup system
# via mail and sorts their information into the appropriate log files
# The log files take the format of "[machine name].log"

$LOGDIR="/Workstation_services/Backups/Reports";

$LOCKFILE="$LOGDIR/filter.lock"; # This is done to avoid file collisions

$mailer='backmgr';      # People to mail messages to

# Initalize variables
$vaild=0; # Is this a valid message from the rbackup.all or rbackup.rerun
          # script

# Sleep until lock file is gone
while (-e $LOCKFILE) 
{
sleep(15);  # sleep, then check again
}

# Install lock file and start working
open(LOCK, ">$LOCKFILE");

# Filter the mail message that was just receieved
# This processes all of the fields that are needed for the report

while(<>)
{
#print $_;

# check to see if message is from rmtback

# Get the machine name from the subject field
if(m/Subject/)
{
@field=split(" ",$_);
$name=@field[1];
$fields=@field;
$n=0;
while($n <= $fields)
{
if(@field[$n] =~ m/completed/i)   # if the word completed is on the subject line
                                  # then it is a valid message
{
$valid=1;
}
$n++;
}
}

# Get the level of the backup
if(m/Level/)
{
@field=split(" ",$_);
$level=@field[1];
}

# Get the start date of the backup
if(m/Start/)
{
@field=split(" ",$_);
$start="@field[2] @field[3]";
$time=@field[4];
}

# Get the finish date of the backup
if(m/Finish/)
{
@field=split(" ",$_);
$finish="@field[2] @field[3]";
$ftime=@field[4];
}

# Get the total number of megabytes backed up
if(m/Total MB:/)
{
@field=split(" ",$_);
$MB=@field[2];
}

# Get the total number of file systems that failed
if(m/Failed/)
{
@field=split(" ",$_);
$failed=@field[1];
}
}

if($valid)  # If this is a valid entry update the logs
{
# Append the entry in the mail message to the end of the log file
if(-e "$LOGDIR/$name.log")
{
open (LOG,">>$LOGDIR/$name.log");
}
else
{
# If the file does not exist create one with the appropriate header
open (LOG,">$LOGDIR/$name.log");
print LOG "Name     Level    Start Time     Finish Time     Total MB Failed \n";
}

# Print the output to the logfile and close it
write LOG;
close(LOG);
} # End if valid entry

# Remove lock file and return to normal processing
$isdeleted = unlink($LOCKFILE);
if ($isdeleted == 0) {
   open(MAIL, "|/usr/bin/mail $mailer");
   print MAIL "Subject: Filter trouble - Could not delete $LOCKFILE.\n";
   close(MAIL);
}

exit; 

# The formatting information for the log file
format LOG=
@<<<<<<<<<<<<<<< @|  @<<<<< @<<<<<<<   @<<<<< @<<<<<<< @#######.## @###
$name, $level, $start ,$time, $finish, $ftime, $MB, $failed
.

# End of File


