Elastalert 是一款基于Elasticsearch的开源告警产品。
1、部署ElastAlert服务,使用Docker方式部署
(1)创建ElastAlert的配置文件elastalert.yaml
# 用来加载rule的目录,默认是example_rules
rules_folder: "/opt/rules"
# 用来设置定时向elasticsearch发送请求
run_every:
minutes: 1
# 用来设置请求里时间字段的范围
buffer_time:
minutes: 15
# elasticsearch的host地址
es_host: localhost
# elasticsearch 对应的端口号
es_port: 9200
# 可选的,es url前缀
#es_url_prefix:elasticsearch
# 可选的,查询es的方式,默认是GET
#es_send_get_body_as:GET
# 可选的,选择是否用SSL连接es,true或者false
#use_ssl: True
#可选的,是否验证TLS证书,设置为true或者false,默认为- true
#verify_certs: True
# es认证的username和password
es_username: admin
es_password: 123456
# elastalert产生的日志在elasticsearch中的创建的索引
writeback_index: elastalert_status
writeback_alias: elastalert_alerts
# 失败重试的时间限制
alert_time_limit:
days: 2
(2)配置ElastAlert的告警规则,以Frequency方式为例,并使用常用的Email和Post方式
# 是此规则的唯一名称。如果两个规则共享相同的名称,ElastAlert将不会启动。
name: "frequncy-test"
# Alert的类型,有11种告警规则,现在以Frequency方式为例
type: frequency
# 查询ElasticSearch中的索引名称,支持通配符
index: envoy-logs*
# 在4小时内出现5次则触发告警
num_events: 5
# 用于设置在4小时内出现5次则触发告警
timeframe:
hours: 4
# 5 分钟内出现相同告警则不重复发送
realert:
minutes: 5
# 用于查找ElasticSearch过滤器列表,查找Envoy名称为envoy1,请求URL以/apis为前缀,耗时在10ms-120ms的请求
filter:
- term:
response_code.keyword: 200
- range:
duration:
gt: 10
lt: 120
- term:
kubernetes.labels.name.keyword: envoy1
- prefix:
uri.keyword:
value: /apis
# 告警方式
alert:
- "email"
- "post"
# Post方式配置
http_post_url: "http://192.168.43.4:8000/elastalert/"
http_post_static_payload:
rule_name: "elastalert test"
rule_message: "test"
# 指定告警的字段,不填写则返回所有字段
http_post_payload:
cluster: cluster
user: test
index: _index
# Email方式配置
smtp_host: smtp.qq.com
smtp_port: 587
smtp_auth_file: "/opt/config/smtp_auth_file.yaml"
email_reply_to: "**********@qq.com"
from_addr: "**********@qq.com"
# 发送到的邮箱地址列表
email:
- "**********@qq.com"
邮箱信息配置:
user: "**********@qq.com"
password: "bfcwwbsdthylbgjf"
(3)运行ElastAlert
docker run -d --name elastalert -v `pwd`/smtp_auth_file.yaml:/opt/config/smtp_auth_file.yaml -v `pwd`/elastalert.yaml:/opt/config/elastalert_config.yaml -v `pwd`/rules:/opt/rules jertel/elastalert-docker
ElastAlert启动截图:
2、邮箱配置
以QQ邮箱为例:
smtp_auth_file.yaml中的:user填写qq邮箱,password添加授权码
QQ邮箱收到的邮件:
3、POST方式配置
Java收到的请求例子如下,具体信息需要自行处理。