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 gitdnf install -y python3 python3-devel
install elastalert
mkdir -p /app/git clone https://github.com/Yelp/elastalert.gitcd /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_rulesrun_every: minutes: 1buffer_time: minutes: 15es_host: 192.168.99.185es_port: 9200es_username: elastices_password: ******writeback_index: elastalert_statuswriteback_alias: elastalert_alertsalert_time_limit: days: 2
字段参数解释
rules_folder: ElastAlert从中加载规则配置文件的位置。run_every: ElastAlert多久查询一次Elasticsearch的时间buffer_time: 用来设置请求里时间字段的范围,默认是15分钟Es_host: elasticsearch host ipEs_port: elasticsearch portwriteback_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.185es_port: 9200es_username: elastices_password: ******name: networklogs-alerttype: frequencyindex: networklogs-*num_events: 1timeframe: minutes: 10 filter:- query: query_string: query: "count: Failed"alert:- "email"email:- "xxxxx@126.com"smtp_host: smtp.qq.comsmtp_port: 25from_addr: xxxx@qq.comsmtp_auth_file: /app/elastalert/elastalert/example_rules/email_auth.yamlalert_subject: "网络设备运行日志异常"alert_text_type: alert_text_onlyalert_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 serversmtp_host: smtp.126.com#报警邮箱的smtp 端口smtp_port: 25#需要把认证信息写到额外配置文件里,需要user和password两个属性smtp_auth_file: /app/elastalert/elastalert/example_rules/email_auth.yamlfrom_addr: ****@qq.com
邮件账户认证信息
#/app/elastalert/elastalert/example_rules/email_auth.yamluser: "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.comsmtp_port: 25from_addr: xxxx@qq.comsmtp_auth_file: /app/elastalert/elastalert/example_rules/email_auth.yamlalert_text_type: alert_text_onlyaalert_text: | 网络设备运行日志异常!!! >time:{} >hostname:{} >count:{} >source:{}alert_text_args: - "@timestamp" - hostname - count - source
邮件认证信息
使用的密码不是登陆邮箱的密码,而是邮箱的邮箱授权码
#/app/elastalert/elastalert/example_rules/email_auth.yamluser: "xxxxxx@qq.com"password: "******"
钉钉告警
钉钉告警插件安装
wget https://github.com/xuyaoqiang/elastalert-dingtalk-plugin/archive/master.zipunzip master.zipcd elastalert-dingtalk-plugin-masterpip3 install pyOpenSSL==16.2.0pip3 install setuptools==46.1.3cp -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_onlyalert_text: | 网络设备运行日志异常 >time:{} >hostname:{} >count:{} >source:{}alert_text_args: - "@timestamp" - hostname - count - source
systemctl 后台运行
elastalert01.service
# vim /usr/lib/systemd/system/elastalert01.service [Unit]Description=elastalert01After=elastalert01.service[Service]Type=simpleUser=rootGroup=rootRestart=on-failurePIDFile=/usr/local/elastalert01.pidWorkingDirectory=/app/elastalertExecStart=/usr/local/bin/elastalert --config /app/elastalert/config.yaml --rule /app/elastalert/example_rules/network1.yamlExecStop=/bin/kill -s QUIT $MAINPIDExecReload=/bin/kill -s HUP $MAINPID[Install]WantedBy=multi-user.target
elastalert02.service
# vim /usr/lib/systemd/system/elastalert02.service [Unit]Description=elastalert02After=elastalert02.service[Service]Type=simpleUser=rootGroup=rootRestart=on-failurePIDFile=/usr/local/elastalert02.pidWorkingDirectory=/app/elastalertExecStart=/usr/local/bin/elastalert --config /app/elastalert/config.yaml --rule /app/elastalert/example_rules/network.yamlExecStop=/bin/kill -s QUIT $MAINPIDExecReload=/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