本文共 5444 字,大约阅读时间需要 18 分钟。
要求
1. 下载安装
可以使用pip安装最新发布的ElastAlert版本:pip install elastalert
或者从github克隆ElastAlert项目并安装
cd /opt/git clone https://github.com/Yelp/elastalert.gitpip install "setuptools>=11.3"python setup.py install
2. 修改配置文件
cd /opt/elastalertcp 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: 15es_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-indexElastic Version:6Mapping used for string:{'type': 'keyword'}New index elastalert_status createdDone!
有关此处将包含哪些数据的信息,请参阅
[root@elk example_rules]# cat /opt/elastalert/example_rules/autoDispatchAdvanceJob.yaml------------------------------------------------------------------------------------------es_host: 10.x.x.x # Elasticsearch的地址es_port: 9200name: autoDispatchAdvanceJob Stop # 规则名称,不能重复,邮件标题就是这个名字type: flatline # 每个规则都有不同的类型,可能采用不同的参数。详细说明:https://elastalert.readthedocs.io/en/latest/ruletypes.html#rule-typesindex: 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:
# 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
参考:
转载于:https://blog.51cto.com/qiangsh/2171812