规则语言参考
这是 Go 实现的受限 DSL,不执行 Lua。使用 Lua 风格的 and/or/not、~= 和 do/end。每个文件可含多个 rule "id" do ... end;-- 注释到行末。
子句和类型
| 子句 | 契约 |
|---|---|
on "record.type" | 必填,只处理指定类型 |
where boolean | 聚合前过滤,缺失/非真不通过 |
group_by field, field | 最多 8 字段,sensor_id 自动加入 |
window "60s" | 固定事件时间窗口,>0 且 ≤1h |
when boolean | 必填,单记录或窗口关闭时求值 |
cooldown "30s" | 无窗口规则按组抑制;不能和 window 共用 |
emit "event.name" do ... end | 必填,且每规则一个 |
每子句最多一次。无窗口 group_by 需要 cooldown。severity 和 message 必须为字面量字符串,避免绕过字段脱敏;severity 支持 debug/info/warning/error/critical,默认 warning。其他输出字段可以引用表达式。
字段类型为 string/number/bool;未知字段拒绝加载。缺失值用 field ~= nil 检查,不隐式转型。http.status 是字符串;process.start_ns 是保精度字符串。
表达式与函数
| 类别 | 语法 |
|---|---|
| 字面量 | 字符串、数字、true、false、nil |
| 比较 | == ~= < <= > >=;有序比较需同类型 |
| 运算 | + - * / %、一元负号;除零/非有限结果为缺失 |
| 逻辑 | and/or 短路、not、括号 |
| 字符串 | contains(value, fragment);matches(value, "RE2 pattern") |
| 网络 | cidr_contains("10.0.0.0/8", source.ip) |
| JSON 标量 | json_string/json_number/json_bool(value, "/0/field") |
| 聚合 | count、count_if、ratio、distinct_count、sum、avg、max |
正则及 JSON pointer 必须为编译期字面量。JSON 非法、路径缺失、类型不符返回缺失;JSON pointer 最多 512 字节/32 段。提取值保留来源敏感性。
聚合只可用于窗口 when/emit/recover_when,不能嵌套或用于 where。count 统计过滤后的记录;其他聚合忽略缺失参数。聚合外仅可引用分组字段和 sensor_id/service/protocol;需要服务隔离时显式 group_by service。
时间、重载和限制
窗口对齐 Unix epoch,区间 [start,end)。每探针水位线为最大已见事件时间减 5 秒;迟到输入丢弃并计数。空闲推进等待输入积压排空;在线新规则的首个部分窗口只暖机。离线 replay 使用事件时间、关闭剩余窗口,不恢复在线检查点。
普通运行将兼容窗口、冷却、水位线和事故状态与事件一起提交到 WAL。重启恢复相容状态;未修改规则热重载保留状态,修改规则重新暖机。失败重载保留旧规则,错误含文件、行列和原因。
默认上限:256 条规则,每规则 10000 个活动窗口/冷却项及另 10000 个生命周期组,每去重聚合 4096 值,规则状态 64 MiB。分组字符串 1024 字节,表达式深度 64 / 4096 节点,源文件 1 MiB,字符串字面量 64 KiB。无循环、递归、函数定义、动态执行或任意 I/O。
告警生命周期
在窗口 when 与 emit 之间加入:
lifecycle do
trigger_windows 2
recovery_windows 2
recover_when count() >= 20 and ratio(error) <= 0.05
repeat "5m"
endtrigger/recovery_windows 为 1..120,默认 1;recover_when 必填。repeat 可省略,否则 >0 且 ≤1h。连续异常触发 firing,持续异常按 repeat 发 ongoing,连续显式健康发 resolved。无流量、缺失、截断、不完整或计算失败不能算恢复。删除/替换活动规则发 closed,原因 rule_replaced_or_removed。完整可运行示例见检测规则。
常用记录字段
公共字段:sensor_id、connection_id、service、protocol、duration_ms、error、incomplete、truncated、source/destination.ip/port。常用类型为 http.request/transaction、db.transaction/message、dns.transaction、tls.handshake、messaging.transaction/message、rpc.transaction、directory.transaction、analysis.anomaly。
自定义字段从插件 schema 加载;与当前实例完全一致的字段契约请结合 plugin info、安装包 schema 和 SDK 文档查看。内容字段默认敏感,不能通过改输出字段名规避脱敏。