目标
基于 docker 运行 ElastAlert2,并集成第三方告警插件,向飞书发送报警信息;
重新 build ElastAlert2 镜像
由于官方的镜像中不包含飞书通知方式,因此需要重新打包镜像;
分别下载 ElastAlert2 和 elastalert-feishu-plugin 源码,见参考;
拷贝 elastalert-feishu-plugin 中的 elastalert_modules 目录到 ElastAlert2 的根目录下,目录结构如图:

修改 Dockerfile:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| 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"]
|
重新 build 镜像
1
| docker build -t elastalertfs:1.0 .
|
准备 elastalert 配置文件 elastalert.yaml, 修改你的 es 地址和端口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| rules_folder: /opt/elastalert/rules
run_every: seconds: 10
buffer_time: minutes: 15
es_host: es es_port: 9200
writeback_index: elastalert_status
alert_time_limit: days: 2
|
创建 rules 目录,在目录内创建报警规则文件 testrule.yaml,注意修改 index 名称、过滤条件、飞书机器人 botid 等;
创建飞书机器人可参考:
自定义机器人指南 - 客户端文档 - 开发文档 - 飞书开放平台 (feishu.cn)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| name: "testrule" type: "frequency" index: "es-index-*" is_enabled: true num_events: 1 realert: minutes: 5 terms_size: 50 timeframe: minutes: 5 timestamp_field: "@timestamp" timestamp_type: "iso" use_strftime_index: false
filter: - bool: filter: - match_all: {} - match_phrase: level: ERROR alert: - "elastalert_modules.feishu_alert.FeishuAlert"
# 飞书机器人接口地址 feishualert_url: "https://open.feishu.cn/open-apis/bot/v2/hook/" # 飞书机器人id feishualert_botid: "botid"
# 告警标题 feishualert_title: "test"
# 这个时间段内的匹配将不告警,适用于某些时间段请求低谷避免误报警 feishualert_skip: start: "01:00:00" end: "09:00:00"
# 告警内容 # 使用{}可匹配matches # 如匹配到的es数据为{"host":"aa.com","ip":"127.0.0.1"} feishualert_body: " 告警策略: {feishualert_title}\n 总请求数: {num_hits}\n 触发时间: {feishualert_time}\n 匹配域名: {host}\n 匹配ip: {ip} "
|
运行 docker,有新增的规则直接放到挂载的宿主机的 rules 目录内即可;
1 2 3 4
| docker run -d --name elastalert --restart=always \ -v $(pwd)/elastalert.yaml:/opt/elastalert/config.yaml \ -v $(pwd)/rules:/opt/elastalert/rules \ elastalertfs:1.0 --verbose
|
tips: 如果要在 k8s 上运行 elastalert2 镜像,务必注意检查容器内环境变量,elastalert2 默认环境变量优先级高于配置文件
参考及引用
https://github.com/jertel/elastalert2
gpYang/elastalert-feishu-plugin: elastalert 飞书插件 (github.com)