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匹配到规则中配置的规则是就会自动向飞书推送报警消息

相关推荐
冼紫菜2 小时前
[特殊字符]实战:使用 Canal + MQ + ES + Redis + XXL-Job 打造高性能地理抢单系统
java·redis·分布式·后端·elasticsearch·rabbitmq·全文检索
程序员沉梦听雨11 小时前
【Elasticsearch】入门篇
大数据·elasticsearch·搜索引擎
Gadus_11 小时前
Elasticsearch性能优化实践
大数据·elasticsearch·搜索引擎·性能优化
八股文领域大手子15 小时前
如何给GitHub项目提PR(踩坑记录
大数据·elasticsearch·github
爱吃龙利鱼15 小时前
elk中kibana一直处于可用和降级之间且es群集状态并没有问题的解决方法
大数据·elk·elasticsearch
腾讯云大数据15 小时前
腾讯云ES一站式RAG方案获信通院“开源大模型+软件创新应用”精选案例奖
大数据·elasticsearch·开源·云计算·腾讯云
苍煜15 小时前
Elasticsearch(ES)中的脚本(Script)
大数据·elasticsearch·搜索引擎
Elastic 中国社区官方博客16 小时前
使用 LangGraph 和 Elasticsearch 构建强大的 RAG 工作流
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索
^cola^16 小时前
ES历史版本下载
elasticsearch
互联网搬砖老肖17 小时前
git 的基本使用
大数据·git·elasticsearch