大网数据广告活动
服务器运维问答

Rsyslog: From a custom log file, Forward only the messages matching a pattern

· 52CE 我爱测

I want to forward messages matching a pattern ( HELLO in this case) from a custom log file ( /home/ubuntu/test.log ) to a remote rsyslog server. Here is the configuration: # cat /etc/rsyslog.d/05-forwarding.conf *.* @@rsyslogserv…

问题原文

原帖链接 · slayedbylucifer · CC BY-SA 3.0

I want to forward messages matching a pattern (HELLO in this case) from a custom log file (/home/ubuntu/test.log) to a remote rsyslog server.

Here is the configuration:

# cat /etc/rsyslog.d/05-forwarding.conf
*.* @@rsyslogserver.mycompany.com:10514

# cat /etc/rsyslog.d/10-custom.conf
$ModLoad imfile
$InputFilePollInterval 1
$InputFileName /home/ubuntu/test.log
$InputFileTag testlogs:
$InputFileStateFile testlogs
$InputRunFileMonitor
:msg, contains, "HELLO" /var/log/testlog_error.log
& stop
:msg, !contains, "HELLO" stop

Problem:

  1. All messages going to to the /var/log/syslog has stopped. << NOT GOOD
  2. Messages containing HELLO word in /home/ubuntu/test.log are going to /var/log/testlog_error.log as well as are getting forwarded to remote rsyslog server, << GOOD
  3. Messages NOT containing HELLO word in /home/ubuntu/test.log are not going to /var/log/testlog_error.log which is GOOD but these messages are getting forwarded to remote rsyslog server. << BAD

My Ideal situation should be:

  1. System and all other messages should continue to go to /var/log/syslog
  2. No Change here. This is working as expected.
  3. Messages NOT containing HELLO word in /home/ubuntu/test.log should completely get discarded. Don't write to local file as well as don't forward such messages to remote server.

Need help me in solving point 1 and point 3 above.

已采纳解答

原帖链接 · Luiz Guilherme Littig Berger · CC BY-SA 3.0

As it is, your file /etc/rsyslog.d/05-forwarding.conf is forwarding all messages passing thought, and not only messages from your custom file /home/ubuntu/test.log. So, you can delete it, as you said you only want to forward messages from your custom file.

Your file /etc/rsyslog.d/10-custom.conf must looks like:

$ModLoad imfile
$InputFilePollInterval 1
$InputFileName /home/ubuntu/test.log
$InputFileTag testlogs:
$InputFileStateFile testlogs
$InputFileFacility local0
$InputRunFileMonitor

:syslogtag, isequal, "testlogs:" {
  :msg, contains, "HELLO" {
    local0.* /var/log/testlog_error.log
    local0.* @@rsyslogserver.mycompany.com:10514
  }
  stop
}

As you can see, I added the following:

$InputFileFacility local0

Now your custom file will be monitored, and it messages will be written in local0 facility. Messages from local0 will have the tag that you associated to it.

:syslogtag, isequal, "testlogs:"

This rule will only match a message that contains the tag testlogs:, that is exactly the messages you want. So, all other messages will skip it, and be logged in /var/log/syslog and everything else from 50-default.conf.

:msg, contains, "HELLO"

Now, all messages from your file that contains HELLO, will be logged in /var/log/testlog_error.log and forwarded to @@rsyslogserver.mycompany.com:10514.

And then, the "stop" will discard all messages from your file that are being written in local0.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-10-21)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。