侧边栏壁纸
博主头像
福福不服博主等级

孩子会穿过大雨,去懂人间的道理。

  • 累计撰写 92 篇文章
  • 累计创建 98 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

ElastAlert2告警不分词查询

Monster
2023-09-13 / 0 评论 / 2 点赞 / 97 阅读 / 2888 字 / 正在检测是否收录...
温馨提示:
请确保在评论和互动中保持礼貌和尊重。避免使用侮辱性、歧视性或攻击性语言。我们鼓励建设性的讨论和意见交流。

原查询条件

需求是带“服务商回传提单异常”的日志捞出来告警,当我按以下查询条件告警时会发现匹配不到我想要的日志

原因:query_string查询默认就是模糊查询(并且是分词查询),例如”123“会拆分查询是否带否带1、带2、带3的日志

条件不要*查询,不然拆分一个*出来匹配,会每条日志都告警

filter:
- query:
    query_string:
      query: "type: production AND message: 服务商回传提单异常 AND level: DEBUG AND source: easyspeed-module-booking"

不分词查询条件

match_phrase:表示精确短语查询,例如”我是真滴帅“必须带有这五个字的才匹配,只有”我是真滴“不行必须要带上”帅“才匹配

我这里这块日志是知道在booking模块的,并且日志记录类型为DEBUG且是production环境的

filter:
  - bool:
      filter:
        - match_all: {}
        - match_phrase:
        	type: production
        - match_phrase:
            message: 服务商回传提单异常
        - match_phrase:
            level: DEBUG
        - match_phrase:
            source: easyspeed-module-booking

完整yaml规则

#rule name 必须是独一的,不然会报错,这个定义完成之后,会成为报警的标题
name: alert_tidanDebug
 
#配置的是frequency,需要两个条件满足,在相同 query_key条件下,timeframe 范围内有num_events个被过滤出来的异常
type: frequency
 
#指定index,支持正则匹配同时如果嫌麻烦直接* 也可
index: easyspeed-cloud-logs-*

#为false表示停止执行规则
is_enabled: true
 
#时间触发的次数
num_events: 1
 
#和num_events参数关联,也就是说3分钟内出现1次会报警
timeframe:
  minutes: 3
 
#同一规则的两次警报之间的最短时间。在此时间内发生的任何警报都将被丢弃。默认值为一分钟。
#realert:
#  minutes: 1
 
#防止同一条规则在一段时间内发出两次警报
#realert:
#  days: 1
 
 
#es查询告警条件
filter:
  - bool:
      filter:
        - match_all: {}
        - match_phrase:
        	type: production
        - match_phrase:
            message: 服务商回传提单异常
        - match_phrase:
            level: DEBUG 
        - match_phrase:
            source: easyspeed-module-booking
#filter:
#- query:
#    query_string:
#      query: "type: production AND message: 服务商回传提单异常 AND level: DEBUG AND source: easyspeed-module-booking"

#只需要的字段 https://elastalert.readthedocs.io/en/latest/ruletypes.html#include
include: ["spanId", "level", "@timestamp", "_index", "source", "traceId","host","message"]
 
#告警方式
alert:
- "elastalert_modules.feishu_alert.FeishuAlert"
 
 
 
# 飞书机器人接口地址
feishualert_url: "https://open.feishu.cn/open-apis/bot/v2/hook/"
 
# 飞书机器人id
feishualert_botid:
  "ee0192bb-5c23-4fxxxxxxxxxxxxx"
 
# 告警标题
feishualert_title:
  "服务商回传提单异常"

#排除告警的时间段
#feishualert_skip:
#  start: "00:00:00"
#  end: "08:00:00"
 
 
# 告警内容
# 使用{}可匹配matches
feishualert_body:
  "
  【告警主题】:  {feishualert_title}\n
  【告警时间】:  {feishualert_time}\n
#告警环境直接手动固定production了,查询条件我已经匹配了是要查production环境的。主要是告警配置文件type: frequency和日志平台里面的字段名也叫type,这里指定type会冲突报错。还想通过告警配置type不指定方式,让elastalert使用默认值,查询官方文档必须要手动指定哭死。
  【告警环境】: 【production】\n
  【告警模块】:  {source}\n
  【日志级别】:  {level}\n
  【spanId】:  {spanId}\n
  【traceId】:  {traceId}\n 
  【host】:  {host}\n
  【message】:  {message}
  "

成功告警通知匹配

2

评论区