【Elastalert】告警模式之spike配置详解实例
打开微信扫一扫,关注微信公众号【数据与算法联盟】 转载请注明出处: http://blog.csdn.net/gamer_gyt 博主微博: http://weibo.com/234654758 Github: https://github.com/thinkgamer 写在前边的话 Elastalert是由python2.6写的一个告警框架,针对ELK日志分析系统来讲具有很大的作用,在 Elastalert-基于Elasticsearch层面的监控告警框架中,我们了解了Elastalert框架和配置使用了frequence的告警规则,这篇文章我们来看下Spike告警规则,相对来讲,spike rule不是很好理解,希望读者们能够细心阅读,有什么不懂的问题,可以留言讨论 该文永久地址:http://blog.csdn.net/Gamer_gyt/article/details/53381279 环境说明 ubuntu:16.04 Elastalert :0.14 Elasticsearch 2.4.1 Logstash 2.4.0 Kibana 4.6.1 其中的发邮件配置部分参考: Elastalert-基于Elasticsearch层面的监控告警框架 配置文件解读 # 可选的,elasticsearch 所在主机的ip地址 # es_host: elasticsearch.example.com # 可选的,elasticsearch的服务端口 # 这里的elasticsearch之所以说是可选是因为在config.ymal中已经配置过了 # es_port: 14900 # 可选,是否使用SSL加密传送数据,这是因为elasticsearch如果被search-guard或者x-pack/shield保护的话,要启用 #use_ssl: True # 可选,对应访问elasticsearch的账号和密码 #es_username: someusername #es_password: somepassword # 必选的,rule的唯一名字 name: Event spike # 必选的,rule的类型 type: spike # 必选的,监控的索引 index: logstash-* # 二选一,cur:当前窗口,ref:参照窗口 后边会着重讲述 threshold_cur: 5 #threshold_ref: 5 # 必选的,窗口参照时间 timeframe: hours: 2 # 必选的,衡论是否进行报警的一个警戒线 spike_height: 3 # 必选的,spike_type有三种类型,up,down,both spike_type: "up" # 必须的,进行匹配过滤的条件 # 更多过滤条件设置参考: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl.html filter: - query: query_string: query: "field: value" - type: value: "some_doc_type" # 必须的,报警方式 alert: - "email" # 必选的,邮件的接受地址 email: - "elastalert@example.com" 重要参数解释 spike_type: up:意味着只会匹配超过当事件发生的次数超过spike_height down:意味着只会匹配超过当事件发生的次数低于spike_height both:都会进行匹配 spike_height:最后一个时间帧中的事件数与上一个命中时触发警报的时间帧的比率 threshold_ref:要触发的警报在参考窗口中必须存在的事件的最小数量。例如,如果spike_height:3和threshold_ref:10,则“引用”窗口必须包含至少10个事件,“当前”窗口至少为触发警报的三倍。 threshold_cur:当前窗口中必须存在的触发警报的最小事件数。例如,如果spike_height:3和threshold_cur:60,则当前窗口具有多于60个事件并且参考窗口少于三分之一时,将发生警报。 (当前窗口和参照窗口,而这个窗口就是一个时间段(timeframe)) spike的报警规则 1、如果设置了threshold_ref或者threshold_cur,那么比较对应的ref或cur是否大于threshold_ref或者threshold_cur,如果大于,接着看2,否则不警报; 2、是否满足公式 ref * spike_height < cur ,如果是则警报,否则不警报。 例如: spike_type:up 12:00-14:00发生事件的次数为5,14:00-16:00发生事件的次数为20 设置threshold_cur:6,spike_height:3 由于4<6所以不进行报警 如果我们设置的threshold_cur:4 由于5>4,那么我们看是否满足公式 ref * spike_height < cur ,5*3 > 6不报警 但是如果我们设置的spike_height:1,那么5*1<6,就会进行报警 (这里不是太好理解,大家多读几遍) 实例 前提是ELK和Elastalert都配置OK logstash的配置文件rsyslog_test.conf input { tcp{ port => 5000 type => syslog } udp{ port => 5000 type => syslog } } output { stdout { codec=> rubydebug } elasticsearch { hosts => ["127.0.0.1:9200"] } } elastalert 的example_spike.ymal es_host: 127.0.0.1 es_port: 9200 name: spike rule type: spike index: logstash-* threshold_ref: 3 timeframe: minutes: 1 spike_height: 1 spike_type: "up" filter: - query: query_string: query: "host: 127.0.0.1" - term: _type: "syslog" use_count_query: true doc_type: syslog #SMTP协议的邮件服务器相关配置(我这里是163 mial) smtp_host: smtp.163.com smtp_port: 25 #用户认证文件,需要user和password两个属性 smtp_auth_file: smtp_auth_file.yaml email_reply_to: thinkgamer@163.com from_addr: thinkgamer@163.com alert: - "email" email: - "1923361654@qq.com" 邮件配置部分参考: Elastalert-基于Elasticsearch层面的监控告警框架 系统的/etc/rsyslog.conf,最后加入 *.* @@localhost:5000 *.* @localhost:5000 启动elasticsearch和logstash的rsyslog_test.conf 启动elastalert的example_spike.ymal python -m elastalert.elastalert –verbose –rule example_rules/example_spike.yaml 适当的控制系统的登录日志,最终spike窗口显示如下: master@ubuntu:/opt/elk/elastalert$ python -m elastalert.elastalert --verbose --rule rules/spike.yaml INFO:elastalert:Starting up INFO:elastalert:Queried rule spike rule from 2016-11-28 01:26 PST to 2016-11-28 01:27 PST: 0 hits INFO:elastalert:Ran spike rule from 2016-11-28 01:26 PST to 2016-11-28 01:27 PST: 0 query hits, 0 matches, 0 alerts sent INFO:elastalert:Sleeping for 59 seconds INFO:elastalert:Queried rule spike rule from 2016-11-28 01:27 PST to 2016-11-28 01:28 PST: 22 hits INFO:elastalert:Ran spike rule from 2016-11-28 01:27 PST to 2016-11-28 01:28 PST: 22 query hits, 0 matches, 0 alerts sent INFO:elastalert:Sleeping for 59 seconds INFO:elastalert:Queried rule spike rule from 2016-11-28 01:28 PST to 2016-11-28 01:29 PST: 98 hits INFO:elastalert:Ran spike rule from 2016-11-28 01:28 PST to 2016-11-28 01:29 PST: 98 query hits, 0 matches, 0 alerts sent INFO:elastalert:Sleeping for 59 seconds INFO:elastalert:Queried rule spike rule from 2016-11-28 01:29 PST to 2016-11-28 01:30 PST: 110 hits INFO:elastalert:Sent email to ['1923361654@qq.com'] INFO:elastalert:Ran spike rule from 2016-11-28 01:29 PST to 2016-11-28 01:30 PST: 110 query hits, 1 matches, 1 alerts sent INFO:elastalert:Sleeping for 59 seconds 这个时候邮箱收到邮件内容: OVER !