[关闭]
@tony-yin 2018-12-16T16:42:23.000000Z 字数 8936 阅读 2851

ES告警详解之ElastAlert

ES


今天聊聊ES的告警,X-Pack提供了报警组件Alert,但是这个功能是需要付费,在寻求其他方案的时候,发现了ElastAlert,可以说这是一款为ES量身定制的告警组件,能够完美替代Alert提供的所有功能。今天就ElastAlert强大的告警功能和笔者实践过程中遇到的一些问题进行分享。

alert

概述

ElastAlert是基于python2开发的一个告警框架,它主要有以下特点:

网上已经有了相当多的基础介绍文章,但是笔者发现大多数文章的内容都是过时的,甚至官方文档经常还会展示一些弃用配置;还有虎头蛇尾的通病,往往不全面,经常对一些关键性的细节不提及;再者一些地方解释地不够清晰,导致歧义。笔者在搭建和测试过程中同时借鉴多篇文章,然后在反复尝试中最后才成功,这其中失败了很多次,浪费了很多时间,所以这篇文章借鉴了上面提到的种种问题,保证本文的全面性、细节性以及具体性。

软件环境

  1. Centos7
  2. Elasticsearch 6.4.2
  3. Kibana 6.4.2

ElastAlert安装

安装较为简单,但为了不虎头蛇尾还是做一个完整的步骤介绍:

克隆代码

  1. $ git clone https://github.com/Yelp/elastalert.git

安装依赖

  1. $ cd elastalert
  2. $ python setup.py install
  3. $ pip install -r requirements.txt

整体配置

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

主要修改几个必需的选项,比如rules_folderes_hostes_port等,那些非必需没有特殊需求就不用更改了:

  1. # 用来加载rule的目录,默认是example_rules
  2. rules_folder: example_rules
  3. # 用来设置定时向elasticsearch发送请求
  4. run_every:
  5. minutes: 1
  6. # 用来设置请求里时间字段的范围
  7. buffer_time:
  8. minutes: 15
  9. # elasticsearch的host地址
  10. es_host: 192.168.232.191
  11. # elasticsearch 对应的端口号
  12. es_port: 9200
  13. # 可选的,es url前缀
  14. #es_url_prefix:elasticsearch
  15. # 可选的,查询es的方式,默认是GET
  16. #es_send_get_body_as:GET
  17. # 可选的,选择是否用SSL连接es,true或者false
  18. #use_ssl: True
  19. #可选的,是否验证TLS证书,设置为true或者false,默认为- true
  20. #verify_certs: True
  21. # es认证的username和password
  22. #es_username: someusername
  23. #es_password: somepassword
  24. # elastalert产生的日志在elasticsearch中的创建的索引
  25. writeback_index: elastalert_status
  26. # 失败重试的时间限制
  27. alert_time_limit:
  28. days: 2

详情请参考文档:http://elastalert.readthedocs.io/en/latest/ruletypes.html#rule-configuration-cheat-sheet

创建ElastAlert索引

可以在/usr/bin/目录下看到以下四个命令:

  1. $ ll /usr/bin/elastalert*
  2. -rwxr-xr-x 1 root root 399 Nov 20 16:39 /usr/bin/elastalert
  3. -rwxr-xr-x 1 root root 425 Nov 20 16:39 /usr/bin/elastalert-create-index
  4. -rwxr-xr-x 1 root root 433 Nov 20 16:39 /usr/bin/elastalert-rule-from-kibana
  5. -rwxr-xr-x 1 root root 419 Nov 20 16:39 /usr/bin/elastalert-test-rule

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

  1. $ elastalert-create-index

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

