[ElastAlert]介绍和安装-1

来源:互联网 发布:淘宝账号不能手机登陆 编辑:程序博客网 时间:2024/04/27 17:45

写在前面的话

最近干起了运维的活;代码改造,搭建elk,搭建告警,此处做些个总结。


环境介绍

Ubuntu14
Elasticsearch 5.1.2
Kibana 5.1.2


安装

官网网址:

https://elastalert.readthedocs.io/en/latest/running_elastalert.html#tutorial

执行:

git clone https://github.com/Yelp/elastalert.git cd elastalertpython setup.py install            //可能需要sudoPip install -r requirements.txt    //可能需要sudocp config.yaml.example config.yaml

注意如果你用的是elk5.0, elastalert master还不支持,你需要自己切换分支到 support_es5


结构介绍

安装完成后会自带三个命令:

elastalert-create-index ElastAlert 会把执行记录存放到一个 ES 索引中,该命令就是用来创建这个索引的,默认情况下,索引名叫 elastalert_status。其中有 4 个 _type,都有自己的 @timestamp 字段,所以同样也可以用 kibana 来查看这个索引的日志记录情况。

注意:实际上,kibana5.0上并不能打开此索引,可能是兼容性问题,日后在想办法。

elastalert-rule-from-kibana 从 Kibana3 已保存的仪表盘中读取 Filtering 设置,帮助生成 config.yaml 里的配置。不过注意,它只会读取 filtering,不包括 queries。
没使用过

elastalert-test-rule 测试自定义配置中的 rule 设置。
注意:es5的话还不支持 test 功能


基本使用

运行命令,加载所有rules:

python -m elastalert.elastalert --config ./config.yaml

或者单独执行 rules_folder 里的某个 rule:

python -m elastalert.elastalert --config ./config.yaml --rule ./examele_rules/one_rule.yaml

Elastalert支持的警告类型

  • Command
  • Email
  • JIRA
  • OpsGenie
  • SNS
  • HipChat
  • Slack
  • Telegram
  • Debug

详见:http://elastalert.readthedocs.io/en/latest/ruletypes.html#alert-content


配制结构

和 Watcher 类似(或者说也只有这种方式),ElastAlert 配置结构也分几个部分,但是它有自己的命名。(Watcher快出个官方工具吧,纯接口太累了)


config.ymal中的配置项

  • 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:失败重试的时间限制

详见:http://elastalert.readthedocs.io/en/latest/ruletypes.html#rule-configuration-cheat-sheet


Elastalert的rule规则

  • name:配置,每个rule需要有自己独立的name,一旦重复,进程将无法启动。
  • type:配置,选择某一种数据验证方式。
  • index:配置,从某类索引里读取数据,目前已经支持Ymd格式,需要先设置 use_strftime_index:true,然后匹配索引,配置形如:index: logstash-es-test%Y.%m.%d,表示匹配logstash-es-test名称开头,以年月日作为索引后缀的index。
  • filter:配置,设置向ES请求的过滤条件。
  • timeframe:配置,累积触发报警的时长。
  • alert:配置,设置触发报警时执行哪些报警手段。不同的type还有自己独特的配置选项。目前ElastAlert 有以下几种自带ruletype:
    • 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

测试 demo

背景:我已尽用docker 搭建好了elk,日志已经接入,就差告警了。
按优先级,有个需求,就是日志 status > 500 时候就行告警。

先在kibana 上把查询语句调试好了,比较简单

status: >=500

编辑elastalert下的config.yaml文件

