ElastAlert 使用心得 - lyonwang/TechNotes GitHub Wiki
From Michael Chen
Event Trigger
- any:只要有匹配就报警;
- blacklist:compare_key字段的内容匹配上 blacklist数组里任意内容;
- whitelist:compare_key字段的内容一个都没能匹配上whitelist数组里内容;
- change:在相同query_key条件下,compare_key字段的内容,在 timeframe范围内发送变化;
- frequency:在相同 query_key条件下,timeframe 范围内有num_events个被过滤出来的异常;
- spike:在相同query_key条件下,前后两个timeframe范围内数据量相差比例超过spike_height。其中可以通过spike_type设置具体涨跌方向是up,down,both 。还可以通过threshold_ref设置要求上一个周期数据量的下限,threshold_cur设置要求当前周期数据量的下限,如果数据量不到下限,也不触发;
- flatline:timeframe 范围内,数据量小于threshold阈值;
- new_term:fields字段新出现之前terms_window_size(默认30天)范围内最多的terms_size (默认50)个结果以外的数据;
- cardinality:在相同 query_key条件下,timeframe范围内cardinality_field的值超过 max_cardinality 或者低于min_cardinality
- Percentage Match: 在buffer_time 中匹配所设置的字段的百分比高于或低于阈值时,此规则将匹配。默认情况下为全局的buffer_time。
Frequency: 頻率超過某個值後告警的寫法
es_host: elasticsearch
es_port: 9200
name: Example rule
type: frequency
index: filebeat-* # 要搜尋的index 名稱
num_events: 3 # timeframe時間內超過3次
timeframe:
minutes: 1 # 一分鐘超過 num_events 次數
realert:
minutes: 1
filter: # 搜尋符合條件的資料
- term:
prospector.type: "log"
alert:
...
Spike: 一定時間內超過一定峰值
Flatline: 數值低於設定值後告警
es_host: elasticsearch
es_port: 9200
name: Heartbeat Alert
type: flatline
index: application-*
threshold: 1
timeframe:
minutes: 1
realert:
minutes: 10
filter:
- term:
LogMessage.keyword: "Heartbeat"
alert:
- "email"
alert_subject: "運作失常警報"
email:
- "[email protected]"
from_addr: "[email protected]"
email_add_domain: "@xxx.com"
smtp_auth_file: /opt/elastalert/smtp_auth_file.yaml
smtp_host: smtp.xxx.com
smtp_ssl: false
smtp_port: 587
is_enabled: false
Filter
filter: 模糊比對
filter:
- query:
query_string:
query: "username: bob" #搜尋 username 欄位 有 bob 的資料
- query:
query_string:
query: "_type: login_logs" # 搜尋 _type 欄位有 login_logs 的資料
# 這兩個搜尋條件會以 AND 的方式進行搜尋
filter:
- query:
query_string:
query: "username: bob" #搜尋 username 欄位 有 bob 的資料
query: "_type: login_logs" # 搜尋 _type 欄位有 login_logs 的資料
#經過測試後, 發現寫在一起也有作用
term: 精準比對
filter
- term:
name_field: "bob" # 搜尋 name_field這個欄位值是bob的資料
- term:
_type: "login_logs" #搜尋 _type 這個欄位值是 login_logs 的資料
不過 term 的用法不支援 多欄位寫在一起,所以不能用以下的寫法, 會發生錯誤。
filter
- term:
name_field: "bob" # 搜尋 name_field這個欄位值是bob的資料
_type: "login_logs" #搜尋 _type 這個欄位值是 login_logs 的資料
若真要多個欄位寫在一起,需改用 terms
terms:
filter:
- terms:
field: ["value1", "value2"]
filter:
- terms:
fieldX: ["value1", "value2"]
fieldY: ["something", "something_else"]
fieldZ: ["foo", "bar", "baz"]
minimum_should_match: 2
#範例雖然這麼寫, 但測試的結果發現,一次只能寫一行,多行就ERROR了,而且 minimum_should_match: 2 也沒有被支援
wildcard
filter:
- query:
wildcard:
field: "foo*bar"
#須注意每個階層的縮排,縮排若不正確會出錯。
Range
filter:
- range:
status_code:
from: 500
to: 599
# 數值型態的欄位可用區間範圍來搜尋
Negation, and, or的用法
filter:
- or:
- term:
field: "value"
- wildcard:
field: "foo*bar"
- and:
- not:
term:
field: "value"
- not:
term:
_type: "something"
Alert
-
we can set multip alerts.
-
The slack and email notify setting config as below.
es_host: elasticsearch
es_port: 9200
name: Example rule
type: frequency
index: filebeat-*
num_events: 3
timeframe:
minutes: 1
realert:
minutes: 1
filter:
- term:
prospector.type: "log"
alert:
- "email"
- "slack"
email:
- "[email protected]"
from_addr: "[email protected]"
email_add_domain: "@xxx.com"
smtp_auth_file: ../smtp_auth_file.yaml
smtp_host: smtp.xxx.com
smtp_port: 587
slack:
slack_webhook_url: "https://hooks.slack.com/services/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"6c"
- The Email notify setting config as below: Reference Page.
es_host: elasticsearch
es_port: 9200
name: Example rule
type: frequency
index: filebeat-*
num_events: 3
timeframe:
minutes: 1
realert:
minutes: 1
filter:
- term:
prospector.type: "log"
alert:
- "email"
email:
- "[email protected]"
from_addr: "[email protected]"
email_add_domain: "@xxx.com"
smtp_auth_file: ../smtp_auth_file.yaml
smtp_host: smtp.xxx.com
smtp_ssl: false
smtp_port: 587
Please make sure from_addr: "[email protected]" this value , it can't be "xxx" only.
- The content of stmp_auth_file.yaml Reference
user: [email protected]
password: xxxxxxxxxx
測試 rules
- The command of testing your rule(no alert) . Reference page:
elastalert-test-rule /opt/elastalert/rules/example_frequency.yaml
或者
elastalert-test-rule --config /opt/elastalert/config.yaml /opt/elastalert/rules/example_frequency.yaml
- if you want try the really trigger and alert. Try this command:
python -m elastalert.elastalert --verbose --rule /opt/elastalert/rules/example_frequency.yaml --config /opt/elastalert/config.yaml