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"