elastalert安装使用

【1】安装使用

(1.1)官网

https://github.com/Yelp/elastalert

https://elastalert.readthedocs.io/en/latest/running_elastalert.html

(1.2)安装

https://github.com/Yelp/elastalert  我直接下载的zip 包

yum -y install wget openssl openssl-devel gcc gcc-c++
yum -y install python3  git python3-devel
pip3 install --upgrade pip
yum -y install python-pip python-devel

unzip elastalert-master.zip
cd elastalert-master
pip3 install "setuptools">=11.3
pip3 install -r requirements.txt
pip3 install setuptools-rust
#针对Es版本修改
pip3 install "elasticsearch>=5.0.0"
python3 setup.py install

虚拟环境安装参考(并未执行)

yum -y install python3  git python3-devel
cd /usr/local/
git clone https://github.com/Yelp/elastalert.git
pip3 install virtualenv
mkdir -p  /usr/local/elastalert/venv_py3.6_elastalert-0.2.1
python3 -m venv /usr/local/elastalert/venv_py3.6_elastalert-0.2.1/
cd venv_py3.6_elastalert-0.2.1/bin/
. activate
pip install -r requirements.txt
pip3 install setuptools-rust

python setup.py install

(1.3)核验

[root@DB6 /data/software/elastalert-master]$ ll /usr/local/bin/elastalert*
-rwxr-xr-x. 1 root root 973 10月 12 17:25 /usr/local/bin/elastalert
-rwxr-xr-x. 1 root root 999 10月 12 17:25 /usr/local/bin/elastalert-create-index
-rwxr-xr-x. 1 root root 1007 10月 12 17:25 /usr/local/bin/elastalert-rule-from-kibana
-rwxr-xr-x. 1 root root 993 10月 12 17:25 /usr/local/bin/elastalert-test-rule
[root@DB6 /data/software/elastalert-master]$

有环境变量就成了;

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设置

elastalert:实际运行命令

我们再检索一下:

    

【2】修改配置

(2.1)修改总体配置 config.yaml

  

 默认是有个参考案例文件的;

我们复制一个

#当前已经在 elastalert 目录下
cp config.yaml.example config.yaml

vim config.yaml

rules_folder: rules    # 规则目录位置
run_every:    # 多久去查询一下根据定义的规则去es查询是否有符合规则的字段
  minutes: 1
buffer_time:    # 当查询开始一直到结束,最大的缓存时间
  minutes: 15
es_host: 192.168.175.132    # es地址
es_port: 9200            # es端口
#es_username: elastic    # es账户
#es_password: 111qqq        # es密码
writeback_index: elastalert_status    # es里的索引
writeback_alias: elastalert_alerts
alert_time_limit:     # 如果alert当时没有发出去重试多久之后放弃发送
  days: 2

还有一些大多是加密、证书、等参数,有兴趣可以自己看

(2.2)规则告警文件 rules/system.yaml

# 当前已经在 elastalert-master 目录下;
# 构造规则目录,就是 config.yaml 中的  rules_folder: rules    # 规则目录位置
mkdir rules

vim rules/system.yaml

# ---------------- es配置 ----------------------
es_host: 192.168.175.132 # es的IP地址
es_port: 9200 # es的端口
#es_username: elastic  # es的用户名
#es_password: 111qqq # es的密码

# ----------------

# --------------- 报警基础信息配置 ------------------
name: sys_errorlog   # 报警邮件的标题
type: frequency # 类型:频率
index: system-* # 监控的索引,多个使用逗号隔开
num_events: 1 # 时间内触发的次数
timeframe:
 minutes: 1   # 时间,和上边的参数关联,1分钟内有1次会报警

# ---------------------


