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万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章