【问题标题】:How to correctly parse text file using rsyslog and imfile如何使用 rsyslog 和 imfile 正确解析文本文件
【发布时间】:2021-05-27 18:28:53
【问题描述】:

美好的一天

我想使用 imfile 文件输入模块将文本文件导入 rsyslog。但是,rsyslog 并没有像我预期的那样解析文本文件的内容,我正在努力寻找关于它是如何完成的文档。为了测试设置,我使用 imfile 从文本文件中读取,然后使用 omfile 将日志写入另一个文本文件。

文本文件的内容是“标准”系统日志格式的日志:

<PRI>TIMESTAMP HOSTNAME MESSAGE

我要导入 Rsyslog 的示例文本文件 (example_file.txt.) 如下所示:

<34>Feb 15 12:12:12 hostname1 tag1: message1
<34>Feb 16 12:12:12 hostname2 tag2: message2
<34>Feb 17 12:12:12 hostname3 tag3: message3

我在 rsyslog-d 中的 rsyslog 配置文件如下所示:

module(load = "imfile")
input(type = "imfile" file = "/home/.../Desktop/example_file.txt" Tag = "example")
action(type = "omfile" file = "/home/.../Desktop/example_output.log")

example_output.log 中的结果输出如下所示:

Feb 15 17:10:21 username example <34>Feb 15 12:12:12 hostname1 tag1: message1
Feb 15 17:10:21 username example <34>Feb 16 12:12:12 hostname1 tag2: message2
Feb 15 17:10:21 username example <34>Feb 17 12:12:12 hostname1 tag3: message3

如您所见,example_file.txt 中的所有内容都放置在 example_output.log 中生成日志的 MSG 字段中,而不是使用字段信息并将它们放置在正确的位置,例如时间戳、主机名、标签、味精。我在 .txt 文件中尝试过不同的格式,甚至将 .txt 文件保存为 .log 文件,但 rsyslog 每次都将整个内容放在 MSG 字段中。

那么我的问题:

如何告诉 rsyslog 和 imfile 我的 .txt 内容实际上是日志并正确解析它们?

考虑到:

  1. 我正在使用 Linux v4.4.0-ubi4-amd64 (UbiLinux) 开发 Up-Board

  2. 我正在使用 rsyslog8.24(最新稳定版)

  3. 我已经通读了:

    -Rsyslog官方文档,

    -Imfile官方文档,

    -Rainer Gerhards 的 syslog 解析在 rsyslog (http://www.rsyslog.com/doc/syslog_parsing.html),

    -甚至是 BSD Syslog 协议 RFC3164 (http://www.ietf.org/rfc/rfc3164.txt) 的文档

【问题讨论】:

    标签: rsyslog


    【解决方案1】:

    您可以使用templates 从消息中提取字段。这是一个示例模板。

    template(name="structured-format" type="list") {
       constant(value="{")
           property(outname="pri" name="msg" field.number="1" field.delimiter="32" format="jsonf") 
           constant(value=", ")
           property(outname="hostname" name="msg" field.number="4" field.delimiter="32" format="jsonf") 
           constant(value=", ")
           property(name="msg" format="jsonf")
       constant(value="} \n")
    }
    

    您可以像这样在输出中使用此模板。

    action(type = "omfile" file = "/home/.../Desktop/example_output.log" template="structured-format") 
    

    输出如下所示:

    {"pri":"<34>", "hostname":"hostname1", "msg":"<34>Feb 15 12:12:12 hostname1 tag1: message1"}
    

    也就是说,我还没有弄清楚如何从 msg 中排除已解析的字段,只将剩余的字段添加到 msg 字段中。希望您发现这些指针对您有所帮助。

    【讨论】:

      【解决方案2】:

      如何告诉 rsyslog 和 imfile 我的 .txt 内容实际上是日志并正确解析它们?

      这可以使用以下模板来完成(使用以下规则更新 rsyslog.conf)

      #Define a template of type string which just formats output to be send
      #                  to remote as line read from file. Named "tpl1" here.
      
      template(name="tpl1" type="string"
           string="%msg:::sp-if-no-1st-sp%%msg:::drop-last-lf%\n"
          )
      
      
      #Load imfile module to read file with log messages
      #Specify the input (which file to read) and action (where to send log messages 
      #along with template to be used)
      
      module(load="imfile")
      input(type="imfile" file="/var/log/FileWithLogMessages.log" Tag="GiveSomeTag")
      action(type="omfwd" target="192.168.0.1" Port="514" Protocol="udp" 
      template="tpl1")
      

      参考:

      模板 :https://rsyslog.readthedocs.io/en/latest/configuration/templates.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-06-02
        • 1970-01-01
        • 1970-01-01
        • 2015-10-13
        • 1970-01-01
        • 2016-06-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多