TL; DR – 使用rsyslog发送特定日志(到redis服务器):如何select要发送的日志?
我想转发到一个redis服务器的一组( 只有那组 )日志,例如说nginx日志在/var/log/nginx/*.log 。 为此,我正在考虑使用rsyslog 工具 (本例中为local7)。 但是:
local7.*日志到redis服务器:我收到系统的所有日志(auth,authpriv,cron,local7等等) /var/log/nginx/*.log log/nginx/*.log将无法正常工作,但/var/log/nginx/some-access.log将通过rsyslog发送到我的redis服务器。得到一个目录的所有日志呢?) configuration有三个模块在使用,并以这种方式将local7的日志发送到我的redis服务器:
local7.* @redis_ip:port $ModLoad imuxsock # provides support for local system logging $ModLoad omhiredis # support for sending to Redis $ModLoad imfile # For tailing files
另外两个代码块是两种不同的configurationrsyslog的方法。
configuration1(通用configuration的人build议):
$InputFileName /var/log/nginx/*.log $InputFileTag nginx $InputFileFacility local7 $InputRunFileMonitor
configuration2(不同的语法 – 我发现rsyslog文档的版本8.16.0):
input( type="imfile" File="/var/log/nginx/*.log" Tag="nginx:" Facility="local7" )
输出到redis:
action( name="rsyslog_redis" type="omhiredis" mode="queue" key="rsyslog_redis_key" template="jsonlines" # use a JSON template defined below )
如果我简单地删除configuration1或configuration2,并使用authpriv.* @redis_ip:port为例,我仍然会得到所有的日志(所以日志从设施 syslog,cron,auth,authpriv等),如同authpriv。* authpriv.* @redis_ip:port对authpriv.* @redis_ip:port没有影响。
我使用/usr/local/sbin/rsyslog -f /etc/rsyslog.conf启动rsyslog,并用选项-N1检查它是否正确。
我查过的问题对我没有任何改变:
由于我终于有了一些工作,可能我知道我的错误在哪里,这是对我自己的问题的答案:
$ModLoad imuxsock # provides support for local system logging $ModLoad omhiredis # support for sending to Redis $ModLoad imfile # For tailing files if $syslogtag == "nginx:" then { action( name="rsyslog_redis" server="redis_ip" port="redis_port" type="omhiredis" mode="queue" key="rsyslog_redis" # we need the same key in Logstash's config template="jsonlines" # use the JSON template defined below ) } input( type="imfile" File="/var/log/nginx/access.log" Tag="nginx:" )
允许我发送nginx的access.log,只有那些我的redis服务器。
我的错误来自不受if语句限制的块操作(..) 。 因此它发送所有的日志。 这解释了我的第一个注意事项authpriv.* @redis_ip:port不会改变任何东西。
答案: action(..)会发送日志,即使没有定义服务器,所以通过if语句来包围它,以select哪些日志被转发。
注意:我没有添加模板“jsonlines”到configuration,因为人们可以很容易地在互联网上find它,并且这里需要一些不必要的空间。