如何配置Rsyslog mmfields拆分TACACS服务器异常格式Syslog消息?
问题
我有一套运行正常的Rsyslog配置,版本为8.2102.0-10.el8。目前收到一台即将退役的EOL TACACS服务器发送的格式异常的Syslog消息,希望重写该消息以剔除无关内容、缩减日志体积。
消息格式示例:
2023-03-15T10:10:57+00:00 SERVER1 CSCOacs_Passed_Authentications 0887851664 1 0 2023-03-15 10:10:57.267 +00:00 0424495644 5201 NOTICE Passed-Authentication: Authentication succeeded, ACSVersion=acs-5.5.0.46-B.723, ConfigVersionId=33, Device IP Address=10.5.1.2, DestinationIPAddress=10.4.2.1, DestinationPort=49, UserName=testapi, Protocol=Tacacs, RequestLatency=4, Type=Authentication, Action=Login, Privilege-Level=0, Authen-Type=ASCII, Service=Login, User=testapi, Port=eth0, Remote-Address=10.2.2.1, UserName=testapi, AcsSessionID=SERVER1/462353176/34805778, AuthenticationIdentityStore=Internal Users, AuthenticationMethod=PAP_ASCII, SelectedAccessService=Local User Db TACACS, SelectedShellProfile=Admin_Role, IdentityGroup=IdentityGroup:All Groups:Accounts, Step=13013 , Step=15008 , Step=15004 , Step=15012 , Step=15041 , Step=15006 , Step=15013 , Step=24210 , Step=24212 , Step=13045 , Step=13015 , Step=13014 , Step=15037 , Step=15041 , Step=15006 , Step=15013 , Step=24210 , Step=24212 , Step=22037 , Step=15044 , Step=15035 , Step=15042 , Step=15036 , Step=15004 , Step=13015 , SelectedAuthenticationIdentityStores=Internal Users, SelectedAuthenticationIdentityStores=Internal Users, NetworkDeviceName=Cisco, NetworkDeviceGroups=Device Type:All Device Types:Cisco, NetworkDeviceGroups=Location:All Locations:All_Locations, ServiceSelectionMatchedRule=Local_Users_TACACS, IdentityPolicyMatchedRule=Default, AuthorizationPolicyMatchedRule=Rule_1, Action=Login, Privilege-Level=0, Authen-Type=ASCII, Service=Login, Port=eth0, Remote-Address=10.2.2.1, UserIdentityGroup=IdentityGroup:All Groups:Accounts, Response={Type=Authentication; Authen-Reply-Status=Pass; }
当前使用的配置:
module(load="mmfields") template(name="parsed" type="list"){ property(name="timestamp") constant(value=",") property(name="hostname") constant(value=",") property(name="syslogtag") constant(value=",") constant(value="\n") } if ($fromhost-ip contains "10.1.2.3") then { action(type="mmfields" separator="," ) action( type = "omfile" file = "/rsyslogdata/tacacs/parsed.log" template = "parsed" ) }
但输出仅包含时间戳、主机名和syslogtag,未将消息体按逗号拆分为$!f4等字段,整个消息体仍为单个字段,不符合预期。
解决方案
1. 核心问题分析
当前配置未指定mmfields的解析目标字段,默认不会自动拆分消息体;同时模板未引用解析后的结构化字段,导致输出仅保留基础Syslog字段。
2. 修正后的完整配置
module(load="mmfields") # 定义精简后的输出模板,只保留关键字段 template(name="tacacs_cleaned" type="list"){ property(name="timestamp") constant(value=",") property(name="hostname") constant(value=",") property(name="syslogtag") constant(value=",") # 按需保留需要的字段,可根据实际需求增删 property(name="$!fields.UserName") constant(value=",") property(name="$!fields[\"Device IP Address\"]") constant(value=",") property(name="$!fields.Remote-Address") constant(value=",") property(name="$!fields.Protocol") constant(value=",") property(name="$!fields.Action") constant(value="\n") } if ($fromhost-ip contains "10.1.2.3") then { action(type="mmfields" # 先预处理消息体,去掉开头无效前缀 prependRULES=":msg, eregex, \"^.*Passed-Authentication: (.*)$\", set $.cleanMsg = $1" field="$!cleanMsg" # 指定解析预处理后的消息体 separator="," # 按逗号拆分字段 ) action( type = "omfile" file = "/rsyslogdata/tacacs/parsed.log" template = "tacacs_cleaned" ) }
3. 关键配置说明
- prependRULES:通过正则截取消息体有效部分,剔除开头
Passed-Authentication: Authentication succeeded这类无效前缀,避免干扰字段拆分。 - field参数:必须明确指定要解析的字段(这里是预处理后的
$.cleanMsg),否则mmfields不会执行拆分操作。 - 字段引用:解析后的字段可通过
$!fields.字段名访问,若字段名含空格需用引号包裹,如$!fields["Device IP Address"]。 - 日志精简:模板仅保留业务需要的关键字段,直接剔除冗余的重复
Step项、重复用户信息等,大幅压缩日志体积。
内容的提问来源于stack exchange,提问作者ols
相关产品推荐
相关产品推荐

