rsyslog问题请教
最近在看rsyslog手册,对于property replacer一节没看懂(网上也没搜索到例子解析),所以在此向大家请教:1:“%msg:R:.*Sev:. \(.*\) \[.*–end%”
困惑的是:正则表达式到底是“.*Sev:. \(.*\) \[.*”还是:.*Sev,如果是前者,那么缺省options是什么?这个表达式仅仅是选择匹配的日志内容而不做任何字符串替换吗?
如果是后者,该表达式的作用是将匹配到“.*Sev”字符串替换为“:. \(.*\) \[.*”吗?
2:“%msg:R,ERE,1,FIELD,1:for (vlan\*):--end%”
原文是“this takes the first submatch of the second match of said expression”,确实不懂其在表述啥意思,求指教。
谢谢大家! 本帖最后由 rdcwayx 于 2014-07-18 19:34 编辑
这个是 position-based extraction:%propname:fromChar:toChar:options:fieldname%加了R后,就是regex 格式:R,<regexp-type>,<submatch>,<nomatch>,<match-number>看这个介绍里的黑体部分,也就是R:后面的都是正则,但是以冒号分割,前一部分是fromChar, 后一部分是 toChar.
There is also support for regular expressions. To use them, you need to place a "R" into FromChar. This tells rsyslog that a regular expression instead of position-based extraction is desired. The actual regular expression must then be provided in toChar. The regular expression must be followed by the string "--end". It denotes the end of the regular expression and will not become part of it. If you are using regular expressions, the property replacer will return the part of the property text that matches the regular expression. An example for a property replacer sequence with a regular expression is: "%msg:R:.*Sev:. \(.*\) \[.*--end%"
regexp-type is either "BRE" for Posix basic regular expressions or "ERE" for extended ones. The string must be given in upper case. The default is "BRE" to be consistent with earlier versions of rsyslog that did not support ERE. The submatch identifies the submatch to be used with the result. A single digit is supported. Match 0 is the full match, while 1 to 9 are the acutal submatches. The match-number identifies which match to use, if the expression occurs more than once inside the string. Please note that the first match is number 0, the second 1(我加的解释: the second match is number 1) and so on. Up to 10 matches (up to number 9) are supported. Please note that it would be more natural to have the match-number in front of submatch, but this would break backward-compatibility. So the match-number must be specified after "nomatch".
这个例子中, .*Serv 是起始部分 (也就是Serv 前的所有字符包括Serv), . \(.*\) \[.* 是最后的部分,.(任意字符) [任意字符
"%msg:R:.*Sev:. \(.*\) \[.*--end%"
这个例子里, R说明是regex ,格式是ERE (extended regex), 第一个1 是submatch,FIELD 是 nonmatch,第二个 1 是匹配数,1代表第二次匹配。
“%msg:R,ERE,1,FIELD,1:for (vlan\*):--end%”Mode Returned
DFLT "**NO MATCH**"
BLANK "" (empty string)
ZERO "0"
FIELD full content of original field参考资料:
http://w.gdu.me/wiki/Linux/rsyslog_logrotate.html
http://www.cnblogs.com/tobeseeker/archive/2013/03/10/2953250.html
http://www.rsyslog.com/doc/property_replacer.html
http://www.rsyslog.com/doc/rsyslog_conf_nomatch.html 回复 2# rdcwayx
首先感谢版主耐心、细致解答,其次为确保我正确理解了版主的意思,需要与你再确认一些细节。
1、
这个例子中, .*Serv 是起始部分 (也就是Serv 前的所有字符包括Serv), . \(.*\) \[.* 是最后的部分,.(任意字符) [任意字符
"%msg:R:.*Sev:. \(.*\) \[.*--end%"
我的理解是消息截断形式是:如果匹配到正则表达式".*Sev.*. \(.*\) \[.*",那么"msg"值为匹配到的字符串,实际上原始内容,因为是以".*"作为开始、结束符;没有匹配到则是空,也就是是该消息不处理。需要注意的是我用红色标注".*"标识中间出现的字符串,可能有字符串,也可能没有
2、
这个例子里, R说明是regex ,格式是ERE (extended regex), 第一个1 是submatch,FIELD 是 nonmatch,第二个 1 是匹配数,1代表第二次匹配。
“%msg:R,ERE,1,FIELD,1:for (vlan\*):--end%”
我的理解是:正则表达式支持小括号"("、")",每对小括号标记的正则表达式表示一个"submatch",也就是例子中的第一个数字"1",而一个完整的消息可以有多个子串匹配到正则表达式,也就是第二个数字"1"。字符串的替换的规则是:如果消息与表达式匹配,那么返回的内容是第二个匹配到的子串内容,否则返回的原始消息内容。
请问,我的理解是否有问题,谢谢!
你自己在实际环境里测试一下。
理解1 里,如果没有匹配(FIELD),则显示所有的内容。
页:
[1]