Elastalert是用python2写的一个报警框架(目前支持python2.6和2.7,不支持3.x),github地址为 https://github.com/Yelp/elastalert。他提供不同场景的规则配置,若觉得规则、告警不满足需求时,可以用python编写插件Adding a New Rule Type、Adding a New Alerter。
系统:centos6.8
python:2.7.12(请参看升级centos6 默认python版本到2.7.12)
elasticsearch:5.5
kibana:5.5
安装pip包管理工具(参考)
$ pip install elastalert
或者 git clone (推荐)
$ git clone https://github.com/Yelp/elastalert.git
安装模块
$ pip install "setuptools>=11.3" $ python setup.py install
根据不同的Elasticsearch版本安装elasticsearch-py。
Elasticsearch 5.x/2.x
$ pip install "elasticsearch>=5.0.0" ##5.x $ pip install "elasticsearch<3.0.0" ##2.x
将源码放置在你指定的目录下:
$ cd /usr/local/elastalert/ $ cp config.yaml.example config.yaml $ mkdir es_rules [xxx@crmnginx elastalert]$ egrep -v ‘^#|^$‘ config.yaml rules_folder: es_rules run_every: minutes: 5 buffer_time: minutes: 5 es_host: 10.xx.xx.xxx es_port: xxxx use_ssl: false verify_certs: false es_username: elastic es_password: changeme writeback_index: elastalert_status alert_time_limit: days: 2 $ ll [root@crmnginx elastalert]# ll total 124 drwxr-xr-x. 4 root root 4096 Nov 12 09:39 build -rw-r--r--. 1 root root 9737 Nov 12 09:39 changelog.md -rw-r--r--. 1 root root 2001 Nov 12 14:39 config.yaml -rw-r--r--. 1 root root 2029 Nov 12 09:39 config.yaml.example drwxr-xr-x. 2 root root 4096 Nov 12 09:39 dist -rw-r--r--. 1 root root 261 Nov 12 09:39 docker-compose.yml -rw-r--r--. 1 root root 264 Nov 12 09:39 Dockerfile-test drwxr-xr-x. 3 root root 4096 Nov 12 09:39 docs drwxr-xr-x. 2 root root 4096 Nov 12 11:15 elastalert drwxr-xr-x. 2 root root 4096 Nov 12 09:39 elastalert.egg-info drwxr-xr-x. 2 root root 4096 Nov 12 14:14 elastalert_modules drwxr-xr-x. 2 root root 4096 Nov 12 14:51 es_rules drwxr-xr-x. 2 root root 4096 Nov 12 09:39 example_rules -rw-r--r--. 1 root root 11359 Nov 12 09:39 LICENSE -rw-r--r--. 1 root root 480 Nov 12 09:39 Makefile -rw-r--r--. 1 root root 15945 Nov 12 09:39 README.md -rw-r--r--. 1 root root 87 Nov 12 09:39 requirements-dev.txt -rw-r--r--. 1 root root 372 Nov 12 09:39 requirements.txt -rw-r--r--. 1 root root 100 Nov 12 09:39 setup.cfg -rw-r--r--. 1 root root 1650 Nov 12 09:39 setup.py -rw-r--r--. 1 root root 117 Nov 12 11:14 smtp_auth_file.yaml -rw-r--r--. 1 root root 780 Nov 12 09:39 supervisord.conf.example drwxr-xr-x. 2 root root 4096 Nov 12 09:39 tests -rw-r--r--. 1 root root 606 Nov 12 09:39 tox.ini
此处的配置文件是指config.yaml文件。
elastalert-create-index这个命令会在elasticsearch创建索引,便于ElastAlert将有关其查询及其警报的信息和元数据保存回Elasticsearch。这不是必须的步骤,但是强烈建议创建。因为对于审计,测试很有用,并且重启elastalert不影响计数和发送alert。默认情况下,创建的索引叫 elastalert_status。
$ elastalert-create-index --host 10.xxx.xx.xxx --port xxxx --username xxx --password xxx --no-ssl --no-verify-certs
创建成功之后,我们可以在Elasticsearch中看到所创建的索引elastalert_status:
$ curl -u xxx ‘localhost:9200/_cat/indices?v‘ |grep elastalert_status green open elastalert_status eWjVRAzjTX2vmhvAK931Ng 5 1 110 0 513.8kb 266kb
ElastAlert 根据elastalert_status去确定首次启动的时候在什么时间范围内去查询,以避免重复查询。对于每个规则,它将从最近的结束时间开始查询。包括:
在/usr/local/elastalert/目录下创建文件smtp_auth_file.yaml,内容如下:
[root@crmnginx elastalert]$ cat smtp_auth_file.yaml #邮箱用户名 user: no-xx@chtxx.com ##不是邮箱密码,是设置的SMTP密码 password: YdYfxxx
在es_rules目录下修改配置文件,如下:
$ cd /opt/elastalert/es_rules [xxx@crmnginx elastalert]$ egrep -v ‘^#|^$‘ es_rules/frequency.yaml es_host: 10.xxx.xx.xxx es_port: 9200 use_ssl: false verify_certs: false es_username: xxx es_password: xxx name: es_rule writeback_index: elastalert_status type: frequency index: logstash-nginx-* num_events: 5 timeframe: minutes: 5 filter: - query: query_string: query: "status:>=400" query_string: query: "status: >=500" #SMTP configration smtp_host: smtp.263.net smtp_port: 25 #SMTP auth smtp_auth_file: /usr/local/elastalert/smtp_auth_file.yaml email_reply_to: no-xxx@chxxx.com from_addr: no-xxx@chxxx.com # (Required) # The alert is use when a match is found alert: - "email" # (required, email specific) # a list of email addresses to send alerts to email: - "xxx@chxxx.com"
上述规则表示:在elastalert执行的五分钟内,出现五条有status:>=400 或者 status: >=500的日志,则触发告警,并且告警通知将以email的形式从no-xxx@chxxx.com邮箱发送给xxx@chxxx.com
es_host、es_port:应该指向我们要查询的Elasticsearch集群。
name:是这个规则的唯一名称。如果两个规则共享相同的名称,ElastAlert将不会启动。
type:每个规则都有不同的类型,可能会采用不同的参数。该frequency类型表示“在timeframe时间内匹配成功次数超过num_events发出警报”。有关其他类型的信息,请参阅规则类型。
index:要查询的索引的名称。配置,从某类索引里读取数据,目前已经支持Ymd格式,需要先设置use_strftime_index:true,然后匹配索引,配置形如:index: logstash-es-test%Y.%m.%d,表示匹配logstash-es-test名称开头,以年月日作为索引后缀的index。
num_events:此参数特定于frequency类型,是触发警报时的阈值。
timeframe:timeframe是num_events必须发生的时间段。
filter:是用于过滤结果的Elasticsearch过滤器列表。有关
详细信息,请参阅编写过滤规则。
email:是要发送警报的地址列表。
alert:配置,设置触发报警时执行哪些报警手段。不同的type还有自己独特的配置选项。目前ElastAlert 有以下几种自带ruletype:
命令如下:
$ python -m elastalert.elastalert --config ./config.yaml #运行命令,加载所有rules $ python -m elastalert.elastalert --config ./config.yaml --rule ./es_rules/frequency.yaml ## 或者单独执行 rules_folder 里的某个 rule
$ curl -X POST "http://127.0.0.1:9200/logstash-nginx-xx/test" -d ‘{ "@timestamp": "2018-11-12T10:54:41.000Z", "status": "500" }‘
微信报警项目地址:https://github.com/anjia0532/elastalert-wechat-plugin
配置地址:https://anjia0532.github.io/2017/02/16/elastalert-wechat-plugin/
配置文件
$ cd ~/ $ git clone https://github.com/Yelp/elastalert.git $ cd elastalert $ wget https://raw.githubusercontent.com/anjia0532/elastalert-wechat-plugin/master/elastalert_modules/wechat_qiye_alert.py $ touch ~/elastalert/elastalert_modules/__init__.py $ cp config.yaml.example config.yaml $ vi es_rules/wechat_frequency.yaml
[root@crmnginx elastalert]# egrep -v ‘^#|^$‘ es_rules/wechat_frequency.yaml es_host: 10.xxx.xx.xxx es_port: xxxx use_ssl: false verify_certs: false es_username: xx es_password: xxxx name: es_rule writeback_index: elastalert_status type: frequency index: logstash-nginx-* num_events: 1 timeframe: minutes: 1 filter: - query: query_string: query: "status:>=400" query_string: query: "status: >=500" alert: - "elastalert_modules.wechat_qiye_alert.WeChatAlerter" #后台登陆后【设置】->【权限管理】->【普通管理组】->【创建并设置通讯录和应用权限】->【CorpID,Secret】 #设置微信企业号的appid corp_id: ‘xx‘ #设置微信企业号的Secret secret: ‘xxx‘ #后台登陆后【应用中心】->【选择应用】->【应用id】 #设置微信企业号应用id agent_id: ‘x‘ #部门id party_id: ‘x‘ #用户微信号 user_id: xx # 标签id tag_id: xx
在rule文件加上一下内容:
alert:- "elastalert_modules.wechat_qiye_alert.WeChatAlerter" alert_text_type: alert_text_only alert_text: | ========start========= 告警程序: elasticsearch_alert 告警主题: 接口 {} 响应时间大于2秒! 触发时间: {} request: {} responsetime: {} domain: 此域名 {} 下接口告警 status: {} upstreamaddr: {} num_hits: {} num_method: {} clientip: {} 参考来源: {} ========end========== alert_text_args: - request - "@timestamp" - request - responsetime - domain - status - upstreamaddr - num_hits - num_method - client - path