Rule配置

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

  1. # Alert when the rate of events exceeds a threshold
  2. # (Optional)
  3. # Elasticsearch host
  4. es_host: 192.168.232.191
  5. # (Optional)
  6. # Elasticsearch port
  7. es_port: 9200
  8. # (OptionaL) Connect with SSL to Elasticsearch
  9. #use_ssl: True
  10. # (Optional) basic-auth username and password for Elasticsearch
  11. #es_username: someusername
  12. #es_password: somepassword
  13. # (Required)
  14. # Rule name, must be unique
  15. name: Example frequency rule
  16. # (Required)
  17. # Type of alert.
  18. # the frequency rule type alerts when num_events events occur with timeframe time
  19. type: frequency
  20. # (Required)
  21. # Index to search, wildcard supported
  22. index: metricbeat-*
  23. # (Required, frequency specific)
  24. # Alert when this many documents matching the query occur within a timeframe
  25. num_events: 5
  26. # (Required, frequency specific)
  27. # num_events must occur within this amount of time to trigger an alert
  28. timeframe:
  29. hours: 4
  30. # (Required)
  31. # A list of Elasticsearch filters used for find events
  32. # These filters are joined with AND and nested in a filtered query
  33. # For more info: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl.html
  34. filter:
  35. - query_string:
  36. query: "system.process.cpu.total.pct: >10%" // field支持嵌套
  37. smtp_host: smtp.163.com
  38. smtp_port: 25
  39. smtp_auth_file: /opt/elastalert/smtp_auth.yaml
  40. #回复给那个邮箱
  41. email_reply_to: xxx@163.com
  42. ##从哪个邮箱发送
  43. from_addr: xxx@163.com
  44. # (Required)
  45. # The alert is use when a match is found
  46. alert:
  47. - "email"
  48. # (required, email specific)
  49. # a list of email addresses to send alerts to
  50. email:
  51. - "yyy@qq.com"

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

邮件配置

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

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

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

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

高级配置

避免重复告警

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

  1. # 5分钟内相同的报警不会重复发送
  2. realert:
  3. minutes: 5
  4. # 指数级扩大 realert 时间,中间如果有报警,
  5. # 则按照5->10->20->40->60不断增大报警时间到制定的最大时间,
  6. # 如果之后报警减少,则会慢慢恢复原始realert时间
  7. exponential_realert:
  8. hours: 1

聚合相同告警

  1. # 根据报警的内,将相同的报警安装 name 来聚合
  2. aggregation_key: name
  3. # 聚合报警的内容,只展示 name 与 message
  4. summary_table_fields:
  5. - name
  6. - message

告警内容格式化

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

  1. alert_subject: "Error {} @{}"
  2. alert_subject_args:
  3. - name
  4. - "@timestamp"
  5. alert_text_type: alert_text_only
  6. alert_text: |
  7. ### Error frequency exceeds
  8. > Name: {}
  9. > Message: {}
  10. > Host: {} ({})
  11. alert_text_args:
  12. - name
  13. - message
  14. - hostname
  15. - host

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

测试Rule

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

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

详情参考文档:http://elastalert.readthedocs.io/en/latest/running_elastalert.html#testing-your-rule

运行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管理。

其他Rule

摘自:ElastAlert介绍和安装-1
详细请参考文档:https://elastalert.readthedocs.io/en/latest/ruletypes.html#rule-types

其他告警方式

除了email,还有jirawebhook等内置告警方式,由于笔者没有实践,就不一一赘述了。

第三方的微信和钉钉:

也可以根据文档自己实现:https://elastalert.readthedocs.io/en/latest/recipes/adding_alerts.html

elastalert-kibana-plugin

elastalert-kibana-plugin是围绕elastalert做的一个kibana展示插件,可以在kibana上创建、编辑和删除告警,但是说实话这个插件还不是很好用,首先配置就有点麻烦,其次展示效果并不友好,提供配置rule的方式太专业化了,对小白或者一般用户来说要求稍高。

下载安装包

下载6.4.2release安装包

  1. $ wget https://github.com/bitsensor/elastalert-kibana-plugin/releases/download/1.0.1/elastalert-kibana-plugin-1.0.1-6.4.2.zip

本地安装插件

Kibana插件本地安装

  1. $ /usr/share/kibana/bin/kibana-plugin install file:///root/elastalert-kibana-plugin-1.0.1-6.4.2.zip

