####################################################################
# Book-keeper Lite Version 1.0
# Copyright 1998 Edward Preble scripts@datatrendsoftware.com
# Created 12/01/98 Last Modified 12/16/98
# Datatrend Software http://www.datatrendsoftware.com/bookkeeper.html
####################################################################
# This script will do the following once a day:
# 1. Read a list of log files and send them to you over email.
# You can choose to receive one email, or each file mailed separately.
# 2. The script can optionally back up each of the files to a single
# location of choice.
# 3. The script can optionally reset each of the files (clearing them).
####################################################################
# COPYRIGHT NOTICE
# Copyright 1998 Edward Preble All Rights Reserved.
#
# This script is free. You may use and modify this script as you please.
# Do not remove this header, as it is the only thing that gives me credit
# for my work in writing this code. If you wish to sell this code, you
# must obtain permission from the authors first.
#
# Scripts provided by Datatrend Software are supplied "as-is". You agree
# to indemnify the author's from any liability that may arise from the use
# of this script. We will provide support for bugs and operation issues,
# but NOT for installation. Sorry for that, but it is just too time
# consuming and the volume of requests is truly staggering.
#
# Obtain permission before redistributing this software over the Internet or
# in any other medium. In all cases, copyright and header must remain intact
####################################################################
#
# The first subroutine listed here contains the variables that you must
# customize so that the Book-Keeper will work on your system. Do NOT change
# any values after this subroutine. sub read_configs
{
$mailprog = '/usr/lib/sendmail';
$date = '/bin/date';
$config_file = './bookkeeper/bookkeeper.cfg';
$one_email = 'yes';
$email_to = 'you@yoursite.com';
$email_from = 'webmaster@yoursite.com';
$email_subject = 'Book-Keeper Report';
$backup_dir = './backup/';
}
####################################################################
# End of user definitions section. No further changes need to be made.
####################################################################
sub doneyet {
&read_configs;
&get_configs;
open (LAST,"+<./bookkeeper/last.cfg") || &endIt;
flock (LAST,2);
seek (LAST,0,0);
$lastkept = <LAST>;
($lastday,$eol) = split (/\|/,$lastkept);
if ($lastday != $daystamp)
{$lastday = join ("\|",$daystamp,$eol);
seek (LAST,0,0);
print (LAST $lastday);
truncate (LAST,tell(LAST));
&bookkeep;}
close (LAST);
}
sub bookkeep {
$record = 0;
while ($record <= $num)
{if ($option1[$record])
{if ($one_email eq ('yes' or 'YES'))
{&one_email;}
else {&each_email;}
}
if ($option2[$record])
{&backup;}
if ($option3[$record])
{&reset_them;}
$record++;
}
}
#############
sub reset_them {
open (RES,">$location[$record]$filename[$record]") || &endIt;
close (RES);
}
#############
sub backup {
# Read old file
open (TXT,"$location[$record]$filename[$record]") || &endIt;
flock (TXT,2);
seek (TXT,0,0);
@backup_text = <TXT>;
close (TXT);
# chomp @backup_text;
$backup = "@backup_text";
# Create new file, clobbering any present backup.
open (BAK,">$backup_dir$filename[$record]") || &endIt;
flock (TXT,2);
seek (BAK,0,0);
print BAK @backup_text;
close (BAK);
}
#############
sub one_email {
# Add current file to the message body
open (ONE,"$location[$record]$filename[$record]") || &endIt;
flock (ONE,2);
seek (ONE,0,0);
@one_text = 0;
@one_text = <ONE>;
close (ONE);
$body = $body."\n$name[$record] contents:\n"."@one_text";
$complete++;
# If last record, send email.
if ($complete == $num)
{&header;
$body = "Report completed on $timestamp.\n".$body;
print MAIL "$body";
close (MAIL);}
}
#############
sub each_email {
&header;
# Body of email message
print MAIL "$name[$record] contents on $timestamp:\n";
# Read file that will be forwarded and add to body of
message
open (EACH,"$location[$record]$filename[$record]") || &endIt;
flock (EACH,2);
seek (EACH,0,0);
@each_text = <EACH>;
close (EACH);
$body = "@each_text";
print MAIL "$body";
close (MAIL);
}
#############
sub header {
# Header for email
open(MAIL,"|$mailprog -t");
print MAIL "To: $email_to\n";
print MAIL "From: $email_from\n";
print MAIL "Subject: $email_subject\n\n";
}
############
sub get_configs {
open (CFG,"<$config_file") || &endIt;
flock (CFG,2);
seek (CFG,0,0);
@infile = <CFG>;
close (CFG);
$record = 0;
while ($record <= $#infile)
{($name[$record],$location[$record],$filename[$record],$option1[$record],
$option2[$record],$option3[$record],$eol) = split(/\|/,$infile[$record]);
$num += $option1[$record];
$record++;}
chop ($daystamp = `$date +"%e"`); #If the date is
12/06/98
$daystamp =~ s/\s*//g; #6
chop ($hourstamp = `$date +"%H"`); #17
chop ($timestamp = `$date +"%a %D %H%M%Z"`); #Sun 12/06/98 1737EST
chop ($datestamp = `$date +"%Y%m%d"`); #19981206
$yearstamp = substr ($datestamp,0,4); #1998
$monthstamp = substr ($datestamp,4,2); #12
}
#############
sub endIt { exit;}
#############
1; #Return True |