免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 7456 | 回复: 3
打印 上一主题 下一主题

[其他] rsyslog问题请教 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-07-06 10:44 |只看该作者 |倒序浏览
最近在看rsyslog手册,对于property replacer一节没看懂(网上也没搜索到例子解析),所以在此向大家请教:
1:“%msg:R:.*Sev:. \(.*\) \[.*–end%”
困惑的是:正则表达式到底是“.*Sev:. \(.*\) \[.*”还是:.*Sev,如果是前者,那么缺省options是什么?这个表达式仅仅是选择匹配的日志内容而不做任何字符串替换吗?
如果是后者,该表达式的作用是将匹配到“.*Sev”字符串替换为“:. \(.*\) \[.*”吗?

2:“%msg:R,ERE,1,FIELD,1:for (vlan[0-9]\*):--end%”
原文是“this takes the first submatch of the second match of said expression”,确实不懂其在表述啥意思,求指教。

谢谢大家!

论坛徽章:
15
2015年辞旧岁徽章
日期:2015-03-03 16:54:15双鱼座
日期:2015-01-15 17:29:44午马
日期:2015-01-06 17:06:51子鼠
日期:2014-11-24 10:11:13寅虎
日期:2014-08-18 07:10:55酉鸡
日期:2014-04-02 12:24:51双子座
日期:2014-04-02 12:19:44天秤座
日期:2014-03-17 11:43:36亥猪
日期:2014-03-13 08:13:51未羊
日期:2014-03-11 12:42:03白羊座
日期:2013-11-20 10:15:18CU大牛徽章
日期:2013-04-17 11:48:45
2 [报告]
发表于 2014-07-18 19:11 |只看该作者
本帖最后由 rdcwayx 于 2014-07-18 19:34 编辑

这个是 position-based extraction:
  1. %propname:fromChar:toChar:options:fieldname%
复制代码
加了R后,就是regex 格式:
  1. 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[0-9]\*):--end%”
  1. Mode        Returned
  2. DFLT        "**NO MATCH**"
  3. BLANK        "" (empty string)
  4. ZERO        "0"
  5. 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

论坛徽章:
0
3 [报告]
发表于 2014-07-21 10:18 |只看该作者
回复 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[0-9]\*):--end%”

我的理解是:正则表达式支持小括号"("、")",每对小括号标记的正则表达式表示一个"submatch",也就是例子中的第一个数字"1",而一个完整的消息可以有多个子串匹配到正则表达式,也就是第二个数字"1"。字符串的替换的规则是:如果消息与表达式匹配,那么返回的内容是第二个匹配到的子串内容,否则返回的原始消息内容。

请问,我的理解是否有问题,谢谢!



   

论坛徽章:
15
2015年辞旧岁徽章
日期:2015-03-03 16:54:15双鱼座
日期:2015-01-15 17:29:44午马
日期:2015-01-06 17:06:51子鼠
日期:2014-11-24 10:11:13寅虎
日期:2014-08-18 07:10:55酉鸡
日期:2014-04-02 12:24:51双子座
日期:2014-04-02 12:19:44天秤座
日期:2014-03-17 11:43:36亥猪
日期:2014-03-13 08:13:51未羊
日期:2014-03-11 12:42:03白羊座
日期:2013-11-20 10:15:18CU大牛徽章
日期:2013-04-17 11:48:45
4 [报告]
发表于 2014-07-21 13:26 |只看该作者
你自己在实际环境里测试一下。

理解1 里,如果没有匹配(FIELD),则显示所有的内容。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP