ElastAlert通过飞书机器人发送报警通知

前言

公司采用ELK架构搜集业务系统的运行日志,以前开发人员只有在业务出现问题的时候,才会去kibana上进行日志搜索操作,每次都是被用户告知系统出问题了,这简直是被啪啪打脸~

于是痛定思痛,决定主动出击,查阅了许多资料,发现ElastAlert这个服务能够对elasticsearch的索引按条件进行监控,并在匹配设置的条件时自动触发告警,于是决定采用ElastAlert。

重新build ElastAlert2镜像

由于官方的镜像中不包含飞书通知方式,因此我们需要重新打包镜像;

分别下载ElastAlert2和elastalert-feishu-plugin源码

复制代码
源码下载地址:
https://github.com/jertel/elastalert2
https://github.com/gpYang/elastalert-feishu-plugin

拷贝elastalert-feishu-plugin中的elastalert_modules目录到ElastAlert2的根目录下,目录结构如图:

切换到ElastAlert2目录下,编写Dockerfile文件

复制代码
FROM python:3-slim-buster as builder

LABEL description="ElastAlert 2 Official Image"
LABEL maintainer="Jason Ertel"

COPY . /tmp/elastalert

RUN mkdir -p /opt/elastalert && \
cd /tmp/elastalert && \
pip install setuptools wheel && \
python setup.py sdist bdist_wheel