# ---------------- 报警过滤条件 -------------------
# For more info: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl.html
filter: - query: query_string: query: "message: a" alert_text: "system日志异常测试" # 会在报警内容中显示 smtp_host: smtp.qq.com # smtp的地址 smtp_port: 25 # 端口 smtp_auth_file: /data/software/elastalert-master/rules/smtp_auth_file.yaml # 用户密码的文件 email_reply_to: 815202984@qq.com # 发送邮件的邮箱 from_addr: 815202984@qq.com alert: - "email" # 报警类型 email: # 收件人地址 - "guochaoqun@xx.com" # -------------- 重复告警 --------------- realert: minutes: 1 # 1分钟内相同告警不会重复发送 #exponential_realert: # minutes: 10 #指数型增长最大值 # 指数级扩大 realert 时间,中间如果有报警, # 则按照5->10->20->40->60不断增大报警时间直到最大时间, # 如果之后报警减少,则会慢慢恢复原始realert时间 # -------------------------------------------- # ------------ 聚合相同告警 ------------------ aggregation_key: name #聚合报警的内容,只展示 name 与 message summary_table_fields: - name - _index - message # -------------------------- # ------------------------告警内容格式化 # 注意这些格式化的信息,都是从es中拿过来的 alert_subject: "Error {0}@{1}" alert_subject_args: - name # 该name 为本代码上面定义的name - host.ip[0] alert_text_type: alert_text_only alert_text: | > datetime: {0} > Message: {1} > Host: {2} > index: {3} > ip_info: {4} > os_info: {5},{6} > input_type: {7} > log_file: {8} > num_hits: {9} > num_matches: {10} alert_text_args: - "@timestamp" - message - agent.hostname - _index - host.ip[0] - host.os.name - host.os.version - input.type - log.file.path - num_hits - num_matches # alert_text: "system日志异常测试" # 会在报警内容中显示,非格式化内容 # ----------------------

vim  rules/smtp_auth_file.yaml 

user: "815202984@qq.com"
password: 'xxxxxx' # qq 邮箱授权码

(2.3)测试规则告警文件

# 当前目录在 elastalert-master目录下
[root@DB5 /data/software/elastalert-master]$ elastalert-test-rule --config config.yaml rules/system.yaml
INFO:elastalert:Note: In debug mode, alerts will be logged to console but NOT actually sent.
            To send them but remain verbose, use --verbose instead.
/usr/local/lib/python3.6/site-packages/elasticsearch/connection/base.py:209: ElasticsearchWarning: Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.14/security-minimal-setup.html to enable security.
  warnings.warn(message, category=ElasticsearchWarning)
Error running your filter:
TypeError("search() got multiple values for argument 'body'",)
INFO:elastalert:Note: In debug mode, alerts will be logged to console but NOT actually sent.
                To send them but remain verbose, use --verbose instead.
1 rules loaded
INFO:apscheduler.scheduler:Adding job tentatively -- it will be properly scheduled when the scheduler starts
INFO:elastalert:Queried rule sys_errorlog from 2021-10-13 10:11 CST to 2021-10-13 10:12 CST: 0 / 0 hits

Would have written the following documents to writeback index (default is elastalert_status):

elastalert_status - {'rule_name': 'sys_errorlog', 'endtime': datetime.datetime(2021, 10, 13, 2, 12, 41, 392253, tzinfo=tzutc()),
 'starttime': datetime.datetime(2021, 10, 13, 2, 11, 40, 792253, tzinfo=tzutc()), 'matches': 0, 'hits': 0, 
'@timestamp': datetime.datetime(2021, 10, 13, 2, 12, 41, 458879, tzinfo=tzutc()), 'time_taken': 0.026232481002807617}

 如上列代码,基本有标红那块 就表示成功了

但很明显期间也有错误  ,如上面标紫那块,说我们的 query 里面的查询参数不太对!

(2.4)初始化创建信息状态存放索引

# 创建索引,ElastAlert会把执行记录存放到这个索引中
# 索引名叫elastalert_status
elastalert-create-index

[root@DB5 /data/software/elastalert-master]$ elastalert-create-index
/usr/local/lib/python3.6/site-packages/elasticsearch/connection/base.py:209: ElasticsearchWarning: Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.14/security-minimal-setup.html to enable security.
warnings.warn(message, category=ElasticsearchWarning)
Elastic Version: 7.14.1
Reading Elastic 6 index mappings:
Reading index mapping 'es_mappings/6/silence.json'
Reading index mapping 'es_mappings/6/elastalert_status.json'
Reading index mapping 'es_mappings/6/elastalert.json'
Reading index mapping 'es_mappings/6/past_elastalert.json'
Reading index mapping 'es_mappings/6/elastalert_error.json'
Index elastalert_status already exists. Skipping index creation.

