Elasticsearch 之 elastalert監控告警通知

1、配置規則

規則類型

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

2、frequency類型演示

一、複製frequency模板信息python

[root@k8s-slave1 example_rules]# cd /usr/local/elastalert/example_rules/
[root@k8s-slave1 example_rules]# cp example_frequency.yaml test.yaml

二、修改test.yaml監控規則nginx

es_host: 10.3.153.200        #elasticsearch 主機
es_port: 9200                     #主機監聽端口
name: frequency test rule     #規則名,同時也是郵件的標題
type: frequency                     #規則才用的規則
index: logstash-nginx-*        #檢查那個索引信息的內容
num_events: 1                     #在timeframe時間內,出現了多少次,才發送告警
timeframe:
  minutes: 1                          #統計1分鐘內的數據
#如下爲過濾匹配的規則,關於具體的查詢匹配方法,基本於本博客上的elasticsearch 查詢方法文章一致。
filter:                      
- query_string:
    query: "status: >=500"      #status大於500以上
#郵件鏈接的信息
smtp_host: smtp.163.com
smtp_port: 465
smtp_ssl: True
smtp_auth_file: /usr/local/elastalert/smtp_auth_file.yaml    #密碼信息
#回覆那個郵箱
email_reply_to: ibm.chick@163.com
#用那個郵箱賬號發送
from_addr: ibm.chick@163.com
alert:
- "email"
email:
- "ibm.chick@163.com"     #要接受該郵件的人員

三、建立smtp_auth_file.yaml文件數組

[root@k8s-slave1 ~]# cat /usr/local/elastalert/smtp_auth_file.yaml 
#郵箱用戶名
user: ibm.chick@163.com
##不是郵箱密碼,是設置的SMTP密碼
password: xxxxxx

3、啓用該規則

一、啓用規則
Elasticsearch 之 elastalert監控告警通知
備註:當前爲展現效果,啓動方式--verbose模式,正常狀況下應爲後臺啓動,同時能夠對整個rules文件夾的全部規則進行統一加載。app

elastalert --config /usr/local/elastalert/config.yaml --rule /usr/local/elastalert/example_rules/test.yaml &

二、使用代碼往該索引插入數據(製造錯誤信息)
Elasticsearch 之 elastalert監控告警通知elasticsearch

三、發現匹配信息
Elasticsearch 之 elastalert監控告警通知ide

備註:咱們在排查的過程當中,能夠使用debug模式啓動,可獲取的日誌信息更多,以下:
Elasticsearch 之 elastalert監控告警通知測試

四、查看郵件內容
Elasticsearch 之 elastalert監控告警通知debug

4、使用技巧與排錯方法

一、在用python插入數據時,需使用@timestamp字段,此字段數據,是經過--debug模式的檢查時間,複製到程序上,並多加1分鐘內進行插入數據(後期用上logstash後不須要使用)
二、字段過濾filter,可參考elasticsearch 條件查詢文檔方法,同時要注意索引下的字段類型,到底是數字仍是字符,能夠經過mapping映射查詢。
三、在測試驗證環節上,能夠經過debug模式運行規則,可便於整個過程的排查調試。
四、關於elastalert運行狀況與日誌信息,能夠在elasticsearch上進行查看。
Elasticsearch 之 elastalert監控告警通知調試

相關文章
相關標籤/搜索