elastalert系列~2.elastalert部署


部署

​ elastalert的部署可以分为三种方式,分别为:源码安装、pip安装、docker容器安装。elastalert的运行依赖是必须提前部署好elasticsearch服务,非需要安装kibana服务。

1. ElastAlert安装

1.1 源码安装

1
2
3
4
5
6
7
8
9
# 安装依赖
$ yum install gcc libffi-devel python-devel openssl-devel python-setuptools
# 拉取源码
$ git clone https://github.com/Yelp/elastalert.git
$ cd elastalert
# 安装elastalert
$ python setup.py install
# 下载依赖包
$ pip install -r requirements.txt

修改配置

1
2
3
4
# 根据模板生成配置文件
$ cp config.yaml.example config.yaml
# 修改配置
$ vim config.yaml

1.2 pip安装

1
2
3
4
5
6
7
8
# 安装pip包
$ pip install elastalert
# 添加配置
$ vim config.yaml
# 创建索引
$ elastalert-create-index
# 启动指定config.yaml文件的elastalert
$ elastalert --verbose --config config.yaml

1.3 Docker容器安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 下拉镜像
$ docker pull anjia0532/elastalert-docker
# 运行容器,依赖elasticsearch服务
$ docker run -itd --restart=always --name elastalert --network host \
-v /data/elastalert/rules:/opt/elastalert/rules \
-e ELASTICSEARCH_HOST="xx.xx.xx.xx" \
-e ELASTICSEARCH_PORT=9200 \
-e CONTAINER_TIMEZONE="Asia/Shanghai" \
-e SET_CONTAINER_TIMEZONE=True \
-e TZ="Asia/Shanghai" \
-e SET_CONTAINER_TIMEZONE=True \
-e ELASTALERT_BUFFER_TIME=10 \
-e ELASTALERT_RUN_EVERY=1 \
-e ELASTICSEARCH_USER="elastic" \
-e ELASTICSEARCH_PASSWORD='xxxxxxx' \
anjia0532/elastalert-docker

​ 共享本地/data/elastalert/rules规则文件到容器的/opt/elastalert/rules路径,rules中配置相关的yaml文件。配置文件内容如下。

2. 配置config.yaml

​ 配置文件中主要修改几个必需的选项,比如:加载规则路径参数rules_folder、elasticsearch服务的ip地址es_host、elasticsearch服务开放的端口es_port

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
# 用来加载rule的目录,默认是example_rules
rules_folder: example_rules

# 用来设置定时向elasticsearch发送请求
run_every:
minutes: 1

# 用来设置请求里时间字段的范围
buffer_time:
minutes: 15

# elasticsearch的host地址
es_host: 192.168.232.191

# 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: someusername
#es_password: somepassword

# elastalert产生的日志在elasticsearch中的创建的索引
writeback_index: elastalert_status

# 失败重试的时间限制
alert_time_limit:
days: 2

详情请参考文档:http://elastalert.readthedocs…

3. 创建ElastAlert索引

通过搜索find命令,可以找到相关的elastalert命令:

1
2
3
4
5
$ find / -name elastalert*
/xxxx/elastalert
/xxxx/elastalert-create-index
/xxxx/elastalert-rule-from-kibana
/xxxx/elastalert-test-rule
  • elastalert-create-index会创建一个索引,ElastAlert 会把执行记录存放到这个索引中,默认情况下,索引名叫 elastalert_status。其中有4_type,都有自己的@timestamp 字段,所以同样也可以用kibana来查看这个索引的日志记录情况。
  • elastalert-rule-from-kibanaKibana3已保存的仪表盘中读取Filtering 设置,帮助生成config.yaml里的配置。不过注意,它只会读取 filtering,不包括queries
  • elastalert-test-rule测试自定义配置中的rule设置。

执行elastalert-create-index命令在ES创建索引,这不是必须的步骤,但是强烈建议创建。因为对于审计和测试很有用,并且重启ES不影响计数和发送alert.

1
$ elastalert-create-index

具体参见文档: setting-up-elasticsearch

4. Rule配置

4.1 Email告警

rule配置算是ElastAlert最核心的功能了,支持11种告警规则,就不一一介绍了,选用一个最为普遍使用的告警规则frequency,告警方式也选用最普遍的email

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# Alert when the rate of events exceeds a threshold

# (Optional)
# Elasticsearch host
es_host: 192.168.xxx.xxx

# (Optional)
# Elasticsearch port
es_port: 9200

# (OptionaL) Connect with SSL to Elasticsearch
#use_ssl: True

# (Optional) basic-auth username and password for Elasticsearch
#es_username: someusername
#es_password: somepassword

# (Required)
# Rule name, must be unique
name: Example frequency rule

# (Required)
# Type of alert.
# the frequency rule type alerts when num_events events occur with timeframe time
type: frequency

# (Required)
# Index to search, wildcard supported
index: metricbeat-*

# (Required, frequency specific)
# Alert when this many documents matching the query occur within a timeframe
num_events: 5

