ES告警详解之ElastAlert

原文地址: https://www.tony-yin.site/201...

alert

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

概述

ElastAlert是基于python2开发的一个告警框架,它主要有如下特色:前端

  • 成熟,Github上已有5k+ star
  • 开源,文档很全面
  • 开发语言为python,研究和扩展源码较为容易
  • 告警规则丰富
  • 告警方式丰富
  • SMTP以每一个告警为单位,相对独立

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

软件环境

Centos7
Elasticsearch 6.4.2
Kibana 6.4.2

ElastAlert安装

安装较为简单,但为了避免有始无终仍是作一个完整的步骤介绍:git

克隆代码

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

安装依赖

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

总体配置

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

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

# 用来加载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...web

建立ElastAlert索引

能够在/usr/bin/目录下看到如下四个命令:docker

$ ll /usr/bin/elastalert*
-rwxr-xr-x 1 root root 399 Nov 20 16:39 /usr/bin/elastalert
-rwxr-xr-x 1 root root 425 Nov 20 16:39 /usr/bin/elastalert-create-index
-rwxr-xr-x 1 root root 433 Nov 20 16:39 /usr/bin/elastalert-rule-from-kibana
-rwxr-xr-x 1 root root 419 Nov 20 16:39 /usr/bin/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.shell

$ elastalert-create-index

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

Rule配置

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

# Alert when the rate of events exceeds a threshold

# (Optional)
# Elasticsearch host
es_host: 192.168.232.191

# (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次后,即知足告警条件,而后发送邮件。

邮件配置

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

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邮箱有受权码机制,因此密码处应该填写受权码(没有的话则须要开启)。

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

网易受权码设置以下图:

<center>163 client password</center>

高级配置

避免重复告警

避免必定时间段中重复告警,能够配置realertexponential_realert这两个选项:

# 5分钟内相同的报警不会重复发送
realert:
  minutes: 5

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

聚合相同告警

# 根据报警的内,将相同的报警安装 name 来聚合
aggregation_key: name

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

告警内容格式化

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

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

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

测试Rule

能够在运行rule以前先经过elastalert-test-rule命令来测试一下

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

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

运行Rule

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

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

为了让服务后台运行而且能够达到守护进程的效果,在生产环境中笔者建议使用supervisor管理。

邮件效果图以下:

<center>elastalert email</center>

其余Rule

  • any:只要有匹配就报警;
  • blacklistcompare_key字段的内容匹配上blacklist数组里任意内容;
  • whitelistcompare_key字段的内容一个都没能匹配上whitelist数组里内容;
  • change:在相同query_key条件下,compare_key字段的内容,在 timeframe范围内 发送变化;
  • frequency:在相同query_key条件下,timeframe 范围内有num_events个被过滤出 来的异常;
  • spike:在相同query_key条件下,先后两个timeframe范围内数据量相差比例超过spike_height。其中能够经过spike_type设置具体涨跌方向是- updownboth。还能够经过threshold_ref设置要求上一个周期数据量的下限,threshold_cur设置要求当前周期数据量的下限,若是数据量不到下限,也不触发;
  • flatlinetimeframe范围内,数据量小于threshold阈值;
  • new_term:fields字段新出现以前terms_window_size(默认30天)范围内最多的terms_size(默认50)个结果之外的数据;
  • cardinality:在相同 query_key条件下,timeframe范围内cardinality_field的值超过 max_cardinality或者低于min_cardinality

摘自:ElastAlert介绍和安装-1
详细请参考文档:https://elastalert.readthedoc...

其余告警方式

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

第三方的微信和钉钉:

也能够根据文档本身实现:https://elastalert.readthedoc...

elastalert-kibana-plugin

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

下载安装包

下载6.4.2release安装包

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

本地安装插件

Kibana插件本地安装

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

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

Unix:

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

Windows:

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

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

安装Server

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

克隆仓库

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

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

下载指定版本的npm

$ nvm install "$(cat .nvmrc)"

安装依赖

$ npm install

修改配置

这一步很重要,由于不少地方没有说的很清楚,包括docker运行方式在这一块也没说清楚。

$ 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配置进行同步,这个看我的喜爱自定义便可。

{
  "appName": "elastalert-server",
  "port": 3030,
  "elastalertPath": "/root/elastalert",
  "verbose": false,
  "es_debug": false,
  "debug": false,
  "rulesPath": {
    "relative": true,
    "path": "/rules"
  },
  "templatesPath": {
    "relative": true,
    "path": "/rule_templates"
  },
  "es_host": "192.168.232.191",
  "es_port": 9200,
  "writeback_index": "elastalert_status"
}

起服务

npm start

容器方式

官网提供的命令依旧是很模糊,不少同窗直接运行了,也没报错,可是也没正常运行,这是由于跟上面同样,下面这些目录都要对应修改,具体参考上面配置文件便可,最重要的仍是要明白总体架构,三个项目各自的做用,知道原理就一目了然了,但不得不说若是官方文档描述地详细一点,你们也许会更容易地搞成功。

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

总结

本文从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
相关文章
相关标签/搜索