rules_folder: example_rulesrun_every:  seconds: 5  #代表每5秒钟轮询query es。# ElastAlert will buffer results from the most recent# period of time, in case some log sources are not in real timebuffer_time:  minutes: 5  #因为日志进入elk会有延迟,可以配制query 的向前的时间范围,这是5分钟,即查询 time[now-5m, now], 这样包括了相对多的日志数了。# The elasticsearch hostname for metadata writeback# Note that every rule can have it's own elasticsearch hostes_host: 192.168.1.100es_port: 9200#邮箱告警必配smtp_host: smtp.sina.comsmtp_port: 465#保存了邮箱验证的账号密码信息smtp_auth_file: example_rules/smtp_auth_file.yamlfrom_addr: xxxx@sina.comuse_ssl: False# Option basic-auth username and password for elasticsearch#es_username: someusername#es_password: somepassword#elastalert 需要在 es保存数据,这是实用的索引名称,可默认writeback_index: elastalert_status# If an alert fails for some reason, ElastAlert will retry# sending the alert until this time period has elapsedalert_time_limit:  days: 2

编辑example_rules/example_test.yaml 文件内容如下:

# Alert when the rate of events exceeds a threshold# (Optional)# Elasticsearch host#es_host: 192.168.1.100# (Optional)# Elasticsearch port#es_port: 9200# (OptionaL) Connect with SSL to Elasticsearch#use_ssl: false# (Optional) basic-auth username and password for Elasticsearch#es_username: someusername#es_password: somepassword# (Required)# Rule name, must be uniquename: name_alert_qycloud_status_error# (Required)# Type of alert.# the frequency rule type alerts when num_events events occur with timeframe timetype: frequency# (Required)# Index to search, wildcard supported#此规则查询所使用的索引index: monitor-*# (Required, frequency specific)# Alert when this many documents matching the query occur within a timeframenum_events: 5# (Required, frequency specific)# num_events must occur within this amount of time to trigger an alerttimeframe:  minutes: 5# (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#此处是es5 的写法filter:- query_string:    query: "status: >=500"# (Required)# The alert is use when a match is foundalert:#- "email"- "debug"- "command"#我这是用告警使用命令行,自定义更好pipe_match_json: truecommand: ["/home/df/elastalert/php_alert.php"]# (required, email specific)# a list of email addresses to send alerts toemail:- "xxxxx@qq.com"

上面的规则,说明在5分钟的时间窗口中,如果status: >=500 达到5次,就就行告警。

启动elastalert服务,监听elasticsearch

python -m elastalert.elastalert --verbose --rule example_rules/example_test.yaml

看下运行情况:

INFO:elastalert:Sleeping for 4 secondsINFO:elastalert:Queried rule name_alert_qycloud_status_error from 2017-01-29 20:05 CST to 2017-01-29 20:10 CST: 6 / 6 hitsINFO:elastalert:Alert for name_alert_qycloud_status_error at 2017-01-29T12:10:21.651Z:INFO:elastalert:name_alert_qycloud_status_errorAt least 5 events occurred between 2017-01-29 20:05 CST and 2017-01-29 20:10 CST@read_timestamp: 2017-01-29T12:10:24.043Z@timestamp: 2017-01-29T12:10:21.651Z@version: 1_id: AVnqGcU6GEG-kKWj4PKc_index: monitor-2017.01.29_type: json_php_monitorapp: AYSaaS-masterbeat: {    "hostname": "dfdeMacBook-Air.local",     "name": "dfdeMacBook-Air.local",     "version": "5.1.1"}client: 127.0.0.1elapsed: 0ent_id: error: .............

在看下告警使用的php脚本内容,只是保存告警的第一条log信息:

$fp = fopen('php://stdin', 'r');$result = '';while(!feof($fp)) {    $result .= fgets($fp, 128);}fclose($fp);file_put_contents('/tmp/alert_test', $result . "\r\n");

关于邮件报警配制

还没有配制成功。。
主要关注的配制参数:

#邮箱告警必配smtp_host: smtp.sina.comsmtp_port: 465#保存了邮箱验证的账号密码信息smtp_auth_file: example_rules/smtp_auth_file.yamlfrom_addr: xxxx@sina.com

smtp_auth_file.yaml 内容:

user:xxxpassword: xxx

参考网址

http://kibana.logstash.es/content/elasticsearch/other/elastalert.html
http://blog.csdn.net/pujiaolin/article/details/52252950?locationNum=3
http://www.chinacloud.cn/show.aspx?cid=16&id=20922

0 0
原创粉丝点击