跳至正文

编写检测规则 ​

规则在解析后的结构化记录上执行。采集过滤器控制输入范围,where/when 控制事件生成;没有命中规则的业务仍可参与可观测性统计。

访问指定 URL 时上报 ​

如果希望请求头到达就上报,而不是等响应结束,先在主配置启用:

json
{"http":{"request_events":true}}

这是合并片段;修改后重启分析器。然后新建规则文件,并加入主配置 rules 列表:

lua
rule "export_request" do
  on "http.request"
  where service == "billing" and not incomplete
  when http.method == "POST" and http.path == "/admin/export"
  emit "http.export_requested" do
    severity = "info"
    message = "Export endpoint requested"
    method = http.method
  end
end

这里每条匹配请求产生一条事件。没有输出原始 URL、正文或凭据。若需要按响应状态判断,将 on 改为 http.transaction,在 when 中加状态条件;此时无需开启请求阶段事件。http.status 是三位字符串,例如 http.status >= "500"。

限制重复事件 ​

在无窗口规则加入 group_by service 与 cooldown "30s",同探针、同服务在事件时间的 30 秒内最多发出一次。需要每次访问都记录时不要添加冷却。

按错误率触发 ​

lua
rule "database_error_rate" do
  on "db.transaction"
  where not incomplete
  group_by service, db.system
  window "60s"
  when count() >= 20 and ratio(error) >= 0.10
  emit "database.error_rate" do
    severity = "warning"
    message = "Database request error rate exceeded 10 percent"
    requests = count()
    error_ratio = ratio(error)
  end
end

窗口按事件时间对齐,允许 5 秒乱序,结束后每组最多发出一次。未知 error 不算成功;无样本、截断或缺失上下文不能直接代表健康。

试跑和安全重载 ​

sh
netflowscout rules test --config /etc/netflowscout/netflowscout.json \
  --input observations.ndjson --format ndjson --compare-rules ./candidate.nfs
sudo netflowscout check --config /etc/netflowscout/netflowscout.json
sudo systemctl reload netflowscout.service

先将已试跑的规则安装到配置引用的路径,再执行完整 check 和 reload。全部编译成功才切换;失败保留旧引擎。未修改规则保留兼容窗口、冷却和告警状态;新增或修改窗口规则重新暖机。结合状态中的规则版本与真实事件确认生效。

告警的触发和恢复 ​

需要 firing / ongoing / resolved / closed 时,使用带 lifecycle 的窗口规则。下载完整示例。恢复必须有显式健康窗口,空闲不会自动恢复。删除或替换规则生成 closed,不能当作业务恢复。完整语法见规则语言。