將包含特定字串的訊息記錄到 rsyslogd 中的另一個檔案中

將包含特定字串的訊息記錄到 rsyslogd 中的另一個檔案中

我想透過 rsyslogd 將 iptables 產生的日誌訊息儲存到另一個檔案。

目前我使用以下程式碼/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 日誌很好,它們僅記錄到cron.log,但我的 iptables 日誌同時記錄到syslogfirewall.log。有沒有辦法只能登入firewall.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將始終寫入系統日誌,即使它也匹配:訊息,包含,“[塊”並寫入firewall.log。文件中的停止命令太晚了,無法停止任何操作。

也可以看看https://www.rsyslog.com/writing-specific-messages-to-a-file-and-discarding-them/

相關內容