本地安装前面需要加上file://,否则会默认为在线资源去解析url并下载

Unix:

  1. $ sudo bin/elasticsearch-plugin install file:///path/to/plugin.zip

Windows:

假定需要安装的插件本地地址为C:\path\to\plugin.zip

  1. $ bin\elasticsearch-plugin install file:///C:/path/to/plugin.zip

安装Server

上面安装的只是kibana的一个展示插件,插件内部并没有集成server,所以还需要再安装一个server,笔者之前因为没有做这一步,一直卡着,页面显示报错502 Bad Gateway,关键是官方文档也没说清楚一定要装这个。。

克隆仓库

  1. $ git clone https://github.com/bitsensor/elastalert.git elastalert-server
  2. $ cd elastalert-server

这边我们先不用官网说的docker运行的方式,先用本地npm起服务的方式运行。

下载指定版本的npm

  1. $ nvm install "$(cat .nvmrc)"

安装依赖

  1. $ npm install

修改配置

这一步很重要,因为很多地方没有说的很清楚,包括docker运行方式在这一块也没说清楚。

  1. $ vim config/config.json

默认的配置需要修改,尤其是elastalertPathrulesPath中的path选项

elastalertPath表示的是我们最初安装的elastalert仓库的目录,也就是说elastalert-kibana-plugin运行需要三个仓库,分别是elastalertelastalert-kibana-plugin、和elastalert-server,分别对应的是后端代码、前端代码、webserver,这也就是笔者之前提到的安装提到的安装麻烦所在了;

其次rulesPathpath选项表示运用elastalert-kibana-plugin插件创建告警后rule文件存放的目录,上面笔者在elastalert配置的rules_folderexample_rules,这里配置的pathrules,主要是因为elastalert-server目录下用的是这个,笔者也在elastalert项目中创建了个rules的目录,并将rules_folder配置进行同步,这个看个人喜好自定义即可。

  1. {
  2. "appName": "elastalert-server",
  3. "port": 3030,
  4. "elastalertPath": "/root/elastalert",
  5. "verbose": false,
  6. "es_debug": false,
  7. "debug": false,
  8. "rulesPath": {
  9. "relative": true,
  10. "path": "/rules"
  11. },
  12. "templatesPath": {
  13. "relative": true,
  14. "path": "/rule_templates"
  15. },
  16. "es_host": "192.168.232.191",
  17. "es_port": 9200,
  18. "writeback_index": "elastalert_status"
  19. }

起服务

  1. npm start

容器方式

官网提供的命令依旧是很模糊,很多同学直接运行了,也没报错,但是也没正常运行,这是因为跟上面一样,下面这些目录都要对应修改,具体参考上面配置文件即可,最重要的还是要明白整体架构,三个项目各自的作用,知道原理就一目了然了,但不得不说如果官方文档描述地详细一点,大家也许会更容易地搞成功。

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

总结

本文从elastalert的安装讲起,接着涉猎rule配置、email配置等环节,然后通过测试和运行来对rule文件进行验证,最后再详细介绍了elastalert-kibana-plugin的安装和用法。

总的来说,elastalert围绕es所提供的告警功能是很强大的,文中提供的案例只是冰山一角,大家感兴趣的可以多看看官方文档,elastalert的官方文档还是很全的。

至于elastalert-kibana-plugin这个插件,笔者认为一般般,配置过程稍显麻烦,其次功能很弱,跟后端手动修改配置文件没什么两样,也没有同名校验这些机制,相比而言,sentinlUI就显得简单美观了,请听下回分解。

refer

  1. ElastAlert监控日志告警Web攻击行为
  2. ElastAlert:『Hi,咱服务挂了』
  3. ElastAlert介绍和安装-1
  4. elastalert的简单运用
  5. ElastAlert 基于Elasticsearch的监控告警
  6. elastAlert之kibana的插件使用
  7. Install plugins
  8. plugin management custom url
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注