【问题标题】:rsyslog import non standard logsrsyslog 导入非标准日志
【发布时间】:2019-05-06 23:01:16
【问题描述】:

我正在尝试将非标准 syslog 格式的应用程序日志导入 mysql。

示例行:

Dec  5 10:50:06 wifi coova-chilli[10099]: Client process timed out: 2

当我使用 imfile 模块导入日志(然后将其转发到 mysql)时,它工作正常,但整行都进入了消息字段。这也意味着 ReceivedAt 和 DeviceReportedTime 字段是导入日志时的时间戳,而不是消息中的实际事件时间。

我认为答案在于属性替换器,但我似乎无法在网上找到有关如何实际获取实际日期并将其强制放入 DeviceReportedTime 字段的示例。

这就是数据库中的最终结果:

53052   NULL    2018-12-04 16:17:44 2018-12-04 16:17:44 16  5   server   Dec  5 10:50:06 wifi coova-chilli[10099]: Client process timed out: 2  5   NULL    customtag   NULL    NULL    0   NULL    NULL    NULL    NULL    NULL    3   customtag   -   NULL    NULL

我在 /etc/rsyslog.d 中的客户端有以下配置:

module(load="imfile" mode="inotify")

input(type="imfile"
        File="/var/log/appname/applog.log"
        Tag="customtag")

这在服务器端的 /etc/rsysconfig.d 下:

:syslogtag, contains, "customtag":ommysql:10.255.2.6,rsyslogdb,loganalyzer,password

【问题讨论】:

    标签: linux logging centos7 rsyslog


    【解决方案1】:

    这不是完整的答案,因为它不是我以前使用过的 rsyslog 的一部分,但它应该能让你接近最终的解决方案。

    您可以使用 rsyslog 的输入解析库 liblognorm 和模块 mmnormalize。如果 rsyslog 中不包含这些,您可能需要安装一两个额外的软件包。首先,编写一个规则文件myrules.rb,其中包含一行描述您拥有的字段:

    rule=:%date:date-rfc3164% %tag:word% %host:char-to:[%[%pid:number%]: %msg:rest%
    

    您可以通过将示例行作为标准输入提供给测试程序lognormalizer 来使用它:

    echo 'Dec  5 10:50:06 wifi coova-chilli[10099]: Client process timed out: 2' |
    lognormalizer  -r myrules.rb
    

    你应该得到 json 格式的输出:

    { "msg": "Client process timed out: 2", "pid": "10099", 
      "host": "coova-chilli", "tag": "wifi", "date": "Dec  5 10:50:06" }
    

    您现在可以将此模块的使用添加到您的 rsyslog 配置文件中:

    module(load="mmnormalize")
    action(type="mmnormalize" rulebase="myrules.rb")
    template(name="simple" type="string" string="%$!date:::date-rfc3339% %$!host% %$!msg%\n")
    if $parsesuccess=="OK" then action(type="omfile" file="output" template="simple")
    

    现在应该解析输入文件中的相同示例输入行,并且 json 键将作为变量(例如 $!host)在模板中使用。上面应该在输出文件中写一行:

    Dec  5 10:50:06 coova-chilli Client process timed out: 2
    

    关于上面的内容我还有很多不明白的地方,所以你应该为每个关于特定点的新问题单独开一个新的帖子,以便其他人可以回答。

    【讨论】:

    • 非常感谢。我从来不知道那些图书馆。我会试一试,并用我的发现进行更新。
    猜你喜欢
    • 2017-01-07
    • 1970-01-01
    • 2018-02-18
    • 2017-11-07
    • 2022-11-04
    • 1970-01-01
    • 2010-11-15
    • 2016-01-16
    • 1970-01-01
    相关资源
    最近更新 更多