【问题标题】:rsyslog not escaping backslash in JSONrsyslog 没有在 JSON 中转义反斜杠
【发布时间】:2019-01-29 04:15:08
【问题描述】:

我有一个 rsyslogd 实例正在运行,从 syslog 生成以下 JSON:

{"timegenerated":"2019-01-28T09:24:37.033990+00:00","type":"syslog","host":"REDACTED_HOSTNAME","host-ip":"REDACTED_IP","message":"<190>Jan 28 2019 10:24:35: %ASA-X-XXXXXX: Teardown TCP connection 82257709 for outside:REDACTED_IP\/REDACTED_PORT(LOCAL\ususername) to inside:REDACTED_IP\/REDACTED_PORT duration 0:01:52 bytes XXXX TCP FINs from outside (ususername)"}

这是无效的 JSON,因为 \ususe 被解释为 unicode 符号的十六进制表示。它应该被转义为\\ususe

我在 GitHub 上注意到有一个未解决的问题 (https://github.com/rsyslog/rsyslog/issues/1235),尽管它提到了另一个导致合并修复的问题。

这是一些系统信息:

:~# rsyslogd -version
rsyslogd 8.24.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

:~# lsb_release  -a
No LSB modules are available.
Distributor ID: Debian
Description:    Debian GNU/Linux 9.4 (stretch)
Release:    9.4
Codename:   stretch

用于创建 JSON 文档的模板是:

template(name="json_syslog"
  type="list") {
  constant(value="{")
    constant(value="\"timegenerated\":\"") property(name="timegenerated"    dateFormat="rfc3339")
    constant(value="\",\"type\":\"syslograw")
    constant(value="\",\"host\":\"") property(name="fromhost")
    constant(value="\",\"host-ip\":\"") property(name="fromhost-ip")
    constant(value="\",\"message\":\"") property(name="rawmsg" format="jsonr")
  constant(value="\"}\n")

rsyslog 中是否有任何功能可以让我修复此问题,或者它看起来像是上游错误?

【问题讨论】:

    标签: rsyslog


    【解决方案1】:

    我注意到您在消息模板中使用了format="jsonr"。如果您使用json 而不是jsonr 会有所不同,文档非常简要地描述了这一点,因为避免了双重转义值。使用模板与

    constant(value="\",\n\"json\":\"") property(name="rawmsg" format="json")
    constant(value="\",\n\"jsonr\":\"") property(name="rawmsg" format="jsonr")
    

    并提供包含

    的输入
    LOCAL\ususer "abc" 
    

    产生两行

    "json":"LOCAL\\ususer \"abc\",
    "jsonr":"LOCAL\ususer \"abc\",
    

    其中json 格式已将\u 转义为\\u(使用rsyslog-8.27.0 测试)。 如果这不适合您,您可以随时操作消息,例如如下,在您的操作之前添加:

    set $.msg2 = replace($rawmsg, "\\u", "\\\\u");
    

    并在您的模板中使用

    constant(value="\",\"message\":\"") property(name="$.msg2" format="jsonr")
    

    replace 函数进行全局替换,因此您可能希望限制它,例如使用

    set $.msg2 = replace($rawmsg, "LOCAL\\u", "LOCAL\\\\u");
    

    【讨论】:

    • 完美!这就是让我不断回到 StackOverflow 的答案!这工作得很好
    猜你喜欢
    • 1970-01-01
    • 2020-12-13
    • 2013-11-10
    • 1970-01-01
    • 1970-01-01
    • 2015-05-19
    • 1970-01-01
    • 2015-09-21
    相关资源
    最近更新 更多