2015-09-18 64 views
1

我想在rsyslog服务器中构建来自我的D-Link DAP-2310的日志。它有一个非标准的日志格式,我的想法是用一个rsyslog模板中的正则表达式来修复它。当我用rsyslogd -N1解析rsyslog.conf时,结果真的令人沮丧。rsyslog模板 - 在正则表达式中解析失败

msg数据项看起来像AA:BB:CC:DD:EE] [app-name] log message 第一部分是一个mac地址,其中第一部分“[00:”放置在另一个数据项中,不问为什么。第二部分“[app-name]”是发送消息的应用程序/实例。最后一部分“日志消息”是记录的操作。

有趣的部分是i)应用程序名称和ii)日志消息。

我已经在http://www.rsyslog.com/regex/上验证了正则表达式,它们都像魅力一样。

  1. \ [(+)\]
  2. \(+)

完整的模板声明的样子[+ \。]:

template(name=”AP_tmpl” type=”list”) { 
    property(name=”timestamp”)  
    constant(value=” “) 
    property(name=”hostname”) 
    constant(value=” “) 
    property(name=”msg” 
     regex.type=”ERE” 
     regex.submatch=”1”  
     regex.expression=”\[(.+)\]--end” 
     regex.nomatchmode=”BLANK” 
    ) 
    constant(value=” “) 
    property(name=”msg” 
     regex.type=”ERE” 
     regex.submatch=”1” 
     regex.expression=”\[.+\](.+)$--end” 
     regex.nomatchmode=”BLANK” 
    )  
    constant(value=”\n“) 
} 

当我解析conf文件抱怨转义字符。

[email protected]:~$ sudo rsyslogd -N1 
rsyslogd: version 7.4.4, config validation run (level 1), master config /etc/rsyslog.conf 
rsyslogd: error during parsing file /etc/rsyslog.d/41-AP.conf, on or before line 20: invalid character '"' in object definition - is there an invalid escape sequence somewhere? [try http://www.rsyslog.com/e/2207 ] 
rsyslogd: error during parsing file /etc/rsyslog.d/41-AP.conf, on or before line 20: invalid character '\' in object definition - is there an invalid escape sequence somewhere? [try http://www.rsyslog.com/e/2207 ] 
rsyslogd: error during parsing file /etc/rsyslog.d/41-AP.conf, on or before line 20: invalid character '.' in object definition - is there an invalid escape sequence somewhere? [try http://www.rsyslog.com/e/2207 ] 
rsyslogd: error during parsing file /etc/rsyslog.d/41-AP.conf, on or before line 20: invalid character '*' in object definition - is there an invalid escape sequence somewhere? [try http://www.rsyslog.com/e/2207 ] 
rsyslogd: error during parsing file /etc/rsyslog.d/41-AP.conf, on or before line 20: invalid character '\' in object definition - is there an invalid escape sequence somewhere? [try http://www.rsyslog.com/e/2207 ] 
rsyslogd: error during parsing file /etc/rsyslog.d/41-AP.conf, on or before line 20: syntax error on token ']' [try http://www.rsyslog.com/e/2207 ] 
rsyslogd: CONFIG ERROR: could not interpret master config file '/etc/rsyslog.conf'. [try http://www.rsyslog.com/e/2207 ] 
rsyslogd: run failed with error -2207 (see rsyslog.h or try http://www.rsyslog.com/e/2207 to learn what that number means) 

我想不通为什么,正则表达式验证没有任何错误。 http://www.rsyslog.com/e/2207不给。有任何想法吗?

+0

这个'\\ [(。+)\\]'和这个'\\ [。+ \\](。+)'工作吗? – Diego

回答

1

当你写”\[(.+)\]--end”,\[预计是一个特殊字符(如\n),而它不是。为避免反斜杠的特殊用法,您应该使用另一个反斜杠进行转义。所以虽然真正的正则表达式是\[(.+)\]\[.+\](.+),你必须使用的字符串是:”\\[(.+)\\]””\\[.+\\](.+)”

另外,要小心双引号,你可能要",而不是

+0

解析器没有任何错误,很好。但我没有得到任何匹配。我正在编辑nano,并使用普通双引号(“)。 – Tobias

+0

使用此工具来更正您的正则表达式,然后http://regexr.com/ – Diego

+0

rsyslog确实需要perl/sed-like语法以允许更自然地编写正则表达式无论是这样,还是更好的逃避规则或“和”引号之间的区分。 – Otheus