美文网首页
Elastalert告警插件实现日志告警

Elastalert告警插件实现日志告警

作者: 阿当运维 | 来源:发表于2023-04-20 17:44 被阅读0次

安装elastalert插件
elastalert插件直接每隔xxx时间去es检索一遍,触发条件,就会通过告警媒介来发送告警通知,此处介绍邮件形式(也可dingding,但是需要单独装钉钉的插件)

系统: Ubtun20.04.6

环境: python3.8

需求:

elk收集的日志,对nginx日志进行监控告警

实现思路:

nginx日志状态码告警这个需求可以通过找一台机器,安装并配置elastalert插件 每过xxx时间去指定的es服务器拉取匹配的索引信息来 判断状态码的值, 不符合条件就触发告警媒介(如邮件,钉钉)来发送告警。这种不需要过多复杂判断的需求,直接通过中间件本身即可实现。
———————————————————————————————————————————————————

环境准备

python虚拟环境执行:

1.安装

pip3 install virtualenv -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

2.创建

virtualenv /opt/logs-env

3.启动

cd /opt/logs-env
source ./bin/activate

注意: (看到主机前面有(logs-env) 代表现在是在虚拟环境下进行的了)
deactivate --关闭退出虚拟环境

———————————————————————————————————————————————————

安装Elastalert

1.下载包

cd /opt/ && wget https://github.com/Yelp/elastalert/archive/refs/heads/master.zip
unzip elastalert-master.zip

2.更新pip

python3 -m pip install --upgrade pip

3.设置pip永久全局国内源

pip3 config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

4.安装elastalert

cd /opt/elastalert-master
pip3 install setuptools-rust
python3 setup.py install

注: 如果中途出现安装模块慢或者网络超时的情况,根据提示的插件或模块名使用pip3 install xxxx来手动安装下。最终出现Finished processing dependencies for elastalert==0.2.4即为安装完成。

5.修改elastalert配置文件

cd /opt/elastalert-master 
cp config.yaml.example config.yaml

修改完成后配置内容:

#告警规则目录(相对elastalert目录,事先创建好jkyy目录)
rules_folder: example_rules/jkyy

#ElastAlert多久向ES发送一次请求来根据规则文件去查一次(可为seconds,minutes,hours,days,weeks)
run_every:
  minutes: 1

#当查询开始一直到结束,最大的缓存时间(默认15)
buffer_time:
  minutes: 15

#es节点地址,端口(如果es配置了登录认证需要把用户密码也写上)
es_host: 192.168.3.233
es_port: 9200
es_username: xxxxx
es_password: xxxxx

#ElastAlert插件用来存储告警信息操作来创建的索引名
writeback_index: elastalert_status
writeback_alias: elastalert_alerts

#如果告警发送失败,则会在下面时间范围内尝试重新发送
alert_time_limit:
  days: 2

向es创建索引elastalert-create-index

6.告警规则文件配置

关于nginx状态码告警

vim /opt/elastalert-master/example_rules/jkyy/nginx_code.yaml

例如: nginx状态码大于等于400时,发送告警,告警内容为指定内容。(不指定alert_text内容默认为发送全部字段)

es_host: 192.168.3.233
es_port: 9200
es_username: xxxxxx
es_password: xxxxxx
name: test elk rules
type: frequency
index: nginx-logs-*
num_events: 1
timeframe:
  seconds: 5
realert:
  minutes: 0
filter:
- query_string:
      query: "response_code: >=404" #kibana上能查询出来,直接放在这里就行,具体其他写法可以参考官网。
#邮箱配置
smtp_host: smtp.163.com
smtp_port: 465
smtp_ssl: True
smtp_auth_file: /opt/smtp_auth_file.yaml
email_reply_to: x@163.com
from_addr: x@163.com
alert:
- "email"
email:
- "x@163.com"
alert_subject: "nginx 日志告警 500-509"
alert_text: "
客户端IP: {}\n
状 态 码: {}\n
"
alert_text_type: alert_text_only
alert_text_args:
- client_ip
- response_code
#- client_ip,response_code为logstash处理后切分出来的key,可在kibana查询日志的时候看到。

7.邮件账户文件配置

#邮箱用户名
user: xxx@163.com
##不是邮箱密码,是设置的SMTP密码
password: xxxx

8.写入一条测试数据

10.10.11.1 - - [19/Apr/2023:16:33:12 +0800] "GET / HTTP/1.1" 409 0 "-" Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36 - 0.001  192.168.1.132:8005 900 0.000

9.启动配置

调试模式启动:

elastalert --config /opt/elastalert-master/config.yaml --rule nginx_code.yaml --verbose

后台启动:

elastalert --config /opt/elastalert-master/config.yaml --rule /opt/elastalert-master/example_rules/jkyy/nginx_code.yaml &

9.收到告警


有时可能中间件本身处理不了复杂的逻辑判断来实现需求,就需要我们自己用在远程写服务或者脚本来实现了,这时就可以elastalert告警规则文件中使用http post方式来把符合条件的日志信息发送到远程的脚本/服务中去处理,在脚本中来实现逻辑判断,邮件发送告警。

例:
vim /opt/elastalert-master/example_rules/jkyy/frequency.yaml

es_host: 192.168.1.201
es_port: 9200
es_username: log_elasticalert
es_password: xxxx

name: JKYY-API-PROJECT logs monitor
type: frequency
index: filebeat-*
num_events: 1
timeframe:
   seconds: 5
realert:
 minutes: 0
filter:
- query:
   query_string:
           query: "message: \"API-PROJECT\""
alert: post
http_post_url: "http://127.0.0.1:9099/biz_alert"
http_post_headers:
   type: API-PROJECT

直接日志发送给远程服务/脚本来处理 ,http_post_headers可以写一个请求头,服务端也会根据这个header来更好区分来源
————————————————————————————————————————————————
其他方案: Lostash端处理:
将从kafka或者Filebeat中拿到的日志去做格式化处理的时候,过滤阶段该怎么处理怎么处理,或者不做处理都行,只需要在输出阶段写入到es(常规流程)后,判断一下message中是否含有xxx字符,如果有就直接发送到远端API接口去处理,以下是配置:

output {
    elasticsearch {
        hosts => ["http://xxxxx:9200","http://xxxxx:9200","http://xxxxx:9200"]
        index => "nginx-logs-%{+YYYY.MM.dd}"
        document_type => "%{[@metadata][type]}"
        user => "logstash_server"
        password => "xxxxx"
    }
# stdout { codec => rubydebug }
    if [message] =~ /API-QPS/ {  
        http {
          http_method => "post"
          url => "http://xxxx:9099/biz_alert"
          #format => "message"
          message => '%{message}'
          headers => ["type", "API-QPS"]
          retry_failed => true
          automatic_retries => 2
        } 
    }
    if [message] =~ /API-PROJECT/ {
        http {
          http_method => "post"
          url => "http://xxxx:9099/biz_alert"
          #format => "message"
          message => '%{message}'
          headers => ["type", "API-PROJECT"]
          retry_failed => true
          automatic_retries => 2
        }
    }
}

相关文章

网友评论

      本文标题:Elastalert告警插件实现日志告警

      本文链接:https://www.haomeiwen.com/subject/ymzrjdtx.html