elasticsearch5之Elastalert 安裝使用 配置郵件報警和微信報警
簡介
Elastalert是用python2寫的一個報警框架(目前支援python2.6和2.7,不支援3.x),github地址為 https://github.com/Yelp/elastalert。他提供不同場景的規則配置,若覺得規則、告警不滿足需求時,可以用python編寫外掛Adding a New Rule Type、Adding a New Alerter。
環境
系統:centos6.8
python:2.7.12(請參看升級centos6 預設python版本到2.7.12)
elasticsearch:5.5
kibana:5.5
Elastalert內建的告警方式:
- Email JIRA
- OpsGenie
- Commands
- HipChat MS
- Teams Slack
- Telegram
- AWS SNS
- VictorOps
- PagerDuty
- Exotel
- Twilio
- Gitter
安裝
pip安裝elastalert
安裝pip包管理工具(參考)
$ pip install elastalert
或者 git clone (推薦)
$ git clone https://github.com/Yelp/elastalert.git
安裝模組
$ pip install "setuptools>=11.3"
$ python setup.py install
根據不同的Elasticsearch版本安裝elasticsearch-py。
Elasticsearch 5.x/2.x
$ pip install "elasticsearch>=5.0.0" ##5.x
$ pip install "elasticsearch<3.0.0" ##2.x
將原始碼放置在你指定的目錄下:
$ cd /usr/local/elastalert/
$ cp config.yaml.example config.yaml
$ mkdir es_rules
[[email protected] elastalert]$ egrep -v '^#|^$' config.yaml
rules_folder: es_rules
run_every:
minutes: 5
buffer_time:
minutes: 5
es_host: 10.xx.xx.xxx
es_port: xxxx
use_ssl: false
verify_certs: false
es_username: elastic
es_password: changeme
writeback_index: elastalert_status
alert_time_limit:
days: 2
$ ll
[[email protected] elastalert]# ll
total 124
drwxr-xr-x. 4 root root 4096 Nov 12 09:39 build
-rw-r--r--. 1 root root 9737 Nov 12 09:39 changelog.md
-rw-r--r--. 1 root root 2001 Nov 12 14:39 config.yaml
-rw-r--r--. 1 root root 2029 Nov 12 09:39 config.yaml.example
drwxr-xr-x. 2 root root 4096 Nov 12 09:39 dist
-rw-r--r--. 1 root root 261 Nov 12 09:39 docker-compose.yml
-rw-r--r--. 1 root root 264 Nov 12 09:39 Dockerfile-test
drwxr-xr-x. 3 root root 4096 Nov 12 09:39 docs
drwxr-xr-x. 2 root root 4096 Nov 12 11:15 elastalert
drwxr-xr-x. 2 root root 4096 Nov 12 09:39 elastalert.egg-info
drwxr-xr-x. 2 root root 4096 Nov 12 14:14 elastalert_modules
drwxr-xr-x. 2 root root 4096 Nov 12 14:51 es_rules
drwxr-xr-x. 2 root root 4096 Nov 12 09:39 example_rules
-rw-r--r--. 1 root root 11359 Nov 12 09:39 LICENSE
-rw-r--r--. 1 root root 480 Nov 12 09:39 Makefile
-rw-r--r--. 1 root root 15945 Nov 12 09:39 README.md
-rw-r--r--. 1 root root 87 Nov 12 09:39 requirements-dev.txt
-rw-r--r--. 1 root root 372 Nov 12 09:39 requirements.txt
-rw-r--r--. 1 root root 100 Nov 12 09:39 setup.cfg
-rw-r--r--. 1 root root 1650 Nov 12 09:39 setup.py
-rw-r--r--. 1 root root 117 Nov 12 11:14 smtp_auth_file.yaml
-rw-r--r--. 1 root root 780 Nov 12 09:39 supervisord.conf.example
drwxr-xr-x. 2 root root 4096 Nov 12 09:39 tests
-rw-r--r--. 1 root root 606 Nov 12 09:39 tox.ini
配置解析
此處的配置檔案是指config.yaml檔案。
- rules_folder:ElastAlert將載入規則配置檔案的地方,它將嘗試載入資料夾中的每個.yaml檔案(這裡是載入es_rules目錄下)。
- run_every:ElastAlert查詢Elasticsearch的頻率。
- buffer_time:是查詢視窗的大小,從每個查詢執行的時間向後延伸。對於其中use_count_query或use_terms_query設定為true的規則,此值將被忽略。
- es_host:是Elasticsearch叢集的地址,ElastAlert將儲存有關其狀態、查詢執行、警報和錯誤的資料。每個規則也可以設定不同的elasticsearch主機進行查詢。
- es_port:Elasticsearch對應的埠。
- use_ssl: (可選的)是否使用TLS;連線到es_host;設定為True或False。
- verify_certs: (可選的)是否驗證TLS證書; 設定為True或False,預設是True。
- client_cert: (可選的)PEM證書的路徑。
- client_key: (可選的) 作為客戶端金鑰使用的私鑰檔案的路徑。
- ca_certs: (可選的) 用於驗證SSL連線的CA證書的路徑。
- es_username: (可選的) 用於連線Elasticsearch的basic-auth使用者名稱。
- es_password: (可選的) 用於連線Elasticsearch的密碼。
- es_url_prefix: (可選的) Elasticsearch端點的URL字首。
- es_send_get_body_as: (可選的) 查詢Elasticsearch方法- GET,POST或- source,預設是GET。
- writeback_index:是ElastAlert將儲存資料的索引名稱。
- alert_time_limit: 是失敗警報的重試視窗。
建立Elasticsearch索引
elastalert-create-index這個命令會在elasticsearch建立索引,便於ElastAlert將有關其查詢及其警報的資訊和元資料儲存回Elasticsearch。這不是必須的步驟,但是強烈建議建立。因為對於審計,測試很有用,並且重啟elastalert不影響計數和傳送alert。預設情況下,建立的索引叫 elastalert_status。
$ elastalert-create-index --host 10.xxx.xx.xxx --port xxxx --username xxx --password xxx --no-ssl --no-verify-certs
建立成功之後,我們可以在Elasticsearch中看到所建立的索引elastalert_status:
$ curl -u xxx 'localhost:9200/_cat/indices?v' |grep elastalert_status
green open elastalert_status eWjVRAzjTX2vmhvAK931Ng 5 1 110 0 513.8kb 266kb
elastalert_status
ElastAlert 根據elastalert_status去確定首次啟動的時候在什麼時間範圍內去查詢,以避免重複查詢。對於每個規則,它將從最近的結束時間開始查詢。包括:
- @timestamp:檔案上傳到Elasticsearch的時間。這是在執行查詢並且已經處理結果之後。
- rule_name:相應規則的名稱。
- starttime:查詢的開始時間戳。
- endtime:查詢結束時間戳。
- hits:查詢結果的數量。
- matches:處理命中後規則返回的匹配數。請注意,這並不一定意味著警報被觸發。
- time_taken:此查詢執行所需的秒數。
配置SMTP郵件報警
在/usr/local/elastalert/目錄下建立檔案smtp_auth_file.yaml,內容如下:
[[email protected] elastalert]$ cat smtp_auth_file.yaml
#郵箱使用者名稱
user: [email protected]
##不是郵箱密碼,是設定的SMTP密碼
password: YdYfxxx
配置規則
在es_rules目錄下修改配置檔案,如下:
$ cd /opt/elastalert/es_rules
[[email protected] elastalert]$ egrep -v '^#|^$' es_rules/frequency.yaml
es_host: 10.xxx.xx.xxx
es_port: 9200
use_ssl: false
verify_certs: false
es_username: xxx
es_password: xxx
name: es_rule
writeback_index: elastalert_status
type: frequency
index: logstash-nginx-*
num_events: 5
timeframe:
minutes: 5
filter:
- query:
query_string:
query: "status:>=400"
query_string:
query: "status: >=500"
#SMTP configration
smtp_host: smtp.263.net
smtp_port: 25
#SMTP auth
smtp_auth_file: /usr/local/elastalert/smtp_auth_file.yaml
email_reply_to: [email protected]
from_addr: [email protected]
# (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:
- "[email protected]"
上述規則表示:在elastalert執行的五分鐘內,出現五條有status:>=400 或者 status: >=500的日誌,則觸發告警,並且告警通知將以email的形式從[email protected]郵箱傳送給[email protected]
規則配置解析:
-
es_host、es_port:應該指向我們要查詢的Elasticsearch叢集。
-
name:是這個規則的唯一名稱。如果兩個規則共享相同的名稱,ElastAlert將不會啟動。
-
type:每個規則都有不同的型別,可能會採用不同的引數。該frequency型別表示“在timeframe時間內匹配成功次數超過num_events發出警報”。有關其他型別的資訊,請參閱規則型別。
-
index:要查詢的索引的名稱。配置,從某類索引裡讀取資料,目前已經支援Ymd格式,需要先設定use_strftime_index:true,然後匹配索引,配置形如:index: logstash-es-test%Y.%m.%d,表示匹配logstash-es-test名稱開頭,以年月日作為索引字尾的index。
-
num_events:此引數特定於frequency型別,是觸發警報時的閾值。
-
timeframe:timeframe是num_events必須發生的時間段。
-
filter:是用於過濾結果的Elasticsearch過濾器列表。有關
詳細資訊,請參閱編寫過濾規則。 -
email:是要傳送警報的地址列表。
-
alert:配置,設定觸發報警時執行哪些報警手段。不同的type還有自己獨特的配置選項。目前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
demo測試
命令如下:
$ python -m elastalert.elastalert --config ./config.yaml #執行命令,載入所有rules
$ python -m elastalert.elastalert --config ./config.yaml --rule ./es_rules/frequency.yaml ## 或者單獨執行 rules_folder 裡的某個 rule
給Elasticsearch新增測試資料:
$ curl -X POST "http://127.0.0.1:9200/logstash-nginx-xx/test" -d '{
"@timestamp": "2018-11-12T10:54:41.000Z",
"status": "500"
}'
產看郵件報警
配置微信報警
微信報警專案地址:https://github.com/anjia0532/elastalert-wechat-plugin
配置地址:https://anjia0532.github.io/2017/02/16/elastalert-wechat-plugin/
配置檔案
$ cd ~/
$ git clone https://github.com/Yelp/elastalert.git
$ cd elastalert
$ wget https://raw.githubusercontent.com/anjia0532/elastalert-wechat-plugin/master/elastalert_modules/wechat_qiye_alert.py
$ touch ~/elastalert/elastalert_modules/__init__.py
$ cp config.yaml.example config.yaml
$ vi es_rules/wechat_frequency.yaml
[[email protected] elastalert]# egrep -v '^#|^$' es_rules/wechat_frequency.yaml
es_host: 10.xxx.xx.xxx
es_port: xxxx
use_ssl: false
verify_certs: false
es_username: xx
es_password: xxxx
name: es_rule
writeback_index: elastalert_status
type: frequency
index: logstash-nginx-*
num_events: 1
timeframe:
minutes: 1
filter:
- query:
query_string:
query: "status:>=400"
query_string:
query: "status: >=500"
alert:
- "elastalert_modules.wechat_qiye_alert.WeChatAlerter"
#後臺登陸後【設定】->【許可權管理】->【普通管理組】->【建立並設定通訊錄和應用許可權】->【CorpID,Secret】
#設定微信企業號的appid
corp_id: 'xx'
#設定微信企業號的Secret
secret: 'xxx'
#後臺登陸後【應用中心】->【選擇應用】->【應用id】
#設定微信企業號應用id
agent_id: 'x'
#部門id
party_id: 'x'
#使用者微訊號
user_id: xx
# 標籤id
tag_id: xx
微信接收報警截圖
配置微信報警模版
在rule檔案加上一下內容:
alert:
- "elastalert_modules.wechat_qiye_alert.WeChatAlerter"
alert_text_type: alert_text_only
alert_text: |
========start=========
告警程式: elasticsearch_alert
告警主題: 介面 {} 響應時間大於2秒!
觸發時間: {}
request: {}
responsetime: {}
domain: 此域名 {} 下介面告警
status: {}
upstreamaddr: {}
num_hits: {}
num_method: {}
clientip: {}
參考來源: {}
========end==========
alert_text_args:
- request
- "@timestamp"
- request
- responsetime
- domain
- status
- upstreamaddr
- num_hits
- num_method
- client
- path