原文地址: https://www.tony-yin.site/201...
今天聊聊ES
的告警,X-Pack
提供了报警组件Alert
,可是这个功能是须要付费,在寻求其余方案的时候,发现了ElastAlert
,能够说这是一款为ES
量身定制的告警组件,可以完美替代Alert
提供的全部功能。今天就ElastAlert
强大的告警功能和笔者实践过程当中遇到的一些问题进行分享。html
ElastAlert
是基于python2
开发的一个告警框架,它主要有如下特色:前端
网上已经有了至关多的基础介绍文章,可是笔者发现大多数文章的内容都是过期的,甚至官方文档常常还会展现一些弃用配置;还有有始无终的通病,每每不全面,常常对一些关键性的细节不说起;再者一些地方解释地不够清晰,致使歧义。笔者在搭建和测试过程当中同时借鉴多篇文章,而后在反复尝试中最后才成功,这其中失败了不少次,浪费了不少时间,因此这篇文章借鉴了上面提到的种种问题,保证本文的全面性、细节性以及具体性。python
Centos7 Elasticsearch 6.4.2 Kibana 6.4.2
安装较为简单,但为了避免有始无终仍是作一个完整的步骤介绍: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_folder
、es_host
、es_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
能够在/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-kibana
从Kibana3
已保存的仪表盘中读取Filtering
设置,帮助生成config.yaml
里的配置。不过注意,它只会读取 filtering
,不包括queries
。elastalert-test-rule
测试自定义配置中的rule
设置。执行elastalert-create-index
命令在ES
建立索引,这不是必须的步骤,可是强烈建议建立。由于对于审计和测试颇有用,而且重启ES
不影响计数和发送alert
.shell
$ elastalert-create-index
具体参见文档: setting-up-elasticsearchnpm
rule
配置算是ElastAlert
最核心的功能了,支持11
种告警规则,就不一一介绍了,选用一个最为广泛使用的告警规则frequency
,告警方式也选用最广泛的email
。json
# 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 host
、smtp port
、from addr
和to_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></center>
避免必定时间段中重复告警,能够配置realert
和exponential_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
能够自定义告警内容,内部是使用Python
的format
来实现的。
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
以前先经过elastalert-test-rule
命令来测试一下
$ elastalert-test-rule ~/elastalert/example_rules/example_frequency.yaml
详情参考文档:http://elastalert.readthedocs...
启动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></center>
any
:只要有匹配就报警;blacklist
:compare_key
字段的内容匹配上blacklist
数组里任意内容;whitelist
:compare_key
字段的内容一个都没能匹配上whitelist
数组里内容;change
:在相同query_key
条件下,compare_key
字段的内容,在 timeframe
范围内 发送变化;frequency
:在相同query_key
条件下,timeframe
范围内有num_events
个被过滤出 来的异常;spike
:在相同query_key
条件下,先后两个timeframe
范围内数据量相差比例超过spike_height
。其中能够经过spike_type
设置具体涨跌方向是- up
、down
、both
。还能够经过threshold_ref
设置要求上一个周期数据量的下限,threshold_cur
设置要求当前周期数据量的下限,若是数据量不到下限,也不触发;flatline
:timeframe
范围内,数据量小于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
,还有jira
、webhook
等内置告警方式,因为笔者没有实践,就不一一赘述了。
第三方的微信和钉钉:
也能够根据文档本身实现:https://elastalert.readthedoc...
elastalert-kibana-plugin
是围绕elastalert
作的一个kibana
展现插件,能够在kibana
上建立、编辑和删除告警,可是说实话这个插件还不是很好用,首先配置就有点麻烦,其次展现效果并不友好,提供配置rule
的方式太专业化了,对小白或者通常用户来讲要求稍高。
下载6.4.2
的release
安装包
$ 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
上面安装的只是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
默认的配置须要修改,尤为是elastalertPath
和rulesPath
中的path
选项
elastalertPath
表示的是咱们最初安装的elastalert
仓库的目录,也就是说elastalert-kibana-plugin
运行须要三个仓库,分别是elastalert
、elastalert-kibana-plugin
、和elastalert-server
,分别对应的是后端代码、前端代码、webserver
,这也就是笔者以前提到的安装提到的安装麻烦所在了;
其次rulesPath
中path
选项表示运用elastalert-kibana-plugin
插件建立告警后rule
文件存放的目录,上面笔者在elastalert
配置的rules_folder
为example_rules
,这里配置的path
为rules
,主要是由于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
这个插件,笔者认为通常般,配置过程稍显麻烦,其次功能很弱,跟后端手动修改配置文件没什么两样,也没有同名校验这些机制,相比而言,sentinl
的UI
就显得简单美观了,请听下回分解。