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

相关推荐
liuhl09109 小时前
【无标题】
大数据·elasticsearch·搜索引擎
xbgRS11 小时前
Elasticsearch的查询
elasticsearch
Elasticsearch14 小时前
使用 Lucene 搜索你的 Bean —— Highlighting
elasticsearch
Elasticsearch14 小时前
使用 Lucene 搜索你的 Bean —— Elasticsearch
elasticsearch
Elasticsearch19 小时前
最好的 LLM 只有 59% 的时间能写出正确的 Elasticsearch ES|QL。以下是另外 41% 出错的原因
elasticsearch
垚垚学技术_聚焦云原生19 小时前
Ansible Role 生产环境标准目录结构与规范化实践
java·elasticsearch·ansible
小林ixn19 小时前
从 MySQL 的 LIKE 到 ES 倒排索引:一次把全文检索和混合检索讲透
sql·elasticsearch·全文检索·agent·关键词
MayBaymax20 小时前
ES 基础总结
大数据·elasticsearch·搜索引擎
Elastic 中国社区官方博客20 小时前
两个依赖和一个配置块:通过 Prometheus 远程写入将 Spring Boot 指标发送到 Elasticsearch
大数据·数据库·spring boot·elasticsearch·搜索引擎·全文检索·prometheus
SelectDB技术团队21 小时前
ELK 太占磁盘、ES 总报写入拒绝:从归因到可执行的优化清单
大数据·clickhouse·elk·elasticsearch·全文检索·复杂查询·实时更新