要求html
1. 下载安装
可使用pip安装最新发布的ElastAlert版本:python
pip install elastalert
或者从github克隆ElastAlert项目并安装git
cd /opt/ git clone https://github.com/Yelp/elastalert.git pip install "setuptools>=11.3" python setup.py install
2. 修改配置文件github
cd /opt/elastalert cp config.yaml.example config.yaml
[root@elk elastalert]# cat config.yaml ------------------------------------------------------------------------------------------------------- rules_folder: example_rules #ElastAlert加载规则配置文件的目录位置。它将尝试加载此文件夹中的每一个.yaml文件。 run_every: #ElastAlert查询Elasticsearch的频率 minutes: 1 buffer_time: #结果缓存时间,从每一个查询运行的时间向后延伸。use_count_query或use_terms_query设置为true的规则,将忽略此值。 minutes: 15 es_host: elasticsearch.example.com #Elasticsearch集群的地址 es_port: 9200 #Elasticsearch集群的端口 #es_url_prefix: elasticsearch #可选的; Elasticsearch端点的URL前缀。 #es_send_get_body_as: GET #可选的; 查询Elasticsearch方法- GET,POST或source #es_username: someusername #可选的; 用于链接es_host的用户名。 #es_password: somepassword #可选的; 用于链接es_host的密码。 #use_ssl: True #可选的; 链接es_host是否使用TLS; 设为True或False。 #verify_certs: True #可选的; 是否验证TLS证书; 设为True或False。 #ca_certs: /path/to/cacert.pem #可选的; 用于验证SSL链接的CA证书捆绑包的路径 #client_cert: /path/to/client_cert.pem #可选的; PEM证书的路径,用做客户端证书 #client_key: /path/to/client_key.key #可选的; 私钥文件的路径,用做客户端密钥 writeback_index: elastalert_status #ElastAlert将存储数据的索引的名称。下一步会建立这个索引。 alert_time_limit: #若是报警失败,会在两天内重试 days: 2
3. 设置Elasticsearch
ElastAlert将其查询及其警报的有关信息和元数据保存回Elasticsearch。这对于审计,调试很是有用,它容许ElastAlert从新启动并从中断的位置恢复。这不是ElastAlert运行所必需的,但强烈建议。其中有 4 个 _type,都有本身的 @timestamp 字段,因此一样也能够用 kibana 来查看这个索引的日志记录状况。数组
首先,咱们须要为ElastAlert建立一个索引:缓存
[root@elk elastalert]# elastalert-create-index Elastic Version:6 Mapping used for string:{'type': 'keyword'} New index elastalert_status created Done!
有关此处将包含哪些数据的信息,请参阅ElastAlert元数据索引。app
[root@elk example_rules]# cat /opt/elastalert/example_rules/autoDispatchAdvanceJob.yaml ------------------------------------------------------------------------------------------ es_host: 10.x.x.x # Elasticsearch的地址 es_port: 9200 name: autoDispatchAdvanceJob Stop # 规则名称,不能重复,邮件标题就是这个名字 type: flatline # 每一个规则都有不一样的类型,可能采用不一样的参数。详细说明:https://elastalert.readthedocs.io/en/latest/ruletypes.html#rule-types index: elastalert_status # Elasticsearch中的索引名称,须要报警的日志 #num_events: 50 #此参数特定于frequency类型,是触发警报时的阈值。 threshold: 1 # 35分钟内查询内容须要出现的次数 timeframe: minutes: 35 # 时间间隔 filter: - query: query_string: # 须要日志出现的字符串 query: "autoDispatchAdvanceJob" alert: # 告警方式,这里使用QQ邮箱 - "email" smtp_host: smtp.qq.com smtp_port: 587 #用户认证文件,须要user和password两个属性 smtp_auth_file: /ELK/elastalert/smtp-file.yaml # 这个文件包含发件邮箱的帐号密码 email_reply_to: "my_test@qq.com" from_addr: "my_test@qq.com" email: # 能够有多个接收邮箱 - "123456789@qq.com" - "987564321@qq.com"
[root@elk example_rules]# cat /opt/elastalert/example_rules/smtp-file.yaml ---------------------------------------------------------------------------------------------------- #发送邮件的邮箱 user: "123333321@qq.com" ##不是邮箱密码,是设置的POP3密码 password: "sdffnddflcvdhbi"
ElastAlert 有如下几种自带 ruletype:elasticsearch
# cd /opt/elastalert # python -m elastalert.elastalert --config ./config.yaml --verbose
//或者单独执行 rules_folder 里的某个 rule:# python -m elastalert.elastalert --config ./config.yaml --rule ./examele_rules/autoDispatchAdvanceJob.yaml
ide
参考:https://elastalert.readthedocs.io/en/latest/running_elastalert.html#downloading-and-configuring
https://blog.csdn.net/mayifan0/article/details/78023783ui