我是一块砖,哪里需要哪里搬!随着项目数量越来越多,总是遇到服务出现问题后都是由客户先发现问题,再一层一层的反馈到开发人员,这样不仅用户体验不好,还会出现服务挂了很长时间后才被发现,日志已经被自动清除,无法进行bug查找。基于这种情况,我们组搭建起了elk,但是仅仅有elk还是不够的,如何在故障产生后,及时的通知到相关人员这也是非常重要的。
本着开发量尽量少、功能尽量强大、对内存要求尽量低的原则,分析对比了网上多种基于日志的告警系统,大致有以下几种:
1、cat:大众点评开源的告警系统,功能强大,相对重量级;不符合需求!
2、 kafka+sparkstream:完全靠开发;不符合需求!
3、sentinl:kibana插件,友好的web ui,非常方便管理,仅支持发送邮件
安装方式非常的简单:
1)首先根据kibana的版本下载对应的版sentinl包 https://github.com/sirensolutions/sentinl/releases/tag,
./kibana-plugin install file:./sentinl-v6.0.1.zip
然后重启kibana,便可在kibana界面上看到sentinl,如下图:
sentinl的使用和安装都非常的简单,但是仅支持发送邮件,并且邮件内容中无法获取到从es上查询出的内容。不符合需求!
4、ElastAlert:无开发量;告警支持邮件、钉钉、微信、自定义等多种告警方式;能灵活从es中查询出来的内容;符合需求!
1)安装
首先在从下载源码包:网上都说master不支持es5,需要切换到es5的分支,但是并没找到es5的分支,故这里采用了es6分支,本文使用的es版本为5.4.0。ElastAlert只支持python2。上传下载后的包到服务器上,解压。
cd elastalert
pip install -r requirements.txt
python setup.py install
cp config.yaml.example config.yaml
修改config.yaml
# This is the folder that contains the rule yaml files
# Any .yaml file will be loaded as a rule
#rules_folder: example_rules
#rule目录,rules可以存在多个规则
rules_folder: rules
# How often ElastAlert will query Elasticsearch
# The unit can be anything from weeks to seconds
run_every:
#minutes: 1
#每3秒向es请求数据
seconds: 3
# ElastAlert will buffer results from the most recent
# period of time, in case some log sources are not in real time
buffer_time:
#日志会延时进入es,这里表示查询时间范围
minutes: 15
# The Elasticsearch hostname for metadata writeback
# Note that every rule can have its own Elasticsearch host
es_host: 200.200.200.65
# The Elasticsearch port
es_port: 9200
# Connect with TLS to Elasticsearch
#use_ssl: True
# Option basic-auth username and password for Elasticsearch
#es_username: someusername
#es_password: somepassword
# The index on es_host which is used for metadata storage
# This can be a unmapped index, but it is recommended that you run
# elastalert-create-index to set a mapping
writeback_index: elastalert_status
#writeback_index: logstash-2018.06.25
# If an alert fails for some reason, ElastAlert will retry
# sending the alert until this time period has elapsed
alert_time_limit:
# minutes: 2
days: 2
以上字段的解释
Rules_folder:用来加载下一阶段rule的设置,默认是example_rules
Run_every:用来设置定时向elasticsearch发送请求
Buffer_time:用来设置请求里时间字段的范围,默认是45分钟
Es_host:elasticsearch的host地址
Es_port:elasticsearch 对应的端口号
Use_ssl:可选的,选择是否用SSL连接es,true或者false
Verify_certs:可选的,是否验证TLS证书,设置为true或者false,默认为- true
Es_username:es认证的username
Es_password:es认证的password
Es_url_prefix:可选的,es的url前缀(我的理解是https或者http)
Es_send_get_body_as:可选的,查询es的方式,默认的是GET
Writeback_index:elastalert产生的日志在elasticsearch中的创建的索引
Alert_time_limit:失败重试的时间限制
修改后,执行elastalert-create-index ,会自动在es中创建索引 elastalert_status,用来保存各个rule每次的执行结果。
2)配置告警规则
ElastAlert支持11种告警规则,本文主要介绍frequency,其他的告警规则,如果后续有应用将会补上。
首先copy一份默认的
cp example_rules/example_frequency.yaml rules/test_frequency.yaml
在已有的es上随便找来一个index进行测试,如下图:
只要_type的值为syslog,就发送邮件,修改test_frequency.yaml
# Alert when the rate of events exceeds a threshold
# (Optional)
# Elasticsearch host
es_host: 200.200.200.65
# (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: "服务器都挂了你还在睡觉"
# (Required)
# Type of alert.
# the frequency rule type alerts when num_events events occur with timeframe time
type: frequency
#use_strftime_index: true
# (Required)
# Index to search, wildcard supported
#index: logstash-*
index: logstash-*
# (Required, frequency specific)
# Alert when this many documents matching the query occur within a timeframe
#在规定的时间范围内发生N次就触发事件
num_events: 1
# (Required, frequency specific)
# num_events must occur within this amount of time to trigger an alert
timeframe:
hours: 3
# (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
#过滤出_type为syslog的数据
filter:
- term:
_type: "syslog"
# some_field: "some_value"
#_ query_string
# query: "_type: syslog"
# (Required)
# The alert is use when a match is found
#告警方式设置我email
alert:
- "email"
#告警邮件主题,以及动态填充的参数,按顺序对应
alert_subject: "Error {} @{}"
alert_subject_args:
- name
- "@timestamp"
#只发送alert_text的内容
alert_text_type: alert_text_only
#增加邮件内容
alert_text: |
> "你好啊,我是帅气的笑笑"
> Name: {}
> Message: {}
> Host: {} ({})
alert_text_args:
- name
- message
- port
- host
smtp_host: smtp.163.com
smtp_port: 25
#用户认证文件,需要user和password两个属性
# smtp_auth_file.yaml,为刚才编辑的配置文件
smtp_auth_file: /home/elk/test-zx/smtp_auth_file.yaml
email_reply_to:xxx@163.com
from_addr: xxx@163.com
# (required, email specific)
# a list of email addresses to send alerts to
email:
- "xxx@163.com"
/home/elk/test-zx/smtp_auth_file.yaml配置邮箱的smtp账户和密码
user: "xxx@163.com"
password: "xxx"
可以使用下面两种方式测试上面的规则:
elastalert-test-rule --config config.yaml ruels/test_frequency.yaml
python -m elastalert.elastalert --debug --config config.yaml --rule ruels/test_frequency.yaml
触发事件内容:
在真正运行的时候,采用
python -m elastalert.elastalert --config config.yaml
即可。
这里只介绍了alert的一种方式,还支持command、钉钉、微信和自定义扩展