外观
Rsyslog 规则集详解
1. 规则集概述
Rsyslog 规则集是日志处理的核心配置单元,定义了如何根据日志消息的特征进行过滤、处理和转发。规则集由过滤器和动作组成,过滤器决定哪些日志消息需要处理,动作定义具体的处理方式。
规则集工作原理
Rsyslog 按顺序评估每个规则,当找到匹配的规则后执行相应动作。单个消息可能匹配多个规则并执行多个动作。
2. 选择器(Selectors)
选择器是 Rsyslog 最传统的过滤机制,采用 BSD syslogd 风格的 facility.priority 格式。这种方式简单直接,适合基于设施和优先级的粗粒度过滤。
2.1 基本语法
facility.priority action1
facility(设施)包括:
- 标准设施:auth, authpriv, cron, daemon, kern, lpr, mail, mark, news, security, syslog, user, uucp
- 本地设施:local0 ~ local7
- 通配符:*
priority(优先级)包括:
- 标准优先级:emerg, alert, crit, err, warning, notice, info, debug, none
- 通配符:*
2.2 配置示例
bash
#### RULES ####
# Log all kernel messages to the console.
# Logging much else clutters up the screen.
#kern.* /dev/console
# Log anything (except mail) of level info or higher.
# Don't log private authentication messages!
#*.info;mail.none;authpriv.none;cron.none /var/log/messages
# The authpriv file has restricted access.
authpriv.* /var/log/secure
# Log all the mail messages in one place.
mail.* -/var/log/maillog
# Log cron stuff
cron.* /var/log/cron
# Everybody gets emergency messages
*.emerg :omusrmsg:*
# Save news errors of level crit and higher in a special file.
uucp,news.crit /var/log/spooler
# Save boot messages also to boot.log
#local7.* /var/log/boot.log1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
选择器使用技巧
使用通配符可以简化配置,但优先级通配符会匹配该级别及以上的所有消息。如*.warning会匹配 warning、err、crit、alert、emerg 级别。
常见配置错误
不要使用none作为动作,这会丢弃所有匹配的消息。确保配置文件语法正确,避免 rsyslog 启动失败。
3. 基于属性的过滤器(Property-Based Filters)
基于属性的过滤器提供更精细的日志过滤能力,可以根据消息的具体属性内容进行精确匹配,支持字符串比较和正则表达式。
3.1 基本语法
:property, [!]compare-operation, "value"1
3.2 支持的比较操作
| 操作符 | 说明 | 示例 |
|---|---|---|
| contains | 检查属性是否包含指定字符串 | :msg, contains, "error" |
| isequal | 检查属性是否完全等于指定值 | :syslogtag, isequal, "sshd" |
| startswith | 检查属性是否以指定字符串开头 | :msg, startswith, "Failed" |
| endswith | 检查属性是否以指定字符串结尾 | :programname, endswith, "_foo" |
| regex | POSIX BRE 正则表达式匹配 | :msg, regex, "fatal .* error" |
| ereregex | POSIX ERE 正则表达式匹配 | :msg, ereregex, "fatal.*error" |
3.3 值部分转义规则
\"- 引号字符\\- 反斜杠字符
3.4 配置示例
bash
# 过滤包含特定错误ID的消息
:msg, contains, "ID-4711" /var/log/special.log
# 过滤来自特定程序的消息
:programname, isequal, "myapp" /var/log/myapp.log
# 使用正则表达式过滤
:msg, regex, "fatal .* error" /var/log/errors.log
# 排除包含特定字符串的消息
:msg, !contains, "debug" /var/log/production.log1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
属性过滤器前提
属性过滤器必须放在规则的第一列,以冒号开头。确保属性名称和比较操作符的大小写正确。
4. 基于表达式的过滤器(Expression-Based Filters)
基于表达式的过滤器是 Rsyslog 最强大的过滤机制,支持复杂的布尔运算、算术运算和字符串操作。这种现代化的过滤方式提供了接近编程语言的灵活性。
4.1 基本语法
if expr then action-part-of-selector-line1
4.2 支持的操作
- 布尔运算:and, or, not
- 比较运算:==, !=, <, >, <=, >=
- 字符串运算:contains, startswith, endswith
- 算术运算:+, -, *, /, %
4.3 配置示例
bash
# 复杂条件过滤
if $syslogfacility-text == 'local0' and $msg startswith 'DEVNAME' and ($msg contains 'error1' or $msg contains 'error0') then /var/log/somelog
# 排除特定消息
if $syslogfacility-text == 'local0' and $msg startswith 'DEVNAME' and not ($msg contains 'error1' or $msg contains 'error0') then /var/log/somelog
# 基于消息长度过滤
if $msg contains 'error' and (strlen($msg) > 100) then /var/log/long-errors.log
# 基于时间过滤
if ($hour >= 9 and $hour <= 17) and $msg contains 'production' then /var/log/business-hours.log1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
表达式过滤器特性
表达式过滤器支持变量访问(如 $msg、$syslogfacility-text)和函数调用(如 strlen())。当前版本不支持正则表达式,但计划在未来版本中添加。
表达式语法风险
表达式过滤器的语法较为复杂,配置错误可能导致 rsyslog 无法启动。建议在测试环境验证配置后部署到生产环境。
5. 过滤器类型对比
| 特性 | 选择器 | 属性过滤器 | 表达式过滤器 |
|---|---|---|---|
| 复杂度 | 简单 | 中等 | 复杂 |
| 灵活性 | 低 | 中 | 高 |
| 性能 | 高 | 中 | 中低 |
| 学习成本 | 低 | 中 | 高 |
| 适用场景 | 基础分类 | 内容匹配 | 复杂逻辑 |
| 正则支持 | 否 | 是 | 否(计划中) |
| 布尔运算 | 有限 | 有限 | 完整支持 |
6. 高级配置实例
6.1 多条件组合过滤
bash
# 同时使用多种过滤器类型
*.err /var/log/all-errors.log
:programname, startswith, "apache" /var/log/apache.log
if $msg contains 'timeout' and $syslogseverity <= 4 then /var/log/timeouts.log1
2
3
4
2
3
4
6.2 条件分支处理
bash
# 根据消息内容进行不同处理
if $msg contains 'CRITICAL' then /var/log/critical.log
if $msg contains 'WARNING' then /var/log/warnings.log
if $msg contains 'INFO' then /var/log/info.log1
2
3
4
2
3
4
6.3 性能优化配置
bash
# 使用discard动作过滤不需要的消息
:msg, contains, "debug" ~
:msg, contains, "verbose" ~
# 保留重要消息
*.err /var/log/errors.log
auth.* /var/log/security.log1
2
3
4
5
6
7
2
3
4
5
6
7
性能优化建议
将最常用的规则放在配置文件的顶部,避免不必要的消息处理。使用 discard 动作(~)可以有效过滤掉大量不需要的消息。