FROM python:3-slim-buster
ARG GID=1000
ARG UID=1000
ARG USERNAME=elastalert
COPY --from=builder /tmp/elastalert/dist/*.tar.gz /tmp/

RUN apt update && apt -y upgrade && \
	apt -y install jq curl gcc libffi-dev && \
	rm -rf /var/lib/apt/lists/* && \
	pip install /tmp/*.tar.gz && \
	rm -rf /tmp/* && \
	apt -y remove gcc libffi-dev && \
	apt -y autoremove && \
	mkdir -p /opt/elastalert && \
	mkdir -p /usr/local/lib/python3.10/site-packages/elastalert/elastalert_modules && \
	echo "#!/bin/sh" >> /opt/elastalert/run.sh && \
	echo "set -e" >> /opt/elastalert/run.sh && \
 	echo "elastalert-create-index --config /opt/elastalert/config.yaml" \
        >> /opt/elastalert/run.sh && \
	echo "elastalert --config /opt/elastalert/config.yaml \"\$@\"" \
    	>> /opt/elastalert/run.sh && \
	chmod +x /opt/elastalert/run.sh && \
	groupadd -g ${GID} ${USERNAME} && \
	useradd -u ${UID} -g ${GID} -M -b /opt -s /sbin/nologin \
    	-c "ElastAlert 2 User" ${USERNAME}
	COPY ./elastalert_modules/feishu_alert.py /usr/local/lib/python3.10/site-packages/elastalert/elastalert_modules/
	COPY ./elastalert_modules/__init__.py /usr/local/lib/python3.10/site-packages/elastalert/elastalert_modules/
	USER ${USERNAME}
	ENV TZ ""Asia/Shanghai""

WORKDIR /opt/elastalert
ENTRYPOINT ["/opt/elastalert/run.sh"]

构建镜像

复制代码
docker build -t elastalertfs:1.0 . 

我们在物理机上创建/data/elastalertfs目录,存放配置文件和报警规则目录及文件

在/data/elastalertfs下编写elastalert的配置文件config.yaml

复制代码
#指定告警文件存放目录
rules_folder: /opt/elastalert/rules

#ElastAlert查询Elasticsearch的频率,这个单位可以是几周到几秒不等
run_every:
	minutes: 1

#ElastAlert将缓冲最近的一段时间的结果,以防某些日志源不是实时的
buffer_time:
	minutes: 30

#Elasticsearch主机
es_host: 192.168.105.147

#Elasticsearch端口
es_port: 9200

#es_host上的索引,用于元数据存储。这可以是一个未映射的索引,但建议你运行。设置一个映射
writeback_index: elastalert_status

#如果一个警报因某种原因而失败,ElastAlert将重试,直到这个时间段过后
alert_time_limit:
	days: 2

在/data/elastalertfs下创建rules目录,在目录内创建报警规则文件xxx-xxx_error.yaml,可以在该目录下创建多个报警规则文件,注意修改index名称、过滤条件、飞书机器人botid等;

复制代码
name: "niu_cloud_rule"
type: "any"			// 有多种类型,不同的类型配置项有不同
index: "niu-cloud-logs-*"
is_enabled: true

#时间触发的次数
#num_events: 1

#和num_events参数关联,1分钟内出现1次会报警
#timeframe:
#  minutes: 1

#同一规则的两次警报之间的最短时间。在此时间内发生的任何警报都将被丢弃。默认值为一分钟。
#realert:
#  minutes: 0

#terms_size: 50

use_strftime_index: true

filter:
- query:
	query_string:
  	query: "level.keyword : ERROR AND msg : java*Exception"   //错误级别是ERROR并且msg字段包含java开头Exception结尾的内容就匹配成功,elastalert就会推送报警
  	
include: ["@timestamp","_index", "module", "level", "msg"]	// 这里是es索引中的字段,下边报警模板会使用

alert:
- "elastalert_modules.feishu_alert.FeishuAlert"

# 飞书机器人接口地址
feishualert_url: "https://open.feishu.cn/open-apis/bot/v2/hook/"

# 飞书机器人id
feishualert_botid: "xxxxxx-xxx-xxxx-xxxx-xxxxxxxxxxx"

# 告警标题
feishualert_title: "niu-cloud业务ERROR异常"

# 这个时间段内的匹配将不告警,适用于某些时间段请求低谷避免误报警
#feishualert_skip:
#  start: "01:00:00"
#  end: "08:00:00"

# 告警内容,使用{}可匹配matches
feishualert_body:
"
【告警主题】: {feishualert_title}\n
【告警时间】: {feishualert_time}\n
【告警模块】: {module}\n
【业务索引】: {_index}\n
【时间戳】: {@timestamp}\n
【日志级别】: {level}\n
【错误日志】: {msg}
"

docker方式运行并挂载配置目录

复制代码
docker run -d --restart=always --name elastalertfs -v /data/elastalertfs/config.yaml:/opt/elastalert/config.yaml -v /data/elastalertfs/rules:/opt/elastalert/rules -v /etc/localtime:/etc/localtime elastalertfs:v1.0

当elastalert监控es匹配到规则中配置的规则是就会自动向飞书推送报警消息

相关推荐
Elasticsearch12 小时前
使用 Jina 远程 MCP 服务器的 Agentic 工作流
elasticsearch
Elastic 中国社区官方博客12 小时前
在 Elastic 中使用 OpenTelemetry 内容包可视化 OpenTelemetry 数据
大数据·开发语言·数据库·elasticsearch·搜索引擎
Arva .16 小时前
ES 面试
elasticsearch·面试
鸿蒙程序媛16 小时前
【工具汇总】git 常用命令行汇总
大数据·git·elasticsearch
Elasticsearch17 小时前
多大才算太大?Elasticsearch 容量规划最佳实践
elasticsearch
Elastic 中国社区官方博客18 小时前
用于 IntelliJ IDEA 的新 ES|QL 插件
java·大数据·数据库·ide·elasticsearch·搜索引擎·intellij-idea
大志哥12319 小时前
整理安装ES和Logstash
大数据·elasticsearch·搜索引擎
Slow菜鸟19 小时前
Git Worktree 使用教程
大数据·git·elasticsearch
大气层煮月亮2 天前
RAG 检索技术 - Elasticsearch
大数据·elasticsearch·搜索引擎
Dontla2 天前
异步知识库索引管线:与在线问答链路解耦架构介绍(离线构建,在线查询)分层索引、Elasticsearch
elasticsearch·架构