会根据预设模板、json 等信息创建,创建默认的  elastalert_status  (这里因为我之前创建过了 索引提示信息说早已存在)

我们查看一下ES中,是否有这些索引了;如下图,是有的

  

【3】启动、使用 elastalert

(3.1)启动 elastalert

# 当前目录在 elastalert-master 目录下
nohup python3 -m elastalert.elastalert --verbose --config config.yaml --rule rules/system.yaml &

#相关多rule文件参考,在总配置文件指定规则目录: rules_folder: rules ,还有如果邮件的账户密码文件,则需要配置到其他目录去,不能放到该目录下
nohup python3 -m elastalert.elastalert --verbose --config config.yaml &

(3.2)查看日志

[root@DB5 /data/software/elastalert-master]$ less nohup.out 
/usr/local/lib/python3.6/site-packages/elasticsearch/connection/base.py:209: ElasticsearchWarning: Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.14/security-minimal-setup.html to enable security.
  warnings.warn(message, category=ElasticsearchWarning)
INFO:elastalert:Starting up
INFO:elastalert:Disabled rules are: []
INFO:elastalert:Sleeping for 59.999911 seconds
INFO:elastalert:Queried rule sys_errorlog from 2021-10-13 10:05 CST to 2021-10-13 10:20 CST: 0 / 0 hits
/usr/local/lib/python3.6/site-packages/elasticsearch/connection/base.py:209: ElasticsearchWarning: Camel case format name dateOptionalTime is deprecated and will be removed in a future version. Use snake case name date_optional_time instead.
  warnings.warn(message, category=ElasticsearchWarning)
INFO:elastalert:Ran sys_errorlog from 2021-10-13 10:05 CST to 2021-10-13 10:20 CST: 0 query hits (0 already seen), 0 matches, 0 alerts sent
INFO:elastalert:Background configuration change check run at 2021-10-13 10:21 CST
INFO:elastalert:Background alerts thread 0 pending alerts sent at 2021-10-13 10:21 CST
INFO:elastalert:Disabled rules are: []
INFO:elastalert:Sleeping for 59.999893 seconds
INFO:elastalert:Queried rule sys_errorlog from 2021-10-13 10:06 CST to 2021-10-13 10:21 CST: 0 / 0 hits
INFO:elastalert:Ran sys_errorlog from 2021-10-13 10:06 CST to 2021-10-13 10:21 CST: 0 query hits (0 already seen), 0 matches, 0 alerts sent
INFO:elastalert:Background configuration change check run at 2021-10-13 10:22 CST
INFO:elastalert:Background alerts thread 0 pending alerts sent at 2021-10-13 10:22 CST
INFO:elastalert:Disabled rules are: []
INFO:elastalert:Sleeping for 59.99981 seconds
INFO:elastalert:Queried rule sys_errorlog from 2021-10-13 10:07 CST to 2021-10-13 10:22 CST: 0 / 0 hits
INFO:elastalert:Ran sys_errorlog from 2021-10-13 10:07 CST to 2021-10-13 10:22 CST: 0 query hits (0 already seen), 0 matches, 0 alerts sent
INFO:elastalert:Background configuration change check run at 2021-10-13 10:23 CST
INFO:elastalert:Background alerts thread 0 pending alerts sent at 2021-10-13 10:23 CST
INFO:elastalert:Disabled rules are: []
INFO:elastalert:Sleeping for 59.999838 seconds

(3.3)查看告警信息

(1)先查看一下 elastalert 的运行日志

