elastalert的简单运用

作者: 囧雪啥都不知道 | 来源:发表于2018-06-19 15:33 被阅读5次

    elastalert是yelp使用python开发的elasticsearch告警工具。
    github: https://github.com/Yelp/elastalert

    很多人使用ELK stack来做日志收集分析,Kibana强大而方便,跟踪分析统计样样俱全,然而,但是,却有一个问题。Automation!你让我每天写query、查kibana?死给你看。

    于是yelp的懒鬼们开发了elastalert,配好yaml,你只要等着收邮件就好了。

    elastalert依照一定频率查询es,将查询结果对比告警阈值,超过阈值即进行告警。告警方式包括但不局限于邮箱、jira等。虽然官方没有提供微信等告警方式,但是也有第三方版本可以使用。

    使用elastalert需要配置rule.yaml文档,来定义你要进行告警的条件、内容。

    我们可以定义多个yaml,同时进行多种告警。也即对于每个rule.yaml,其包含一种告警发生规则,即阈值;包含一些告警方式,即email、jira等。

    使用elastalert的步骤简单分为以下几步:
    1. 安装elastalert
    2. 配置config.yaml
    3. 建立elastalert的索引
    4. 配置规则yaml
    5. 测试你的规则
    6. 运行elastalert

    elastalert文档齐全,以上步骤在官方文档种又详细介绍:
    http://elastalert.readthedocs.io/en/latest/running_elastalert.html

    我们来看一个简单的example_rule.yaml例子:

    name: Large Number of 500 Responses
    es_host: elasticsearch.example.com
    es_port: 9200
    index: logstash-responses-*
    filter:
      - term:
          response_code: 500
    type: frequency
    num_events: 100
    timeframe:
      hours: 1
    alert:
      - email
    email: example@example.com
    

    你可能一看就明白这个yaml做了哪些事情,但是我们还是把它分成三段来具体了解一下:

    1. 定义要监控的文档
    es_host: elasticsearch.example.com
    es_port: 9200
    index: logstash-responses-*
    filter:
      - term:
          response_code: 500
    

    ip、port、index、query,通过这四项来定义我们要监控的文档。filter指的是es query(DSL) 中的filter。

    在yaml中配置filter:
    http://elastalert.readthedocs.io/en/latest/recipes/writing_filters.html
    关于filter:
    https://www.elastic.co/guide/en/elasticsearch/reference/current/query-filter-context.html

    1. 选择告警模式
    type: frequency
    num_events: 100
    timeframe:
      hours: 1
    

    type即告警模式,elastalert官方提供了11种type,简单说一下:
    + any(全部): 定义要监控文档中的每个命中都会生成一个警报
    + blacklist(黑名单): 检查和名单中的特定字段,如果他们在黑名单中则匹配
    + whitelist(白名单): 与黑名单类似,此规则会将某个字段与白名单进行比较,如果白名单不包含此词,则匹配
    + change(变化): 监视某个字段,当该字段改变时匹配
    + frequency(频率): 给定事件范围内,至少有一定数量时匹配
    + spike(环比): 给定时间段内的事件量比前一时间段大或小时,匹配
    + flatline(基线): 某个时间段内的时间总数低于给阈值
    + new_term(新词): 当指定字段出现新的词时匹配
    + cardinality(基数): 当某个时间段内特定字段的唯一值总数高或低于阈值时,匹配
    + metric_aggregation(聚合): 计算窗孔中度量值高或低于阈值时匹配
    + percentage_match(百分比): 匹配值所占百分比高或低与阈值时匹配。

    FYI:http://elastalert.readthedocs.io/en/latest/ruletypes.html#rule-types

    1. 告警方式
    alert:
      - email
    email: example@example.com
    

    例子中我们选择邮件告警,发送到example@example.com

    发送邮件需要配置邮件服务器,可以配置在config.yaml中,也可以在rule.yaml分别配置。

    3.1

    # email subject
    alert_subject: "{} something occur in {}"
    alert_subject_args:
        - "@timestamp"
        - area
    
    # alert text only configured
    alert_text_type: alert_text_only
    
    # "|" means remove useless characters
    alert_text: |
        occured time: {:%Y-%m-%d %H:%M:%S}
        period: {:%Y-%m-%d %H:%M:%S} to {:%Y-%m-%d %H:%M:%S}
        recent event:
        {}
    
    alert_text_args:
        - "@timestamp"
        - starttime
        - endtime
        - match_body
    
    
    

    alert_subject 更改邮件的标题
    alert_text_type: alert_text_only 因为我们要自定义邮件内容,所以使用alert_text_only
    alert_text 自定义邮件内容,{}用于引用参数,参考python的string format。此处我需要格式化日期格式,故在{}中添加参数:%Y-%m-%d %H:%M:%S
    alert_text_args 这里的参数依次对应alert_text 中的 {}。此处可用的参数包括elastalert_status/elastalert中文档内_source/match_body下的所有字段,以及一些保留字段。可用的字段存储在此dict

    • 由于elastalert没有存储'endtime',需要在代码中取当前时间代替endtime,并存入上方dict
    • 由于yaml中不可用'@'开头,取'@timestamp'时需要加上引号;但是这会导致取出的'@timestamp' 格式为str;格式化'@timestamp'时,需要使用datetime.datetime.strptime转换

    相关文章

      网友评论

        本文标题:elastalert的简单运用

        本文链接:https://www.haomeiwen.com/subject/tgcaeftx.html