ElastAlert對ELK日誌進行郵箱報警

運行ElastAlert前提

要求

  • Elasticsearch
  • ISO8601或Unix帶時間戳的數據
  • Python 2.7
  • pip, 查看 requirements.txt
  • Ubuntu 14.x上的軟件包:python-pip python-dev libffi-dev libssl-dev

下載和配置

1. 下載安裝
可以使用pip安裝最新發布的ElastAlert版本:

pip install elastalert

或者從github克隆ElastAlert項目並安裝

cd /opt/
git clone https://github.com/Yelp/elastalert.git
pip install "setuptools>=11.3"
python setup.py install

2. 修改配置文件

cd /opt/elastalert
cp 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: 15
es_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-index
Elastic Version:6
Mapping used for string:{'type': 'keyword'}
New index elastalert_status created
Done!

有關此處將包含哪些數據的信息,請參閱ElastAlert元數據索引。

自定義規則文件

[root@elk example_rules]# cat /opt/elastalert/example_rules/autoDispatchAdvanceJob.yaml
------------------------------------------------------------------------------------------

es_host: 10.x.x.x  # Elasticsearch的地址
es_port: 9200
name: autoDispatchAdvanceJob Stop  #  規則名稱,不能重複,郵件標題就是這個名字
type: flatline  #  每個規則都有不同的類型,可能採用不同的參數。詳細說明:https://elastalert.readthedocs.io/en/latest/ruletypes.html#rule-types
index: 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: "[email protected]"                                                               
from_addr: "[email protected]"
email:  #  可以有多個接收郵箱
- "[email protected]"
- "[email protected]"
[root@elk example_rules]# cat /opt/elastalert/example_rules/smtp-file.yaml
----------------------------------------------------------------------------------------------------

#發送郵件的郵箱
user: "[email protected]"
##不是郵箱密碼,是設置的POP3密碼
password: "sdffnddflcvdhbi"

ElastAlert 有以下幾種自帶 ruletype:

  • 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。

    運行elastalert

# 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://elastalert.readthedocs.io/en/latest/running_elastalert.html#downloading-and-configuring
https://blog.csdn.net/mayifan0/article/details/78023783

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章