#!/usr/bin/perl
use strict;

#
# $Id: maillogs,v 4.1 2004/11/16 21:20:01 matt Exp $
#

use vars qw( $VERSION );

$VERSION = "4.00";

=head1 NAME

Mail::Logs - A log file processer for mail server logs

=head1 SYNOPSIS

A script that maintains ever increasing counters for mail logs and functions as a postprocessor for multilog. Currently handles log counting of the following types:

	qmail mail delivery (send)
	qmail-pop3d         (pop3) (edit file to set pop3 daemon)

	courier pop3d       (pop3) (edit file to set pop3 daemon)
	courier pop3dssl    (pop3)
	courier imapd       (imap)
	courier imapdssl    (imap)

	vpopmail smtp-auth  (smtp)

	sqwebmail           (webmail)
	squirrelmail        (webmail)

	rblsmtpd            (rbl)

	spamasssassin       (spamassassin)
	qmailscanner        (qmailscanner)

This script and modules is (at last) easily extendible as I continue to find new stats on my mail system that I want to count and graph.


=head1 DESCRIPTION

I have "farms" of mail servers that share a hostname. I needed 
a way to collect the logs from all the servers and process them.

This is a piece of that puzzle. Maillogs does several things. First,
it acts as a simple log postprocessor. By default it takes files that
qmail outputs (/var/log/mail/[send|smtp|pop3]/current) and collects 
a bunch of counters about the logs and stores them in counter files.

For smtp logs we count denied RBL connections (rblsmtpd). 

For qmail-send we log a slew of message send statistics. 

For pop3, IMAP, webmail, and SMTP-AUTH, we count  the number of 
connections and the number of successful logins.

Counters are written to files in /var/log/mail/counters/ by default.
You can alter the locations of the logs files by editing the 
toaster.conf file included with this script. That file should be
installed in /usr/local/etc.

=cut


# ------------------------------------------------------------------------------

use lib "lib";
use Mail::Toaster::Perl 1;    my $perl = new Mail::Toaster::Perl;
use Mail::Toaster::Logs;      my $logs = new Mail::Toaster::Logs;
use Mail::Toaster::Utility 1; my $utility = new Mail::Toaster::Utility;

use File::Path;
use Getopt::Std;
use POSIX;

$perl->module_load( {module=>"Date::Parse",  ports_dir=>"p5-TimeDate", ports_group=>"devel"} );
$perl->module_load( {module=>"Date::Format", ports_dir=>"p5-TimeDate", ports_group=>"devel"} );
 
use vars qw( $opt_r $opt_v %count %spam );
getopts('rv');

my $conf = $utility->parse_config( { file=>"toaster.conf", debug=>$opt_v, etcdir=>'' } );

$|++;

$logs->CheckSetup($conf);

my $iam = $logs->what_am_i();

if    ( $iam eq "maillogs" ) { $logs->CheckForFlags($conf,$ARGV[0],$opt_v) }
elsif ( $iam eq "sendlog"  ) { $logs->RollSendLogs($conf, $opt_v ) } 
elsif ( $iam eq "smtplog"  ) { $logs->RollRblLogs ($conf, $opt_v ) } 
elsif ( $iam eq "pop3log"  ) { $logs->RollPOP3Logs($conf, $opt_v ) } 
else                         { print "not available.\n";    };

0;
__END__

=head2 what_am_i

what_am_i - Is this a schizophrenic program?

No, because schizophrenic denotes a disorder. This application is designed
to behave differently depending on how it's called. It has (currently) four
 distinct personalities. Allow me to introduce you to multilog.

The multilog program (http://cr.yp.to/daemontools/multilog.html)
has a poorly documented ability to run a postprocessor on logs
after they reach a predetermined size (or are otherwise triggered).

This script is designed to be that postprocessor. If you rename this file to
be named one of "smtplog", "pop3log", or "sendlog" it will become a log
file processor for that qmail server protocol. See the maillogs FAQ for 
more details.

If called as maillogs, it will return ever incrementing counters for the 
protocol you entered. Run maillogs without any parameters to see the supported
options. It's expected that you'll be calling maillogs from an SNMP agent 
allowing this solution to scale from single to clustered systems.


=head1 INSTALLATION

To install this module type the following:

	perl Makefile.PL
	make
	make test
	make install

To install as a post-processor for qmail type the following:

	make qmail


=head1 DEPENDENCIES

This program requires these other modules and libraries:

Mail::Toaster - http://www.tnpi.biz/computing/mail/toaster/

=head1	Design considerations

=over 4

=item * Counters will be polled via SNMP. Script must be able to return counts instantly, even when dealing with HUGE logs.

=item * Must work with multilog and syslog logging formats

=item * Outputs data in a format suitable for polling via SNMP

=item * Simple configuration

=item * Fail safe, errors must be noticed and reported but not fatal

=back

=cut

=head1 AUTHOR

Matt Simerson <matt@tnpi.biz>

=head1 BUGS

None known. Report any to author.

=head1 SEE ALSO

http://www.tnpi.biz/internet/mail/

=head1 COPYRIGHT

Copyright (c) 2004, The Network People, Inc.
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

Neither the name of the The Network People, Inc. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

=cut
