Rsyslog 解析问题 - 发送到 Rsyslog 的系统日志消息中没有 header

Rsyslog parsing issue - no header in syslog message that is sent to Rsyslog

我有一个应用程序,它通过 TCP 将日志作为 JSON 流发送到 Rsyslog 服务器。但是它不会在系统日志消息中放置任何 header 。发送的示例原始消息如下:

{"query_class":"C_INTERNET","source_ip":"10.1.1.1","query_type":"A","trans_id":502,"err_code":"NXDOMAIN","@type":"dns","dest_ip":"10.10.10.1","dest_port":53,"uid":"CDdCwH1lD2ToFS5y02","epochdate":1595000476.3491,"query":"www.google.com","@host":"host-10-01","rejected":true,"source_port":54764,"proto":"udp"}

Rsyslog 将此消息转发到另一台服务器并以如下格式放置 header(未应用模板):

<13>Jun 16 10:43:09 host01.example.local

我不知道这个header是从哪里来的(可能是在消息中没有header时应用了默认配置)。

问题是,header 中缺少 TAG,导致原始消息无法正确解析。

当 Rsyslog 将日志转发到另一台机器或将其写入文件时,它是这样的:

<13>Jun 16 10:43:09 host01.example.local {"query_class" "C_INTERNET","source_ip":"10.1.1.1","query_type":"A","trans_id":502,"err_code":"NXDOMAIN","@type":"dns","dest_ip":"10.10.10.1","dest_port":53,"uid":"CDdCwH1lD2ToFS5y02","epochdate":1595000476.3491,"query":"www.google.com","@host":"host-10-01","rejected":true,"source_port":54764,"proto":"udp"}

{"query_class" 被提取并成为标签(“:”被删除)。其余的成为系统日志消息。

遗憾的是,无法对源进行任何更改以配置 header。 如何在 Rsyslog 中向此原始消息添加 header 并将其转发到另一台远程计算机或将其写入文件?或者,如何更改默认 header 配置以添加自定义进程名称标签(如果应用了默认配置)?

rsyslog 可能正在使用 omfwdRSYSLOG_TraditionalForwardFormat 默认模板,即:

template(name="RSYSLOG_TraditionalForwardFormat" type="string"
     string="<%PRI%>%TIMESTAMP% %HOSTNAME% %syslogtag:1:32% %msg:::sp-if-no-1st-sp%%msg%")

定义您自己的模板并在您的转发规则中使用它,例如:

template(name="myfwd" type="string"
     string="<%PRI%>%TIMESTAMP% %HOSTNAME% dummytag %rawmsg%\n")
if $fromhost == 'thehost' then {
    action(type="omfwd" target="theserver" protocol="tcp" template="myfwd")
}