filebeat7.5.1+elasticsearch7.5.1+elastAlert0.2.1+elastalert-dingtalk-plugin實現日誌監控釘釘告警

環境

CentOS7.5
jdk1.8 
python3.6
1、首先先理清版本問題
  • elastAlert0.2.1 支持elasticsearch7+版本
  • elastAlert0.2.1 需要python3.6 版本
  • elastalert-dingtalk-plugin 插件需要修改elastalert依賴信息以及elasticsearch依賴信息

下載

git clone https://github.com/Yelp/elastalert.git
git clone https://github.com/xuyaoqiang/elastalert-dingtalk-plugin.git
2、python3.6環境安裝

python官網
選擇3.6.10版本下載wget 命令速度很慢,建議可以下載後上傳服務器
下載完成後編譯安裝

tar -zxf Python-3.6.10.tgz
# 安裝依賴包
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc  libffi-devel
cd Python-3.6.10
# 編譯
./configure --prefix=/usr/local/python3.6
#安裝
make && make install

安裝完成後會在 /usr/local目錄下生成 python3.6目錄
然後創建軟鏈接

ln -s /usr/local/python3.6/bin/python3 /usr/bin/python3

測試打印版本號,顯示3.6.10 表示安裝成功

python3 -V

python3.6 自帶pip3 可以直接創建軟鏈接

ln -s /usr/local/python3.6/bin/pip3 /usr/bin/pip3

Elasticsearch7.5.1 安裝

配置config/elasticsearch.yaml

node.name: node-1
network.host: 192.168.0.99
http.port: 9200
cluster.initial_master_nodes: ["node-1"]
#開啓跨域方便前端工具訪問
http.cors.enabled: true
http.cors.allow-origin: "*"

配置系統 /etc/security/limits.conf

* soft nofile 65535
* hard nofile 65535
* - nofile 65536
* - memlock unlimited

配置系統/etc/sysctl.conf

vm.max_map_count=262144

配置完成後執行

/sbin/sysctl -p

退出終端重新進入纔會生效

接着就可以直接啓動Elasticsearch了
啓動命令 -d 後臺啓動命令

./bin/elasticsearch -d

查看日誌有沒有異常

tail -100f logs/elasticsearch.log

安裝filebeat7.5.1

Filebeat是一個採集器需要安裝在你需要採集日誌的服務器上
配置filebeat.yml,這裏以採集nginx access.lo爲例日誌最好使用json格式輸出。具體怎麼設置nginx日誌格式,自行查資料。
不同版本的filebeat配置可能會有一些不一致的地方。詳細請參考官方文檔

filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /var/log/nginx/access.log
  json.keys_under_root: true
  json.overwrite_keys: true
filebeat.config.modules:
  path: ${path.config}/modules.d/*.yml
  reload.enabled: false

setup.template.settings:
  index.number_of_shards: 1

setup.template.name: "nginx-access"
setup.template.pattern: "nginx-access-*"
setup.ilm.enabled: false
output.elasticsearch:
  # Array of hosts to connect to.
  hosts: ["xxx.xxx.xxx.xxx:9200"]
  index: nginx-access-%{+yyyy-MM-dd}

後臺啓動命令

 nohup ./filebeat -e -c filebeat.yml >/dev/null 2>&1 &

ElastAlert安裝(基於python3.6環境)

ElastAlert doc文檔 相關配置可以在這裏查看
python3.6 我們上面已經安裝完成了
現在可以時使用 python3 命令完成ElastAlert安裝
進入 elastalert 安裝目錄

##安裝依賴
pip3 install -r requirements.txt
python3 setup.py install

安裝完成後會在python3.6目錄生成4個elastalert*命令
添加軟鏈接

ln -s /usr/local/python3.6/bin/elastalert* /usr/bin

添加完成後就可以使用命令了
安裝

  • elastalert-dingtalk-plugin 模塊

進入elastalert-dingtalk-plugin 目錄
修改依賴requirement.txt

elastalert==0.2.1
elasticsearch>=7.0.0
pyOpenSSL==16.2.0
requests==2.18.1
setuptools>=11.3

然後安裝依賴,安裝完成無錯進行如下操作,如果報錯自行解決,正常不會報錯

pip3 install -r requirements.txt 

然後配置 config.yaml 。這個配置實際是elastalert的配置文件,包括後面需要配置的rules 也都是elastalert配置,詳情參考ElastAlert doc文檔

rules_folder: rules

run_every:
  minutes: 1

buffer_time:
  minutes: 15

es_host: 192.168.0.99

es_port: 9200

writeback_index: elastalert_status
alert_time_limit:
  days: 

然後配置rules/api_error.yaml

name: API錯誤響應(status >= 400)
type: frequency
index: nginx-access-*
num_events: 5
timeframe:
    minutes: 1
filter:
- range:
    status:
      from: 400
      to: 599
#只需要的字段 https://elastalert.readthedocs.io/en/latest/ruletypes.html#include
include: ["_index","uri","remote_addr","http_x_forwarded_for","status"]
alert:
- "elastalert_modules.dingtalk_alert.DingTalkAlerter"

dingtalk_webhook: "https://oapi.dingtalk.com/robot/send?access_token=xxxxx"
dingtalk_msgtype: "text"

然後在elastalert-dingtalk-plugin目錄下運行啓動命令
命令詳情參考GITHUB

#單獨指定某個規則啓動
python3 -m elastalert.elastalert --verbose --rule rules/api_error.yaml

具體每個項目的規則以及更多的配置,請自行深入。

發佈了119 篇原創文章 · 獲贊 28 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章