ELK+elastalert 相关---这篇文章是由(三度)大佬知道完成,转载一份他的笔记
一、安装elastalert
1、环境
- CentOS:7.6
- Python:3.6.9
- pip:19.3
- elastalert:0.2.1
- elk:7.7
2、配置Python3.6.9环境
安装依赖包
yum -y install wget openssl openssl-devel gcc gcc-c++
下载包
wget https://www.python.org/ftp/python/3.6.9/Python-3.6.9.tgz
安装
mkdir -p /usr/local/python
tar xf Python-3.6.9.tgz
cd Python-3.6.9
./configure --prefix=/usr/local/python --with-openssl
make && make install
配置
mv /usr/bin/python /usr/bin/python_old
ln -s /usr/local/python/bin/python3 /usr/bin/python
ln -s /usr/local/python/bin/pip3 /usr/bin/pip
pip install --upgrade pip
注意,所有依赖python2的脚本或者命令,需要更改为python2.7,因为现在默认的python版本为3.6,例如
sed -i '1s/python/python2.7/g' /usr/bin/yum
sed -i '1s/python/python2.7/g' /usr/libexec/urlgrabber-ext-down
验证
$ python -V
Python 3.6.9
$ pip -V
pip 19.3 from /usr/local/python/lib/python3.6/site-packages/pip (python 3.6)
###########################################
到这里说一下 ,因为后面elastalert运行会遇到个错误 ,在san度大佬的指导下,装个模块即可解决
pip install elasticsearch==7.0.0
错误是这样的
# python -m elastalert.elastalert --verbose --config config.yaml --rule example_rules/my_rule.yaml
Traceback (most recent call last):
File "/usr/local/python/lib/python3.6/runpy.py", line 183, in _run_module_as_main
mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
File "/usr/local/python/lib/python3.6/runpy.py", line 109, in _get_module_details
import(pkg_name)
File "/usr/local/python/lib/python3.6/site-packages/elastalert-0.2.4-py3.6.egg/elastalert/init.py", line 6, in
from elasticsearch import RequestsHttpConnection
ImportError: cannot import name 'RequestsHttpConnection'
###########################################
3、安装elastalert
下载包
git clone https://github.com/Yelp/elastalert.git
cd elastalert
安装
pip install "elasticsearch<7,>6"
pip install -r requirements.txt
python setup.py install
安装成功后可以看到四个命令
ll /usr/local/python/bin/elastalert*
/usr/local/python/bin/elastalert
/usr/local/python/bin/elastalert-create-index
/usr/local/python/bin/elastalert-rule-from-kibana
/usr/local/python/bin/elastalert-test-rule
软连接到/usr/bin下,方便使用
ln -s /usr/local/python/bin/elastalert* /usr/bin
- elastalert 报警执行的命令,会根据报警规则执行相应操作。
- elastalert-create-index会创建一个索引,ElastAlert会把执行记录存放到这个索引中,默认情况下,索引名叫elastalert_status。其中有4个_type,都有自己的@timestamp字段,所以同样也可以用kibana来查看这个索引的日志记录情况。
- elastalert-rule-from-kibana从Kibana3已保存的仪表盘中读取Filtering设置,帮助生成config.yaml里的配置。不过注意,它只会读取filtering,不包括queries。
- elastalert-test-rule测试自定义配置中的rule设置。
二、使用
官方文档:https://elastalert.readthedocs.io
规则文档:https://elastalert.readthedocs.io/en/latest/ruletypes.html
1、主配置文件设置
1,cd到安装的elastalert目录下
2, cp config.yaml.example config.yaml //复制一份config.yaml
config.yaml 有效配置
# egrep -v '^$|^#' config.yaml
rules_folder: rules
run_every:
minutes: 1
buffer_time:
minutes: 15
es_host: 192.168.0.60
es_port: 9200
writeback_index: elastalert_status
writeback_alias: elastalert_alerts
alert_time_limit:
days: 2
3,配置rule
cp example.rules rules //复制一份rules目录
cd rules
# egrep -v '^$|^#' frequency.yaml
name: packetbeat
type: frequency
index: packetbeat-*
num_events: 1
timeframe:
hours: 1
filter:
- query:
query_string:
query: "host.name: node1-61"
smtp_host: smtp.qq.com
smtp_port: 465
smtp_auth_file: /opt/elastalert/smtp_auth_file.yaml //邮箱账号密码的配置 密码要加 ' '
#回复给那个邮箱
email_reply_to: xxx1@qq.com
###从哪个邮箱发送
from_addr: xxx1@qq.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:
- "xxx2@qq.com"
邮箱这块目前没有配置明白,先记录一下
2、创建告警索引
执行elastalert-create-index命令在ES创建索引,这不是必须的步骤,但是强烈建议创建。因为对于审计和测试很有用,并且重启ES不影响计数和发送alert。
$ elastalert-create-index
Elastic Version: 7.3.2
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'
New index elastalert_status created
Done!
看到这个输出,就说明创建成功了,也可以请求一下看看:
$ curl 127.0.0.1:9200/_cat/indices?v
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
green open elastalert_status_status lh8LL4iCQeSn0afyzxBX7w 1 1 0 0 460b 230b
green open elastalert_status i7B7IfCuSb2Sex8U5KoTZg 1 1 0 0 460b 230b
green open elastalert_status_past et2aF44VR4WQnxB8T7zD4Q 1 1 0 0 460b 230b
green open elastalert_status_silence lhXHEsuUQeGZaW3cRLp5pQ 1 1 0 0 460b 230b
green open elastalert_status_error zykwk4KtSyyOY7ckxQTrkA 1 1 0 0 460b 230b
注意里边在配置邮件通知的时候,还需要引用外部的一个文件,这个文件里用于存放对应邮箱的用户名密码,或者是邮箱邮箱的授权码
$ vim /opt/elastalert/smtp_auth_file.yaml
user: "test01@163.com"
password: "xxxxxxx"
4、规则测试
测试 规则。
# elastalert-test-rule --config config.yaml ./rules/frequency.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.
Didn't get any results.
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
.........
elastalert_status - {'rule_name': 'Nginx_err', 'endtime': datetime.datetime(2020, 1, 11, 7, 30, 59, 793352, tzinfo=tzutc()), 'starttime': datetime.datetime(2020, 1, 10, 7, 30, 59, 793352, tzinfo=tzutc()), 'matches': 0, 'hits': 0, '@timestamp': datetime.datetime(2020, 1, 11, 7, 31, 0, 76042, tzinfo=tzutc()), 'time_taken': 0.24003815650939941}
如果没有报错,则说明可用。
5、启动
启动方式有两种
(1)指定规则文件路径
python -m elastalert.elastalert --verbose --config config.yaml --rule rules/frequency.yaml
(2)在全局路径config.yaml下,配置规则存放在加载规则rules目录下
python -m elastalert.elastalert --verbose
6、验证
服务启动之后,日志能够很清晰看到整个过程,此时可以在刚刚的索引原点请求几个不存在的接口,造一些404状态,过一会儿应该可以看到日志中的说明,有告警发出,邮箱应该也能收到了。
三、优化
1、启动方式
上边的启动命令只是在前台启动,并不给力,可以使用nohup启动,或者是通过supervisord管理,会更加方便。 supervisord如何安装就不说了.
创建配置文件:
$cat /etc/supervisord.d/elastalert1.ini
[program:elastalert1]
directory=/data/elastalert1/
command=python -m elastalert.elastalert --verbose --config /data/elastalert1/config.yaml
process_name=elastalert1
autorestart=true
startsecs=15
stopsignal=INT
stopasgroup=true
killasgroup=true
redirect_stderr=true
stdout_logfile=/data/log/elastalert1.log
stdout_logfile_maxbytes=5MB
然后启动即可
supervisorctl update
supervisorctl start elastalert1
注意以上全是转载自 三度 大佬-真大佬