什么是elastalert?

    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将会再写一篇文章。