ELK日志报警插件ElastAlert

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

ELK日志报警插件ElastAlert

它通过将Elasticsearch与两种类型的组件(规则类型和警报)结合使用。定期查询Elasticsearch,并将数据传递到规则类型,该规则类型确定何时找到匹配项。发生匹配时,将为该警报提供一个或多个警报,这些警报将根据匹配采取行动。
这是由一组规则配置的,每个规则定义一个查询,一个规则类型和一组警报。

ElastAlert包含几种具有常见监视范例的规则类型:
	匹配Y时间内至少有X个事件的地方”(frequency类型)
	当事件发生率增加或减少时匹配”(spike类型
	在Y时间内少于X个事件时进行匹配”(flatline类型
	当某个字段与黑名单/白名单匹配时匹配”(blacklist并whitelist输入)
	匹配任何与给定过滤器匹配的事件”(any类型)
	当某个字段在一段时间内具有两个不同的值时进行匹配”(change类型)
	当字段中出现从未见过的术语时进行匹配”(new_term类型)
	当字段的唯一值数量大于或小于阈值(cardinality类型)时匹配
	
告警支持邮件、钉钉、微信、自定义等多种告警方式;能灵活从es中查询出来的内容

python3.6安装

tar xf Python-3.6.8.tar.xz
yum -y install wget sqlite-devel xz gcc automake zlib-devel openssl-devel epel-release
cd Python-3.6.8/
./configure && make && make install

elastalert-create-index

运行该elastalert-test-rule工具将测试您的配置文件是否成功加载,并在过去的24小时内以调试模式运行它:
elastalert-test-rule my_rules/esport_interface_frequency.yaml 

python3 -m elastalert.elastalert --verbose --rule my_rules/esport_interface_frequency.yaml

安装elastalert

#安装ElastAlert的最新发行版:
pip3 install elastalert
#克隆ElastAlert存储库里面有配置文件可以参考
wget https://github.com/Yelp/elastalert/archive/v0.2.4.tar.gz
tar xf elastalert-0.2.4.tar.gz
mv elastalert-0.2.4 /opt/
ln -s elastalert-0.2.4 elastalert

安装之后会自带三个命令
#elastalert-create-index
ElastAlert会把执行记录存放到一个ES 索引中,该命令就是用来 创建这个索引的,默认情况下,索引名叫elastalert_status。其中有4个 _type,都有 自己的@timestamp字段,所以同样也可以用kibana,来查看这个索引的日志记录情况。

#elastalert-rule-from-kibana
从Kibana3已保存的仪表盘中读取Filtering设置,帮助生成config.yaml里的配置。不过注意,它只会读取filtering,不包括queries。

#elastalert-test-rule
测试自定义配置中的rule设置 

钉钉报警插件安装

wget https://github.com/xuyaoqiang/elastalert-dingtalk-plugin/archive/master.zip
unzip elastalert-dingtalk-plugin-master.zip
cd elastalert-dingtalk-plugin-master
pip3 install -r requirements.txt
cp -r elastalert_modules /opt/elastalert/
pip3 install requests
pip3 uninstall urllib3
pip3 uninstall chardet

规则范例

#可以在example_rules /中找到不同类型的规则的示例。
example_spike.yaml
是“峰值”规则类型的示例,它使您可以警告某个时间段内的平均事件发生率增加给定因子的时间。当在过去2个小时内发生与过滤器匹配的事件比前2个小时的事件数多3倍时,此示例将发送电子邮件警报。

example_frequency.yaml
是“频率”规则类型的示例,它将在一个时间段内发生给定数量的事件时发出警报。此示例将在4小时内出现50个与给定过滤器匹配的文档时发送电子邮件。

example_change.yaml
是“更改”规则类型的示例,当两个文档中的某个字段发生更改时,它将发出警报。在此示例中,当两个文档具有相同的“用户名”字段但“ country_name”字段的值不同时,会在24小时之内发送警报电子邮件。

example_new_term.yaml
是“新术语”规则类型的示例,当一个或多个新值出现在一个或多个字段中时,它将发出警报。在此示例中,在示例登录日志中遇到新值(“用户名”,“计算机”)时,将发送一封电子邮件。

编写管理规则

cp config.yaml.example config.yaml
[root@daily-elk elastalert]# grep -Ev '#|^$' config.yaml
rules_folder: example_rules
run_every:
  seconds: 3
buffer_time:
  minutes: 15
es_host: 10.0.1.20
es_port: 9200
writeback_index: elastalert_status
writeback_alias: elastalert_alerts
alert_time_limit:
  days: 1
rules_folder: my_rules

#修改后,执行elastalert-create-index ,会自动在es中创建索引 elastalert_status,用来保存各个rule每次的执行结果。
#字段解释
rules_folder:	是ElastAlert从中加载规则配置文件的位置。它将尝试加载文件夹中的每个.yaml文件。没有任何有效规则,ElastAlert将无法启动。随着此文件夹中文件的更改,ElastAlert还将加载新规则,停止运行缺少的规则并重新启动修改后的规则
run_every:	是ElastAlert多久查询一次Elasticsearch的时间
buffer_time:	用来设置请求里时间字段的范围,默认是45分钟
Es_host:	elasticsearch的host地址
Es_port:	elasticsearch对应的端口号
writeback_index:	是ElastAlert将在其中存储数据的索引的名称
writeback_alias:	别名
alert_time_limit:	是失败警报的重试窗口

配置告警规则

#copy一份默认的规则
cp example_rules/example_frequency.yaml example_rules/my_rule.yaml 
[root@daily-elk elastalert]# grep -Ev '#|^$' example_rules/my_rule.yaml 
es_host: 10.0.1.20
es_port: 9200
name: my_text
type: frequency
index: text-*
use_strftime_index: true
num_events: 1
timeframe:
    minutes: 5
filter:
- query:
    query_string:
      query: "ERROR"
alert:
- "elastalert_modules.dingtalk_alert.DingTalkAlerter"
dingtalk_webhook: https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxxxxxxxxxxx
dingtalk_msgtype: text
#字段解释
Es_host:	elasticsearch的host地址
Es_port:	elasticsearch对应的端口号
name:	是此规则的唯一名称。如果两个规则共享相同的名称,则ElastAlert将不会启动
type:	每个规则具有不同的类型,可能采用不同的参数。该frequency类型的意思是“当num_events出现多个警报时发出警报timeframe
index:	要查询的索引的名称
use_strftime_index: 	如果为true,则ElastAlert将为每个查询使用datetime.strftime格式化索引
num_events:	此参数特定于frequency类型,并且是触发警报时的阈值。
timeframe:	是num_events必须发生的时间段。
filter:	是用于过滤结果的Elasticsearch过滤器列表
#详细参考
https://elastalert.readthedocs.io/en/latest/recipes/writing_filters.html#writingfilters

alert:	是在每次规则中运行的警报的列表
#详细参考
https://elastalert.readthedocs.io/en/latest/ruletypes.html#alerts

测试规则

elastalert-test-rule example_rules/my_rule.yaml

调试运行

python3 -m elastalert.elastalert --verbose --rule example_rules/my_rule.yaml

生产运行

python -m elastalert.elastalert --config config.yaml

原文地址:https://www.cnblogs.com/opesn/p/12994199.html