ELK 日志報警插件 ElastAlert


https://cloud.tencent.com/developer/article/1697065

 

在日常運維中使用 elk 對業務訪問日志,設備以及軟件運行日志進行統一管理、存儲、追溯、分析。日常運維理想的狀態是能夠實時監測日志的狀態,當異常日志產生時能夠主動發送告警事件快速定位故障。然而在 elastic 開源基礎版沒有開放告警功能,我們可以使用 logstash 對接 zabbix 實現告警也可以使用第三方插件 Elastalert 實現告警功能。接下來介紹如何利用 Elastalert 工具實現日志的告警。

Elastalert是Yelp 公司基於python開發的ELK 日志報警插件,Elastalert 通過查詢 ElasticSearch 中的記錄進行比對,通過配置報警規則對匹配規則的日志進行警報。 Elastalert 將Elasticsearch與兩種類型的組件(規則類型和警報)結合使用。定期查詢Elasticsearch,並將數據傳遞到規則類型,該規則類型確定何時找到匹配項。發生匹配時,將為該警報提供一個或多個警報,這些警報將根據匹配采取行動。是由一組規則配置的,每個規則定義一個查詢,一個規則類型和一組警報。

Elastalert 項目開源地址

https://github.com/Yelp/elastalert

Elastalert 使用手冊

https://elastalert.readthedocs.io/en/latest/elastalert.html#overview

平台架構圖

software version

elastalert:0.2.0 Python 3.6.8 elastic 6.8 Centos 8.1.1911

install software

dnf install -y wget   gcc openssl-devel epel-release git dnf install -y python3 python3-devel

install elastalert

mkdir -p  /app/ git clone https://github.com/Yelp/elastalert.git cd /app/elastalert/ pip3 install "setuptools>=11.3" pip3 install -r requirements.txt #根據版本選擇 #pip3 uninstall elasticsearch #pip3 install "elasticsearch<7,>6" python3 setup.py install

elastalert 配置文件

.egrep  -v "*#|^$" config.yaml rules_folder: example_rules run_every: minutes: 1 buffer_time: minutes: 15 es_host: 192.168.99.185 es_port: 9200 es_username: elastic es_password: ****** writeback_index: elastalert_status writeback_alias: elastalert_alerts alert_time_limit: days: 2

字段參數解釋

rules_folder: ElastAlert從中加載規則配置文件的位置。run_every: ElastAlert多久查詢一次Elasticsearch的時間 buffer_time: 用來設置請求里時間字段的范圍,默認是15分鍾 Es_host: elasticsearch host ip Es_port: elasticsearch port writeback_index: 是ElastAlert將在其中存儲數據的索引的名稱 writeback_alias: 別名 alert_time_limit: 失敗警報的重試窗口

創建 elastalert-create-index

用於在elasticsearch創建索引,便於ElastAlert將有關其查詢及其警報的信息和元數據保存回Elasticsearch。對於審計,測試很有用,並且重啟elastalert不影響計數和發送alert。

elastalert-create-index --config config.yaml

查看創建索引名稱

curl -u elastic:xxxxx -XGET http://192.168.99.185:9200/_cat/indices |grep elastalert |sort -n

elastalert_status_status

ElastAlert 根據elastalert_status去確定首次啟動的時候在什么時間范圍內去查詢,以避免重復查詢。對於每個規則,它將從最近的結束時間開始查詢。包括:

  • @timestamp:文件上傳到Elasticsearch的時間。這是在運行查詢並且已經處理結果之后。
  • rule_name:相應規則的名稱。
  • starttime:查詢的開始時間戳。
  • endtime:查詢結束時間戳。
  • hits:查詢結果的數量。
  • matches:處理命中后規則返回的匹配數。請注意,這並不一定意味着警報被觸發。
  • time_taken:此查詢運行所需的秒數。

ElastAlert 規則類型

any,blacklist,whitelist,change,frequency,spike,flatline,new_term,cardinality 。

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

告警規則范例

在example_rules/ 中找到不同類型的規則的示例。

example_spike.yaml是“峰值”規則類型的示例,它使您可以警告某個時間段內的平均事件發生率增加給定因子的時間。當在過去2個小時內發生與過濾器匹配的事件比前2個小時的事件數多3倍時,此示例將發送電子郵件警報。

example_frequency.yaml是“頻率”規則類型的示例,它將在一個時間段內發生給定數量的事件時發出警報。此示例將在4小時內出現50個與給定過濾器匹配的文檔時發送電子郵件。

example_change.yaml是“更改”規則類型的示例,當兩個文檔中的某個字段發生更改時,它將發出警報。在此示例中,當兩個文檔具有相同的“用戶名”字段但“ country_name”字段的值不同時,會在24小時之內發送警報電子郵件。

example_new_term.yaml是“新術語”規則類型的示例,當一個或多個新值出現在一個或多個字段中時,它將發出警報。在此示例中,在示例登錄日志中遇到新值(“用戶名”,“計算機”)時,將發送一封電子郵件。

配置告警規則

es_host: 192.168.99.185 es_port: 9200 es_username: elastic es_password: ****** name: networklogs-alert type: frequency index: networklogs-* num_events: 1 timeframe: minutes: 10 filter: - query: query_string: query: "count: Failed" alert: - "email" email: - "xxxxx@126.com" smtp_host: smtp.qq.com smtp_port: 25 from_addr: xxxx@qq.com smtp_auth_file: /app/elastalert/elastalert/example_rules/email_auth.yaml alert_subject: "網絡設備運行日志異常" alert_text_type: alert_text_only alert_text: | 網絡設備運行日志異常 >time:{} >hostname:{} >count:{} >source:{} alert_text_args: - "@timestamp" - hostname - count - source