INFO:elastalert:Queried rule sys_errorlog from 2021-10-15 15:02 CST to 2021-10-15 15:04 CST: 1 / 1 hits
INFO:elastalert:Sent email to ['guochaoqun@bianfeng.com']
INFO:elastalert:Ran sys_errorlog from 2021-10-15 15:02 CST to 2021-10-15 15:04 CST: 1 query hits (0 already seen), 1 matches, 1 alerts sent
INFO:elastalert:Background configuration change check run at 2021-10-15 15:05 CST
INFO:elastalert:Disabled rules are: []
INFO:elastalert:Sleeping for 59.999812 seconds
INFO:elastalert:Background alerts thread 0 pending alerts sent at 2021-10-15 15:05 CST
INFO:elastalert:Queried rule sys_errorlog from 2021-10-15 15:02 CST to 2021-10-15 15:05 CST: 4 / 4 hits
INFO:elastalert:Sent email to ['guochaoqun@bianfeng.com']
INFO:elastalert:Ignoring match for silenced rule sys_errorlog
INFO:elastalert:Ignoring match for silenced rule sys_errorlog
INFO:elastalert:Ran sys_errorlog from 2021-10-15 15:02 CST to 2021-10-15 15:05 CST: 4 query hits (1 already seen), 3 matches, 1 alerts sent

(2)查看告警邮件

   

有一个问题,3个命中,3个信息才对,结果只显示了其中一个;

【4】深入告警配置文件

(4.1)报警方式

elastalert的报警方式有很多种,像邮件、微信、钉钉、post等等,我们主要介绍以下几种常用的

邮件报警

alert:
- "email"
email:
 - "test@qq.com"
smtp_host: smtp.163.com
smtp_port: 25
smtp_auth_file: /opt/elastalert/smtp_auth_file.yaml
from_addr: test01@163.com
email_reply_to: teast02@163.com

修改/data/software/elastalert-master/rules/smtp_auth_file.yaml信息

企业微信机器人报警

微信报警模板

