Guest User

Spamdkye-Stats-Report.pl

a guest
Oct 24th, 2011
769
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/perl -w
  2.  
  3. # Spamdyke-Stats-Report.pl
  4. #
  5. # by Brent Gardner
  6. #
  7. # Uses spamdyke-stats script to generate a report about the previous
  8. # day's Spamdyke activity.
  9. #
  10. # Uses qmail-inject to email the report to a specified recipient
  11. #
  12. #####################################################################
  13.  
  14. use diagnostics;
  15. use strict;
  16. use Time::TAI64 qw(:tai);
  17. use Time::HiRes qw(time);
  18. use Date::Calc  qw(Today Add_Delta_Days);
  19. use POSIX       qw(strftime);
  20.  
  21.  
  22. # This is the machine that's running this script.
  23. # In my world it's also the machine that generated the SMTP logs that are
  24. # processed by this script.
  25. #
  26. my $MachineName = "smtp.example.com";
  27.  
  28.  
  29. # This is the directory containing this script and the spamdyke-stats script.
  30. #
  31. my $ScriptRoot = "/usr/share/qmt/scripts";
  32.  
  33.  
  34. # This is used as a root name for temp files generated by this script.
  35. # The sample setting implies a directory called 'tmp' in the $ScriptRoot
  36. # directory.
  37. #
  38. my $TempFile = "$ScriptRoot/tmp/spamdyke-stats-report";
  39.  
  40.  
  41. # This is the path to the directory containing the SMTP log files generated
  42. # by qmail.
  43. #
  44. my $SMTPLogRoot = "/var/log/qmail/smtp";
  45.  
  46.  
  47. # Today's date in this format: YYYY/MM/DD
  48. #
  49. my $Today = sprintf( "%04d/%02d/%02d", Today() );
  50.  
  51.  
  52. # Yesterday's date in this format: YYYY/MM/DD
  53. #
  54. my $Yesterday = sprintf( "%04d/%02d/%02d", Add_Delta_Days( Today(), -1 ) );
  55.  
  56.  
  57. # This is the email address that the report will be sent to.
  58. # The '@' symbol needs to be escaped.
  59. #
  60. my $MailTo = "itbox\@example.com";
  61.  
  62.  
  63. my $LogFilePath  = "";
  64. my $LogFile      = "";
  65. my $LogFileTime  = "";
  66. my $ScriptOutput = "";
  67.  
  68.  
  69. # Open a temporary output file for log content,
  70. # overwriting any existing content in that file.
  71. #
  72. open TempFileRaw, ">$TempFile.raw" or die $!;
  73.  
  74.  
  75. # Process the current qmail SMTP log file
  76. #
  77. $LogFilePath = "$SMTPLogRoot/current";
  78.  
  79. ProcessQmailSMTPLog($LogFilePath);
  80.  
  81.  
  82. # Look for qmail SMTP log files that are older than current but were
  83. # modified today or yesterday
  84. #
  85. opendir LogDir, $SMTPLogRoot or die $!;
  86.  
  87. while ( $LogFile = readdir(LogDir) ) {
  88.  
  89.     # Only look at files that are probably qmail log files
  90.     #
  91.     next unless ( $LogFile =~ /\.[su]$/ );
  92.  
  93.     # Only look at log files where the modified date is today or yesterday
  94.     #
  95.     $LogFileTime = strftime "%Y/%m/%d", localtime((stat("$SMTPLogRoot/$LogFile"))[9]);
  96.     next unless ( ( $LogFileTime eq $Yesterday ) or ( $LogFileTime eq $Today ) );
  97.  
  98.     # Process targeted log files. (entries that match yesterday's
  99.     # date are copied to the temp output file)
  100.     #
  101.     ProcessQmailSMTPLog("$SMTPLogRoot/$LogFile");
  102.  
  103. }
  104.  
  105. closedir LogDir;
  106.  
  107.  
  108. close TempFileRaw;
  109.  
  110.  
  111. # Prepare an email message to be sent using qmail-inject
  112. #
  113. open MailBody, ">$TempFile.body" or die $!;
  114.  
  115.  
  116. # Begin the message.
  117. #
  118. print MailBody "To: <$MailTo>\n";
  119. print MailBody "subject: $MachineName Spamdyke Stats Report for $Yesterday\n\n";
  120. print MailBody "This report contains statistical information about messages that were \n";
  121. print MailBody "rejected by Spamdyke on $MachineName because of RBL lookups or misbehaved \n";
  122. print MailBody "remote SMTP server.\n\n";
  123. print MailBody "These messages were rejected before $MachineName acknowleged receipt of the \n";
  124. print MailBody "message.  This means that the sending server is responsible for generating \n";
  125. print MailBody "any Non-Delivery Report.\n\n";
  126. print MailBody "This also means that SpamAssassin did not have to process these messages.  \n";
  127. print MailBody "This saves a lot of processing power.  SpamAssassin checks can be CPU, \n";
  128. print MailBody "memory and network-intensive.\n\n";
  129. print MailBody "This report was generated ";
  130. print MailBody localtime() . "\n\n\n";
  131.  
  132. # Add info from the spamdyke-stats script.
  133. #
  134. $ScriptOutput = `cat $TempFile.raw | $ScriptRoot/spamdyke-stats`;
  135.  
  136. print MailBody "$ScriptOutput\n\n\n";
  137.  
  138.  
  139. print MailBody "Path to this script: $ScriptRoot/Spamdyke-Stats-Report.pl\n";
  140.  
  141.  
  142. close MailBody;
  143.  
  144.  
  145. # Mail the report using qmail-inject.
  146. #
  147. system("/var/qmail/bin/qmail-inject -f$MachineName < $TempFile.body");
  148.  
  149.  
  150. # This sub will copy log entries that match yesterday's date into the temp
  151. # file.
  152. #
  153. sub ProcessQmailSMTPLog {
  154.  
  155.     my $Path      = shift;
  156.     my $TimeStamp = "";
  157.  
  158.     open SMTPLog, "<", $Path or die $!;
  159.  
  160.     while (<SMTPLog>) {
  161.  
  162.         my $Line = $_;
  163.  
  164.         # log file lines that don't start with '@' will be ignored.
  165.         #
  166.         if ( substr($Line,0,1) eq '@' ) {
  167.  
  168.             $TimeStamp = substr($Line,0,25);
  169.  
  170.             # if the date in the timestamp is yesterday then it is copied to
  171.             # the temp file.
  172.             #
  173.             if ( tai2strftime($TimeStamp,"%Y/%m/%d") eq $Yesterday ) {
  174.                 print TempFileRaw $Line;
  175.             }
  176.  
  177.         }
  178.  
  179.     }
  180.  
  181.     close SMTPLog;
  182.  
  183. }
  184.  
  185.  
  186.  
  187.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×