[Box Backup-dev] BoxReport.pl (was ChrisMerge_1828)

Matt Brown boxbackup-dev@fluffy.co.uk
Fri, 21 Sep 2007 09:39:23 +0100


--Apple-Mail-2--544117263
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
	charset=US-ASCII;
	delsp=yes;
	format=flowed

Hi Dave,

>> P.S If anyone is interested in the boxreport.pl script (its a  
>> rehash  of the sa-stats.pl script for spamassassin) they are  
>> welcome :-)
>
> Yes I would like to try out this script to see what it does. It  
> lets the clients know something is going on.

Here is the first version I rushed together, I am sure it can be made  
a lot nicer and more streamlined - I am not great with perl, but does  
the trick for me. I am looking to put an array in for the bits to  
capture rather than lots of if statements, sort out the reporting  
dates etc...

Anyway, the usual - if it breaks, deletes anything etc - use at own  
risk applies :-)

I will be making some changes to this script and will post back to  
the list when done.

Regards

Matt Brown


--Apple-Mail-2--544117263
Content-Transfer-Encoding: 7bit
Content-Type: text/x-perl-script;
	x-unix-mode=0644;
	name=boxreport.pl
Content-Disposition: attachment;
	filename=boxreport.pl

#!/usr/bin/perl

# boxreport.pl
# Purpose     : To report all files that have been sent to a BoxBackup server from a syslog based file.
# Author      : Matt Brown <matt@mbrown.co.uk> 14th September 2007   
# Code based upon the sa-stats.pl script written for SpamAssassin.

use Parse::Syslog;
use Getopt::Long;
use Pod::Usage;
use Date::Manip;
use POSIX qw/strftime floor/;
use Time::Local;
use strict;

# Configuration section
my %opt = ();
$opt{'logfile'} = '/var/log/box';                                       # Default Log file
$opt{'sendmail'} = '/usr/sbin/sendmail';                                # Path to sendmail stub
$opt{'from'} = 'Box Backup Admin <box@example.com>';                # Who is the mail from
$opt{'mail'} = 'user@example.com';                                  # Email address to send report to

##################################################################################################################

# Set vars to satisfy Use Strict;

my ($start,$end,$bbstart,$bbend,$seconds,$minutes,$hours,
        $date,$f1,$f2,$s1,$s2,$s3,$data,$summary);

# Get options

Getopt::Long::Configure("bundling");
        GetOptions('logfile|l=s'  => \$opt{'logfile'},
        'debug|D'      => \$opt{'debug'},
        'mail=s'       => \$opt{'mail'},
        'sendmail=s'   => \$opt{'sendmail'},
        'from=s'       => \$opt{'from'},
        'help|h'       => \$opt{'help'},
        'date=s'          => \$opt{'date'})
        or pod2usage({-verbose => 0, -message => "Unknown options.", -exitval => 2});

# Get report period, default to today if not set.

if (!$opt{'date'}){
        $start = timelocal($seconds, $minutes, $hours, (localtime)[3,4,5]);
        $end = $start+86400;
}

# show help on usage

if ($opt{'help'}) {
        pod2usage();
}

# Now start getting the relevant bits from
# log file we want .. very messy but works
# (but only if extended logging enabled)

my $parser = Parse::Syslog->new( $opt{'logfile'} );
        while (my $sl = $parser->next) {

if ( $sl->{'timestamp'} >= $start && $sl->{'timestamp'} <= $end) {

        if ($sl->{'text'} =~ /\bbackup-start\b/i){
        &dbg($sl->{'timestamp'} . ' ' . $sl->{'host'} . ' ' . $sl->{'program'} . ' ' . $sl->{'pid'} . ' ' . $sl->{'text'} . "\n");
        $date = UnixDate(ParseDate("epoch $sl->{'timestamp'}"), '%d/%m/%y %H:%M:%S');
        $bbstart = $date;
        }

        if ($sl->{'text'} =~ /\bStoreFile\b/i){
        &dbg($sl->{'timestamp'} . ' ' . $sl->{'host'} . ' ' . $sl->{'program'} . ' ' . $sl->{'pid'} . ' ' . $sl->{'text'} . "\n");
        ($f1, $f2) = split ('"',$sl->{'text'});
        $data .=  $f2 ."\n";
        }

        if ($sl->{'text'} =~ /\bFile statistics\b/i){
        &dbg($sl->{'text'});
        ($s1, $s2, $s3) = split (':', $sl->{'text'});
        $summary = &ltrim($s2) . ":" . $s3;
        }

        if ($sl->{'text'} =~ /\bbackup-finish\b/i){
        &dbg($sl->{'timestamp'} . ' ' . $sl->{'host'} . ' ' . $sl->{'program'} . ' ' . $sl->{'pid'} . ' ' . $sl->{'text'} . "\n");
        $date = UnixDate(ParseDate("epoch $sl->{'timestamp'}"), '%d/%m/%y %H:%M:%S');
        $bbend = $date;
        }
  }
}

# Perl does not seem to have a trim function ?
# so this does the trick for a left trim :-)

sub ltrim($){
        my $string = shift;
        $string =~ s/^\s+//;
        return $string;
}

# Common for all debug info

sub dbg {
        print STDERR @_ if ($opt{debug});
}


# Build Report - here we can add later maybe
# html or something else more creative PDF ?

 my $rpt = '';
 $rpt = &build_text_report();

# Send report via email

if ($opt{'mail'}) {
        open (SENDMAIL, "|$opt{'sendmail'} -oi -t -odb") or die "Can't open sendmail: $!\n";
        print SENDMAIL "From: $opt{'from'}\n";
        print SENDMAIL "To: $opt{'mail'}\n";
        print SENDMAIL "Subject: Box Backup Report\n\n";
        print SENDMAIL $rpt;
        close (SENDMAIL);
   } else {
        print $rpt;
}

#All done
exit 0;

sub build_text_report {
my $rpt = '';
    $rpt .=         "------------------------------------------------------------------------\n";
    $rpt .=         "Report Title       : Box Backup - Backup Statistics\n";
    $rpt .=         "Report Date        : " . strftime("%d-%m-%Y", localtime) . "\n";
    $rpt .=         "Backup Start       : " . $bbstart . "\n";
    $rpt .=         "Backup Finish      : " . $bbend . "\n";
    $rpt .=         "------------------------------------------------------------------------\n\n";
    $rpt .=         "Files sent during this backup:\n\n";
    $rpt .=         $data . "\n";
    $rpt .=         "-----------------------------------------------------------------------------------------------------------------------------------------------------------\n";
    $rpt .=         $summary . "\n";
    $rpt .=         "------------------------------------------------------------------------------------------------------------------------------------------------------------\n";
    return $rpt;
}

__END__

=head1 NAME

boxreport.pl - Reports list of files submitted to Box Backup server from a chosen logfile

=head1 VERSION

    $Revision: 0.1 $

=head1 SYNOPSIS

 Usage: boxreport.pl [options]

 Options:
   -l, --logfile=filename       logfile to read
                                (default: /var/log/box)
   -h, --help                   Displays this message
   -D, --debug                  Sets debug mode
   --mail=emailaddress          Sends report to emailaddress
   --sendmail=/path/to/sendmail Location of sendmail binary
                                (default: /usr/sbin/sendmail)
   --from=emailaddress          Sets From: field of email
   --date=report_date           i.e 01/01/1970
=cut

--Apple-Mail-2--544117263--