ElastAlert安装与使用

2019-03-06  本文已影响0人  小浩945

安装

官方文档:https://elastalert.readthedocs.io/en/latest/running_elastalert.html

我简单总结一下附带我遇到的坑:

环境

安装

可以setup.py脚本安装也可以使用pip 安装,但是必须从git上拉下来,因为好多配置文件是必须要有的。

#安装依赖包
yum install gcc libffi-devel python-devel openssl-devel python-setuptools
#安装
git clone https://github.com/Yelp/elastalert.git 
pip install -r requirements.txt  
pip install "setuptools>=11.3"
python setup.py install
#或者 pip install elastalert
Elasticsearch 5.0+:
pip install "elasticsearch>=5.0.0"
Elasticsearch 2.X:
pip install "elasticsearch<3.0.0"

配置

cd elastalert  
cp config.yaml.example config.yaml #基本信息
vim config.yaml

基本配置 config.yaml

#将从该文件夹下读取*.yaml配置文件
rules_folder: your_rule_folder
#查询es中 elastalert_status 索引的频率,也可以是minutes,days 等等
run_every:
  hours: 1
# 每两分钟查询一次es 匹配 rules 的数据,存放到elastalert_status中
buffer_time:
  minutes: 2
#host
es_host: 127.0.0.1
#port
es_port: 9200
# Option basic-auth username and password for Elasticsearch
es_username: username
es_password: pwd

规则配置 your_rule.yaml

例子:

es_host: 127.0.0.1 
es_port: 9200
es_username: xxx
es_password: xxx

# (Required)
# Rule name, must be unique
# 给rule一个唯一的名字
name: prod

# (Required)
# Type of alert.下面各种type详细介绍
type: any
#注意了!!!!
#这是最大的坑,而且官方文档也没有比较明确的说明,elastalert只会把一条hit的记录发送给你,如果你想获取多条需要使用聚合功能,
aggregation:
#  "* * * * *" means: run as the "run_every" in config.yaml
 schedule: "* * * * *"
aggregate_by_match_time: true

# (Required)
# sd asd Index to search, wildcard supported
index: '*-prod-*'


# (Required)
#这里使用的是ES 的DSL语句做过滤条件
filter:
#逻辑组合
- bool:
    #必须存在
    must:
      - match:
          level: "ERROR"
    #必须不存在,即过滤掉的
    must_not:
      - match:
          stackTrace: "org.apache.catalina.connector.ClientAbortException: java.io.IOException: Broken pipe"
      - match:    
          message: "[SUCCESS]"
 
# (Required)
# 报警方式,有邮件 jira等等,github上还有微信等,官网介绍https://elastalert.readthedocs.io/en/latest/ruletypes.html#alerts
#我用的是command 即执行一个命令,比较灵活一些
alert:
- command
#- email
pipe_match_json: true #把参数以json文件流的形式传入,python中以 sys.stdin.read()接受
command: ["/root/elastalert/your_rule_folder/command_alert.py"]

rule设置各自独立以文件方式存储在rules_folder设置的目录里。其中可以定义下面这些参数:

type :不同的类型还有自己独特的配置选项。目前ElastAlert有以下几种自带ruletype:

初始化

安装完成后会自带三个命令:

直接运行:

  1. elastalert-create-index

  2. elastalert-test-rule your_rule_folder/your_rule.yaml
    

使用

在elastalert/下运行

python -m elastalert.elastalert --config ./config.yaml --verbose --rule your_rule_folder/your_rule.yaml

-m 相当于import,叫做当做模块来启动

--config 指定配置文件 默认./config.yaml

--verbose 把详细信息打印出来

--rule 指定 rule文件

上一篇 下一篇

猜你喜欢

热点阅读