Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl -w
- # Spamdyke-Stats-Report.pl
- #
- # by Brent Gardner
- #
- # Uses spamdyke-stats script to generate a report about the previous
- # day's Spamdyke activity.
- #
- # Uses qmail-inject to email the report to a specified recipient
- #
- #####################################################################
- use diagnostics;
- use strict;
- use Time::TAI64 qw(:tai);
- use Time::HiRes qw(time);
- use Date::Calc qw(Today Add_Delta_Days);
- use POSIX qw(strftime);
- # This is the machine that's running this script.
- # In my world it's also the machine that generated the SMTP logs that are
- # processed by this script.
- #
- my $MachineName = "smtp.example.com";
- # This is the directory containing this script and the spamdyke-stats script.
- #
- my $ScriptRoot = "/usr/share/qmt/scripts";
- # This is used as a root name for temp files generated by this script.
- # The sample setting implies a directory called 'tmp' in the $ScriptRoot
- # directory.
- #
- my $TempFile = "$ScriptRoot/tmp/spamdyke-stats-report";
- # This is the path to the directory containing the SMTP log files generated
- # by qmail.
- #
- my $SMTPLogRoot = "/var/log/qmail/smtp";
- # Today's date in this format: YYYY/MM/DD
- #
- my $Today = sprintf( "%04d/%02d/%02d", Today() );
- # Yesterday's date in this format: YYYY/MM/DD
- #
- my $Yesterday = sprintf( "%04d/%02d/%02d", Add_Delta_Days( Today(), -1 ) );
- # This is the email address that the report will be sent to.
- # The '@' symbol needs to be escaped.
- #
- my $MailTo = "itbox\@example.com";
- my $LogFilePath = "";
- my $LogFile = "";
- my $LogFileTime = "";
- my $ScriptOutput = "";
- # Open a temporary output file for log content,
- # overwriting any existing content in that file.
- #
- open TempFileRaw, ">$TempFile.raw" or die $!;
- # Process the current qmail SMTP log file
- #
- $LogFilePath = "$SMTPLogRoot/current";
- ProcessQmailSMTPLog($LogFilePath);
- # Look for qmail SMTP log files that are older than current but were
- # modified today or yesterday
- #
- opendir LogDir, $SMTPLogRoot or die $!;
- while ( $LogFile = readdir(LogDir) ) {
- # Only look at files that are probably qmail log files
- #
- next unless ( $LogFile =~ /\.[su]$/ );
- # Only look at log files where the modified date is today or yesterday
- #
- $LogFileTime = strftime "%Y/%m/%d", localtime((stat("$SMTPLogRoot/$LogFile"))[9]);
- next unless ( ( $LogFileTime eq $Yesterday ) or ( $LogFileTime eq $Today ) );
- # Process targeted log files. (entries that match yesterday's
- # date are copied to the temp output file)
- #
- ProcessQmailSMTPLog("$SMTPLogRoot/$LogFile");
- }
- closedir LogDir;
- close TempFileRaw;
- # Prepare an email message to be sent using qmail-inject
- #
- open MailBody, ">$TempFile.body" or die $!;
- # Begin the message.
- #
- print MailBody "To: <$MailTo>\n";
- print MailBody "subject: $MachineName Spamdyke Stats Report for $Yesterday\n\n";
- print MailBody "This report contains statistical information about messages that were \n";
- print MailBody "rejected by Spamdyke on $MachineName because of RBL lookups or misbehaved \n";
- print MailBody "remote SMTP server.\n\n";
- print MailBody "These messages were rejected before $MachineName acknowleged receipt of the \n";
- print MailBody "message. This means that the sending server is responsible for generating \n";
- print MailBody "any Non-Delivery Report.\n\n";
- print MailBody "This also means that SpamAssassin did not have to process these messages. \n";
- print MailBody "This saves a lot of processing power. SpamAssassin checks can be CPU, \n";
- print MailBody "memory and network-intensive.\n\n";
- print MailBody "This report was generated ";
- print MailBody localtime() . "\n\n\n";
- # Add info from the spamdyke-stats script.
- #
- $ScriptOutput = `cat $TempFile.raw | $ScriptRoot/spamdyke-stats`;
- print MailBody "$ScriptOutput\n\n\n";
- print MailBody "Path to this script: $ScriptRoot/Spamdyke-Stats-Report.pl\n";
- close MailBody;
- # Mail the report using qmail-inject.
- #
- system("/var/qmail/bin/qmail-inject -f$MachineName < $TempFile.body");
- # This sub will copy log entries that match yesterday's date into the temp
- # file.
- #
- sub ProcessQmailSMTPLog {
- my $Path = shift;
- my $TimeStamp = "";
- open SMTPLog, "<", $Path or die $!;
- while (<SMTPLog>) {
- my $Line = $_;
- # log file lines that don't start with '@' will be ignored.
- #
- if ( substr($Line,0,1) eq '@' ) {
- $TimeStamp = substr($Line,0,25);
- # if the date in the timestamp is yesterday then it is copied to
- # the temp file.
- #
- if ( tai2strftime($TimeStamp,"%Y/%m/%d") eq $Yesterday ) {
- print TempFileRaw $Line;
- }
- }
- }
- close SMTPLog;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement