ELK 日志报警插件 ElastAlert

时间:2022-07-24
本文章向大家介绍ELK 日志报警插件 ElastAlert,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

在日常运维中使用 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