規則參數介紹

#Elasticsearch  機器
es_host: 192.168.99.185 #Elasticsearch 端口 es_port: 9200 #是否使用ssl 鏈接 #use_ssl: True #如果elasticsearch 有認證,填寫用戶名和密碼的地方 #es_username: username #es_password: password #rule name 必須是獨一的,不然會報錯,這個定義完成之后,會成為報警郵件的標題 name: xx-xx-alert #配置的是frequency,需要兩個條件滿足,在相同 query_key條件下,timeframe 范圍內有num_events個被過濾出來的異常 type: frequency #指定index,支持正則匹配,支持多個index,同時如果嫌麻煩直接* 也可以。index: es-nginx*,winlogbeat* #時間出發的次數 num_events: 5 #和num_events參數關聯,也就是說在4分鍾內出發5次會報警 timeframe: minutes: 4 #用來拼配告警規則,elasticsearch 的query語句,支持 AND&OR等。filter: - query: query_string: query: "message:error OR Error" #告警方式,常用郵箱告警和釘釘告警 alert: - "email" #接受報警郵箱的地址,可以指定多個。email: - "test@126.com" - "test1@126.com" #報警郵箱的smtp server smtp_host: smtp.126.com #報警郵箱的smtp 端口 smtp_port: 25 #需要把認證信息寫到額外配置文件里,需要user和password兩個屬性 smtp_auth_file: /app/elastalert/elastalert/example_rules/email_auth.yaml from_addr: ****@qq.com

郵件賬戶認證信息

#/app/elastalert/elastalert/example_rules/email_auth.yaml user: "xxxxxx@qq.com" password: "******"

測試elastalert規則

elastalert-test-rule example_rules/network.yaml

運行告警規則

python3 -m elastalert.elastalert --verbose --rule example_rules/network.yaml

郵箱告警

郵箱告警模塊配置文件

alert:
- "email" email: - "xxxxx@126.com" smtp_host: smtp.qq.com smtp_port: 25 from_addr: xxxx@qq.com smtp_auth_file: /app/elastalert/elastalert/example_rules/email_auth.yaml alert_text_type: alert_text_only aalert_text: | 網絡設備運行日志異常!!! >time:{} >hostname:{} >count:{} >source:{} alert_text_args: - "@timestamp" - hostname - count - source

郵件認證信息

使用的密碼不是登陸郵箱的密碼,而是郵箱的郵箱授權碼

#/app/elastalert/elastalert/example_rules/email_auth.yaml user: "xxxxxx@qq.com" password: "******"

釘釘告警

釘釘告警插件安裝

wget https://github.com/xuyaoqiang/elastalert-dingtalk-plugin/archive/master.zip unzip master.zip cd elastalert-dingtalk-plugin-master pip3 install pyOpenSSL==16.2.0 pip3 install setuptools==46.1.3 cp -r elastalert_modules /app/elastalert/

釘釘告警模塊配置文件

alert:
- "elastalert_modules.dingtalk_alert.DingTalkAlerter" dingtalk_webhook: "https://oapi.dingtalk.com/robot/send?access_token=xxxxxx" dingtalk_msgtype: "text" alert_subject: "網絡設備運行日志異常" alert_text_type: alert_text_only alert_text: | 網絡設備運行日志異常 >time:{} >hostname:{} >count:{} >source:{} alert_text_args: - "@timestamp" - hostname - count - source

systemctl 后台運行

elastalert01.service

# vim /usr/lib/systemd/system/elastalert01.service [Unit] Description=elastalert01 After=elastalert01.service [Service] Type=simple User=root Group=root Restart=on-failure PIDFile=/usr/local/elastalert01.pid WorkingDirectory=/app/elastalert ExecStart=/usr/local/bin/elastalert --config /app/elastalert/config.yaml --rule /app/elastalert/example_rules/network1.yaml ExecStop=/bin/kill -s QUIT $MAINPID ExecReload=/bin/kill -s HUP $MAINPID [Install] WantedBy=multi-user.target

elastalert02.service

# vim /usr/lib/systemd/system/elastalert02.service [Unit] Description=elastalert02 After=elastalert02.service [Service] Type=simple User=root Group=root Restart=on-failure PIDFile=/usr/local/elastalert02.pid WorkingDirectory=/app/elastalert ExecStart=/usr/local/bin/elastalert --config /app/elastalert/config.yaml --rule /app/elastalert/example_rules/network.yaml ExecStop=/bin/kill -s QUIT $MAINPID ExecReload=/bin/kill -s HUP $MAINPID [Install] WantedBy=multi-user.target

啟動elastalert01.service

systemctl start elastalert01.service 
systemctl enable elastalert01.service

查看elastalert進程

ps -ef |grep elastalert

本文分享自微信公眾號 - 開源搬運工宋師傅(gh_9e345a3a1074),作者:songhp

原文出處及轉載信息見文內詳細說明,如有侵權,請聯系 yunjia_community@tencent.com 刪除。

原始發表時間:2020-09-10

本文參與騰訊雲自媒體分享計划,歡迎正在閱讀的你也加入,一起分享。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM