【问题标题】:Pass comand line parameters to shell script via omprog (rsyslog module)通过 omprog(syslog 模块)将命令行参数传递给 shell 脚本
【发布时间】:2016-03-12 07:04:09
【问题描述】:

通过 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): $1" >> /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\"")

我在系统日志中获得了 logger 参数,但在 /home/user/sink 中没有。 已经在这个问题上停留了一整天,非常感谢任何帮助。

【问题讨论】:

    标签: linux shell logging rsyslog


    【解决方案1】:

    关于无法动态生成的动作参数的更多信息 -- 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.
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 似乎我被你提到的那个文档中的例子误导了。无论如何,当我调整我的 shell 脚本以从标准输入读取时,即使我创建了我的模板,也没有任何改变。
      猜你喜欢
      • 2018-04-02
      • 2012-06-19
      • 2017-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-23
      • 2012-10-01
      • 2015-01-21
      相关资源
      最近更新 更多