rsyslog学习7 -- Filter Conditions过滤条件

Filter Conditions

rsyslog支持以下条件

  • 传统的severity和facility
  • Property-Based Filters 基于属性
  • 基于表达式
  • BSD-style blocks(不再向后兼容)

Selectors

Selectors 是传统的筛选方式. 简洁,高效,特别是在v7中,比advanced模式高效,目前的版本中两者已经没有差别。

Selector 由 facility和priority组成,由”.”分割,大小写敏感,可以写成十进制数字,但最好不这么做。个体可以参考syslog(3). 这些名字在文件/usr/include/syslog.h 定义,类似 LOG_-values

facility 关键字: auth, authpriv, cron, daemon, kern, lpr, mail, mark, news, security (same as auth), syslog, user, uucp and local0 through local7。 security 不应被再被app使用。

Priority : debug, info, notice, warning, warn (same as warning), err, error (same as err), crit, alert, emerg, panic (same as emerg). The keywords error, warn and panic are deprecated and should not be used anymore. The priority defines the severity of the message.

rsyslog和syslog行为类似,并有一些扩展,比如理解(“*’‘)和none,也可以使用”,”指定多个多个priority. 也可以指定多个facility,但要注意的时,如果是多个facility语句,priority将被忽略

多个selectors可以(“;’‘)连接,后者覆盖前者,还可以使用(“=’‘)和(“!’‘)

Property-Based Filters

rsyslogd特有。可以过滤任何属性。参考 rsyslog properties documentation

property-based filter以”:”起始,后跟属性名字,然后是”,”,比较操作符,”,”,比较值(用双引号包围)。逗号之间可以有空格和tab。属性名和比较符大小写敏感。

1
:property, [!]compare-operation, "value"

Compare-Operations

contains

​ 是否包含某值,精确匹配,不支持通配符

isequal

​ 两者必须精确匹配,一般用于 syslogtag or FROMHOST

startswith

​ Checks if the value is found exactly at the beginning of the property value. For example, if you search for “val” with

1
:msg, startswith, "val"

​ 可匹配 “values are in this message” 但不匹配 “There are values in this message” (“contains” 可以). startswith远比regex高效

regex

​ Compares the property against the provided POSIX BRE regular expression.

ereregex

​ Compares the property against the provided POSIX ERE regular expression.

可在比较操作之前使用(!)来反向操作。比如 “This is an informative message”

1
2
3
4
5
# 不匹配
:msg, contains, "error"

# 匹配
:msg, !contains, "error"

Value Part

vaule是一个引号间的字串,支持一些转义

“ - the quote character (e.g. “String with “Quotes””)

\ - the backslash character (e.g. “C:\tmp”)

一般来说,主要是针对真实的 msg 做一些过滤

1
2
# 注意,大小写敏感,不会匹配“id-4711”
:msg, contains, "ID-4711"
1
2
# 匹配 fatal和error,中间任何字符。
msg, regex, "fatal .* error"

使用属性过滤是一个挑战,可以使用debug来测试,rsyslogd -d

不支持布尔操作,如果要过滤facility或severity,建议使用 selectors

Expression-Based Filters

可以过滤任意复杂的表达式,包括布尔,算术和字串操作,类似于完整的脚本语言,不过在语法有些许区别。

表达式过滤以关键字 if 开始,类似于

1
if expr then action-part-of-selector-line

if 和 then是关键字,必有存在,expr是表达式,参考 expression documentation ,action-part-of-selector-line是action

BSD-style Blocks

v7+不再支持,所以不建议再使用

例子

1
2
*.* /var/log/file1 # 传统方式
if $msg contains 'error' then /var/log/errlog # 基于表达式的方式

Right now, you need to specify numerical values if you would like to check for facilities and severity. These can be found in RFC 5424. If you don’t like that, you can of course also use the textual property - just be sure to use the right one. As expression support is enhanced, this will change. For example, if you would like to filter on message that have facility local0, start with “DEVNAME” and have either “error1” or “error0” in their message content, you could use the following filter:

1
2
# 必须在一行
if $syslogfacility-text == 'local0' and $msg startswith 'DEVNAME' and ($msg contains 'error1' or $msg contains 'error0') then /var/log/somelog
1
2
# 如果要保存除了 error1 或 error0 的日志,只要加上 not
if $syslogfacility-text == 'local0' and $msg startswith 'DEVNAME' and not ($msg contains 'error1' or $msg contains 'error0') then /var/log/somelog

如果要忽略大小写的比较,使用 “contains_i” 代替 “contains” ,使用 “startswith_i” 代替 “startswith”. 注意,表达式过滤不支持正则表达式

0%