rsyslog模板 - 解析正则表达式

时间:2015-09-18 20:19:59

标签: regex rsyslog

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

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

有趣的部分是i)app-name和ii)日志消息。

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

  1. \ [(+)\]
  2. \ [+ \](+)
  3. 完整的模板声明如下:

    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文件时,它会抱怨转义字符。

    tobias@ubuntutest:~$ 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什么都没有。有什么想法吗?

1 个答案:

答案 0 :(得分:2)

当你写”\[(.+)\]--end”时,\[应该是一个特殊字符(如\n),而不是\[(.+)\]。为避免特殊使用反斜杠,您应该使用另一个反斜杠来逃避它。因此,虽然真正的正则表达式是\[.+\](.+)”\\[(.+)\\]”,但您必须使用的字符串是:”\\[.+\\](.+)”"

另外,请注意双引号,您可能需要,而不是- (NSInteger)getOperationIdentifierForTask:(NSURLSessionTask*)task { for (NSString *key in self.operations) { HttpTaskOperation *value = [self.operations objectForKey:key]; if (value.task == task) return [key integerValue]; //********** } return 0; }