特定の文字列を含むメッセージをrsyslogdの別のファイルに記録する

特定の文字列を含むメッセージをrsyslogdの別のファイルに記録する

iptables によって生成されたログ メッセージを rsyslogd 経由で別のファイルに保存したい。

現在、私は次のコードを使用しています/etc/rsyslog.d/20-custom.conf:

# Log cron to cron.log and not to syslog
*.*;cron,auth,authpriv.none         -/var/log/syslog
cron.*                               /var/log/cron.log

# Log firewall to extra log
:msg,contains,"[BLOCK " /var/log/firewall.log

& stop

私の cron ログは問題なく、 にのみ記録されますが、 iptables ログはとcron.logの両方に記録されます。 にのみ記録する方法はありますか?syslogfirewall.logfirewall.log

私のシステム仕様:

> rsyslogd -v
rsyslogd 8.16.0, compiled with:
        PLATFORM:                               x86_64-pc-linux-gnu
        PLATFORM (lsb_release -d):
        FEATURE_REGEXP:                         Yes
        GSSAPI Kerberos 5 support:              Yes
        FEATURE_DEBUG (debug build, slow code): No
        32bit Atomic operations supported:      Yes
        64bit Atomic operations supported:      Yes
        memory allocator:                       system default
        Runtime Instrumentation (slow code):    No
        uuid support:                           Yes
        Number of Bits in RainerScript integers: 64

Linux MYHOSTNAME 4.4.0-116-generic #140-Ubuntu SMP Mon Feb 12 21:23:04 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

> lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 16.04.4 LTS
Release:        16.04
Codename:       xenial

答え1

ファイアウォールのログ部分を他のルールの前に移動して、次のようになります。

# Log firewall to extra log
:msg,contains,"[BLOCK " /var/log/firewall.log
& stop

# Log cron to cron.log and not to syslog
*.*;cron,auth,authpriv.none         -/var/log/syslog
cron.*                               /var/log/cron.log

ルールは順番に考慮されるので、今のようにメッセージに一致する*.*;cron、auth、authpriv.none常にsyslogに書き込まれます。:msg,「"[BLOCK "」が含まれていますそして、firewall.log に書き込まれます。停止コマンドは、ファイル内では遅すぎるため、何も停止できません。

参照https://www.rsyslog.com/writing-specific-messages-to-a-file-and-discarding-them/

関連情報