zoukankan      html  css  js  c++  java
  • elastalert

    http://blog.51cto.com/kexiaoke/1977481

    什么是?

        ElastAlert是一个简单的框架,用于从弹性搜索中的数据中提取异常,尖峰或其他感兴趣的模式。在Yelp,我们使用Elasticsearch,Logstash和Kibana来管理我们越来越多的数据和日志。 Kibana非常适合可视化和查询数据,但是我们很快就意识到,它需要一个配套工具来提醒我们的数据不一致。 在这个需求之外,ElastAlert被创建。如果您的数据正在几乎实时写入Elasticsearch,并希望在数据匹配某些模式时收到警报,那么ElastAlert是您一个很好的工具。

        安装elastalert

        克隆代码仓库到本地

    git clone https://github.com/Yelp/elastalert.git

        进入到本地代码目录,因为依赖setuptools所以先安装setuptools 

    pip install "setuptools>=11.3"

        然后安装几个依赖 yum -y install glib gcc python-devel  libffi-devel openssl-devel

        然后开始安装  elastalert

     python setup.py install

        最后根据ES版本开始安装elasticsearch

    pip install "elasticsearch>=5.0.0"

        

        安装之后会自带三个命令

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

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

      • elastalert-test-rule:测试自定义配置中的rule设置。

        Elastalert支持的告警类型:

    • Email

    • JIRA

    • OpsGenie

    • Commands

    • HipChat

    • MS Teams

    • Slack

    • Telegram

    • AWS SNS

    • VictorOps

    • PagerDuty

    • Exotel

    • Twilio

    • Gitter

        配置elastalert [config.yaml]

        接下来,打开config.yaml.example。在其中,您会找到几个配置选项。可以在不更改任何这些设置的情况下运行ElastAlert。

    rules_folder 是ElastAlert将加载规则配置文件的地方。 它将尝试加载文件夹中的每个.yaml文件。 没有任何有效的规则,ElastAlert将不会启动。 ElastAlert还会加载新的规则,停止运行缺少的规则,并在该文件夹中的文件更改时重新启动修改的规则。 对于本教程,我们将使用example_rules文件夹。

    run_every ElastAlert将如何查询Elasticsearch。

    buffer_time 是查询窗口的大小,从每个查询运行的时间向后延伸。 对于将use_count_query或use_terms_query设置为true的规则,此值将被忽略。

    es_host 是弹性搜索集群的地址,ElastAlert将存储有关其状态,查询运行,警报和错误的数据。 每个规则也可以使用不同的弹性搜索主机进行查询。

    es_port 是对应于es_host的端口。

    use_ssl: Optional; whether or not to connect to es_host using TLS; set to True or False.

    verify_certs: Optional; whether or not to verify TLS certificates; set to True or False. The default is True

    client_cert: Optional; path to a PEM certificate to use as the client certificate

    client_key: Optional; path to a private key file to use as the client key

    ca_certs: Optional; path to a CA cert bundle to use to verify SSL connections

    es_username: Optional; basic-auth username for connecting to es_host.

    es_password: Optional; basic-auth password for connecting to es_host.

    es_url_prefix: Optional; URL prefix for the Elasticsearch endpoint.

    es_send_get_body_as: Optional; Method for querying Elasticsearch - GETPOST or source. The default is GET

    writeback_index is the name of the index in which ElastAlert will store data. We will create this index later.

    alert_time_limit is the retry window for failed alerts.

    Save the file as config.yaml

        创建elastalert_status索引

        首先,我们需要为ElastAlert创建一个索引,通过运行elastalert-create-index并按照以下说明进行写入:

    elastalert-create-index
    New index name (Default elastalert_status)
    Name of existing index to copy (Default None)
    New index elastalert_status created
    Done!
    到此,elastalert已经安装完成,具体配置规则,测试规则,运行elastalert将会再写一篇文章。

    接第一篇,之前已经创建好规则了,这篇主要讲如何把安装的elastalert用起来。

        http://kexiaoke.blog.51cto.com/5530023/1977481

        创建规则

        每个规则定义要执行的查询,触发匹配的参数,以及每个匹配的触发警报列表。 我们将使用example_rules / example_frequency.yaml作为模板:

    # (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: logstash-*
    
    # (Required, frequency specific)
    # Alert when this many documents matching the query occur within a timeframe
    num_events: 50
    
    # (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:
    - term:
        some_field: "some_value"
    
    # (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:
    - "405942850@qq.com"
     

    es_host和es_port应该指向我们要查询的Elasticsearch集群。

    name是此规则的唯一名称。如果两个规则共享相同的名称,ElastAlert将不会启动。

    类型:每个规则具有不同的类型,可能会采用不同的参数。频率类型表示“在时间范围内超过num_event发生时的警报”。有关其他类型的信息,请参阅规则类型。

    index:要查询的索引的名称。如果您使用Logstash,默认情况下索引将匹配“logstash- *”。

    num_events:此参数特定于频率类型,是触发警报时的阈值。

    时间段是必须发生num_event的时间段。

    过滤器是用于过滤结果的Elasticsearch过滤器列表。这里我们有一个单字段过滤器用于some_field匹配some_value的文档。有关详细信息,请参阅编写过滤规则。如果不需要过滤器,则应将其指定为空列表:filter:[]

    警报是每个匹配上运行的警报的列表。有关警报类型的更多信息,请参阅警报。电子邮件警报需要SMTP服务器才能发送邮件。默认情况下,它将尝试使用localhost。这可以通过smtp_host选项更改。

    电子邮件是要发送警报的地址列表。

    还有许多其他可选配置选项,请参阅常见配置选项。

    所有文档必须具有时间戳字段。 ElastAlert将默认使用@timestamp,但可以使用timestamp_field选项更改。默认情况下,ElastAlert使用ISO8601时间戳,但是通过设置timestamp_type支持unix times-tamps。

    就这样,这个规则是指“在4小时内有50个以上的some_field == some_value的文件时,发送电子邮件至405942850@qq.com。

        测试规则

    elastalert-test-rule --alert example_rules/example_frequency.yaml
    官方的例子是没有加--alert的,如果不加就真的是只显示结果,不会发出邮件,如果想看结果邮件
    ,需要加上--alert参数!!!
     

        运行ElastAlert

    python -m elastalert.elastalert --verbose  --rule example_frequency.yaml
     

        以上命令代表启动后每隔1分钟(这个值是在config.yaml的

    run_every:

      minutes: 1

    指定的)查询一下最近4个小时内有没有50个以上的some_field == some_value的状态,如果有,则发出报警。千万注意是自启动以来!!!

      在elasticalert/ruletypes.py中定义的各种RuleType类构成了ElastAlert背后的主要逻辑。 在每个规则的内存中保存一个实例,通过使用给定的过滤器查询Elasticsearch返回的所有数据,并基于该数据生成匹配。

    要选择规则类型,请将type选项设置为规则配置文件中规则类型的名称:

        type: <rule type>

     

    Any    

    any:任何规则将匹配一切。 查询返回的每个命中将生成一个警报。

     

    Change --- 改变

        有关使用此规则类型的示例配置文件,请查看example_rules / example_change.yaml。 

    change:此规则将监视某个字段,并在该字段更改时进行匹配。 该领域必须改变具有相同query_key的最后一个事件。 

    该规则需要三个附加选项:

        compare_key:要监视更改的字段的名称。 由于这是字符串列表,我们可以有多个键。 如果任何一个字段发生变化,将触发警报。

        ignore_null:如果为true,则没有compare_key字段的事件不会被计数为已更改。 目前这个检查compare_key中的所有字段

        query_key:此规则应用于每个query_key的基础上。 该字段必须出现在所有检查的事件中。

    还有一个可选字段:

    timeframe:更改之间的最长时间。 在这段时间之后,ElastAlert会忘记旧的价值compare_key字段。

     

    Frequency --- 频率

    对于使用此规则类型的示例配置文件,请查看example_rules / example_frequency.yaml。

    frequency:当给定时间段内至少有一定数量的事件时,此规则匹配。 这可能

    按每查询键计算。

    此规则需要两个附加选项:

    num_events:将触发警报的事件数量。 时间范围:num_events必须发生的时间。

    timeframe:num_events必须发生的时间。

    Spike

    spike:在给定时间段内事件的体积比前一段时间大或小,这一规则匹配。它使用两个滑动窗口来比较事件的当前和参考频率。我们将这两个窗口称为“引用”和“当前”。

    这个规则需要三个附加选项:

    spike_height:事件的数量在过去的时间比之前的时间表,当将触发警报。

    spike_type:“up”,“down”或“both”。“Up”指的是规则只会在事件的次数被spike_height乘以更高时匹配。“Down”指的是比当前数字高的spike_height。“都”将匹配。

    timeframe:该规则将平均在这个时间段内发生的事件的比率。例如,小时:1表示“当前”窗口将从现在到一个小时之前,而“参考”窗口将从一个小时前到两个小时前。该规则将不处于活动状态,直到从第一个事件发生的时间至少为两个时间段。这是为了防止在确定基准率之前触发警报。这可以使用alert_on_new_data覆盖。

     

    Flatline --- 扁平线

    flatline:当事件的总数低于给定阈值一段时间时,此规则匹配。 此规则需要两个附加选项:

    threshold:不触发警报的最小事件数量。

    timeframe:必须包含小于阈值事件的时间段。

     

    Cardinality --- 基数

     

    cardinality:当某个时间范围内特定字段的唯一值总数高于或低于阈值时,此规则匹配。

    这条规则要求:

    timeframe:计算唯一值数量的时间段。

    cardinality_field:用于计算基数的字段。

    此规则需要以下两个选项之一:

    max_cardinality:如果数据的基数大于这个数字,将会触发警报。 每个提高基数的新事件都会触发警报。

    min_cardinality:如果数据的基数低于这个数字,将会触发警报。 在发送任何警报之前,必须从第一个事件开始已经过去了时间范围。 当一场比赛发生时,时间框架将被重置,并且必须在额外的警报之前再度过去。

    可选的:

    query_key:按此字段计算的组基数。 对于query_key字段的每个唯一值,基数将被分开计数。

    Percentage Match ---百分比匹配

    percentage_match:当计算窗口内的匹配桶中的文档百分比大于或小于阈值时,此规则匹配。 计算窗口默认为buffer_time。

    这条规则要求:

    match_bucket_filter:ES过滤DSL。 这为匹配桶定义了一个过滤器,它应该匹配主查询过滤器返回的文档的一个子集。

    doc_type:指定要搜索的文档的_type。

    此规则还需要以下两个选项中的至少一个:

    min_percentage:如果匹配文件的百分比小于这个数字,就会触发警报。

    max_percentage:如果匹配文档的百分比大于这个数字,将会触发警报。

     

  • 相关阅读:
    Realtime crowdsourcing
    maven 常用插件汇总
    fctix
    sencha extjs4 command tools sdk
    首次吃了一颗带奶糖味的消炎药,不知道管用不
    spring mvc3 example
    ubuntu ati driver DO NOT INSTALL recommand driver
    yet another js editor on windows support extjs
    how to use springsource tools suite maven3 on command
    ocr service
  • 原文地址:https://www.cnblogs.com/bigben0123/p/10042594.html
Copyright © 2011-2022 走看看