目标
基于docker运行ElastAlert2,并集成第三方告警插件,向飞书发送报警信息;
重新build ElastAlert2镜像
由于官方的镜像中不包含飞书通知方式,因此需要重新打包镜像;
分别下载ElastAlert2和elastalert-feishu-plugin源码,见参考;
拷贝elastalert-feishu-plugin中的elastalert_modules目录到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"]
重新build镜像
docker build -t elastalertfs:1.0 .
准备elastalert配置文件elastalert.yaml,修改你的es地址和端口
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)
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目录内即可;
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)