elastalert 配置语法:html
简单rule规则:nginx
logstash-*
query
查询语法,将须要匹配的信息给匹配compare_key: "request" blacklist: - /index.html #request字段匹配有请求/index.html就报警 - "!file /tmp/blacklist1.txt" - "!file /tmp/blacklist2.txt"
blacklist
,此规则会将某个字段与白名单进行比较,若是列表中不包含该字词则匹配。(过滤器)
compare_key
字段的事件不匹配compare_key: "request" ignore_null: "true" whitelist: - /index.html #request字段匹配过滤请求/index.html的请求 - "!file /tmp/blacklist1.txt" - "!file /tmp/blacklist2.txt"
compare_key
字段的事件将不计为已更改query_key
计数
num_events
数量type: frequency index: n-nanjing-console num_events: 5 timeframe: minutes: 1 filter: - term: status: "404"
最近一分钟内触发五次404的请求才触发报警后端
用处比较流量突起,温度计api
timeframe
时间段内的比较
水平线如下触发报警app
threshold
一段时间内事件总数低于给定时间时,此规则匹配
字段的值与30天前的数据是不是新出现,如比较后是新值,则触发报警elasticsearch
基线上下的值,触发报警测试
metric_agg_key
字段上执行聚合操做。聚合类型:min
,max
,avg
,sum
,cardinality
,value_count
buffer_time
注: 多个type能够写在一个规则配置文件中,按顺序进行匹配url
lucene语法规则code
咱们在使用的时候要预防报警风暴(在实际使用中咱们遇到过可能一分钟成百上千的错误,要是都是发出来,就有问题了)。咱们利用下面的一些措施来控制报警风暴:
1 aggregation: 设置一个时长,则该时长内,全部的报警(同一个配置文件内的报警)最终合并在一块儿发送一次:
2 realert: 设置一个时长,在该时间内,相同 query_key 的报警只发一个
3 exponential_realert: 设置一个时长,必须大于realert 设置,则在realert到exponential_realert之间,每次报警以后,realert 自动翻倍htm
每条规则均可以附加任意数量的警报
alert_subject: "Alter {0} occurred at {1} {2}" alert_subject_args: - _index - "@timestamp" - request alert_text: "最近三分钟有三次以上404请求"
注意:
格式化程序的参数将从与警报相关的匹配对象中提供。若是规则匹配索引中多个对象,则仅使用第一个匹配来填充格式化程序的参数。若是缺乏参数列表中提到的字段,则电子邮件使用
alert_missing_value
代替。
smtp配置:
smtp_host: smtp.qq.com smtp_port: 25 smtp_auth_file: /opt/elastalert/rule_templates/smtp_auth_file.yaml #账号密码配置在此 from_addr: "xxxx@qq.com" alert: - "email" email: - "xxxx@qq.com" $ cat /opt/elastalert/rule_templates/smtp_auth_file.yaml user: xxxx@qq.com password: xxxxxxxxxx
命令输出,容许执行任意命令并从匹配中传递参数或stdin
alert: - command command: ["/bin/send_alert", "--username", "{match[username]}"]
其它更新配置信息,请参考官方文档
五分钟内流量总和超过200M就发邮件
es_host: 192.168.20.6 es_port: 9200 run_every: minutes: 5 name: nanjing_flow type: metric_aggregation index: n-xxx-* buffer_time: minutes: 5 metric_agg_key: body_bytes_sent metric_agg_type: sum max_threshold: 209715200 use_run_every_query_size: true alert_text_type: alert_text_only alert_subject: "Alter nanjing 最近五分钟流量超200M,请注意!!!" alert_text: | 最近五分钟总流量: {0} B kibana url: http://xxxxx alert_text_args: - metric_body_bytes_sent_sum smtp_host: smtp.qq.com smtp_port: 25 smtp_auth_file: /opt/elastalert/rule_templates/smtp_auth_file.yaml from_addr: "xxxx@qq.com" alert: - "email" email: - "xxxx@qq.com"
nginx例子,对后端请求超过3秒的发送邮件。须要对特定的接口,好比认证接口过滤(不计算在内)
es_host: 192.168.20.6 es_port: 9200 run_every: seconds: 30 name: xxx_reponse_time index: n-xxx-* type: whitelist compare_key: "request" ignore_null: true whitelist: - /index.html - /siteapp/ecsAuthentication/hasAuthentication type: frequency num_events: 1 timeframe: seconds: 30 filter: - query_string: query: "upstream_response_time: >3 " alert_text_type: alert_text_only alert_subject: "Alter {0} 接口后端处理超过3秒!!!" alert_subject_args: - _index html_table_title: "<h2>This is a heading</h2>" alert_text: | timestamp: {0} request_method: {1} request: {2} request_body: {3} request_time: {4} s upstream_response_time: {5} s body_bytes_sent: {6} B status: {7} remote_addr: {8} http_x_forwarded_for: {9} upstream_addr: {10} agent: {11} alert_text_args: - timestamp - request_method - request - request_body - request_time - upstream_response_time - body_bytes_sent - status - remote_addr - http_x_forwarded_for - upstream_addr - agent smtp_host: smtp.qq.com smtp_port: 25 smtp_auth_file: /opt/elastalert/rule_templates/smtp_auth_file.yaml from_addr: "xxx@qq.com" alert: - "email" email: - "xxxxx@qq.com"