通过 omprog(syslog 模块)将命令行参数传递给 shell 脚本

Pass comand line parameters to shell script via omprog (rsyslog module)

通过 rsyslog 模块 omprog 将系统日志消息作为命令行参数传递给 shell 脚本时遇到了麻烦。

我的 /etc/rsyslog.conf:

module(load="omprog")
if $syslogtag contains 'user'
then action(type="omprog" binary="/usr/bin/test")

我的/usr/bin/test:

#!/bin/sh
printf "\n$(date): " >> /home/user/sink

在 运行

logger qqq

我在 /var/log/messages 中得到 'qqq',在 /home/user/sink 中得到当前时间戳。
在 运行

/usr/bin/test some_message

我进入 /home/user/sink 当前时间戳以及 'some_message'。但是一旦我将 /etc/rsyslog.conf 中的操作字符串修改为
或者

then action(type="omprog" binary="/usr/bin/test some_message")

then action(type="omprog" binary="/usr/bin/test \"$msg\"")

我在系统日志中获取了记录器参数,但在 /home/user/sink 中没有任何内容。 已经在这个问题上卡了一整天了,非常感谢任何帮助。

据我了解rsyslog's documentation,二进制只是一个文字值,因此不会根据 rsyslog 变量对其进行评估。二进制 - 你 program/commandline - 将通过 stdin 提供,当然你可以更改该操作的模板以仅包含 $msg,使用操作的 template 参数。

有关无法动态生成的动作参数的更多信息 -- https://github.com/rsyslog/rsyslog/issues/1149

--

rgerhards commented on Sep 21, 2016

The key problem is: what shall the action do if a parameter changes? e.g. shut
down existing connection and create a new one? If so, shall it really do this 
if parameters frequently change?

IMHO to handle this decently, the plugin itself must handle these kinds of 
dynamic properties and act accordingly. So there is no generic solution possible.