系統:centos7 elk 版本:7.6.2
週期性的查詢Elastsearch而且將數據傳遞給規則類型,規則類型定義了須要查詢哪些數據。php
當一個規則匹配觸發,就會給到一個或者多個的告警,這些告警具體會根據規則的配置來選擇告警途徑,就是告警行爲,好比郵件、釘釘、tg、slack、企業微信等html
ElastAlert 手冊python
Tips:Elastalert 0.2.0 以後使用 Python 3.6,再也不使用 Python 2 版本git
#安裝EPEL和IUS軟件源 yum install epel-release -y yum install https://centos7.iuscommunity.org/ius-release.rpm -y #安裝python3.6 yum install python36u python36u-devel python36u-pip -y #ln -s /usr/bin/python3.6 /bin/python3 #ln -s /usr/bin/pip3.6 /bin/pip3
pip3 install elastalert
cd /opt/soft/ git clone https://github.com/Yelp/elastalert.git cd elastalert cp config.yaml.example config.yaml mkdir rules
https://github.com/Yelp/elastalert/tree/master/example_rulesgithub
#規則目錄 rules_folder: /opt/soft/elastalert/rules #多久從ES中查詢一次 run_every: seconds: 30 #是查詢窗口的大小,從每一個查詢運行的時間向後延伸。對於其中use_count_query或use_terms_query設置爲true的規則,此值將被忽略。 buffer_time: minutes: 15 #鏈接elasticsearch配置 es_host: 127.0.0.1 es_port: 9200 es_username: elastic es_password: xxxxxxxx #elastalert索引名稱 writeback_index: elastalert_status writeback_alias: elastalert_alerts #失敗重試限制 alert_time_limit: days: 2
**如下配置沒使用,只作介紹 **web
#### 使用 TLS 鏈接誒 elastsearch #use_ssl: True #驗證 tls 證書 #verify_certs: True #帶正文的GET請求是Elasticsearch的默認選項。若是由於某些緣由失敗了,你能夠經過'GET','POST'或'source',具體能夠查看如下(http://elasticsearch-py.readthedocs.io/en/master/connection.html?highlight=send_get_body_as#transport) #es_send_get_body_as: GET # 開啓 ssl 認證證書 #verify_certs: True #ca_certs: /path/to/cacert.pem #client_cert: /path/to/client_cert.pem #client_key: /path/to/client_key.key
**Tips : **若是索引已存在,則不會從新建立docker
elastalert-create-index --index elastalert
輸出結果:json
ProcessController: Elastic Version: 7.6.2 Reading Elastic 6 index mappings: Reading index mapping 'es_mappings/6/silence.json' Reading index mapping 'es_mappings/6/elastalert_status.json' Reading index mapping 'es_mappings/6/elastalert.json' Reading index mapping 'es_mappings/6/past_elastalert.json' Reading index mapping 'es_mappings/6/elastalert_error.json'
# 測試規則文件 elastalert-test-rule rule.yaml # 啓動監控報警 python3 -m elastalert.elastalert --verbose --rule /root/elastalert/example_rules/rule.yaml
Tips: 啓動指定家目錄的緣由主要是有調用,能夠少寫路徑,好比不須要指定 config.yml,rule 裏面的 yml 文件能夠少寫路徑指定==vim
# 建立elastalert服務文件 vim /etc/systemd/system/elastalert.service
[Unit] Description=elastalert After=elasticsearch.service [Service] Type=simple User=root Group=root Restart=on-failure WorkingDirectory=/opt/soft/elastalert ExecStart=/usr/bin/python3 -m elastalert.elastalert --verbose --config /opt/soft/elastalert/config.yaml [Install] WantedBy=multi-user.target
安裝supervisorc#
yum install -y supervisor systemctl enable supervisord
編寫elastalert supervisord守護進程
vim /etc/supervisord.d/elastalert.ini
[program:elastalert] directory=/opt/soft/elastalert command=/usr/bin/python3 -m elastalert.elastalert --verbose autostart=true startsecs=5 autorestart=true startretries=10 redirect_stderr=true stdout_logfile_maxbytes=20MB stdout_logfile_backups=5 stdout_logfile=/var/log/elastalert/elastalert.log
啓動
systemctl enable supervisord systemctl restart supervisord systemctl start supervisord systemctl stop supervisord supervisorctl status
curl -X POST "http://elastic:passwd@127.0.0.1:9200/test-alert/test" -H 'Content-Type: application/json' -d '{"@timestamp": "'$(date --iso-8601=seconds)'", "field": "value"}'
輸出:
{"_index":"test-alert","_type":"test","_id":"inH1I3cBMJwNYi416aLN","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1}
curl -X DELETE http://elastic:passwd@127.0.0.1:9200/test-alert
輸出:
{"acknowledged":true}
curl 'http://elastic:passwd@127.0.0.1:9200/_cat/indices?v'
curl -X GET http://elastic:passwd@127.0.0.1:9200/test-alert/_search
輸出結果:
{"took":2,"timed_out":false,"shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":4,"relation":"eq"},"max_score":1.0,"hits":[{"index":"test-alert","type":"test","id":"inH1I3cBMJwNYi416aLN","score":1.0,"source":{"@timestamp": "2021-01-21T08:01:52+00:00", "field": "value"}},{"index":"test-alert","type":"test","id":"0ocXJHcBMJwNYi41y3rG","score":1.0,"source":{"@timestamp": "2021-01-21T08:37:55+0000", "field": "value"}},{"index":"test-alert","type":"test","id":"aZAiJHcBMJwNYi41QTqW","score":1.0,"source":{"@timestamp": "2021-01-21T08:49:20+0000", "field": "value"}},{"index":"test-alert","type":"test","id":"1ZMmJHcBMJwNYi41h4z8","score":1.0,"source":{"@timestamp": "2021-01-21T08:54:00+0000", "field": "value"}}]}}
vim /opt/soft/elastalert/rules/emailtt.yml
name: test-alert type: any # es 的監控索引 index: test-ale* # 發現 1 次就告警 num_events: 1 #1 分鐘檢查1次,和上面一塊兒配合就是 1 分鐘內觸發 1 次規則就告警 timeframe: minutes: 1 #告警規則,查詢索引內 field 字段的值 value,能夠用正則進行匹配 filter: - query: query_string: query: "field: *value*" ## smtp 郵件 server 配置 smtp_host: smtp.zoho.com smtp_port: 465 smtp_ssl: true from_addr: "test@qq.com" ### 發送郵件的帳號密碼 smtp_auth_file: /opt/soft/elastalert/elastalert/smtp_auth_file.yml #告警方式 alert: - "email" #設置只須要的告警字段 include: ["_index","uri","remote_addr","http_x_forwarded_for","status"] #郵件標題 alert_subject: "test-alert 正式環境 告警 {}" #告警郵件接收人 email: - "test@qq.com" - "test1@qq.comk"
vi /opt/soft/elastalert/elastalert/smtp_auth_file.yml
# 發件箱的qq郵箱地址,也就是用戶名 user: test@qq.com # 不是qq郵箱的登陸密碼,是受權碼 password: passwd
cd /opt/soft/elastalert python3 -m elastalert.elastalert --verbose --rule /opt/soft/elastalert/rules/emailtt.yml
cd /opt/soft/elastalert git clone https://github.com/xuyaoqiang/elastalert-dingtalk-plugin cp elastalert-dingtalk-plugin/elastalert_modules/dingtalk_alert.py elastalert/
#查看依賴文件elastalert-dingtalk-plugin/requirements.txt,在對比 pip list 以後,我發現只須要安裝如下內容便可 pip3 install pyOpenSSL==16.2.0 pip3 install requests==2.18.1 pip3 install setuptools>=11.3
vim /opt/soft/elastalert/rules/dingtt.yml
name: Example frequency rule type: any index: test-alert num_events: 1 timeframe: minutes: 1 filter: - query: query_string: query: "field: value" #告警方式 alert: - "elastalert.dingtalk_alert.DingTalkAlerter" #釘釘接口 dingtalk_webhook: "https://oapi.dingtalk.com/robot/send?access_token=釘釘機器人 api token" dingtalk_msgtype: "text"
cd /opt/soft/elastalert python3 -m elastalert.elastalert --verbose --rule /opt/soft/elastalert/rules/dingtt.yml
具體能夠查看釘釘機器人開發文檔
https://github.com/anjia0532/elastalert-wechat-plugin
https://anjia0532.github.io/2017/02/16/elastalert-wechat-plugin/
https://github.com/anjia0532/elastalert-docker
# 注意上下字段對應 alert_text: | kibana_url: "https://hostname:5601/app/kibana" alarm_reason: "1分鐘內login.php至少被訪問10次" alarm_name: {} request_uri: {} request_ip: {} response_status: {} alert_text_args: - name - request - clientip - response alert_text_type: alert_text_only
kibana中elastalert插件,能夠實如今kibana界面上編輯elastalert的告警規則配置。
kibana 的 elastalert 插件須要使用新的 bitsensor/elastalert api 服務
官網主推:bitsensor/elastalert-kibana-plugin
可是樓上那個不少小版本都沒有,以上沒有的話推薦使用如下版本:
nsano-rururu/elastalert-kibana-plugin
目前版本支持:
/usr/share/kibana/bin/kibana-plugin install https://github.com/nsano-rururu/elastalert-kibana-plugin/releases/download/1.2.0/elastalert-kibana-plugin-1.2.0-7.6.2.zip --allow-root
默認狀況下,插件將鏈接到 localhost:3030,也就是 kibana 和 elastalert 在同一臺服務器上面,那麼就不須要添加如下配置。若是您的ElastAlert服務器在其餘主機或端口上運行,請在config / kibana.yml文件中添加更改如下選項:
elastalert-kibana-plugin.serverHost: 192.168.1.1 elastalert-kibana-plugin.serverPort: 9000
查看 nsano 提供的 elastalert api 服務支持列表
我使用的是 praecoapp/elastalert-server 提供的 docker 鏡像對應 elastalert 0.2.4 版本
cd /opt/soft/ git clone https://github.com/bitsensor/elastalert.git elastalert_docker cd elastalert_docker mkdir rules
vi config/config.json
由於個人 kibana 和 elastalert docker 是同一臺機,因此我主要修改 es 的配置就能夠
{ "appName": "elastalert-server", "port": 3030, "wsport": 3333, "elastalertPath": "/opt/elastalert", "verbose": false, "es_debug": false, "debug": false, "rulesPath": { "relative": true, "path": "/rules" }, "templatesPath": { "relative": true, "path": "/rule_templates" }, "es_host": "192.168.3.30", "es_port": 9200, "writeback_index": "elastalert_status" }
vi ./config/elastalert.yaml
es_host: 192.168.3.30 es_port: 9200 rules_folder: rules run_every: seconds: 5 buffer_time: minutes: 1 es_username: elastic es_password: passwd writeback_index: elastalert_status alert_time_limit: days: 2
vi docker-compose.yml
version: '3' services: elastalert: image: praecoapp/elastalert-server:20210104 container_name: elastalert hostname: elastalert restart: always network_mode: "host" ports: - "3030:3030" - "3333:3333" volumes: - ./config/elastalert.yaml:/opt/elastalert/config.yaml - ./config/elastalert-test.yaml:/opt/elastalert/config-test.yaml - ./config/config.json:/opt/elastalert-server/config/config.json - ./rules:/opt/elastalert/rules - ./rule_templates:/opt/elastalert/rule_templates #- ./elastalert:/opt/elastalert/elastalert,#這一句先註釋掉,下面有個操做
docker-compose up -d ##而後把 docker 鏡像裏面的 elastalert 複製出來 docker cp elastalert:/opt/elastalert/elastalert . ##再把docker-compose.yml 裏面的 volumes 的註釋項去掉從新啓動 docker-compose up -d
cd /opt/soft/elastalert_docker git clone https://github.com/xuyaoqiang/elastalert-dingtalk-plugin cp elastalert-dingtalk-plugin/elastalert_modules/dingtalk_alert.py elastalert/ ##安裝依賴 docker exec -it elastalert pip install pyOpenSSL==16.2.0 docker exec -it elastalert pip install requests==2.18.1 docker exec -it elastalert pip install setuptools>=11.3 #建立規則 touch rules/dingtt.yml #受權,不然頁面沒法編輯,具體能夠看下面的報錯示例 chown centos.centos rules/dingtt.yml #從新啓動 elastalert 容器 docker restart elastalert ##查看日誌 docker logs -f --tail=20 elastalert
01:50:07.285Z ERROR elastalert-server: Routes: Request for '/rules/:id' failed with error: [Error: EACCES: permission denied, open '/opt/elastalert/rules/dingtt.yaml'] { errno: -13, code: 'EACCES', syscall: 'open', path: '/opt/elastalert/rules/dingtt.yaml' }
解決辦法:
ElastAlert包含幾種具備常見監視範例的規則類型: any: 匹配任何與給定過濾器匹配的事件,這個是查到了什麼便直接報警,屬於自定義選項; frequency: 匹配Y時間內至少有X個事件的地方 spike: 當事件發生率增長或減小時匹配,API 流量陡然上升並立刻恢復的時候; flatline: 在Y時間內少於X個事件時進行匹配,內存或者CPU使用率降低的時候; blacklist並whitelist輸入: 當某個字段與黑名單/白名單匹配時匹配,昨天的那個疑似爬蟲的 IP 地址又出現了; change: 當某個字段在一段時間內具備兩個不一樣的值時進行匹配,應用的狀態忽然從 UP 轉爲 DOWN; new_term: 當字段中出現從未見過的術語時進行匹配,某個枚舉類型字段,忽然出現了未定義的類型; cardinality:當字段的惟一值數量大於或小於閾值時匹配,線上的 API 服務器忽然掛了一臺,它是根據惟一值的數量來斷定的;
# 用來區分報警,跟 realert 配合使用,在這裏意味着, # 5 分鐘內若是有重複報警,那麼當 name 不一樣時,會當作不一樣的報警處理,能夠是數組 query_key: - name # 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
在這裏,你能夠自定義 alert 的內容,它的內部使用 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
Email Slack 企業微信 釘釘 alerta:https://github.com/alerta/alerta,告警接收平臺 JIRA OpsGenie Commands HipChat MS Teams Telegram AWS SNS VictorOps PagerDuty Exotel Twilio Gitter
整理下實施ELK最起碼要實現的需求:
https://github.com/Yelp/elastalert
https://github.com/bitsensor/elastalert
https://github.com/bitsensor/elastalert-kibana-plugin
https://github.com/xuyaoqiang/elastalert-dingtalk-plugin
https://github.com/anjia0532/elastalert-wechat-plugin
http://www.javashuo.com/article/p-yrjmqnxo-ko.html