当前位置:   article > 正文

ELK系列(五) Elastalert2搭建

elastalert2

ELK系列传送门

ELK系列(一) ElasticSearch 8.9.2集群搭建

ELK系列(二)Kafka集群3.4.0搭建

ELK系列(三) Kibana8.9.2搭建

ELK系列(四) Logstash8.9.2搭建

ELK系列(五) Elastalert2搭建

ELK数据流规划

  1. ELK接入nginx-acces日志数据流规划
  2. 数据流向
  3. filebeat -------> kafka ----> logstash ----> ES --------> kibana
  4. 收集 mq 处理日志 存储、搜索 展示、分析
  5. |
  6. |
  7. elastalert2---钉钉告警
  8. ES集群搭建已完成 可见传送门ELK系列(一) ElasticSearch 8.9.2集群搭建
  9. Kafka集群搭建已完成 可以传送门ELK系列(二) Kafka集群3.4.0搭建
  10. Kibana搭建已完成 可见传送门ELK系列(三) Kibana8.9.2搭建
  11. Logstash搭建已完成 可见传送门ELK系列(四) Logstash8.9.2搭建

主机规划

  1. 主机名 内网地址 搭建程序
  2. ELK8-1 192.168.0.1 elasticsearch+kafka+zookeeper
  3. ELK8-2 192.168.0.2 elasticsearch+kafka+zookeeper
  4. ELK8-3 192.168.0.3 elasticsearch+kafka+zookeeper
  5. logstash1 192.168.0.4 kibana+logstash
  6. syslog 192.168.0.60 elastalert2
  7. 新增主机syslog部署elastalert2
  8. 基础环境配置和系统参数优化参考前文

Elastalert2搭建

  1. Elastalert2依赖配置
  2. yum -y groupinstall "Development Tools"
  3. yum -y install openssl11
  4. yum -y install openssl11-devel #elastalert2依赖openssl11替换openssl
  5. ln -sf /usr/lib64/pkgconfig/openssl11.pc /usr/lib64/pkgconfig/openssl.pc
  6. ln -s /usr/bin/openssl11 /usr/bin/openssl
  7. wget https://www.python.org/ftp/python/3.12.1/Python-3.12.1.tgz
  8. #elastalert2底层依靠 python环境配置
  9. [root@syslog src]# yum install zlib zlib-devel libffi-devel bzip2-devel make gcc -y
  10. tar -zxvf Python-3.12.0.tgz
  11. cd Python-3.12.0/
  12. ./configure --with-ssl --prefix=/usr/local/lib/python3.12
  13. make
  14. make install
  15. ln -s /usr/local/lib/python3.12/bin/python3.12 /usr/bin/python3
  16. ln -s /usr/local/lib/python3.12/bin/pip3.12 /usr/bin/pip
  17. pip install --upgrade pip
  18. # cat /root/.pip/pip.conf #pip加速
  19. [global]
  20. # 清华大学
  21. index-url = https://pypi.tuna.tsinghua.edu.cn/simple/
  22. pip install elastalert2
  23. git clone https://github.com/jertel/elastalert2.git
  24. $ pip install "setuptools>=11.3"
  25. $ python3 setup.py install
  26. [root@syslog elastalert2]# pwd
  27. /data/elastalert2
  28. [root@syslog elastalert2]# ls
  29. build CHANGELOG.md config.yaml dist docs elastalert2.egg-info LICENSE nohup.out requirements-dev.txt rules setup.py
  30. certs chart CONTRIBUTING.md Dockerfile elastalert examples Makefile README.md requirements.txt setup.cfg tests
  31. ln -s /usr/local/lib/python3.12/bin/elastalert* /usr/bin #提升到系统命令
  32. systemctl纳管
  33. [root@syslog system]# cat elastalert.service
  34. [Unit]
  35. Description=ElastAlert Service
  36. After=network.target
  37. [Service]
  38. Type=simple
  39. ExecStart=/usr/bin/python3 -m elastalert.elastalert --config /data/elastalert2/config.yaml
  40. Restart=always
  41. User=root
  42. Group=root
  43. [Install]
  44. WantedBy=multi-user.target
  45. systemctl start elastalert

Elastalert2通过SSL链接ES集群

  1. Elastalert2通过es证书创建c端证书和key去链接es
  2. [root@ELK8-1 elasticsearch]# ./bin/elasticsearch-keystore show xpack.security.http.ssl.keystore.secure_password
  3. warning: ignoring JAVA_HOME=/usr/local/jdk-20.0.1; using bundled JDK
  4. r_FBdQ9VSxasdzcyE9G9oA
  5. [root@syslog certs]# openssl pkcs12 -in http.p12 -nocerts -nodes > http.key
  6. Enter Import Password:
  7. [root@syslog certs]# ls
  8. http_ca.crt http.key http.p12
  9. [root@syslog certs]# openssl pkcs12 -in http.p12 -clcerts -nokeys > http.crt
  10. Enter Import Password:
  11. [root@syslog elastalert2]# cd certs/
  12. [root@syslog certs]# ls
  13. http_ca.crt http.crt http.key http.p12
  14. [root@syslog elastalert2]# cat config.yaml | grep -v "^$"
  15. rules_folder: /data/elastalert2/rules #告警规则目录
  16. run_every:
  17. minutes: 1 #间隔60s去遍历一次规则判断
  18. buffer_time:
  19. minutes: 15
  20. es_host: 192.168.0.1
  21. es_host: 192.168.0.2
  22. es_host: 192.168.0.3
  23. es_port: 9200
  24. use_ssl: True
  25. verify_certs: True
  26. ssl_show_warn: True
  27. es_username: elastic
  28. es_password: tIxjmD8nWabee6Rs0QRm
  29. ca_certs: /data/elastalert2/certs/http_ca.crt
  30. client_cert: /data/elastalert2/certs/http.crt
  31. client_key: /data/elastalert2/certs/http.key
  32. writeback_index: elastalert_status #告警内容会写入该索引
  33. alert_time_limit:
  34. days: 2

Elastalert2告警规则

  1. 告警规则希望有大佬多交流 具体不是太会写
  2. # Rule Configuration for Individual IPs (Nested Rule)
  3. name: nginx_access_4xx_individual_ips
  4. type: frequency
  5. index: "gf20.54*" #查询该索引
  6. num_events: 100 # Individual IP 404s threshold #60s超过100触发
  7. timeframe:
  8. seconds: 60
  9. query_key: client.ip #查询单ip的状态码字段
  10. filter:
  11. - query:
  12. bool:
  13. must:
  14. - query_string:
  15. query: "response_code: [400 TO 599]"
  16. alert:
  17. - dingtalk #告警方式
  18. alert_text_type: "alert_text_only"
  19. dingtalk_webhook: "https://oapi.dingtalk.com/robot/send?access_token=qw125s478z12o6643fc98b204cc4d99642d3f7fa7da5400e90c21e93025955a2"
  20. dingtalk_access_token: "qw125s478z12o6643fc98b204cc4d99642d3f7fa7da5400e90c21e93025955a2"
  21. dingtalk_msgtype: "text"
  22. alert_text: | #定义告警文本匹配下列参数
  23. 告警程序: ElasticSearch_Alert
  24. 来源 Ip: {}
  25. 域 名: {}
  26. 调用方式: {}
  27. 请求链接: {}
  28. 触发条件: 60s 内 {} 状态码 超过 {} 次
  29. alert_text_args:
  30. - client.ip
  31. - domain
  32. - http_method
  33. - request_path
  34. - response_code
  35. - num_events

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/579030
推荐阅读
相关标签
  

闽ICP备14008679号