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

相关推荐
Elastic 中国社区官方博客13 小时前
AI 可观察性:公共部门使命韧性的支柱
大数据·人工智能·功能测试·elasticsearch·搜索引擎·语言模型·全文检索
Elastic 中国社区官方博客13 小时前
可执行操作的 AI agents:使用 Agent Builder 和 Workflows 自动化 IT 请求
大数据·数据库·人工智能·elasticsearch·搜索引擎·自动化·全文检索
玄〤14 小时前
Elasticsearch 概念与基础实操 (索引、映射与文档操作)(黑马微服务课day12)
大数据·elasticsearch·微服务·全文检索
海兰14 小时前
Elasticsearch Mapping 解析
大数据·elasticsearch·搜索引擎
海兰14 小时前
Elasticsearch 自管理集群配置指南
大数据·elasticsearch·jenkins
海兰15 小时前
Elasticsearch 全文检索概述
elasticsearch·serverless·全文检索
海兰15 小时前
Elasticsearch 搜索方案与技术栈深度解析
大数据·elasticsearch·django
Elasticsearch1 天前
Elasticsearch:使用 Workflow 查询天气,发送消息到 Slack
elasticsearch
Elasticsearch1 天前
可执行操作的 AI agents:使用 Agent Builder 和 Workflows 自动化 IT 请求
elasticsearch
闻哥1 天前
Elasticsearch查询优化实战:从原理到落地的全方位调优指南
java·大数据·elasticsearch·搜索引擎·面试·全文检索·springboot