使用 rsyslog 捕获多行事件并将它们存储到文件中

问题描述

我们有一个集中的 rsyslog 基础设施,可以使用 imtcp 模块从世界各地的设备发送的 TCP 中捕获事件。

这个想法是从系统日志 (TCP) 读取并将事件存储到磁盘,每个事件一行。这些事件稍后由其他使用者处理。

就我们所见,一些事件一旦存储在磁盘上就会被拆分为多个事件,从而破坏了我们流程的其余部分。

使用 tcpdump 捕获单个包,我们确认源系统日志正在向我们发送包含多行的整个事件(典型的 Java 异常)。

[root@xx xx.xx.xx.xx]# tcpdump -i bond0 tcp port 50520 -A -c 1
tcpdump: verbose output suppressed,use -v or -vv for full protocol decode
listening on bond0,link-type EN10MB (Ethernet),capture size 262144 bytes
12:12:26.062110 IP xx.xx.xx.xx.com.41444 > xx.xx.xx.com.50520: Flags [P.],seq 3270590174:3270590613,ack 2646946316,win 27,options [nop,nop,TS val 3937801207 ecr 2623497312],length 439
E....`@.<.ML..A....N...X..>...2......q.....
....._d`<13> xxx #2.0.#2021 02 10 12:19:50:898#+00#Info#com.xx.xx.xx.xx.xx#
##JavaEE/xx#xx#xx#JavaEE/xx#com.xx.xx.xx.xx.APIServiceHandler#xx#xx##xx#xx##0#Thread[HTTP Worker [@xx],5,Dedicated_Application_Thread]#Plain##
Is the user getting thru SSO? xx:true#

1 packet captured
44 packets received by filter
2 packets dropped by kernel

由于这是一个全球系统,我们不能要求设备所有者修改格式,所有操作都应该在我们这边进行。

这是我们的 rsyslog.conf 文件

$MaxMessageSize 128k
# Global configuration/modules
module(load="imtcp" MaxListeners="100")
module(load="imfile" mode="inotify")
module(load="impstats" interval="10" resetCounters="on" format="cee" ruleset="monitoring")
module(load="mmjsonparse")
module(load="mmsequence")
module(load="omelasticsearch")
module(load="omudpspoof")

# Include all conf files
$IncludeConfig /etc/rsyslog.d/*.conf

这是我们的模板,它从 tcp 读取并写入文件 (etc/rsyslog.d/template.conf)

template(name="outjsonfmt_device" type="list") {
         constant(value="{")
         property(outname="device_ip" name="fromhost-ip" format="jsonf")
         constant(value=",")
         property(outname="time_collect" name="timegenerated" dateFormat="rfc3339" format="jsonf")
         constant(value=",")
         constant(value="\"device_type\":\"device\"")
         constant(value=",")
         property(outname="collector_id" name="$myhostname" format="jsonf")
         constant(value=",")
         property(outname="msg" name="rawmsg-after-pri" format="jsonf" )
         constant(value="}\n")
}

template(name="device-out-filename" type="string" string="/data1/input/device/%fromhost-ip%/device_%$now-utc%_%$hour-utc%.log")
ruleset(name="writeRemoteDataToFile_device") {
         action(type="omfile" dynaFileCacheSize="10000" dirCreateMode="0700" FileCreateMode="0644" dirOwner="user" dirGroup="logstash" fileOwner="user" fileGroup="user" dynafile="device-out-filename" template="outjsonfmt_device")
}
input(type="imtcp" port="50520" ruleset="writeRemoteDataToFile_device")

在将事件写入磁盘之前,我们如何配置 rsyslog 以在事件中间转义换行符?我们已经尝试过 $EscapeControlCharactersOnReceive 没有成功和其他类似的参数

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)