config.yaml配置說明
#用來加載rule的目錄,默認是example_rules
rules_folder: example_rules
#用來設置定時向elasticsearch發送請求
run_every:
minutes: 1
#用來設置請求里時間字段的范圍
buffer_time:
minutes: 15
#elasticsearch的host地址
es_host: 192.168.232.191
#elasticsearch 對應的端口號
es_port: 9200
#可選的,es url前綴
#es_url_prefix:elasticsearch
#可選的,查詢es的方式,默認是GET
#es_send_get_body_as:GET
#可選的,選擇是否用SSL連接es,true或者false
#use_ssl: True
#可選的,是否驗證TLS證書,設置為true或者false,默認為- true
#verify_certs: True
#es認證的username和password
#es_username: someusername
#es_password: somepassword
#elastalert產生的日志在elasticsearch中的創建的索引
writeback_index: elastalert_status
#失敗重試的時間限制
alert_time_limit:
days: 2
創建ElastAlert索引
可以在/usr/bin/目錄下看到以下四個命令:
$ ll /usr/bin/elastalert*
-rwxr-xr-x 1 root root 399 Nov 20 16:39 /usr/bin/elastalert
-rwxr-xr-x 1 root root 425 Nov 20 16:39 /usr/bin/elastalert-create-index
-rwxr-xr-x 1 root root 433 Nov 20 16:39 /usr/bin/elastalert-rule-from-kibana
-rwxr-xr-x 1 root root 419 Nov 20 16:39 /usr/bin/elastalert-test-rule
- elastalert-create-index會創建一個索引,ElastAlert 會把執行記錄存放到這個索引中,默認情況下,索引名叫 elastalert_status。其中有4個_type,都有自己的@timestamp 字段,所以同樣也可以用kibana來查看這個索引的日志記錄情況。
- elastalert-rule-from-kibana從Kibana已保存的儀表盤中讀取Filtering 設置,幫助生成config.yaml里的配置。不過注意,它只會讀取 filtering,不包括queries。
- elastalert-test-rule測試自定義配置中的rule設置。
執行elastalert-create-index在ES創建索引,這不是必須的步驟,但是強烈建議創建。因為對於審計和測試很有用,並且重啟ES不影響計數和發送
$ elastalert-create-index
1.郵箱報警配置
# Alert when the rate of events exceeds a threshold
# (Optional)
# Elasticsearch host
es_host: 192.168.232.191
# (Optional)
# Elasticsearch port
es_port: 9200
# (OptionaL) Connect with SSL to Elasticsearch
#use_ssl: True
# (Optional) basic-auth username and password for Elasticsearch
#es_username: someusername
#es_password: somepassword
# (Required)
# Rule name, must be unique
name: Example frequency rule
# (Required)
# Type of alert.
# the frequency rule type alerts when num_events events occur with timeframe time
type: frequency
# (Required)
# Index to search, wildcard supported
index: metricbeat-*
# (Required, frequency specific)
# Alert when this many documents matching the query occur within a timeframe
num_events: 5
# (Required, frequency specific)
# num_events must occur within this amount of time to trigger an alert
timeframe:
hours: 4
# (Required)
# A list of Elasticsearch filters used for find events
# These filters are joined with AND and nested in a filtered query
# For more info: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl.html
filter:
- query_string:
query: "system.process.cpu.total.pct: > 10%" // field支持嵌套
smtp_host: smtp.163.com
smtp_port: 25
smtp_auth_file: /opt/elastalert/smtp_auth.yaml
#回復給那個郵箱
email_reply_to: xxx@163.com
##從哪個郵箱發送
from_addr: xxx@163.com
# (Required)
# The alert is use when a match is found
alert:
- "email"
# (required, email specific)
# a list of email addresses to send alerts to
email:
- "yyy@qq.com"
上述配置表示選擇metricbeat作為告警索引,在4小時內講匹配過濾規則,當CPU使用百分比的值為10%超過5次后,即滿足告警條件,然后發送郵件
還有一個smtp_auth.yaml文件,這個里面記錄了發送郵箱的賬號和密碼,163郵箱有授權碼機制,所以密碼處應該填寫授權碼(沒有的話則需要開啟)。
#發送郵件的郵箱
user: xxx@163.com
#不是郵箱密碼,是設置的POP3密碼
password: xxx
高級配置
避免重復告警
# 5分鍾內相同的報警不會重復發送
realert:
minutes: 5
# 指數級擴大 realert 時間,中間如果有報警,
# 則按照5>10>20>40>60不斷增大報警時間到制定的最大時間,
# 如果之后報警減少,則會慢慢恢復原始realert時間
exponential_realert:
hours: 1
聚合相同告警
# 根據報警的內容,將相同的報警安裝 name 來聚合
aggregation_key: name
# 聚合報警的內容,只展示 name 與 message
summary_table_fields:
- name
- message
告警內容格式化
可以自定義告警內容,內部是使用Python的format來實現的
alert_subject: "Error {} @{}"
alert_subject_args:
- name
- "@timestamp"
alert_text_type: alert_text_only
alert_text: |
### Error frequency exceeds
> Name: {}
> Message: {}
> Host: {} ({})
alert_text_args:
- name
- message
- hostname
- host
測試rule
可以在運行rule之前先通過elastalert-test-rule命令來測試一下
$ elastalert-test-rule ~/elastalert/example_rules/example_frequency.yaml
運行rule
啟動elastalert服務,監聽es,這里加了--rule example_frequency.yaml
表示只運行example_frequency.yaml這一個rule文件,如果不加該選項則會運行rules_folder下所有rule文件,上面配置中的rules_folder為默認的example_rules。
$ python -m elastalert.elastalert --verbose --rule example_frequency.yaml
為了讓服務后台運行並且可以達到守護進程的效果,在生產環境中筆者建議使用supervisor管理。