ElastAlert是一個簡單的框架,用於經過Elasticsearch中的數據異常警告,峯值或其餘感興趣的模式。
監控類型html
因爲Yelp官方提供有docker鏡像,docker的便捷性,因此採用docker進行部署python
githup官方庫:https://github.com/Yelp/elastalert
git
docker image:bitsensor/elastalert:latest
github
安裝方式:web
下載相應的配置文件 git clone https://github.com/bitsensor/elastalert.git; cd elastalert 啓動鏡像 docker run -d -p 3030:3030 \ -v `pwd`/config/elastalert.yaml:/opt/elastalert/config.yaml \ -v `pwd`/config/config.json:/opt/elastalert-server/config/config.json \ -v `pwd`/rules:/opt/elastalert/rules \ -v `pwd`/rule_templates:/opt/elastalert/rule_templates \ --net="host" \ --name elastalert bitsensor/elastalert:latest
ES 7.X用docker跑,問題比較多docker
詳細資料查看官方資料。shell
規則配置 configjson
# rule規則所在路徑 rules_folder: /data/elastalert/rules # 運行時間間隔 run_every: minutes: 1 buffer_time: minutes: 15 es_host: 10.88.0.34 es_port: 9200 # 寫入ES中的index writeback_index: elastalert_status writeback_alias: elastalert_alerts # 告警發送失敗的,時間限制,超過2天后,則丟棄 alert_time_limit: days: 2
rule框架
# Rule name, must be unique name: web request status # Type of alert. #type: spike type: frequency # num_events must occur within this amount of time to trigger an alert # 在5m內,查到的數量多餘20,曾觸發報警 timeframe: minutes: 5 num_events: 20 # Index to search, wildcard supported # 索引和時間filed index: web-2018.06.26 timestamp_field: "@timestamp" # 匹配規則 filter: - query: term: status: value: 404 # 郵件標題 alert_subject: "Surge in attacks on {}" alert_subject_args: - http_host # 郵件內容 alert_text_type: alert_text_only alert_text: "Surge in attacks on {}" alert_text_args: - host # The alert is use when a match is found alert: - "email" email: - "xx@qq.com"
binary Running測試
# 安裝 git clone https://github.com/Yelp/elastalert.git pip install -r requirements.txt python setup.py install cp config.yaml.example config.yaml # 建立index elastalert-create-index # 測試 elastalert-test-rule --config config.yaml rules/test_frequency.yaml # 正式 python3 -m elastalert.elastalert --config config.yaml
1.elastalter會生成一個elastalter_status索引,裏面會記錄rule匹配的詳細信息
{ "_index": "elastalert_status_status", "_type": "_doc", "_id": "yosRkHAB-hBL9ipWbCYu", "_version": 1, "_score": null, "_source": { "rule_name": "Nginx request img check", "endtime": "2020-02-29T08:30:25.822237Z", "starttime": "2020-02-29T08:26:32.605579Z", # 匹配到多少天 "matches": 0, # 命中 "hits": 0, "@timestamp": "2020-02-29T08:30:25.836591Z", "time_taken": 0.014328718185424805 }, "fields": { "@timestamp": [ "2020-02-29T08:30:25.836Z" ], "endtime": [ "2020-02-29T08:30:25.822Z" ], "starttime": [ "2020-02-29T08:26:32.605Z" ] }, "sort": [ 1582965025836 ] }