# (Required, frequency specific)
# num_events must occur within this amount of time to trigger an alert
timeframe:
hours: 4

# (Required)
# A list of Elasticsearch filters used for find events
# These filters are joined with AND and nested in a filtered query
# For more info: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl.html
filter:
- query_string:
query: "system.process.cpu.total.pct: >10%" // field支持嵌套

smtp_host: smtp.163.com
smtp_port: 25
smtp_auth_file: /opt/elastalert/smtp_auth.yaml
#回复给那个邮箱
email_reply_to: xxx@163.com
##从哪个邮箱发送
from_addr: xxx@163.com
# (Required)
# The alert is use when a match is found
alert:
- "email"

# (required, email specific)
# a list of email addresses to send alerts to
email:
- "yyy@qq.com"

上述配置表示选择metricbeat作为告警索引,在4小时内将匹配过滤条件,当CPU使用百分比的值为10%超过5次后,即满足告警条件,然后发送邮件。

4.2 邮件配置

​ 上述配置中已经展示了一部分邮件配置,主要有smtp hostsmtp portfrom addrto_addr等。这里笔者选择一个网易163的邮箱作为发送邮箱,一个QQ邮箱作为接收邮件进行测试,所以smpt host应该为smtp.163.com

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
smtp_host: smtp.163.com
smtp_port: 25
smtp_auth_file: /opt/elastalert/smtp_auth.yaml
#回复给那个邮箱
email_reply_to: xxx@163.com
##从哪个邮箱发送
from_addr: xxx@163.com
# (Required)
# The alert is use when a match is found
alert:
- "email"
# (required, email specific)
# a list of email addresses to send alerts to
email:
- "yyy@qq.com"

还有一个smtp_auth.yaml文件,这个里面记录了发送邮箱的账号和密码,163邮箱有授权码机制,所以密码处应该填写授权码(没有的话则需要开启)。

1
2
3
4
#发送邮件的邮箱
user: xxx@163.com
##不是邮箱密码,是设置的POP3密码
password: xxx

4.3 避免重复告警

避免一定时间段中重复告警,可以配置realertexponential_realert这两个选项:

1
2
3
4
5
6
7
8
9
# 5分钟内相同的报警不会重复发送
realert:
minutes: 5

# 指数级扩大 realert 时间,中间如果有报警,
# 则按照5->10->20->40->60不断增大报警时间到制定的最大时间,
# 如果之后报警减少,则会慢慢恢复原始realert时间
exponential_realert:
hours: 1

4.4 聚合相同告警

1
2
3
4
5
6
7
# 根据报警的内,将相同的报警 根据name来聚合
aggregation_key: name

# 聚合报警的内容,只展示 namemessage
summary_table_fields:
- name
- message

4.5 告警内容格式化

​ 可以自定义告警内容,内部是使用Pythonformat来实现的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
alert_subject: "Error {} @{}"
alert_subject_args:
- name
- "@timestamp"

alert_text_type: alert_text_only
alert_text: |
### Error frequency exceeds
> Name: {}
> Message: {}
> Host: {} ({})
alert_text_args:
- name
- message
- hostname
- host

当然还有更多高级配置,详情请参考文档。

4.6 调试测试Rule

【手动测试】可以在运行rule之前先通过elastalert-test-rule命令来测试一下

1
$ elastalert-test-rule ~/elastalert/example_rules/example_frequency.yaml

详情参考文档:http://elastalert.readthedocs…

4.7 正式运行Rule

【手动运行】启动elastalert服务,监听es,这里加了--rule example_frequency.yaml表示只运行example_frequency.yaml这一个rule文件,如果不加该选项则会运行rules_folder下所有rule文件,上面配置中的rules_folder为默认的example_rules

1
$ python -m elastalert.elastalert --verbose --rule example_frequency.yaml

为了让服务后台运行并且可以达到守护进程的效果,在生产环境中推荐使用supervisor进行管理

5. elastalert-kibana-plugin插件

elastalert-kibana-plugin是围绕elastalert做的一个kibana展示插件,可以在kibana上创建、编辑和删除告警。

关于elastalert-kibana-plugin插件的使用与配置很复杂,暂不写

总结:

​ 官方网站:https://github.com/Yelp/elastalert 也提供了容器化运行elastalert的配置,但是官网提供的命令依旧是很模糊,命令中的目录都要对应修改,具体参考上面配置文件即可,最重要的还是要明白整体架构,这样才能用好elastalert。

1
2
3
4
5
6
7
docker run -d -p 3030:3030 \
-v `pwd`/config/elastalert.yaml:/opt/elastalert/config.yaml \
-v `pwd`/config/config.json:/opt/elastalert-server/config/config.json \
-v `pwd`/rules:/opt/elastalert/rules \
-v `pwd`/rule_templates:/opt/elastalert/rule_templates \
--net="host" \
--name elastalert bitsensor/elastalert:latest

文章作者: 王海飞
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 王海飞 !
  目录