git clone https://github.com/anjia0532/elastalert-wechat-plugin.git
cp elastalert-wechat-plugin/elastalert_modules/* elastalert_modules/

添加报警方式

alert:
 - "elastalert_modules.wechat_qiye_alert.WeChatAlerter"
#后台登陆后【设置】->【权限管理】->【普通管理组】->【创建并设置通讯录和应用权限】->【CorpID,Secret】
# 设置微信企业号的appid
corp_id: xxx
# 设置微信企业号的Secret
secret: xxx
# 后台登陆后【应用中心】->【选择应用】->【应用id】
# 设置微信企业号应用id
agent_id: xx
# 部门id
party_id: xx
# 用户微信号
user_id: xx
# 标签id,多个用 | 分隔

钉钉报警方式

钉钉报警模板

git clone https://github.com/xuyaoqiang/elastalert-dingtalk-plugin.git
cp elastalert-dingtalk-plugin/elastalert_modules/dingtalk_alert.py elastalert_modules/

添加报警方式

alert:
- "elastalert_modules.dingtalk_alert.DingTalkAlerter"
dingtalk_webhook: "https://oapi.dingtalk.com/robot/send?access_token=fb6500f4c85b8cfe66fa9586870f3ce16c848eab1e1cb23110388d6d443f1e"
dingtalk_msgtype: text

(4.2)报警频率

#限定时间内,发生事件次数
num_events: 3
#与上面参数结合使用,表示在2分钟内发生3次就报警
timeframe:
  minutes: 2

(4.3)避免重复告警

避免一定时间段中重复告警,可以配置realertexponential_realert这两个选项:

# 5分钟内相同的报警不会重复发送
realert:
  minutes: 5
# 指数级扩大 realert 时间,中间如果有报警,
# 则按照5->10->20->40->60不断增大报警时间到制定的最大时间,
# 如果之后报警减少,则会慢慢恢复原始realert时间
exponential_realert:
  hours: 1

(4.4)聚合相同告警

# 根据报警的内容将相同的报警按照 name 来聚合
aggregation_key: name
# 聚合报警的内容,只展示 name 与 message
summary_table_fields:
  - name
  - message

(4.5)告警内容格式化

可以自定义告警内容,内部是使用Pythonformat来实现的。

alert_subject: "Error {1} @{2}"
alert_subject_args:
  - name
  - "@timestamp"
alert_text_type: alert_text_only
alert_text: |
  > Name: {1}
  > Message: {2}
  > Host: {3} ({4})
alert_text_args:
  - name
  - message
  - hostname
  - host

(4.6)elastalert最佳实践整理配置文件

# ---------------- es配置 ----------------------
es_host: 192.168.175.132 # es的IP地址
es_port: 9200 # es的端口
#es_username: elastic  # es的用户名
#es_password: 111qqq # es的密码

# ----------------
# --------------- 报警基础信息配置 ------------------
name: sys_errorlog   # 报警邮件的标题
type: frequency # 类型:频率
index: system-* # 监控的索引,多个使用逗号隔开
num_events: 1 # 时间内触发的次数
timeframe:
 minutes: 1   # 时间,和上边的参数关联,1分钟内有1次会报警
# ---------------------
# ---------------- 报警过滤条件 -------------------
# For more info: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl.html filter: - query: query_string: query: "message: a" alert_text: "system日志异常测试" # 会在报警内容中显示 smtp_host: smtp.qq.com # smtp的地址 smtp_port: 25 # 端口 smtp_auth_file: /data/software/elastalert-master/rules/smtp_auth_file.yaml # 用户密码的文件 email_reply_to: 815202984@qq.com # 发送邮件的邮箱 from_addr: 815202984@qq.com alert: - "email" # 报警类型 email: # 收件人地址 - "guochaoqun@bianfeng.com" # -------------- 重复告警 --------------- realert: minutes: 1 # 1分钟内相同告警不会重复发送 #exponential_realert: # minutes: 10 #指数型增长最大值 # 指数级扩大 realert 时间,中间如果有报警, # 则按照5->10->20->40->60不断增大报警时间直到最大时间, # 如果之后报警减少,则会慢慢恢复原始realert时间 # -------------------------------------------- # ------------ 聚合相同告警 ------------------ aggregation_key: name #聚合报警的内容,只展示 name 与 message summary_table_fields: - name - _index - message # -------------------------- # ------------------------告警内容格式化
# 内容是通过 es中的行提取出来的 alert_subject: "Error {0}@{1}" alert_subject_args: - name # 该name 为本代码上面定义的name - host.ip[0] alert_text_type: alert_text_only alert_text: | > datetime: {0} > Message: {1} > Host: {2} > index: {3} > ip_info: {4} > os_info: {5},{6} > input_type: {7} > log_file: {8} > num_hits: {9} > num_matches: {10} alert_text_args: - "@timestamp" - message - agent.hostname - _index - host.ip[0] - host.os.name - host.os.version - input.type - log.file.path - num_hits - num_matches # alert_text: "system日志异常测试" # 会在报警内容中显示,非格式化内容 # ----------------------

(4.7)config.yaml 中的 type规则

  • any:只要有匹配就报警;
  • blacklistcompare_key字段的内容匹配上blacklist数组里任意内容;
  • whitelistcompare_key字段的内容一个都没能匹配上whitelist数组里内容;
  • change:在相同query_key条件下,compare_key字段的内容,在 timeframe范围内 发送变化;
  • frequency:在相同query_key条件下,timeframe 范围内有num_events个被过滤出 来的异常;
  • spike:在相同query_key条件下,前后两个timeframe范围内数据量相差比例超过spike_height。其中可以通过spike_type设置具体涨跌方向是- updownboth。还可以通过threshold_ref设置要求上一个周期数据量的下限,threshold_cur设置要求当前周期数据量的下限,如果数据量不到下限,也不触发;
  • flatlinetimeframe范围内,数据量小于threshold阈值;
  • new_term:fields字段新出现之前terms_window_size(默认30天)范围内最多的terms_size(默认50)个结果以外的数据;
  • cardinality:在相同 query_key条件下,timeframe范围内cardinality_field的值超过 max_cardinality或者低于min_cardinality

(4.8)rules下的 filter写法

官网:https://elastalert.readthedocs.io/en/latest/recipes/writing_filters.html

【5】深入聚合(解决hit只显示一行message的痛点)

转自官网:https://elastalert.readthedocs.io/en/latest/ruletypes.html?highlight=aggregation

(5.1)聚合的概念

聚合

aggregation:此选项允许您将多个匹配项聚合到一个警报中。每次找到匹配项时,ElastAlert 将等待该时间段,并将在该时间段内发生的所有匹配项一起发送给特定规则。aggregation

例如:

aggregation:
  hours: 2

意味着如果一场比赛发生在 12:00,另一场发生在 1:00,第三场发生在 2:30,则将在 2:00 发送一个警报,包含前两场比赛,另一个在 4:30,包含第三场比赛加上在 4:30 之前发生的任何其他比赛。如果您期望有大量匹配并且只想要定期报告,这将非常有用。(可选,时间,默认无)

如果您希望汇总所有警报并定期发送它们,您可以使用该schedule字段执行此操作

例如,如果您希望每周一和周五收到提醒:

aggregation:
  schedule: '2 4 * * mon,fri'

这使用 Cron 语法,您可以在此处阅读更多信息确保包含计划字段或标准日期时间字段(例如hoursminutesdays),而不是同时包含两者。

默认情况下,在一个过程中发生的所有事件 聚合窗口组合在一起。但是,如果您的规则aggregation_key设置了该字段,则共享公共键值的每个事件将被分组在一起。将为每个新遇到的键值创建一个单独的聚合窗口。

(5.2)根据 Key 聚合分组

 

例如,如果您希望接收按触发事件的用户分组的警报,您可以设置:

aggregation_key: 'my_data.username'

然后,假设一个 聚合 10 分钟的窗口,如果您收到以下数据点:

{'my_data': {'username': 'alice', 'event_type': 'login'}, '@timestamp': '2016-09-20T00:00:00'}
{'my_data': {'username': 'bob', 'event_type': 'something'}, '@timestamp': '2016-09-20T00:05:00'}
{'my_data': {'username': 'alice', 'event_type': 'something else'}, '@timestamp': '2016-09-20T00:06:00'}

这应该会产生 2 个警报:一个包含 alice 的两个事件,发送于 2016-09-20T00:10:00,另一个包含 bob 的一个事件发送于2016-09-20T00:16:00

为了 聚合s,有时会在查看媒体(电子邮件、jira 票等)中存在大量文档。如果您设置了该summary_table_fields字段,Elastalert 将提供所有结果中指定字段的摘要。

例如,如果您希望汇总出现在文档中的 usernames 和 event_types,以便快速浏览最相关的字段,您可以设置:

summary_table_fields:
    - my_data.username
    - my_data.event_type

然后,对于上面列出的 alice 和 bob 事件的相同示例数据,Elastalert 将在警报介质中提供以下汇总表:

  

注意

  默认情况下, 聚合时间是相对于当前系统时间,而不是比赛时间。

  这意味着在过去的事件上运行 elastalert 将导致与在这些事件发生时运行 elastalert 的警报不同。这种行为可以通过设置来改变aggregate_by_match_time

aggregate_by_match_time

  将此设置为 true 将导致 聚合s 相对于第一个事件的时间戳创建,而不是当前时间。这对于查询历史数据很有用,或者如果使用非常大的 buffer_time 并且您希望从单个查询中发生多个聚合。

(5.3)聚合列 aggregation_key

(5.3.1)聚合列案例

深入研究了一下,这个列,会把多个告警之间根据 后面的值列来分组聚合;

aggregation:
minutes: 1
aggregation_key: [host.ip[0]] summary_table_fields: - _index - host.ip[0] - message

如下图:我们发现ip一样的在同一封邮件中;   

  

另外的案例:

aggregation:
 minutes: 1
 # schedule: '* * * * *'
 # 聚合报警的内容,只展示 name 与 message

aggregation_key: ['host.ip[0]' ,'_index']
summary_table_fields:
  - _index
  - host.ip[0]
  - message

我们可以发现一下子来了2封邮件:

  

 分别是:根据ip/index分组

  

   

 这就很坑爹啊,正经人肯定想要都在一起啊

(5.3.2)聚合key总结

(1)这个聚合其实就是把相同值放到一个告警里去,但是 如果聚合列有不同值,那么不同值会分开成多个告警

(2)如(5.3.1)中的第二个案例,就是上图;所以略显鸡肋;

(3)如果我们要的是针对这整个索引的命中聚合,那么就去掉

aggregation_key: ['host.ip[0]' ,'_index']

 这个参数

(5.4)全体聚合(最佳实践) 

aggregation:
 minutes: 1
 # schedule: '* * * * *'

#aggregation_key: ['host.ip[0]' ,'_index']   #去掉根据查询结果列分组
summary_table_fields:
  - _index
  - host.ip[0]
  - message

得到的结果:

  

如果得不到这种结果;

可能是下面参数的问题,去掉下面这2个重复告警参数即可

realert:
 minutes: 1 # 1分钟内相同告警不会重复发

相关参考:

  

【最佳实践】rules文件

# ---------------- es配置 ----------------------
es_host: 192.168.175.132 # es的IP地址
es_port: 9200 # es的端口
#es_username: elastic  # es的用户名
#es_password: 111qqq # es的密码

# ----------------

# --------------- 报警基础信息配置 ------------------
name: sys_errorlog   # 报警邮件的标题
type: frequency # 类型:频率
index: system-* # 监控的索引,多个使用逗号隔开
num_events: 1 # 时间内触发的次数
timeframe:
 minutes: 1   # 时间,和上边的参数关联,1分钟内有1次会报警

# ---------------------


# ---------------- 报警过滤条件 -------------------
filter:
- query:
    query_string:
      query: "message: a"

alert_text: "system日志异常测试" # 会在报警内容中显示
smtp_host: smtp.qq.com # smtp的地址
smtp_port: 25 # 端口
smtp_auth_file: /data/software/elastalert-master/rules/smtp_auth_file.yaml       # 用户密码的文件
email_reply_to: 815202984@qq.com            # 发送邮件的邮箱
from_addr: 815202984@qq.com
alert:
- "email" # 报警类型
email:    # 收件人地址
- "815202984@qq.com"

# -------------- 重复告警 ---------------
#realert:
#  minutes: 2  # 1分钟内相同告警不会重复发送

  #exponential_realert:
  #  minutes: 10  #指数型增长最大值


# 指数级扩大 realert 时间,中间如果有报警,
# 则按照5->10->20->40->60不断增大报警时间直到最大时间,
# 如果之后报警减少,则会慢慢恢复原始realert时间
# --------------------------------------------


# ------------ 聚合相同告警 ------------------
aggregation:
 minutes: 1
 # schedule: '* * * * *'

#aggregation_key: ['host.ip[0]' ,'_index']
summary_table_fields:
  - _index
  - host.ip[0]
  - log.file.path
  - message

# --------------------------

# ------------------------告警内容格式化

alert_subject: "Error {0}@{1}"
alert_subject_args:
  - name
  - host.ip[0]
alert_text_type: alert_text_only
alert_text: |
  > datetime: {0}
  > Message: {1}
  > Host: {2} 
  > index: {3}
  > ip_info: {4}
  > os_info: {5},{6}
  > input_type: {7}
  > log_file: {8}
  > num_hits: {9}
  > num_matches: {10}
alert_text_args:
  - "@timestamp"
  - message
  - agent.hostname
  - _index
  - host.ip[0]
  - host.os.name
  - host.os.version
  - input.type
  - log.file.path
  - num_hits
  - num_matches
  


# alert_text: "system日志异常测试" # 会在报警内容中显示,非格式化内容
# ----------------------

【故障处理】

(1)ERROR:root:Error while running alert email: Error connecting to SMTP host: Connection unexpectedly closed

可能启用了ssl加密传输的,所以邮件服务也需要加以下配置,在auth_smtp.yaml 文件中

 smtp_ssl: true

【参考文档】

https://www.cnblogs.com/fatzi/p/13020824.html

相关系列及碰到的问题:https://www.cnblogs.com/sanduzxcvbnm/category/1842765.html

修改因pyhton3 覆盖了 python 命令导致无法 yum

filter写法官网:https://elastalert.readthedocs.io/en/latest/recipes/writing_filters.html

聚集通知,显示所有hit信息:https://www.jianshu.com/p/47c77defd18b

原文地址:https://www.cnblogs.com/gered/p/15400623.html