logwatchでpostfixのログをパースさせてたんですが、最近Unmatched Entriesが増えてきたので、
Unmatchedを減らすべく、既存のフィルタを改造してみました。
対象にしたのはこれ。
# rpm -q logwatch
logwatch-7.3-9.el5_6
オリジナルを直接触るのは気が引けるので、ローカル用にコピーします。
# cp -p /usr/share/logwatch/scripts/services/postfix /etc/logwatch/scripts/services/
# vi /etc/logwatch/scripts/services/postfix
というわけで、オリジナル版のフィルタとdiffを取ってみました。
# diff -u /usr/share/logwatch/scripts/services/postfix /etc/logwatch/scripts/services/postfix
--- /usr/share/logwatch/scripts/services/postfix 2011-03-31 06:57:37.000000000 +0900
+++ /etc/logwatch/scripts/services/postfix 2013-03-02 10:37:16.000000000 +0900
@@ -224,7 +224,7 @@
} elsif (($User) = ($ThisLine =~ /^[a-zA-Z0-9]+: to=\<([^ ]*)>,(?: orig_to=\<(?:[^ ]*)>,)? .*, status=bounced .*: User unknown in virtual (alias|mailbox) table/)) {
# another unknown user probably could combine with local unknown but again my perl is weak
$UnknownUsers{$User}++;
- } elsif (($Dest, $Relay, $Msg) = ($ThisLine =~ /^[a-zA-Z0-9]+: to=\<([^ ]*)>,(?: orig_to=\<(?:[^ ]*)>,)? relay=([^ ]*).*, delay=-?[0-9]+, status=bounced \(([^)]*)/ )) {
+ } elsif (($Dest, $Relay, $Msg) = ($ThisLine =~ /^[a-zA-Z0-9]+: to=\<([^ ]*)>,(?: orig_to=\<(?:[^ ]*)>,)? relay=([^ ]*).*, delay=[^ ]*,(?: delays=[^ ]*,)?(?: dsn=[^ ]*,)? status=bounced \(([^)]*)/ )) {
# unknown user
# $Msg = " hello "
# print "bounce message from " . $Dest . " msg : " . $Relay . "\n";
@@ -394,6 +394,10 @@
$UnknownWarnings{$Warn}++;
} elsif (($Host,$Filter) = ($ThisLine =~ /RCPT from ([^ ]*\[[^ ]*\]): <[^ ]*\[[^ ]*\]>: Client host triggers FILTER (\S*)/)) {
$HostTrigFilter{"$Host,$Filter"}++;
+ } elsif ( ($Server, $Helo) = ($ThisLine =~ /^NOQUEUE: reject: RCPT from ([^ ]*\[[^ ]*\]): 450 4.7.1 Client host rejected: cannot find your hostname, [^ ]*; from=<[^ ]*> to=<[^ ]*> proto=[^ ]* helo=<([^ ]*)>$/)) {
+ $InvalidClientHostName{"$Server"}++;
+ } elsif ( ($Server, $Helo) = ($ThisLine =~ /^NOQUEUE: reject: RCPT from ([^ ]*\[[^ ]*\]): 504 5.5.2 <[^ ]*>: Helo command rejected: need fully-qualified hostname; from=<[^ ]*> to=<[^ ]*> proto=[^ ]* helo=<([^ ]*)>$/)) {
+ $InvalidHeloName{"RCPT from $Server: HELO=$Helo"}++;
} else {
push @OtherList,$ThisLine;
}
@@ -691,6 +695,40 @@
}
}
+if (keys %InvalidClientHostName) {
+ if($Detail >= 5) {
+ print "\n\nInvalid client hostname:\n";
+ foreach $Message (sort {$a cmp $b} keys %InvalidClientHostName) {
+ print " $Message: $InvalidClientHostName{$Message} Time(s)\n";
+ }
+ }
+ else {
+ $n=0;
+ $mn=scalar(keys %InvalidClientHostName);
+ foreach $Message (keys %InvalidClientHostName) {
+ $n+=$InvalidClientHostName{$Message};
+ }
+ print "\n\nInvalid client hostname:\n: $mn Message(s), $n Time(s)";
+ }
+}
+
+if (keys %InvalidHeloName) {
+ if($Detail >= 5) {
+ print "\n\nInvalid HELO command:\n";
+ foreach $Message (sort {$a cmp $b} keys %InvalidHeloName) {
+ print " $Message: $InvalidHeloName{$Message} Time(s)\n";
+ }
+ }
+ else {
+ $n=0;
+ $mn=scalar(keys %InvalidHeloName);
+ foreach $Message (keys %InvalidHeloName) {
+ $n+=$InvalidHeloName{$Message};
+ }
+ print "\n\nInvalid HELO command:\n: $mn Message(s), $n Time(s)";
+ }
+}
+
if (keys %LocalBounce) {
if ($Detail >= 5) {
print "\n\nLocal Bounce:\n";
各chunkのコメントです。
1つめ
bounceした記録の書式が多少変わっていたので、オリジナルの機能を失わないように正規表現を書き足してみました。
2つめ
postfixで下記の設定をした時に出るエラーに対応してみました。
smtpd_client_restrictions = reject_unknown_client
smtpd_helo_restrictions = reject_non_fqdn_hostname
3つめ
2つめでマッチしたログの出力部分です。
/etc/logwatch配下に置いたスクリプトは標準のスクリプトに優先して使われるようで、
期待通りの動作となりました。
0 件のコメント:
コメントを投稿